www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 8665] New: auto function and implicit conversion in return statement corrupt returned value

reply d-bugmail puremagic.com writes:
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



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: -------
Sep 15 2012
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8665


Jonathan M Davis <jmdavisProg gmx.com> changed:

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



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: -------
Sep 15 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8665





 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: -------
Sep 15 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8665


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

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



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: -------
Sep 15 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8665




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: -------
Sep 16 2012
prev sibling parent d-bugmail puremagic.com writes:
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



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: -------
Oct 28 2012