www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 10413] New: .init incorrectly accepts any expression

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=10413

           Summary: .init incorrectly accepts any expression
           Product: D
           Version: D1 & D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: clugdbug yahoo.com.au



According to the spec, only variables, fields, and types support the .init
property. But all kinds of other things compile:

int foo() { return 1; }

static assert(foo.init == 0);

This gets changed into typeof(foo()).init.

---
diff --git a/src/mtype.c b/src/mtype.c
index 1c83eb9..8217e8d 100644
--- a/src/mtype.c
+++ b/src/mtype.c
   -2051,6 +2051,11    Expression *Type::dotExp(Scope *sc, Expression *e,
Identifier *ident, int flag)
         char *s = e->toChars();
         e = new StringExp(e->loc, s, strlen(s), 'c');
     }
+    else if (ident == Id::init && ex->op != TOKtype)
+    {
+        error(e->loc, ".init can only be applied to variables, fields, and
types. Did you mean typeof(%s).init ?", e->toChars());
+        e = new ErrorExp();
+    }
     else
         e = getProperty(e->loc, ident, flag);

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 19 2013
parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=10413




Pull request:
https://github.com/D-Programming-Language/dmd/pull/2240

This fails Phobos unit tests in multiple places, for example:

std/traits.d(3654): Error: static assert  (is(void == double)) is false

With constructions like:
template Foo(T...)

     T[0].init
is OK if T[0] is a type, but if it is a value, it won't compile.
It is in fact pretty bizarre that 2.init == 0, so this should probably be
changed.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 25 2013