D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 6759 - missing initialization in foreach with alias this
Summary: missing initialization in foreach with alias this
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: Other All
: P2 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-10-02 05:03 UTC by Martin Nowak
Modified: 2011-10-05 12:04 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Martin Nowak 2011-10-02 05:03:02 UTC
struct Range
{
    size_t front() { return 0; }
    void popFront() { empty = true; }
    bool empty;
}

struct ARange
{
    Range range;
    alias range this;
}

void main()
{
    ARange arange;
    assert(arange.front == 0);
    foreach(e; arange)
    {}
}

---

Foreach creates a reference to arange.
The expression that initializes the reference is dropped
for alias this aggregates.

This is a regression introduced by
https://github.com/D-Programming-Language/dmd/commit/6a2aefdb468d20aa8d498c8930c2613d78a91238
as a fix to #2781.
Comment 1 Kenji Hara 2011-10-02 12:37:22 UTC
Your patch was already merged (commit: f53ff46), and it looks correct to me the fixing lack of merging prelude before semantic.

But, your sample code works before merging (commit: 07f719e), so that code is not test case of this issue.

Do you have right test case?
Comment 3 Martin Nowak 2011-10-05 12:04:09 UTC
(In reply to comment #1)
> Your patch was already merged (commit: f53ff46), and it looks correct to me the
> fixing lack of merging prelude before semantic.
> 
> But, your sample code works before merging (commit: 07f719e), so that code is
> not test case of this issue.
> 
> Do you have right test case?

This is probably due to constant folding or optimization.
The merged version has 'assert(e == 0);' in the loop body.