Bug 3156 – auto works like scope instead of type inference, which leads to silent breakage

Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D1 (retired)
Platform
All
OS
All
Creation time
2009-07-08T14:08:00Z
Last change time
2014-04-18T09:12:03Z
Keywords
wrong-code
Assigned to
nobody
Creator
nfxjfg

Comments

Comment #0 by nfxjfg — 2009-07-08T14:08:44Z
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.
Comment #1 by nfxjfg — 2009-07-08T14:32:59Z
don't mind lol
Comment #2 by jarrett.billingsley — 2009-07-08T14:37:34Z
Woahwoahwoahwoah. Why did you close this? This is extremely wrong. I'm surprised no one has run into this yet. Also, since when has scope worked on arrays? O__O
Comment #3 by jarrett.billingsley — 2009-07-08T14:38:11Z
(In reply to comment #2) > Woahwoahwoahwoah. Why did you close this? This is extremely wrong. I'm > surprised no one has run into this yet. > > Also, since when has scope worked on arrays? O__O OH. I see. Stupid string literals are fixed-size arrays.
Comment #4 by nfxjfg — 2009-07-08T14:42:34Z
Yes, it's because string literals are of fixed size. If they were dynamic arrays, the symptoms described in the above report wouldn't exist. Anyway, making them dynamic arrays would require a language change for sure. Maybe it could be proposed for D 2.0. Sorry for the bogus report and the noise.
Comment #5 by smjg — 2009-07-08T17:59:30Z
> 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. Unless I'm mistaken, this hasn't changed. That meaning is just invoked using a different syntax, with a type between 'auto' and the variable's name. > I suggest to fix this and make auto to do type inference only, just > like the specification describes the auto keyword. Indeed, I don't know whether Walter just forgot to remove it from the compiler, planned to do it later or what. But it ought to be at least deprecated now. This meaning of 'scope' was introduced in 0.174 back in 2006. See also bug 2716.