Bug 1814 – DMD/GDC does not prevent typedef violations

Status
RESOLVED
Resolution
INVALID
Severity
major
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2008-02-03T12:05:00Z
Last change time
2015-06-09T05:14:53Z
Keywords
accepts-invalid
Assigned to
bugzilla
Creator
default_357-line

Comments

Comment #0 by default_357-line — 2008-02-03T12:05:05Z
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
Comment #1 by default_357-line — 2008-02-03T12:06:07Z
(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 :)
Comment #2 by bugzilla — 2008-03-05T00:37:19Z
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.