Skip to content

Roslyn didn't report CS8333 for local function. #42259

@yyjdelete

Description

@yyjdelete

Version Used:
VS 16.5.0 Preview 5.0

Steps to Reproduce:
https://sharplab.io/#v2:EYLgxg9gTgpgtADwGwBYA+ABATARgLABQGAzAATakDCpA3oaQ+TkuSqQLICGAlgHYAUASjoFGY0rABmpPgBdSnUgF4JMae35ZBAbnriGUmb3nBlq9f2I69+jDgCc/TgGpg10ftI3xhuR358RvLcgsoAfOYyuh4MAL7eAPQJvsb+gX4h4ZHc0bFAA

    static void Main(){
        ref int a = ref M(2);
        ref int b = ref M(3);
        Console.WriteLine(a+b);
        
        ref int M(in int i) => ref i;
    }
    //ref int M(in int i) => ref i;

Expected Behavior:
Also report CS8333 as it does for normal functions.
error CS8333: Cannot return variable 'in int' by writable reference because it is a readonly variable

Actual Behavior:
Code compile without any error, but it return an ref of local var from inner stack.
And it may lead to unexpected behavior(for the above code, get 6 in netcoreapp3.1, and 5 for net472).

Update:
CS8333 will also be reported if the local function is defined as block instead of expression.

    static void Main(){
        ref int a = ref M(2);
        ref int b = ref M(3);
        Console.WriteLine(a+b);
        
        ref int M(in int i) {
            return ref i;
        }
    }

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions