D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 6782 - inout-correct range is not iterable using foreach with type deduction inside non-inout function
Summary: inout-correct range is not iterable using foreach with type deduction inside ...
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: patch, rejects-valid
Depends on:
Blocks:
 
Reported: 2011-10-07 13:07 UTC by Steven Schveighoffer
Modified: 2011-10-08 16:26 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 Steven Schveighoffer 2011-10-07 13:07:02 UTC
This should compile:

struct range
{
    int *ptr;
    @property inout(int)* front() inout
    {
        return ptr;
    }

    @property bool empty() const
    {
        return ptr is null;
    }

    void popFront()
    {
        ptr = null;
    }
}

void main()
{
    int x = 5;
    auto r = range(&x);
    foreach(p; r) // line 24
    {
    }
}

bug.d(24): Error: variable bug.main.p inout variables can only be declared inside inout functions
bug.d(24): Error: cannot implicitly convert expression (__r1.front()) of type int* to inout(int)*

The range foreach rewrite somehow gets inout into the resulting type of r.front, where it should really be the resulting wild type (in this case int*)

Changing the foreach loop to:

foreach(int *p; r)
{
}

compiles.