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

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
( 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:
------- You are receiving this mail because: -------

Reply via email to