www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 1814] New: DMD/GDC does not prevent typedef violations

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

           Summary: DMD/GDC does not prevent typedef violations
           Product: D
           Version: unspecified
          Platform: All
        OS/Version: All
            Status: NEW
          Keywords: accepts-invalid
          Severity: major
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla digitalmars.com
        ReportedBy: default_357-line yahoo.de


The spec says the following about typedefs:

"A typedef or enum can be implicitly converted to its base type, but going the
other way requires an explicit conversion."

Why, then, does the following work (on the most recent DMD and GDC)?

import std.stdio;
typedef int Foo;
void test(Foo foo) { }
void main() { test(3); /*ILLEGAL!*/ Foo f = cast(Foo) 2; writefln(f+3); /*ALSO
ILLEGAL*/ }

This basically makes typedef no better than alias.
Looking forward to a fix,

 --downs


-- 
Feb 03 2008
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1814





------- Comment #1 from default_357-line yahoo.de  2008-02-03 12:06 -------
(In reply to comment #0)
 The spec says the following about typedefs:
 
 "A typedef or enum can be implicitly converted to its base type, but going the
 other way requires an explicit conversion."
 
 Why, then, does the following work (on the most recent DMD and GDC)?
 
 import std.stdio;
 typedef int Foo;
 void test(Foo foo) { }
 void main() { test(3); /*ILLEGAL!*/ Foo f = cast(Foo) 2; writefln(f+3); /*ALSO
 ILLEGAL*/ }

Ignore the second one, please, I understand what happens. The first one is still wrong, though.
  --downs
 

--downs :) --
Feb 03 2008
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1814


bugzilla digitalmars.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |INVALID




------- Comment #2 from bugzilla digitalmars.com  2008-03-05 00:37 -------
It turns out that disallowing:
    typedef int Foo;
    Foo f;
    f = 3;
is very onerous. It will require inserting casts everywhere a literal is used.
So implicitly converting an integer literal to a typedef is allowed. What is
not allowed is:
    int i = 3;
    f = i;
I'll update the documentation to clarify this.


-- 
Mar 04 2008