g__local|1_0'()
- IL_0005: ret
+ IL_0000: ldarg.0
+ IL_0001: ldarg.0
+ IL_0002: ldfld int32 C::'k__BackingField'
+ IL_0007: call instance int32 C::nameof(int32)
+ IL_000c: ret
} // end of method C::get_P
+ .method public hidebysig
+ instance int32 nameof (
+ int32 x
+ ) cil managed
+ {
+ // Method begins at RVA 0x205e
+ // Code size 2 (0x2)
+ .maxstack 8
+ IL_0000: ldc.i4.0
+ IL_0001: ret
+ } // end of method C::nameof
.method public hidebysig specialname rtspecialname
instance void .ctor () cil managed
{
- // Method begins at RVA 0x2057
+ // Method begins at RVA 0x2061
// Code size 7 (0x7)
.maxstack 8
IL_0000: ldarg.0
IL_0001: call instance void [mscorlib]System.Object::.ctor()
IL_0006: ret
} // end of method C::.ctor
- .method assembly hidebysig static
- int32 'g__local|1_0' () cil managed
- {
- .custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = (
- 01 00 00 00
- )
- .custom instance void MyAttribute::.ctor(string) = (
- 01 00 05 66 69 65 6c 64 00 00
- )
- // Method begins at RVA 0x205f
- // Code size 2 (0x2)
- .maxstack 8
- IL_0000: ldc.i4.0
- IL_0001: ret
- } // end of method C::'g__local|1_0'
// Properties
.property instance int32 P()
{
@@ -765,6 +819,81 @@ .property instance int32 P()
Assert.Equal(0, accessorBindingData.NumberOfPerformedAccessorBinding);
}
+ [Fact]
+ public void TestNameOfField_NameofIsLocalFunctionInvocation()
+ {
+ var comp = CreateCompilation(@"
+public class C
+{
+ public int P
+ {
+ get
+ {
+ return nameof(field);
+
+ int nameof(int x) => 0;
+ }
+ }
+}
+");
+ var accessorBindingData = new SourcePropertySymbolBase.AccessorBindingData();
+ comp.TestOnlyCompilationData = accessorBindingData;
+ comp.VerifyDiagnostics();
+ VerifyTypeIL(comp, "C", @"
+.class public auto ansi beforefieldinit C
+ extends [mscorlib]System.Object
+{
+ // Fields
+ .field private initonly int32 'k__BackingField'
+ .custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = (
+ 01 00 00 00
+ )
+ // Methods
+ .method public hidebysig specialname
+ instance int32 get_P () cil managed
+ {
+ // Method begins at RVA 0x2050
+ // Code size 12 (0xc)
+ .maxstack 8
+ IL_0000: ldarg.0
+ IL_0001: ldfld int32 C::'
k__BackingField'
+ IL_0006: call int32 C::'g__nameof|1_0'(int32)
+ IL_000b: ret
+ } // end of method C::get_P
+ .method public hidebysig specialname rtspecialname
+ instance void .ctor () cil managed
+ {
+ // Method begins at RVA 0x205d
+ // Code size 7 (0x7)
+ .maxstack 8
+ IL_0000: ldarg.0
+ IL_0001: call instance void [mscorlib]System.Object::.ctor()
+ IL_0006: ret
+ } // end of method C::.ctor
+ .method assembly hidebysig static
+ int32 'g__nameof|1_0' (
+ int32 x
+ ) cil managed
+ {
+ .custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = (
+ 01 00 00 00
+ )
+ // Method begins at RVA 0x2065
+ // Code size 2 (0x2)
+ .maxstack 8
+ IL_0000: ldc.i4.0
+ IL_0001: ret
+ } // end of method C::'g__nameof|1_0'
+ // Properties
+ .property instance int32 P()
+ {
+ .get instance int32 C::get_P()
+ }
+} // end of class C
+");
+ Assert.Equal(0, accessorBindingData.NumberOfPerformedAccessorBinding);
+ }
+
[Fact]
public void TestFieldIsShadowedByField_ReferencedFromRegularLambda()
{
@@ -844,6 +973,34 @@ .property instance int32 P()
Assert.Equal(0, accessorBindingData.NumberOfPerformedAccessorBinding);
}
+ [Theory]
+ [InlineData("private void field() { }")]
+ [InlineData("private int field { get => 0; }")]
+ [InlineData("private int field;")]
+ public void TestNameOfField_FieldIsMember(string member)
+ {
+ var comp = CreateCompilation($@"
+System.Console.WriteLine(new C().P);
+
+public class C
+{{
+ {member}
+
+ public string P
+ {{
+ get
+ {{
+ return nameof(field);
+ }}
+ }}
+}}
+");
+ var accessorBindingData = new SourcePropertySymbolBase.AccessorBindingData();
+ comp.TestOnlyCompilationData = accessorBindingData;
+ CompileAndVerify(comp, expectedOutput: "field");
+ Assert.Equal(0, accessorBindingData.NumberOfPerformedAccessorBinding);
+ }
+
[Fact(Skip = "PROTOTYPE(semi-auto-props): Assigning in constructor is not yet supported.")]
public void TestFieldOnlyGetter()
{
@@ -4535,6 +4692,8 @@ public MyAttribute(string s) { }
var fieldKeywordSymbolInfo = speculativeModel.GetSymbolInfo(fieldNode);
var fieldKeywordSymbolInfo2 = model.GetSpeculativeSymbolInfo(attributeSyntax.SpanStart, fieldNode, bindingOption);
+ Assert.Equal(comp.GetTypeByMetadataName("C").GetFieldsToEmit().Single(), fieldKeywordSymbolInfo.Symbol.GetSymbol());
+
if (bindingOption == SpeculativeBindingOption.BindAsTypeOrNamespace)
{
Assert.True(fieldKeywordSymbolInfo2.IsEmpty);
@@ -4542,8 +4701,7 @@ public MyAttribute(string s) { }
}
else
{
- Assert.Equal(fieldKeywordSymbolInfo, fieldKeywordSymbolInfo2);
- Assert.Equal(comp.GetTypeByMetadataName("C").GetFieldsToEmit().Single(), fieldKeywordSymbolInfo.Symbol.GetSymbol());
+ Assert.Equal(fieldKeywordSymbolInfo2, fieldKeywordSymbolInfo);
}
var typeInfo = model.GetSpeculativeTypeInfo(attributeSyntax.SpanStart, fieldNode, bindingOption);
@@ -4597,14 +4755,16 @@ public MyAttribute(string s) { }
var fieldKeywordSymbolInfo = speculativeModel.GetSymbolInfo(fieldNode);
var fieldKeywordSymbolInfo2 = model.GetSpeculativeSymbolInfo(attributeSyntax.SpanStart, fieldNode, bindingOption);
- if (bindingOption == SpeculativeBindingOption.BindAsExpression)
+ Assert.Equal(comp.GetTypeByMetadataName("C").GetFieldsToEmit().Single(), fieldKeywordSymbolInfo.Symbol.GetSymbol());
+
+ if (bindingOption == SpeculativeBindingOption.BindAsTypeOrNamespace)
{
- Assert.Equal(fieldKeywordSymbolInfo, fieldKeywordSymbolInfo2);
+ Assert.True(fieldKeywordSymbolInfo2.IsEmpty);
+ Assert.Null(fieldKeywordSymbolInfo2.Symbol);
}
else
{
- Assert.True(fieldKeywordSymbolInfo2.IsEmpty);
- Assert.Null(fieldKeywordSymbolInfo2.Symbol);
+ Assert.Equal(fieldKeywordSymbolInfo2, fieldKeywordSymbolInfo);
}
var typeInfo = model.GetSpeculativeTypeInfo(attributeSyntax.SpanStart, fieldNode, bindingOption);
@@ -4616,7 +4776,6 @@ public MyAttribute(string s) { }
Assert.Null(aliasInfo);
Assert.Equal("System.Int32 C.k__BackingField", comp.GetTypeByMetadataName("C").GetFieldsToEmit().Single().ToTestDisplayString());
- Assert.Equal(comp.GetTypeByMetadataName("C").GetFieldsToEmit().Single(), fieldKeywordSymbolInfo.Symbol.GetSymbol());
Assert.Equal(runNullableAnalysis == "always" ? 0 : 1, accessorBindingData.NumberOfPerformedAccessorBinding);
}