Skip to content

Commit

Permalink
add test demonstrating legal and illegal combinations of ref/out and …
Browse files Browse the repository at this point in the history
…properties in which the new message still makes sense
  • Loading branch information
ArcadeMode committed Oct 16, 2022
1 parent 14896d5 commit ac14042
Showing 1 changed file with 32 additions and 19 deletions.
51 changes: 32 additions & 19 deletions src/Compilers/CSharp/Test/Semantic/Semantics/RefFieldTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8832,31 +8832,44 @@ static void Main()
/// Ref auto-properties are not supported.
/// </summary>
[Fact]
public void RefAutoProperty_03()
public void RefAndOutProperties()
{
var source =
@"using System;
class C
{
public static string P0 { get; }
public static string P1 { get; set; }
}
class Program
@"
ref string S = ref C.S; //CS0206
var c = new C();
var str = C.M(ref c.N); //ref prop
var str2 = C.M(ref c.N2); //CS0206
var str3 = C.M2(out c.N); //ref prop
var str4 = C.M2(out c.N2); //CS0206
ref struct C
{
static void Main()
public static string S { get; set; }
private ref int n;
public ref int N => ref n;
public int N2 { get; set; }
public static string M(ref int number)
{
ref var p0 = ref C.P0;
ref var p1 = ref C.P1;
return number.ToString();
}
}";
var comp = CreateCompilation(source);
public static string M2(out int number)
{
number = 42;
return number.ToString();
}
}
";
var comp = CreateCompilation(source, targetFramework: TargetFramework.Net70);
comp.VerifyEmitDiagnostics(
// (11,26): error CS8145: Auto-implemented properties cannot return by reference
// ref var p0 = ref C.P0;
Diagnostic(ErrorCode.ERR_AutoPropertyCannotBeRefReturning, "C.P0").WithLocation(11, 26),
// (12,26): error CS8145: Auto-implemented properties cannot return by reference
// ref var p1 = ref C.P1;
Diagnostic(ErrorCode.ERR_AutoPropertyCannotBeRefReturning, "C.P1").WithLocation(12, 26)
// (2,20): error CS0206: An indexer or auto-implemented property may not be used as an out or ref value
// ref string S = ref C.S; //CS0206
Diagnostic(ErrorCode.ERR_RefProperty, "C.S").WithLocation(2, 20),
// (5,20): error CS0206: An indexer or auto-implemented property may not be used as an out or ref value
// var str2 = C.M(ref c.N2);
Diagnostic(ErrorCode.ERR_RefProperty, "c.N2").WithLocation(5, 20),
// (7,21): error CS0206: An indexer or auto-implemented property may not be used as an out or ref value
// var str4 = C.M2(out c.N2);
Diagnostic(ErrorCode.ERR_RefProperty, "c.N2").WithLocation(7, 21)
);
}

Expand Down

0 comments on commit ac14042

Please sign in to comment.