Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The compiler should allow the return of a span declared and initialized by switch expression. #44588

Closed
HobbsCode opened this issue May 27, 2020 · 0 comments · Fixed by #45242

Comments

@HobbsCode
Copy link

Version Used:
.netcoreapp3.1
VS 16.6.0

Steps to Reproduce:

public static ReadOnlySpan<T> GetSpan1<T>(int x)
{
    // As return statement. Works fine.

    return x switch
    {
        0 => default,
        _ => default,
    };
}

public static ReadOnlySpan<T> GetSpan2<T>(int x)
{
    // Forward declare local variable, then init. Works fine.

    ReadOnlySpan<T> span;

    span = x switch
    {
        0 => default,
        _ => default,
    };

    return span;
}

public static ReadOnlySpan<T> GetSpan3<T>(int x)
{
    // Declare and init to local variable in single statement produces compiler complaint upon return.

    ReadOnlySpan<T> span = x switch
    {
        0 => default,
        _ => default,
    };

    return span; // Compiler Complaint: "may expose referenced variables outside of their declaration scope."
}

Expected Behavior:
The compiler should allow the returning of the span in the method GetSpan3().

Actual Behavior:
The compiler complains that the return statement "may expose referenced variables outside of their declaration scope."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants