Thread overview
[Issue 5936] New: DMD Segfault when forward-referencing pure auto-return member function with parameter.
May 06, 2011
kennytm@gmail.com
[Issue 5936] Regression(2.050): Segfault when forward-referencing pure auto-return member function with parameter.
Jun 06, 2011
Don
Jun 06, 2011
Don
Jun 30, 2011
kennytm@gmail.com
Jul 27, 2011
kennytm@gmail.com
Aug 24, 2011
Don
Aug 24, 2011
Walter Bright
May 06, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5936

           Summary: DMD Segfault when forward-referencing pure auto-return
                    member function with parameter.
           Product: D
           Version: D2
          Platform: Other
        OS/Version: Mac OS X
            Status: NEW
          Keywords: ice-on-valid-code, rejects-valid
          Severity: regression
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: kennytm@gmail.com
            Blocks: 340


--- Comment #0 from kennytm@gmail.com 2011-05-06 08:44:05 PDT ---
Test case:

---------------------------
import std.c.stdio;
int a() {
    return V.s(0);
}
struct V {
    static pure auto s(int x) {
        return 55;
    }
}
void main() {
    printf("%d\n", a());
}
---------------------------
Bus error
---------------------------

The bug appears only if all of the following conditions are met:
 1. The function 's' must be 'pure'.
 2. The function 's' must have auto-return.
 3. The function 's' must have at least one named parameter. For example, if
    the parameter is changed to 'pure auto s(...)' the bug disappears.
 4. The function 's' must be placed after its first use. For example, moving
    'struct V' above 'int a()' the bug disappears.

The program works correctly in 2.048. Therefore I have marked this as a regression.

============================

If instead of calling the function, we just compute the pointer of 's', we'll get a forward reference error instead:

---------------------------
void a() {
    cast(void)(&V.s);
}
struct V {
    static pure auto s(int x) {
        return 0;
    }
}
---------------------------
x.d(2): Error: forward reference to s
---------------------------

But this error also happens on 2.048.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 06, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5936


Don <clugdbug@yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |patch
                 CC|                            |clugdbug@yahoo.com.au
            Summary|DMD Segfault when           |Regression(2.050): Segfault
                   |forward-referencing pure    |when forward-referencing
                   |auto-return member function |pure auto-return member
                   |with parameter.             |function with parameter.


--- Comment #1 from Don <clugdbug@yahoo.com.au> 2011-06-06 06:18:17 PDT ---
The segfault is a simple null pointer dereference which can easily be patched.

mtype.c, TypeFunction::parameterEscapes, line 5369

    if (purity)
    {   /* With pure functions, we need only be concerned if p escapes
         * via any return statement.
         */
-        Type* tret = nextOf()->toBasetype();
-        if (!isref && !tret->hasPointers())
+        Type* tret = nextOf() ? nextOf()->toBasetype() : NULL;
+        if (!isref && tret && !tret->hasPointers())
        {   /* The result has no references, so p could not be escaping
             * that way.
             */
            return FALSE;
        }

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 06, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5936



--- Comment #2 from Don <clugdbug@yahoo.com.au> 2011-06-06 06:21:15 PDT ---
*** Issue 5844 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 30, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5936



--- Comment #3 from kennytm@gmail.com 2011-06-30 11:27:04 PDT ---
(In reply to comment #1)
> The segfault is a simple null pointer dereference which can easily be patched.
> 
> mtype.c, TypeFunction::parameterEscapes, line 5369
> 
>     if (purity)
>     {   /* With pure functions, we need only be concerned if p escapes
>          * via any return statement.
>          */
> -        Type* tret = nextOf()->toBasetype();
> -        if (!isref && !tret->hasPointers())
> +        Type* tret = nextOf() ? nextOf()->toBasetype() : NULL;
> +        if (!isref && tret && !tret->hasPointers())
>         {   /* The result has no references, so p could not be escaping
>              * that way.
>              */
>             return FALSE;
>         }

This patch only fixes 5844. In git master this becomes

   Internal error: toir.c 190

with or without your patch.

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



--- Comment #4 from kennytm@gmail.com 2011-07-27 11:48:10 PDT ---
(In reply to comment #3)
> (In reply to comment #1)
> This patch only fixes 5844. In git master this becomes
> 
>    Internal error: toir.c 190
> 
> with or without your patch.

As of the latest git master the ICE no longer happens and the test cases now behaves normally.

Also, one more test case which involves CTFE, based on std.complex.complex:

---------------------------
auto bug5936c(R)(R i) @safe pure nothrow {
    return true;
}
static assert( bug5936c(0) );
---------------------------

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 24, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5936



--- Comment #5 from Don <clugdbug@yahoo.com.au> 2011-08-24 11:39:00 PDT ---
https://github.com/D-Programming-Language/dmd/pull/330

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 24, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5936


Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |bugzilla@digitalmars.com
         Resolution|                            |FIXED


--- Comment #6 from Walter Bright <bugzilla@digitalmars.com> 2011-08-24 13:40:31 PDT ---
https://github.com/D-Programming-Language/dmd/commit/b9ee455e4e8c129b429d8e22fd4f6d15885274fb

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