September 21, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=11080



--- Comment #20 from bearophile_hugs@eml.cc 2013-09-21 14:55:15 PDT ---
(In reply to comment #19)

> Thanks for raising the problem, but this one is a separate issue.

It's a separate issue, but if you disallow dynamic arrays in boolean evaluation contexts, then it also disallows code like assert("something going wrong") and you don't need to add a special rule to D for such buggy case.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
September 21, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=11080



--- Comment #21 from Maxim Fomin <maxim@maxim-fomin.ru> 2013-09-21 15:03:02 PDT ---
(In reply to comment #20)
> (In reply to comment #19)
> 
> > Thanks for raising the problem, but this one is a separate issue.
> 
> It's a separate issue, but if you disallow dynamic arrays in boolean evaluation contexts, then it also disallows code like assert("something going wrong") and you don't need to add a special rule to D for such buggy case.

Yes, but one of the way to fix the issue (which I consider as a better one) is to cast array not to pointer but to length and still allow arrays in boolean conditions.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
September 23, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=11080



--- Comment #22 from Don <clugdbug@yahoo.com.au> 2013-09-23 03:58:11 PDT ---
> ...rules you wouldn't be able to apply to normal functions. For example an
"enforce" that refuses array literals. Can't happen.

Actually it's the other way around. Currently, assert is special.

void enforce(bool b, string msg = "xxx")
{
}

void main()
{
   enforce("zzz");
}


zunk.d(8): Error: function zunk.enforce (bool b, string msg = "xxx") is not
callable using argument types (string)

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
September 23, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=11080



--- Comment #23 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2013-09-23 04:21:26 PDT ---
(In reply to comment #22)
> > ...rules you wouldn't be able to apply to normal functions. For example an
> "enforce" that refuses array literals. Can't happen.
> 
> Actually it's the other way around. Currently, assert is special.
> 
> void enforce(bool b, string msg = "xxx")
> {
> }
> 
> void main()
> {
>    enforce("zzz");
> }
> 
> 
> zunk.d(8): Error: function zunk.enforce (bool b, string msg = "xxx") is not
> callable using argument types (string)

That's not the Phobos enforce. This is:

-----
import std.exception;

void main()
{
   enforce("zzz");  // works fine
}
-----

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
September 23, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=11080



--- Comment #24 from monarchdodra@gmail.com 2013-09-23 05:06:46 PDT ---
(In reply to comment #23)
> (In reply to comment #22)
> > > ...rules you wouldn't be able to apply to normal functions. For example an
> > "enforce" that refuses array literals. Can't happen.
> > 
> > Actually it's the other way around. Currently, assert is special.
> > 
> > void enforce(bool b, string msg = "xxx")
> > {
> > }
> > 
> > void main()
> > {
> >    enforce("zzz");
> > }
> > 
> > 
> > zunk.d(8): Error: function zunk.enforce (bool b, string msg = "xxx") is not
> > callable using argument types (string)
> 
> That's not the Phobos enforce. This is:

I think his point was that enforce *could* be written to not accept string literals, countering my earlier point that "assert" would have a special "no-string-literals", which would not have been possible to implement with enforce.

I don't think the example is relevant though, because this new enforce would
*also* turn down:
string s;
enforce(s);

Which assert would still support.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
September 23, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=11080



--- Comment #25 from monarchdodra@gmail.com 2013-09-23 05:19:57 PDT ---
(In reply to comment #17)
> (In reply to comment #12)
> > (In reply to comment #11)
> > > Assert("string") is a bug. There should be no discussion here.
> > 
> > Why is it a bug? That's the discussion we're having. An array literal that evaluates to null *will* trigger it. A user can test it.
> > 
> > void main()
> > {
> >     enum string s1 = "string";
> >     enum string s2 = null;
> > 
> >     assert( s1);
> >     assert(!s2);
> > 
> >     assert( "");
> >     assert(!string.init);
> > }
> > 
> > These all seem like legit use cases to me.
> 
> No, there is difference between array type object and array literal expression. Code like assert("Array literal") (note that this is not assert(s) where 's' refers to a string) is always a bug because the expression is always true and indicates that user actually wanted assert(some_condition, "array literal"). There is no reason to write such code. As I have pointed out above, even in situations which are not surely error, dmd still aborts compilation. In this regard discussed issue is clear because there is no doubt whether use case is a bug or not.

Fine.

But in that case, as a variation of 4733, we could simply ban *any* "implicit call to explicit conversion to bool" for *all* array literals, regardless of type and context? It would be a step in the right direction (IMO), be more generic, and cover other "most probably wrong" use cases. EG:

if ("hello"){}
assert([1, 2, 3]);

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
September 23, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=11080



--- Comment #26 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2013-09-23 05:46:52 PDT ---
(In reply to comment #24)
> I think his point was that enforce *could* be written to not accept string literals

Well yeah, so now that's yet another special case. Then you'll have a bunch of Phobos functions working, others not working, and 3rd party libraries doing their own thing.

I agree that assert("foo") is meaningless, but I think we should fix it by
disallowing implicit bool conversion from a string. Can you really tell at a
glance whether `if (getString())` checks for ".length != 0" or "!is null"? I
can't, and I always have to look up the specs to remind myself (not in my code,
I don't use this form of implicit bool check).

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
1 2 3
Next ›   Last »