Thread overview
[Issue 8665] New: auto function and implicit conversion in return statement corrupt returned value
Sep 16, 2012
Kenji Hara
Sep 16, 2012
Jonathan M Davis
Sep 16, 2012
Kenji Hara
Sep 16, 2012
Kenji Hara
Sep 16, 2012
Jonathan M Davis
Oct 28, 2012
yebblies
September 16, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8665

           Summary: auto function and implicit conversion in return
                    statement corrupt returned value
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Keywords: wrong-code
          Severity: critical
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: k.hara.pg@gmail.com


--- Comment #0 from Kenji Hara <k.hara.pg@gmail.com> 2012-09-15 20:31:19 PDT ---
From digitalmars.d.leran forum: http://forum.dlang.org/thread/oubtkhvccronbumdgbll@forum.dlang.org

Reduced test case:

auto foo(bool val)
{
    if (val)
        return 42;
    else
        return 1.5;
}

void main()
{
    assert(foo(true) == 42); // assertion failure
    assert(foo(false) == 1.5);
}

The return type of foo is double, but returning 42 is broken in runtime.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
September 16, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8665


Jonathan M Davis <jmdavisProg@gmx.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jmdavisProg@gmx.com


--- Comment #1 from Jonathan M Davis <jmdavisProg@gmx.com> 2012-09-15 20:50:14 PDT ---
According to the spec, this code shouldn't compile in the first place, since there are multiple types being returned. To quote the spec:

> If there are mul­ti­ple Re­turn­State­ments, the types of them must match ex­actly. If there are no Re­turn­State­ments, the re­turn type is in­ferred to be void.

So, depending on what's causing the corruption, it doesn't need to be fixed but rather the compiler needs to be fixed so that it this example outright fails to compile.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
September 16, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8665



--- Comment #2 from Kenji Hara <k.hara.pg@gmail.com> 2012-09-15 21:11:12 PDT ---
(In reply to comment #1)
> According to the spec, this code shouldn't compile in the first place, since there are multiple types being returned. To quote the spec:
> 
> > If there are mul­ti­ple Re­turn­State­ments, the types of them must match ex­actly. If there are no Re­turn­State­ments, the re­turn type is in­ferred to be void.
> 
> So, depending on what's causing the corruption, it doesn't need to be fixed but rather the compiler needs to be fixed so that it this example outright fails to compile.

But, I have already *fixed* bug 5899. It is getting common type of null and other reference types. Therefore, now, multiple return statements in auto function calculates the most derived common type as the actual return type.

So, I think the spec should be improved.
(I think I have to do it...)

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
September 16, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8665


Kenji Hara <k.hara.pg@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |pull


--- Comment #3 from Kenji Hara <k.hara.pg@gmail.com> 2012-09-15 22:48:35 PDT ---
https://github.com/D-Programming-Language/dmd/pull/1118

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
September 16, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8665



--- Comment #4 from Jonathan M Davis <jmdavisProg@gmx.com> 2012-09-16 16:24:17 PDT ---
If the decision is that we want different behavior than the spec dictates, then the spec needs to be changed, but as it stands, according to the spec, this code is supposed to be illegal, and I was just pointing that out.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 28, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8665


yebblies <yebblies@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |yebblies@gmail.com
         Resolution|                            |FIXED
         AssignedTo|nobody@puremagic.com        |k.hara.pg@gmail.com


--- Comment #5 from yebblies <yebblies@gmail.com> 2012-10-28 23:55:02 EST ---
Whether of not taking the common type for auto returns is desirable does not change the fact that it should not generate wrong code.

https://github.com/D-Programming-Language/dmd/commit/1ee74a1a3b0480edc439dbe1fb004e027e296088

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