Skip to content

Scoped constructor parameters should be able to reference top-level locals #80745

@RikkiGibson

Description

@RikkiGibson
class C2
{
    public C2(scoped Span<int> span)
    {
        int x = 0;
        span = new Span<int>(ref x); // error
    }

    void M(scoped Span<int> span)
    {
        int x = 0;
        span = new Span<int>(ref x); // ok
    }
}

Expected: both span assignments are ok
Actual: It is an error in constructor but ok in ordinary method.

This is happening because of public override BoundNode? VisitConstructorMethodBody() in RefSafetyAnalysis.cs. Creating the LocalScope in that situation (which is necessary to hold any temps etc created for visiting the constructor initializer), is causing us to increment the local scope depth an extra time, treating the top-level locals as if they have a smaller lifetime than the scoped parameters.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions