D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 14730 - Wrong closure var access with -inline
Summary: Wrong closure var access with -inline
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P1 major
Assignee: No Owner
URL:
Keywords: pull, wrong-code
Depends on:
Blocks:
 
Reported: 2015-06-24 13:21 UTC by Kenji Hara
Modified: 2022-06-05 08:51 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Kenji Hara 2015-06-24 13:21:16 UTC
The assertion fails when you compile the code with -inline.

void main()
{
    static auto makeS2(int x)
    {
        struct S2
        {
            int n;
            int get() { return x; }     // x will be a cloaure variable
        }
        return S2(x);
    }
    auto s2a = makeS2(1);

    // Fails, because inlined get() returns incorrect value
    assert(s2a.get() == 1);
}

The parameter 'x' of makeS2 function is a closure variable. It's offset is calculated in makeS2->toObjFile(). But with -inline, the get function call is expanded in main(). Then the inlined code will access x via the hidden field of s2a, by using 0 offset (wrong).

To fix the problem, we need to determine all offsets of closure vars in front-end.
Comment 2 github-bugzilla 2016-02-07 03:18:59 UTC
Commits pushed to master at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/6f0f6db976af6fe6ebbaba05b968a354a58994ad
fix Issue 14730 - Wrong closure var access with -inline

https://github.com/D-Programming-Language/dmd/commit/2d0bcf1fbd4db3d4a96c5413cdda292c0f151ed4
Merge pull request #4765 from 9rnsr/fix14730

Issue 14730 - Wrong closure var access with -inline