[Issue 3156] New: auto works like scope instead of type inference, which leads to silent breakage
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Wed Jul 8 14:08:45 PDT 2009
http://d.puremagic.com/issues/show_bug.cgi?id=3156
Summary: auto works like scope instead of type inference, which
leads to silent breakage
Product: D
Version: 1.045
Platform: All
OS/Version: All
Status: NEW
Keywords: wrong-code
Severity: normal
Priority: P2
Component: DMD
AssignedTo: nobody at puremagic.com
ReportedBy: nfxjfg at gmail.com
If you write
auto x = "huh";
it appears dmd allocates space on the stack and copies the string. Thus x will
point to the stack, and you can't return that string from a function. auto
seems to work like scope here (scope x = "huh"), but most users will expect it
to do type inference. E.g. they will expect above code to be equal to
char[] x = "huh";
The problem with the current behavior of auto is that it leads to invalid
programs. While the compiler raises an error if you try to return x directly,
the compiler warns only in the most simple cases:
char[] foo() { auto x = "huh"; char[] y = x; return y; }
The array contents returned by foo() will point to invalidated memory. Reading
that array will never lead to a program crash. This causes silent failures.
The specification
(http://www.digitalmars.com/d/1.0/declaration.html#AutoDeclaration) says auto
does type inference. There's no word about memory allocation. I think the above
behavior is left over from the past, when auto used to do the same as scope. I
suggest to fix this and make auto to do type inference only, just like the
specification describes the auto keyword.
--
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
More information about the Digitalmars-d-bugs
mailing list