Thread overview
[Issue 3156] New: auto works like scope instead of type inference, which leads to silent breakage
Jul 08, 2009
nfxjfg@gmail.com
Jul 08, 2009
nfxjfg@gmail.com
Jul 08, 2009
nfxjfg@gmail.com
Jul 09, 2009
Stewart Gordon
July 08, 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@puremagic.com
        ReportedBy: nfxjfg@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: -------
July 08, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=3156


nfxjfg@gmail.com changed:

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




--- Comment #1 from nfxjfg@gmail.com  2009-07-08 14:32:59 PDT ---
don't mind lol

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
July 08, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=3156


Jarrett Billingsley <jarrett.billingsley@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jarrett.billingsley@gmail.c
                   |                            |om




--- Comment #2 from Jarrett Billingsley <jarrett.billingsley@gmail.com>  2009-07-08 14:37:34 PDT ---
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

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
July 08, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=3156





--- Comment #3 from Jarrett Billingsley <jarrett.billingsley@gmail.com>  2009-07-08 14:38:11 PDT ---
(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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
July 08, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=3156





--- Comment #4 from nfxjfg@gmail.com  2009-07-08 14:42:34 PDT ---
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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
July 09, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=3156


Stewart Gordon <smjg@iname.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |smjg@iname.com




--- Comment #5 from Stewart Gordon <smjg@iname.com>  2009-07-08 17:59:30 PDT ---
> 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.

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