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

Clarify error message when attempting to ref a property #64764

Merged
merged 9 commits into from
Oct 18, 2022
2 changes: 1 addition & 1 deletion src/Compilers/CSharp/Portable/CSharpResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -985,7 +985,7 @@
<value>Cannot call an abstract base member: '{0}'</value>
</data>
<data name="ERR_RefProperty" xml:space="preserve">
<value>A property or indexer may not be passed as an out or ref parameter</value>
<value>An indexer or auto-implemented property may not be used as an out or ref value</value>
</data>
<data name="ERR_ManagedAddr" xml:space="preserve">
<value>Cannot take the address of, get the size of, or declare a pointer to a managed type ('{0}')</value>
Expand Down
4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ static void Main()
// (9,40): error CS1510: A ref or out value must be an assignable variable
// TypedReference tr3 = __makeref(123); // CS1510
Diagnostic(ErrorCode.ERR_RefLvalueExpected, "123").WithLocation(9, 40),
// (10,40): error CS0206: A property or indexer may not be passed as an out or ref parameter
// (10,40): error CS0206: An indexer or auto-implemented property may not be used as an out or ref value
// TypedReference tr4 = __makeref(P); // CS0206
Diagnostic(ErrorCode.ERR_RefProperty, "P").WithLocation(10, 40),
// (11,40): error CS0199: A static readonly field cannot be used as a ref or out value (except in a static constructor)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1278,7 +1278,7 @@ void M()
";
var comp = CreateCompilation(new[] { source, IsExternalInitTypeDefinition }, parseOptions: TestOptions.Regular9);
comp.VerifyEmitDiagnostics(
// (8,16): error CS0206: A property or indexer may not be passed as an out or ref parameter
// (8,16): error CS0206: An indexer or auto-implemented property may not be used as an out or ref value
// M2(out Property); // 1
Diagnostic(ErrorCode.ERR_RefProperty, "Property").WithLocation(8, 16)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33580,7 +33580,7 @@ public void M()
// (12,9): warning CS8602: Dereference of a possibly null reference.
// field.ToString(); // 4
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "field").WithLocation(12, 9),
// (14,15): error CS0206: A property or indexer may not be passed as an out or ref parameter
// (14,15): error CS0206: An indexer or auto-implemented property may not be used as an out or ref value
// M(ref Property); // 5
Diagnostic(ErrorCode.ERR_RefProperty, "Property").WithLocation(14, 15),
// (15,20): warning CS8625: Cannot convert null literal to non-nullable reference type.
Expand Down
61 changes: 61 additions & 0 deletions src/Compilers/CSharp/Test/Semantic/Semantics/RefFieldTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8828,6 +8828,67 @@ static void Main()
Diagnostic(ErrorCode.ERR_RefLocalOrParamExpected, "s.P2").WithLocation(22, 9));
}

[Fact]
[WorkItem(60807, "https://github.com/dotnet/roslyn/issues/60807")]
public void RefAndOut_PropertiesAndIndexers_As_ValuesAndParameters()
{
var source =
@"
var c = new C();
var r = new R();
//expressions
ref int n = ref c.N; //CS0206
ref var l = ref c[0]; //CS0206
ref var l2 = ref r[0];//OK
_ = M(ref c.N); //CS0206
_ = M(ref r.N); //OK
_ = M(ref c[0]); //CS0206
_ = M(ref r[0]); //OK
_ = M2(out c.N); //CS0206
_ = M2(out r.N); //OK
_ = M2(out c[0]); //CS0206
_ = M2(out r[0]); //OK
//defenitions
jjonescz marked this conversation as resolved.
Show resolved Hide resolved
static string M(ref int number) { return """"; }
static string M2(out int number) { number = 42; return """"; }
class C
{
public int N { get; set; }
private int[] arr = new int[100];
public int this[int i] => arr[i];
}
ref struct R
{
public R(){}
private ref int n;
public ref int N => ref n;
private ref int[] arr = new int[1];
public ref int this[int i] => ref arr[i];
}
";
var comp = CreateCompilation(source, targetFramework: TargetFramework.Net70);
comp.VerifyEmitDiagnostics(
// (5,17): error CS0206: An indexer or auto-implemented property may not be used as an out or ref value
// ref int n = ref c.N; //CS0206
Diagnostic(ErrorCode.ERR_RefProperty, "c.N").WithLocation(5, 17),
// (6,17): error CS0206: An indexer or auto-implemented property may not be used as an out or ref value
// ref var l = ref c[0]; //CS0206
Diagnostic(ErrorCode.ERR_RefProperty, "c[0]").WithLocation(6, 17),
// (8,11): error CS0206: An indexer or auto-implemented property may not be used as an out or ref value
// _ = M(ref c.N); //CS0206
Diagnostic(ErrorCode.ERR_RefProperty, "c.N").WithLocation(8, 11),
// (10,11): error CS0206: An indexer or auto-implemented property may not be used as an out or ref value
// _ = M(ref c[0]); //CS0206
Diagnostic(ErrorCode.ERR_RefProperty, "c[0]").WithLocation(10, 11),
// (12,12): error CS0206: An indexer or auto-implemented property may not be used as an out or ref value
// _ = M2(out c.N); //CS0206
Diagnostic(ErrorCode.ERR_RefProperty, "c.N").WithLocation(12, 12),
// (14,12): error CS0206: An indexer or auto-implemented property may not be used as an out or ref value
// _ = M2(out c[0]); //CS0206
Diagnostic(ErrorCode.ERR_RefProperty, "c[0]").WithLocation(14, 12)
);
}

[Fact]
public void RefAccessor_Value()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3178,7 +3178,7 @@ static void M()
";

CreateCompilationWithMscorlib46(text).VerifyDiagnostics(
// (8,26): error CS0206: A property or indexer may not be passed as an out or ref parameter
// (8,26): error CS0206: An indexer or auto-implemented property may not be used as an out or ref value
// ref int rl = ref P;
Diagnostic(ErrorCode.ERR_RefProperty, "P").WithLocation(8, 26));
}
Expand All @@ -3199,7 +3199,7 @@ void M()
";

CreateCompilationWithMscorlib46(text).VerifyDiagnostics(
// (8,26): error CS0206: A property or indexer may not be passed as an out or ref parameter
// (8,26): error CS0206: An indexer or auto-implemented property may not be used as an out or ref value
// ref int rl = ref this[0];
Diagnostic(ErrorCode.ERR_RefProperty, "this[0]").WithLocation(8, 26));
}
Expand Down
Loading