D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 4651 - Docs: Returned classes that have access to stack variables of its enclosing function
Summary: Docs: Returned classes that have access to stack variables of its enclosing f...
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dlang.org (show other issues)
Version: D2
Hardware: Other Windows
: P2 normal
Assignee: No Owner
URL:
Keywords: spec
Depends on:
Blocks:
 
Reported: 2010-08-15 19:23 UTC by Andrej Mitrovic
Modified: 2012-01-23 23:30 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Andrej Mitrovic 2010-08-15 19:23:25 UTC
This is an outdated statement:

"While a non-static nested class can access the stack variables of its enclosing function, that access becomes invalid once the enclosing function exits: 

class Base
{
    int foo() { return 1; }
}

Base func()
{   int m = 3;

    class Nested : Base
    {
	int foo() { return m; }
    }

    Base b = new Nested;

    assert(b.foo() == 3);	// Ok, func() is still active
    return b;
}

int test()
{
    Base b = func();
    return b.foo();		// Error, func().m is undefined
}
"

As is stated in TDPL, variables in the local function get allocated on the heap if DMD detects that a returning class needs access to it. This section needs to be updated.

The next section with the code example for a workaround can be removed:

"If this kind of functionality is needed, the way to make it work is to make copies of the needed variables within the nested class's constructor"
Comment 1 github-bugzilla 2012-01-23 21:43:00 UTC
Commit pushed to https://github.com/D-Programming-Language/d-programming-language.org

https://github.com/D-Programming-Language/d-programming-language.org/commit/c58bfaea9250e432ec3929bc59f0bad30f006812
fix Issue 4651 - Docs: Returned classes that have access to stack variables of its enclosing function
Comment 2 Andrej Mitrovic 2012-01-23 22:30:37 UTC
(In reply to comment #1)
> Commit pushed to
> https://github.com/D-Programming-Language/d-programming-language.org
> 
> https://github.com/D-Programming-Language/d-programming-language.org/commit/c58bfaea9250e432ec3929bc59f0bad30f006812
> fix Issue 4651 - Docs: Returned classes that have access to stack variables of
> its enclosing function

Since you're on a roll (Walter) you could also $(D1) wrap the section on nested functions, Issue 4556. If not, I'll make a pull myself.
Comment 3 Walter Bright 2012-01-23 23:30:43 UTC
Feel free to do a pull.