Repository: dotnet/roslyn
File: src/Compilers/CSharp/Test/Semantic/Semantics/RefLocalsAndReturnsTests.cs (C#)

Code before:
        this = ref s;
    }
}");
            comp.VerifyDiagnostics(
                // (6,9): error CS8374: Cannot ref-assign 'this' to 's' because 'this' has a narrower escape scope than 's'.
                //         s = ref this;
                Diagnostic(ErrorCode.ERR_RefAssignNarrower, "s = ref this").WithArguments("s", "this").WithLocation(6, 9),
                // (7,9): error CS8373: The left-hand side of a ref assignment must be a ref variable.
                //         this = ref s;
                Diagnostic(ErrorCode.ERR_RefLocalOrParamExpected, "this").WithLocation(7, 9));
        }

        [Fact]
        public void RefReassignLifetimeIsLHS()
        {
            var comp = CreateCompilation(@"
class C
{
    ref int M()
    {
        int x = 0;
        ref int rx = ref x;
        return ref (rx = ref (new int[1])[0]);
    }
}");
            comp.VerifyDiagnostics();
        }

        [Fact]
        public void InReassignmentWithConversion()
        {
            var comp = CreateCompilation(@"
class C
{
    void M(string s)
    {
        ref string rs = ref s;
        M2(in (rs = ref s));
    }
    void M2(in object o) {}
}");
            comp.VerifyDiagnostics(
                // (7,16): error CS1503: Argument 1: cannot convert from 'in string' to 'in object'
                //         M2(in (rs = ref s));
                Diagnostic(ErrorCode.ERR_BadArgType, "rs = ref s").WithArguments("1", "in string", "in object").WithLocation(7, 16));
        }

        [Fact]
        public void RefEscapeInForeach()
        {
            var comp = CreateCompilationWithMscorlibAndSpan(@"

Review comment (suggestion):
Perhaps test should be renamed to `RefReassignLifetimeIsRHS`.

Code after:
        this = ref s;
    }
}");
            comp.VerifyDiagnostics(
                // (6,9): error CS8374: Cannot ref-assign 'this' to 's' because 'this' has a narrower escape scope than 's'.
                //         s = ref this;
                Diagnostic(ErrorCode.ERR_RefAssignNarrower, "s = ref this").WithArguments("s", "this").WithLocation(6, 9),
                // (7,9): error CS8373: The left-hand side of a ref assignment must be a ref variable.
                //         this = ref s;
                Diagnostic(ErrorCode.ERR_RefLocalOrParamExpected, "this").WithLocation(7, 9));
        }

        [Fact]
        public void RefReassignLifetimeIsRHS()
        {
            var comp = CreateCompilation(@"
class C
{
    ref int M()
    {
        int x = 0;
        ref int rx = ref x;
        return ref (rx = ref (new int[1])[0]);
    }
}");
            comp.VerifyDiagnostics();
        }

        [Fact]
        public void InReassignmentWithConversion()
        {
            var comp = CreateCompilation(@"
class C
{
    void M(string s)
    {
        ref string rs = ref s;
        M2(in (rs = ref s));
    }
    void M2(in object o) {}
}");
            comp.VerifyDiagnostics(
                // (7,16): error CS1503: Argument 1: cannot convert from 'in string' to 'in object'
                //         M2(in (rs = ref s));
                Diagnostic(ErrorCode.ERR_BadArgType, "rs = ref s").WithArguments("1", "in string", "in object").WithLocation(7, 16));
        }

        [Fact]
        public void RefEscapeInForeach()
        {
            var comp = CreateCompilationWithMscorlibAndSpan(@"