www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 11824] New: A stack variable escaping problem in CTFE Phobos code

reply d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=11824

           Summary: A stack variable escaping problem in CTFE Phobos code
           Product: D
           Version: D2
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Keywords: rejects-valid
          Severity: normal
          Priority: P2
         Component: Phobos
        AssignedTo: nobody puremagic.com
        ReportedBy: bearophile_hugs eml.cc



import std.array: array;
import std.range: repeat;
auto r = [1].repeat(1).array;
void main() {}



DMD 2.065alpha gives me:

...\dmd2\src\phobos\std\array.d(45): Error: returning a pointer to a local
stack variable
...\dmd2\src\phobos\std\array.d(49):        called from here:
trustedGetAddr(result[i])
...\dmd2\src\phobos\std\array.d(49):        called from here:
emplace(trustedGetAddr(result[i]), e)
test.d(3):        called from here: array(repeat([1], 1u))

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Dec 26 2013
next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=11824


Vladimir Panteleev <thecybershadow gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |thecybershadow gmail.com
           Severity|normal                      |regression



17:47:33 EET ---
I have encountered the same problem:

import std.algorithm;
import std.array;

auto f(int arr) { return [5]; }
immutable x = [1].map!f.array();

This is a regression from 2.064.2.

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 03 2014
prev sibling next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=11824




17:57:23 EET ---
Introduced in https://github.com/D-Programming-Language/phobos/pull/1655

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 03 2014
prev sibling next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=11824


monarchdodra gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |monarchdodra gmail.com




 Introduced in https://github.com/D-Programming-Language/phobos/pull/1655
The fix is trivial, I'll push it tonight. -- Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Feb 03 2014
prev sibling next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=11824


monarchdodra gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |CTFE



I can work *around* the issue, but there is a CTFE bug in there to begin with.

Here is a reduced test case:

//----
int foo(T)()
{
    T[] arr = new T[](1);
    T* getAddr(ref T a)
    {
        return &a;
    }
    getAddr(arr[0]);
    return 5;
}

void main()
{    
    enum a = foo!int(); //OK!
    enum b = foo!(int[])(); //FAILS!
}
//----
main.d(6): Error: returning a pointer to a local stack variable
main.d(8):        called from here: getAddr(arr[0])
main.d(15):        called from here: foo()
//----

First of all: "foo()" ? What are the template parameters?

Second, I *think* there is a rejects valid: getAddr rejects "return &a" if a is
of type "T[]", but not "T".

To be perfectly pedantic: a happens to be a stack variable, yes, but *not*
inside "getAddr" 's scope. So this should not be rejected during CTFE I think.

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 03 2014
prev sibling next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=11824


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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |pull
          Component|Phobos                      |DMD
           Platform|x86                         |All
         OS/Version|Windows                     |All




 I can work *around* the issue, but there is a CTFE bug in there to begin with.
 
[snip]
 To be perfectly pedantic: a happens to be a stack variable, yes, but *not*
 inside "getAddr" 's scope. So this should not be rejected during CTFE I think.
Yes, this is a compiler bug. https://github.com/D-Programming-Language/dmd/pull/3204 -- Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Feb 03 2014
prev sibling next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=11824




Commits pushed to master at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/dc828cccfe92b98d3ae334eedd082d4f04cf36c1
fix Issue 11824 - A stack variable escaping problem in CTFE Phobos code

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


[REG2.065a] Issue 11824 - A stack variable escaping problem in CTFE Phobos code

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 06 2014
prev sibling next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=11824


bearophile_hugs eml.cc changed:

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


-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 06 2014
prev sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=11824




*** Issue 11428 has been marked as a duplicate of this issue. ***

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