k__BackingField""
+ IL_0022: ret
+}
+");
}
[Fact]
@@ -2841,15 +2891,32 @@ public static void Main()
}
}
";
- var comp = CreateCompilation(src);
+ var comp = CreateCompilation(new[] { src, IsExternalInitTypeDefinition }, parseOptions: TestOptions.Regular10);
comp.VerifyDiagnostics(
- // (2,15): error CS0843: Auto-implemented property 'R.P' must be fully assigned before control is returned to the caller.
+ // (2,15): error CS0843: Auto-implemented property 'R.P' must be fully assigned before control is returned to the caller. Consider updating to language version 'preview' to auto-default the property.
// record struct R(int P = 42)
- Diagnostic(ErrorCode.ERR_UnassignedThisAutoProperty, "R").WithArguments("R.P").WithLocation(2, 15),
+ Diagnostic(ErrorCode.ERR_UnassignedThisAutoPropertyUnsupportedVersion, "R").WithArguments("R.P", "preview").WithLocation(2, 15),
// (2,21): warning CS8907: Parameter 'P' is unread. Did you forget to use it to initialize the property with that name?
// record struct R(int P = 42)
Diagnostic(ErrorCode.WRN_UnreadRecordParameter, "P").WithArguments("P").WithLocation(2, 21)
);
+
+ var verifier = CompileAndVerify(new[] { src, IsExternalInitTypeDefinition }, parseOptions: TestOptions.RegularNext, verify: Verification.Skipped);
+ verifier.VerifyDiagnostics(
+ // (2,21): warning CS8907: Parameter 'P' is unread. Did you forget to use it to initialize the property with that name?
+ // record struct R(int P = 42)
+ Diagnostic(ErrorCode.WRN_UnreadRecordParameter, "P").WithArguments("P").WithLocation(2, 21)
+ );
+ verifier.VerifyIL("R..ctor(int)", @"
+{
+ // Code size 8 (0x8)
+ .maxstack 2
+ IL_0000: ldarg.0
+ IL_0001: ldc.i4.0
+ IL_0002: stfld ""int R.k__BackingField""
+ IL_0007: ret
+}
+");
}
[Fact]
@@ -3932,11 +3999,18 @@ record struct Pos2(int X)
public int X { get { return x; } set { x = value; } }
}
";
- var comp = CreateCompilation(source);
+ var comp = CreateCompilation(source, parseOptions: TestOptions.Regular10);
comp.VerifyEmitDiagnostics(
- // (2,15): error CS0171: Field 'Pos.x' must be fully assigned before control is returned to the caller
+ // (2,15): error CS0171: Field 'Pos.x' must be fully assigned before control is returned to the caller. Consider updating to language version 'preview' to auto-default the field.
// record struct Pos(int X)
- Diagnostic(ErrorCode.ERR_UnassignedThis, "Pos").WithArguments("Pos.x").WithLocation(2, 15),
+ Diagnostic(ErrorCode.ERR_UnassignedThisUnsupportedVersion, "Pos").WithArguments("Pos.x", "preview").WithLocation(2, 15),
+ // (5,16): error CS8050: Only auto-implemented properties can have initializers.
+ // public int X { get { return x; } set { x = value; } } = X;
+ Diagnostic(ErrorCode.ERR_InitializerOnNonAutoProperty, "X").WithArguments("Pos.X").WithLocation(5, 16)
+ );
+
+ comp = CreateCompilation(source, parseOptions: TestOptions.RegularNext);
+ comp.VerifyEmitDiagnostics(
// (5,16): error CS8050: Only auto-implemented properties can have initializers.
// public int X { get { return x; } set { x = value; } } = X;
Diagnostic(ErrorCode.ERR_InitializerOnNonAutoProperty, "X").WithArguments("Pos.X").WithLocation(5, 16)
@@ -7451,12 +7525,34 @@ record struct C
}
";
- var comp = CreateCompilation(src);
+ var comp = CreateCompilation(src, parseOptions: TestOptions.Regular10);
comp.VerifyDiagnostics(
- // (8,13): error CS0188: The 'this' object cannot be used before all of its fields have been assigned
+ // (8,13): error CS0188: The 'this' object cannot be used before all of its fields have been assigned. Consider updating to language version 'this' to auto-default the unassigned fields.
// _ = this with { X = 42 }; // 1
- Diagnostic(ErrorCode.ERR_UseDefViolationThis, "this").WithArguments("this").WithLocation(8, 13)
+ Diagnostic(ErrorCode.ERR_UseDefViolationThisUnsupportedVersion, "this").WithArguments("this", "preview").WithLocation(8, 13)
);
+
+ var verifier = CompileAndVerify(src, parseOptions: TestOptions.RegularNext);
+ verifier.VerifyDiagnostics();
+ verifier.VerifyIL("C..ctor(string)", @"
+{
+ // Code size 31 (0x1f)
+ .maxstack 2
+ .locals init (C V_0)
+ IL_0000: ldarg.0
+ IL_0001: ldc.i4.0
+ IL_0002: stfld ""int C.k__BackingField""
+ IL_0007: ldarg.0
+ IL_0008: ldobj ""C""
+ IL_000d: stloc.0
+ IL_000e: ldloca.s V_0
+ IL_0010: ldc.i4.s 42
+ IL_0012: call ""void C.X.set""
+ IL_0017: ldarg.0
+ IL_0018: initobj ""C""
+ IL_001e: ret
+}
+");
}
[Fact]
@@ -11131,11 +11227,20 @@ public void ExplicitConstructors_10()
public object F;
public S(int i) : this() { F = i; }
}";
- var comp = CreateCompilation(source);
+ var comp = CreateCompilation(source, parseOptions: TestOptions.Regular10);
comp.VerifyDiagnostics(
- // (1,15): error CS0171: Field 'S.F' must be fully assigned before control is returned to the caller
+ // (1,15): error CS0171: Field 'S.F' must be fully assigned before control is returned to the caller. Consider updating to language version 'preview' to auto-default the field.
+ // record struct S(object F)
+ Diagnostic(ErrorCode.ERR_UnassignedThisUnsupportedVersion, "S").WithArguments("S.F", "preview").WithLocation(1, 15),
+ // (1,24): warning CS8907: Parameter 'F' is unread. Did you forget to use it to initialize the property with that name?
// record struct S(object F)
- Diagnostic(ErrorCode.ERR_UnassignedThis, "S").WithArguments("S.F").WithLocation(1, 15),
+ Diagnostic(ErrorCode.WRN_UnreadRecordParameter, "F").WithArguments("F").WithLocation(1, 24),
+ // (4,23): error CS8982: A constructor declared in a 'record struct' with parameter list must have a 'this' initializer that calls the primary constructor or an explicitly declared constructor.
+ // public S(int i) : this() { F = i; }
+ Diagnostic(ErrorCode.ERR_RecordStructConstructorCallsDefaultConstructor, "this").WithLocation(4, 23));
+
+ comp = CreateCompilation(source, parseOptions: TestOptions.RegularNext);
+ comp.VerifyDiagnostics(
// (1,24): warning CS8907: Parameter 'F' is unread. Did you forget to use it to initialize the property with that name?
// record struct S(object F)
Diagnostic(ErrorCode.WRN_UnreadRecordParameter, "F").WithArguments("F").WithLocation(1, 24),
diff --git a/src/Compilers/CSharp/Test/Semantic/Semantics/SemanticErrorTests.cs b/src/Compilers/CSharp/Test/Semantic/Semantics/SemanticErrorTests.cs
index 19e90100b283e..9412dcf3b0640 100644
--- a/src/Compilers/CSharp/Test/Semantic/Semantics/SemanticErrorTests.cs
+++ b/src/Compilers/CSharp/Test/Semantic/Semantics/SemanticErrorTests.cs
@@ -6464,11 +6464,21 @@ public static void Main()
MyStruct aStruct = new MyStruct();
}
}";
- var comp = CreateCompilation(text);
+ var comp = CreateCompilation(text, parseOptions: TestOptions.Regular10);
comp.VerifyDiagnostics(
- // (4,4): error CS0171: Field 'MyStruct.i' must be fully assigned before control is returned to the caller
+ // (4,4): error CS0171: Field 'MyStruct.i' must be fully assigned before control is returned to the caller. Consider updating to language version 'preview' to auto-default the field.
// MyStruct(int initField) // CS0171
- Diagnostic(ErrorCode.ERR_UnassignedThis, "MyStruct").WithArguments("MyStruct.i"),
+ Diagnostic(ErrorCode.ERR_UnassignedThisUnsupportedVersion, "MyStruct").WithArguments("MyStruct.i", "preview").WithLocation(4, 4),
+ // (15,16): warning CS0219: The variable 'aStruct' is assigned but its value is never used
+ // MyStruct aStruct = new MyStruct();
+ Diagnostic(ErrorCode.WRN_UnreferencedVarAssg, "aStruct").WithArguments("aStruct").WithLocation(15, 16),
+ // (8,15): warning CS0649: Field 'MyStruct.i' is never assigned to, and will always have its default value 0
+ // public int i;
+ Diagnostic(ErrorCode.WRN_UnassignedInternalField, "i").WithArguments("MyStruct.i", "0").WithLocation(8, 15)
+ );
+
+ var verifier = CompileAndVerify(text, parseOptions: TestOptions.RegularNext);
+ verifier.VerifyDiagnostics(
// (15,16): warning CS0219: The variable 'aStruct' is assigned but its value is never used
// MyStruct aStruct = new MyStruct();
Diagnostic(ErrorCode.WRN_UnreferencedVarAssg, "aStruct").WithArguments("aStruct"),
@@ -6476,6 +6486,15 @@ public static void Main()
// public int i;
Diagnostic(ErrorCode.WRN_UnassignedInternalField, "i").WithArguments("MyStruct.i", "0")
);
+ verifier.VerifyIL("MyStruct..ctor", @"
+{
+ // Code size 8 (0x8)
+ .maxstack 2
+ IL_0000: ldarg.0
+ IL_0001: ldc.i4.0
+ IL_0002: stfld ""int MyStruct.i""
+ IL_0007: ret
+}");
}
[Fact]
@@ -6961,14 +6980,31 @@ public static void Main()
}
}";
- CreateCompilation(text).
+ CreateCompilation(text, parseOptions: TestOptions.Regular10).
VerifyDiagnostics(
- // (17,17): error CS0188: The 'this' object cannot be used before all of its fields are assigned to
+ // (17,17): error CS0188: The 'this' object cannot be used before all of its fields have been assigned. Consider updating to language version 'this' to auto-default the unassigned fields.
// Goo(); // CS0188
- Diagnostic(ErrorCode.ERR_UseDefViolationThis, "Goo").WithArguments("this"),
+ Diagnostic(ErrorCode.ERR_UseDefViolationThisUnsupportedVersion, "Goo").WithArguments("this", "preview").WithLocation(17, 17),
+ // (8,24): warning CS0649: Field 'MyClass.S.a' is never assigned to, and will always have its default value 0
+ // public int a;
+ Diagnostic(ErrorCode.WRN_UnassignedInternalField, "a").WithArguments("MyNamespace.MyClass.S.a", "0").WithLocation(8, 24));
+
+ var verifier = CompileAndVerify(text, parseOptions: TestOptions.RegularNext).
+ VerifyDiagnostics(
// (8,24): warning CS0649: Field 'MyNamespace.MyClass.S.a' is never assigned to, and will always have its default value 0
// public int a;
Diagnostic(ErrorCode.WRN_UnassignedInternalField, "a").WithArguments("MyNamespace.MyClass.S.a", "0"));
+ verifier.VerifyIL("MyNamespace.MyClass.S..ctor", @"
+{
+ // Code size 14 (0xe)
+ .maxstack 2
+ IL_0000: ldarg.0
+ IL_0001: ldc.i4.0
+ IL_0002: stfld ""int MyNamespace.MyClass.S.a""
+ IL_0007: ldarg.0
+ IL_0008: call ""void MyNamespace.MyClass.S.Goo()""
+ IL_000d: ret
+}");
}
[Fact, WorkItem(579533, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/579533"), WorkItem(864605, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/864605")]
@@ -6992,11 +7028,17 @@ void F()
}
}";
- CreateCompilationWithMscorlib40AndSystemCore(source).VerifyDiagnostics(
+ CreateCompilationWithMscorlib40AndSystemCore(source, parseOptions: TestOptions.Regular10).VerifyDiagnostics(
// (10,18): error CS0837: The first operand of an 'is' or 'as' operator may not be a lambda expression, anonymous method, or method group.
+ // var b1 = F is Action;
Diagnostic(ErrorCode.ERR_LambdaInIsAs, "F is Action").WithLocation(10, 18),
- // (10,18): error CS0188: The 'this' object cannot be used before all of its fields are assigned to
- Diagnostic(ErrorCode.ERR_UseDefViolationThis, "F").WithArguments("this"));
+ // (10,18): error CS0188: The 'this' object cannot be used before all of its fields have been assigned. Consider updating to language version 'this' to auto-default the unassigned fields.
+ // var b1 = F is Action;
+ Diagnostic(ErrorCode.ERR_UseDefViolationThisUnsupportedVersion, "F").WithArguments("this", "preview").WithLocation(10, 18));
+
+ CreateCompilationWithMscorlib40AndSystemCore(source, parseOptions: TestOptions.RegularNext).VerifyDiagnostics(
+ // (10,18): error CS0837: The first operand of an 'is' or 'as' operator may not be a lambda expression, anonymous method, or method group.
+ Diagnostic(ErrorCode.ERR_LambdaInIsAs, "F is Action").WithLocation(10, 18));
}
[Fact, WorkItem(579533, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/579533"), WorkItem(864605, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/864605")]
@@ -7020,13 +7062,18 @@ void F()
}
}";
- CreateCompilationWithMscorlib40AndSystemCore(source).VerifyDiagnostics(
+ CreateCompilationWithMscorlib40AndSystemCore(source, parseOptions: TestOptions.Regular10).VerifyDiagnostics(
// (10,18): error CS0837: The first operand of an 'is' or 'as' operator may not be a lambda expression, anonymous method, or method group.
// var b1 = this.F is Action;
Diagnostic(ErrorCode.ERR_LambdaInIsAs, "this.F is Action").WithLocation(10, 18),
- // (10,18): error CS0188: The 'this' object cannot be used before all of its fields are assigned to
+ // (10,18): error CS0188: The 'this' object cannot be used before all of its fields have been assigned. Consider updating to language version 'this' to auto-default the unassigned fields.
// var b1 = this.F is Action;
- Diagnostic(ErrorCode.ERR_UseDefViolationThis, "this").WithArguments("this"));
+ Diagnostic(ErrorCode.ERR_UseDefViolationThisUnsupportedVersion, "this").WithArguments("this", "preview").WithLocation(10, 18));
+
+ CreateCompilationWithMscorlib40AndSystemCore(source, parseOptions: TestOptions.RegularNext).VerifyDiagnostics(
+ // (10,18): error CS0837: The first operand of an 'is' or 'as' operator may not be a lambda expression, anonymous method, or method group.
+ // var b1 = this.F is Action;
+ Diagnostic(ErrorCode.ERR_LambdaInIsAs, "this.F is Action").WithLocation(10, 18));
}
[Fact, WorkItem(579533, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/579533")]
@@ -7051,9 +7098,53 @@ void Add(int value)
}
}
";
- CreateCompilationWithMscorlib40AndSystemCore(source).VerifyDiagnostics(
- // (10,19): error CS0188: The 'this' object cannot be used before all of its fields are assigned to
- Diagnostic(ErrorCode.ERR_UseDefViolationThis, "Add").WithArguments("this"));
+ CreateCompilationWithMscorlib40AndSystemCore(source, parseOptions: TestOptions.Regular10).VerifyDiagnostics(
+ // (10,19): error CS0188: The 'this' object cannot be used before all of its fields have been assigned. Consider updating to language version 'this' to auto-default the unassigned fields.
+ // /*this.*/ Add(d);
+ Diagnostic(ErrorCode.ERR_UseDefViolationThisUnsupportedVersion, "Add").WithArguments("this", "preview").WithLocation(10, 19));
+
+ var verifier = CompileAndVerify(source, new[] { CSharpRef }, parseOptions: TestOptions.RegularNext);
+ verifier.VerifyDiagnostics();
+ verifier.VerifyIL("S..ctor", @"
+{
+ // Code size 105 (0x69)
+ .maxstack 9
+ IL_0000: ldarg.0
+ IL_0001: ldnull
+ IL_0002: stfld ""dynamic S.value""
+ IL_0007: ldsfld ""System.Runtime.CompilerServices.CallSite<<>A{00000004}> S.<>o__1.<>p__0""
+ IL_000c: brtrue.s IL_004d
+ IL_000e: ldc.i4 0x102
+ IL_0013: ldstr ""Add""
+ IL_0018: ldnull
+ IL_0019: ldtoken ""S""
+ IL_001e: call ""System.Type System.Type.GetTypeFromHandle(System.RuntimeTypeHandle)""
+ IL_0023: ldc.i4.2
+ IL_0024: newarr ""Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo""
+ IL_0029: dup
+ IL_002a: ldc.i4.0
+ IL_002b: ldc.i4.s 9
+ IL_002d: ldnull
+ IL_002e: call ""Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo.Create(Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfoFlags, string)""
+ IL_0033: stelem.ref
+ IL_0034: dup
+ IL_0035: ldc.i4.1
+ IL_0036: ldc.i4.0
+ IL_0037: ldnull
+ IL_0038: call ""Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo.Create(Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfoFlags, string)""
+ IL_003d: stelem.ref
+ IL_003e: call ""System.Runtime.CompilerServices.CallSiteBinder Microsoft.CSharp.RuntimeBinder.Binder.InvokeMember(Microsoft.CSharp.RuntimeBinder.CSharpBinderFlags, string, System.Collections.Generic.IEnumerable, System.Type, System.Collections.Generic.IEnumerable)""
+ IL_0043: call ""System.Runtime.CompilerServices.CallSite<<>A{00000004}> System.Runtime.CompilerServices.CallSite<<>A{00000004}>.Create(System.Runtime.CompilerServices.CallSiteBinder)""
+ IL_0048: stsfld ""System.Runtime.CompilerServices.CallSite<<>A{00000004}> S.<>o__1.<>p__0""
+ IL_004d: ldsfld ""System.Runtime.CompilerServices.CallSite<<>A{00000004}> S.<>o__1.<>p__0""
+ IL_0052: ldfld ""<>A{00000004} System.Runtime.CompilerServices.CallSite<<>A{00000004}>.Target""
+ IL_0057: ldsfld ""System.Runtime.CompilerServices.CallSite<<>A{00000004}> S.<>o__1.<>p__0""
+ IL_005c: ldarg.0
+ IL_005d: ldarg.1
+ IL_005e: callvirt ""void <>A{00000004}.Invoke(System.Runtime.CompilerServices.CallSite, ref S, dynamic)""
+ IL_0063: newobj ""System.NotImplementedException..ctor()""
+ IL_0068: throw
+}");
}
[Fact, WorkItem(579533, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/579533")]
@@ -7078,9 +7169,53 @@ void Add(int value)
}
}
";
- CreateCompilationWithMscorlib40AndSystemCore(source).VerifyDiagnostics(
- // (10,9): error CS0188: The 'this' object cannot be used before all of its fields are assigned to
- Diagnostic(ErrorCode.ERR_UseDefViolationThis, "this").WithArguments("this"));
+ CreateCompilationWithMscorlib40AndSystemCore(source, parseOptions: TestOptions.Regular10).VerifyDiagnostics(
+ // (10,9): error CS0188: The 'this' object cannot be used before all of its fields have been assigned. Consider updating to language version 'this' to auto-default the unassigned fields.
+ // this.Add(d);
+ Diagnostic(ErrorCode.ERR_UseDefViolationThisUnsupportedVersion, "this").WithArguments("this", "preview").WithLocation(10, 9));
+
+ var verifier = CompileAndVerify(source, new[] { CSharpRef }, parseOptions: TestOptions.RegularNext);
+ verifier.VerifyDiagnostics();
+ verifier.VerifyIL("S..ctor", @"
+{
+ // Code size 105 (0x69)
+ .maxstack 9
+ IL_0000: ldarg.0
+ IL_0001: ldnull
+ IL_0002: stfld ""dynamic S.value""
+ IL_0007: ldsfld ""System.Runtime.CompilerServices.CallSite<<>A{00000004}> S.<>o__1.<>p__0""
+ IL_000c: brtrue.s IL_004d
+ IL_000e: ldc.i4 0x100
+ IL_0013: ldstr ""Add""
+ IL_0018: ldnull
+ IL_0019: ldtoken ""S""
+ IL_001e: call ""System.Type System.Type.GetTypeFromHandle(System.RuntimeTypeHandle)""
+ IL_0023: ldc.i4.2
+ IL_0024: newarr ""Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo""
+ IL_0029: dup
+ IL_002a: ldc.i4.0
+ IL_002b: ldc.i4.s 9
+ IL_002d: ldnull
+ IL_002e: call ""Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo.Create(Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfoFlags, string)""
+ IL_0033: stelem.ref
+ IL_0034: dup
+ IL_0035: ldc.i4.1
+ IL_0036: ldc.i4.0
+ IL_0037: ldnull
+ IL_0038: call ""Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo.Create(Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfoFlags, string)""
+ IL_003d: stelem.ref
+ IL_003e: call ""System.Runtime.CompilerServices.CallSiteBinder Microsoft.CSharp.RuntimeBinder.Binder.InvokeMember(Microsoft.CSharp.RuntimeBinder.CSharpBinderFlags, string, System.Collections.Generic.IEnumerable, System.Type, System.Collections.Generic.IEnumerable)""
+ IL_0043: call ""System.Runtime.CompilerServices.CallSite<<>A{00000004}> System.Runtime.CompilerServices.CallSite<<>A{00000004}>.Create(System.Runtime.CompilerServices.CallSiteBinder)""
+ IL_0048: stsfld ""System.Runtime.CompilerServices.CallSite<<>A{00000004}> S.<>o__1.<>p__0""
+ IL_004d: ldsfld ""System.Runtime.CompilerServices.CallSite<<>A{00000004}> S.<>o__1.<>p__0""
+ IL_0052: ldfld ""<>A{00000004} System.Runtime.CompilerServices.CallSite<<>A{00000004}>.Target""
+ IL_0057: ldsfld ""System.Runtime.CompilerServices.CallSite<<>A{00000004}> S.<>o__1.<>p__0""
+ IL_005c: ldarg.0
+ IL_005d: ldarg.1
+ IL_005e: callvirt ""void <>A{00000004}.Invoke(System.Runtime.CompilerServices.CallSite, ref S, dynamic)""
+ IL_0063: newobj ""System.NotImplementedException..ctor()""
+ IL_0068: throw
+}");
}
[Fact]
@@ -12781,8 +12916,23 @@ static int Main()
}
}
";
- DiagnosticsUtils.VerifyErrorsAndGetCompilationWithMscorlib(text,
- new ErrorDescription[] { new ErrorDescription { Code = (int)ErrorCode.ERR_UnassignedThisAutoProperty, Line = 5, Column = 12 } });
+ CreateCompilation(text, parseOptions: TestOptions.Regular10)
+ .VerifyDiagnostics(
+ // (5,12): error CS0843: Auto-implemented property 'S.AIProp' must be fully assigned before control is returned to the caller. Consider updating to language version 'preview' to auto-default the property.
+ // public S(int i) { } //CS0843
+ Diagnostic(ErrorCode.ERR_UnassignedThisAutoPropertyUnsupportedVersion, "S").WithArguments("S.AIProp", "preview").WithLocation(5, 12));
+
+ var verifier = CompileAndVerify(text, parseOptions: TestOptions.RegularNext);
+ verifier.VerifyDiagnostics();
+ verifier.VerifyIL("S..ctor", @"
+{
+ // Code size 8 (0x8)
+ .maxstack 2
+ IL_0000: ldarg.0
+ IL_0001: ldc.i4.0
+ IL_0002: stfld ""int S.k__BackingField""
+ IL_0007: ret
+}");
}
[Fact]
diff --git a/src/Compilers/CSharp/Test/Semantic/Semantics/StructConstructorTests.cs b/src/Compilers/CSharp/Test/Semantic/Semantics/StructConstructorTests.cs
index fec19fb91cccc..330be3c3ed098 100644
--- a/src/Compilers/CSharp/Test/Semantic/Semantics/StructConstructorTests.cs
+++ b/src/Compilers/CSharp/Test/Semantic/Semantics/StructConstructorTests.cs
@@ -876,21 +876,18 @@ static void Main()
// (13,12): error CS8773: Feature 'parameterless struct constructors' is not available in C# 9.0. Please use language version 10.0 or greater.
// public S1() { Y = 1; }
Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion9, "S1").WithArguments("parameterless struct constructors", "10.0").WithLocation(13, 12),
- // (13,12): error CS0171: Field 'S1.X' must be fully assigned before control is returned to the caller
+ // (13,12): error CS0171: Field 'S1.X' must be fully assigned before control is returned to the caller. Consider updating to language version 'preview' to auto-default the field.
// public S1() { Y = 1; }
- Diagnostic(ErrorCode.ERR_UnassignedThis, "S1").WithArguments("S1.X").WithLocation(13, 12),
- // (20,12): error CS0171: Field 'S2.X' must be fully assigned before control is returned to the caller
+ Diagnostic(ErrorCode.ERR_UnassignedThisUnsupportedVersion, "S1").WithArguments("S1.X", "preview").WithLocation(13, 12),
+ // (20,12): error CS0171: Field 'S2.X' must be fully assigned before control is returned to the caller. Consider updating to language version 'preview' to auto-default the field.
// public S2(object y) { Y = y; }
- Diagnostic(ErrorCode.ERR_UnassignedThis, "S2").WithArguments("S2.X").WithLocation(20, 12));
+ Diagnostic(ErrorCode.ERR_UnassignedThisUnsupportedVersion, "S2").WithArguments("S2.X", "preview").WithLocation(20, 12));
- comp = CreateCompilation(source, options: TestOptions.ReleaseExe);
- comp.VerifyDiagnostics(
- // (13,12): error CS0171: Field 'S1.X' must be fully assigned before control is returned to the caller
- // public S1() { Y = 1; }
- Diagnostic(ErrorCode.ERR_UnassignedThis, "S1").WithArguments("S1.X").WithLocation(13, 12),
- // (20,12): error CS0171: Field 'S2.X' must be fully assigned before control is returned to the caller
- // public S2(object y) { Y = y; }
- Diagnostic(ErrorCode.ERR_UnassignedThis, "S2").WithArguments("S2.X").WithLocation(20, 12));
+ var verifier = CompileAndVerify(source, parseOptions: TestOptions.RegularNext, expectedOutput:
+@"(, )
+(, 1)
+(, )");
+ verifier.VerifyDiagnostics();
}
[Fact]
@@ -1397,18 +1394,18 @@ struct S4
// (11,12): error CS8773: Feature 'parameterless struct constructors' is not available in C# 9.0. Please use language version 10.0 or greater.
// public S2() { }
Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion9, "S2").WithArguments("parameterless struct constructors", "10.0").WithLocation(11, 12),
- // (11,12): error CS0171: Field 'S2.Y' must be fully assigned before control is returned to the caller
+ // (11,12): error CS0171: Field 'S2.Y' must be fully assigned before control is returned to the caller. Consider updating to language version 'preview' to auto-default the field.
// public S2() { }
- Diagnostic(ErrorCode.ERR_UnassignedThis, "S2").WithArguments("S2.Y").WithLocation(11, 12),
+ Diagnostic(ErrorCode.ERR_UnassignedThisUnsupportedVersion, "S2").WithArguments("S2.Y", "preview").WithLocation(11, 12),
// (16,21): error CS8773: Feature 'struct field initializers' is not available in C# 9.0. Please use language version 10.0 or greater.
// internal object Y = 3;
Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion9, "Y").WithArguments("struct field initializers", "10.0").WithLocation(16, 21),
// (17,12): error CS8773: Feature 'parameterless struct constructors' is not available in C# 9.0. Please use language version 10.0 or greater.
// public S3() { Y = 3; }
Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion9, "S3").WithArguments("parameterless struct constructors", "10.0").WithLocation(17, 12),
- // (17,12): error CS0171: Field 'S3.X' must be fully assigned before control is returned to the caller
+ // (17,12): error CS0171: Field 'S3.X' must be fully assigned before control is returned to the caller. Consider updating to language version 'preview' to auto-default the field.
// public S3() { Y = 3; }
- Diagnostic(ErrorCode.ERR_UnassignedThis, "S3").WithArguments("S3.X").WithLocation(17, 12),
+ Diagnostic(ErrorCode.ERR_UnassignedThisUnsupportedVersion, "S3").WithArguments("S3.X", "preview").WithLocation(17, 12),
// (22,21): error CS8773: Feature 'struct field initializers' is not available in C# 9.0. Please use language version 10.0 or greater.
// internal object Y = 4;
Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion9, "Y").WithArguments("struct field initializers", "10.0").WithLocation(22, 21),
@@ -1416,24 +1413,23 @@ struct S4
// public S4() { X = 4; }
Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion9, "S4").WithArguments("parameterless struct constructors", "10.0").WithLocation(23, 12));
- var expectedDiagnostics = new[]
- {
+ comp = CreateCompilation(source, parseOptions: TestOptions.Regular10);
+ comp.VerifyDiagnostics(
// (2,8): error CS8983: A 'struct' with field initializers must include an explicitly declared constructor.
// struct S1
Diagnostic(ErrorCode.ERR_StructHasInitializersAndNoDeclaredConstructor, "S1").WithLocation(2, 8),
- // (11,12): error CS0171: Field 'S2.Y' must be fully assigned before control is returned to the caller
+ // (11,12): error CS0171: Field 'S2.Y' must be fully assigned before control is returned to the caller. Consider updating to language version 'preview' to auto-default the field.
// public S2() { }
- Diagnostic(ErrorCode.ERR_UnassignedThis, "S2").WithArguments("S2.Y").WithLocation(11, 12),
- // (17,12): error CS0171: Field 'S3.X' must be fully assigned before control is returned to the caller
+ Diagnostic(ErrorCode.ERR_UnassignedThisUnsupportedVersion, "S2").WithArguments("S2.Y", "preview").WithLocation(11, 12),
+ // (17,12): error CS0171: Field 'S3.X' must be fully assigned before control is returned to the caller. Consider updating to language version 'preview' to auto-default the field.
// public S3() { Y = 3; }
- Diagnostic(ErrorCode.ERR_UnassignedThis, "S3").WithArguments("S3.X").WithLocation(17, 12),
- };
+ Diagnostic(ErrorCode.ERR_UnassignedThisUnsupportedVersion, "S3").WithArguments("S3.X", "preview").WithLocation(17, 12));
- comp = CreateCompilation(source, parseOptions: TestOptions.Regular10);
- comp.VerifyDiagnostics(expectedDiagnostics);
-
- comp = CreateCompilation(source);
- comp.VerifyDiagnostics(expectedDiagnostics);
+ comp = CreateCompilation(source, parseOptions: TestOptions.RegularNext);
+ comp.VerifyDiagnostics(
+ // (2,8): error CS8983: A 'struct' with field initializers must include an explicitly declared constructor.
+ // struct S1
+ Diagnostic(ErrorCode.ERR_StructHasInitializersAndNoDeclaredConstructor, "S1").WithLocation(2, 8));
}
[WorkItem(57870, "https://github.com/dotnet/roslyn/issues/57870")]
@@ -1480,18 +1476,18 @@ struct S4
// (11,12): error CS8773: Feature 'parameterless struct constructors' is not available in C# 9.0. Please use language version 10.0 or greater.
// public S2() { }
Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion9, "S2").WithArguments("parameterless struct constructors", "10.0").WithLocation(11, 12),
- // (11,12): error CS0843: Auto-implemented property 'S2.Y' must be fully assigned before control is returned to the caller.
+ // (11,12): error CS0843: Auto-implemented property 'S2.Y' must be fully assigned before control is returned to the caller. Consider updating to language version 'preview' to auto-default the property.
// public S2() { }
- Diagnostic(ErrorCode.ERR_UnassignedThisAutoProperty, "S2").WithArguments("S2.Y").WithLocation(11, 12),
+ Diagnostic(ErrorCode.ERR_UnassignedThisAutoPropertyUnsupportedVersion, "S2").WithArguments("S2.Y", "preview").WithLocation(11, 12),
// (16,21): error CS8773: Feature 'struct field initializers' is not available in C# 9.0. Please use language version 10.0 or greater.
// internal object Y { get; } = 3;
Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion9, "Y").WithArguments("struct field initializers", "10.0").WithLocation(16, 21),
// (17,12): error CS8773: Feature 'parameterless struct constructors' is not available in C# 9.0. Please use language version 10.0 or greater.
// public S3() { Y = 3; }
Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion9, "S3").WithArguments("parameterless struct constructors", "10.0").WithLocation(17, 12),
- // (17,12): error CS0843: Auto-implemented property 'S3.X' must be fully assigned before control is returned to the caller.
+ // (17,12): error CS0843: Auto-implemented property 'S3.X' must be fully assigned before control is returned to the caller. Consider updating to language version 'preview' to auto-default the property.
// public S3() { Y = 3; }
- Diagnostic(ErrorCode.ERR_UnassignedThisAutoProperty, "S3").WithArguments("S3.X").WithLocation(17, 12),
+ Diagnostic(ErrorCode.ERR_UnassignedThisAutoPropertyUnsupportedVersion, "S3").WithArguments("S3.X", "preview").WithLocation(17, 12),
// (22,21): error CS8773: Feature 'struct field initializers' is not available in C# 9.0. Please use language version 10.0 or greater.
// internal object Y { get; } = 4;
Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion9, "Y").WithArguments("struct field initializers", "10.0").WithLocation(22, 21),
@@ -1499,24 +1495,23 @@ struct S4
// public S4() { X = 4; }
Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion9, "S4").WithArguments("parameterless struct constructors", "10.0").WithLocation(23, 12));
- var expectedDiagnostics = new[]
- {
+ comp = CreateCompilation(source, parseOptions: TestOptions.Regular10);
+ comp.VerifyDiagnostics(
// (2,8): error CS8983: A 'struct' with field initializers must include an explicitly declared constructor.
// struct S1
Diagnostic(ErrorCode.ERR_StructHasInitializersAndNoDeclaredConstructor, "S1").WithLocation(2, 8),
- // (11,12): error CS0843: Auto-implemented property 'S2.Y' must be fully assigned before control is returned to the caller.
+ // (11,12): error CS0843: Auto-implemented property 'S2.Y' must be fully assigned before control is returned to the caller. Consider updating to language version 'preview' to auto-default the property.
// public S2() { }
- Diagnostic(ErrorCode.ERR_UnassignedThisAutoProperty, "S2").WithArguments("S2.Y").WithLocation(11, 12),
- // (17,12): error CS0843: Auto-implemented property 'S3.X' must be fully assigned before control is returned to the caller.
+ Diagnostic(ErrorCode.ERR_UnassignedThisAutoPropertyUnsupportedVersion, "S2").WithArguments("S2.Y", "preview").WithLocation(11, 12),
+ // (17,12): error CS0843: Auto-implemented property 'S3.X' must be fully assigned before control is returned to the caller. Consider updating to language version 'preview' to auto-default the property.
// public S3() { Y = 3; }
- Diagnostic(ErrorCode.ERR_UnassignedThisAutoProperty, "S3").WithArguments("S3.X").WithLocation(17, 12),
- };
-
- comp = CreateCompilation(source, parseOptions: TestOptions.Regular10);
- comp.VerifyDiagnostics(expectedDiagnostics);
+ Diagnostic(ErrorCode.ERR_UnassignedThisAutoPropertyUnsupportedVersion, "S3").WithArguments("S3.X", "preview").WithLocation(17, 12));
- comp = CreateCompilation(source);
- comp.VerifyDiagnostics(expectedDiagnostics);
+ comp = CreateCompilation(source, parseOptions: TestOptions.RegularNext);
+ comp.VerifyDiagnostics(
+ // (2,8): error CS8983: A 'struct' with field initializers must include an explicitly declared constructor.
+ // struct S1
+ Diagnostic(ErrorCode.ERR_StructHasInitializersAndNoDeclaredConstructor, "S1").WithLocation(2, 8));
}
[WorkItem(57870, "https://github.com/dotnet/roslyn/issues/57870")]
@@ -1550,168 +1545,261 @@ record struct S4
}
";
- var expectedDiagnostics = new[]
- {
+ var comp = CreateCompilation(new[] { source, IsExternalInitTypeDefinition }, parseOptions: TestOptions.Regular10);
+ comp.VerifyDiagnostics(
// (2,15): error CS8983: A 'struct' with field initializers must include an explicitly declared constructor.
// record struct S1
Diagnostic(ErrorCode.ERR_StructHasInitializersAndNoDeclaredConstructor, "S1").WithLocation(2, 15),
- // (11,12): error CS0843: Auto-implemented property 'S2.Y' must be fully assigned before control is returned to the caller.
+ // (11,12): error CS0843: Auto-implemented property 'S2.Y' must be fully assigned before control is returned to the caller. Consider updating to language version 'preview' to auto-default the property.
// public S2() { }
- Diagnostic(ErrorCode.ERR_UnassignedThisAutoProperty, "S2").WithArguments("S2.Y").WithLocation(11, 12),
- // (17,12): error CS0843: Auto-implemented property 'S3.X' must be fully assigned before control is returned to the caller.
+ Diagnostic(ErrorCode.ERR_UnassignedThisAutoPropertyUnsupportedVersion, "S2").WithArguments("S2.Y", "preview").WithLocation(11, 12),
+ // (17,12): error CS0843: Auto-implemented property 'S3.X' must be fully assigned before control is returned to the caller. Consider updating to language version 'preview' to auto-default the property.
// public S3() { Y = 3; }
- Diagnostic(ErrorCode.ERR_UnassignedThisAutoProperty, "S3").WithArguments("S3.X").WithLocation(17, 12)
- };
+ Diagnostic(ErrorCode.ERR_UnassignedThisAutoPropertyUnsupportedVersion, "S3").WithArguments("S3.X", "preview").WithLocation(17, 12));
- var comp = CreateCompilation(new[] { source, IsExternalInitTypeDefinition }, parseOptions: TestOptions.Regular10);
- comp.VerifyDiagnostics(expectedDiagnostics);
-
- comp = CreateCompilation(new[] { source, IsExternalInitTypeDefinition });
- comp.VerifyDiagnostics(expectedDiagnostics);
+ comp = CreateCompilation(new[] { source, IsExternalInitTypeDefinition }, parseOptions: TestOptions.RegularNext);
+ comp.VerifyDiagnostics(
+ // (2,15): error CS8983: A 'struct' with field initializers must include an explicitly declared constructor.
+ // record struct S1
+ Diagnostic(ErrorCode.ERR_StructHasInitializersAndNoDeclaredConstructor, "S1").WithLocation(2, 15));
}
[Fact]
public void FieldInitializers_09()
{
- var source =
-@"#pragma warning disable 649
+ var source = @"
+using System;
+
+#pragma warning disable 649
record struct S1()
{
- internal object X = 1;
- internal object Y;
+ public object X = 1;
+ public object Y;
}
record struct S2()
{
- internal object X { get; } = 2;
- internal object Y { get; }
+ public object X { get; } = 2;
+ public object Y { get; }
}
record struct S3()
{
- internal object X { get; init; }
- internal object Y { get; init; } = 3;
+ public object X { get; init; }
+ public object Y { get; init; } = 3;
+}
+
+class Program
+{
+ static void Main()
+ {
+ Console.WriteLine(new S1());
+ Console.WriteLine(new S2());
+ Console.WriteLine(new S3());
+ }
}
";
- var expectedDiagnostics = new[]
- {
- // (2,15): error CS0171: Field 'S1.Y' must be fully assigned before control is returned to the caller
+ var comp = CreateCompilation(new[] { source, IsExternalInitTypeDefinition }, parseOptions: TestOptions.Regular10);
+ comp.VerifyDiagnostics(
+ // (5,15): error CS0171: Field 'S1.Y' must be fully assigned before control is returned to the caller. Consider updating to language version 'preview' to auto-default the field.
// record struct S1()
- Diagnostic(ErrorCode.ERR_UnassignedThis, "S1").WithArguments("S1.Y").WithLocation(2, 15),
- // (7,15): error CS0843: Auto-implemented property 'S2.Y' must be fully assigned before control is returned to the caller.
+ Diagnostic(ErrorCode.ERR_UnassignedThisUnsupportedVersion, "S1").WithArguments("S1.Y", "preview").WithLocation(5, 15),
+ // (10,15): error CS0843: Auto-implemented property 'S2.Y' must be fully assigned before control is returned to the caller. Consider updating to language version 'preview' to auto-default the property.
// record struct S2()
- Diagnostic(ErrorCode.ERR_UnassignedThisAutoProperty, "S2").WithArguments("S2.Y").WithLocation(7, 15),
- // (12,15): error CS0843: Auto-implemented property 'S3.X' must be fully assigned before control is returned to the caller.
+ Diagnostic(ErrorCode.ERR_UnassignedThisAutoPropertyUnsupportedVersion, "S2").WithArguments("S2.Y", "preview").WithLocation(10, 15),
+ // (15,15): error CS0843: Auto-implemented property 'S3.X' must be fully assigned before control is returned to the caller. Consider updating to language version 'preview' to auto-default the property.
// record struct S3()
- Diagnostic(ErrorCode.ERR_UnassignedThisAutoProperty, "S3").WithArguments("S3.X").WithLocation(12, 15)
- };
+ Diagnostic(ErrorCode.ERR_UnassignedThisAutoPropertyUnsupportedVersion, "S3").WithArguments("S3.X", "preview").WithLocation(15, 15));
- var comp = CreateCompilation(new[] { source, IsExternalInitTypeDefinition }, parseOptions: TestOptions.Regular10);
- comp.VerifyDiagnostics(expectedDiagnostics);
+ var verifier = CompileAndVerify(new[] { source, IsExternalInitTypeDefinition }, parseOptions: TestOptions.RegularNext, expectedOutput:
+@"
+S1 { X = 1, Y = }
+S2 { X = 2, Y = }
+S3 { X = , Y = 3 }
+", verify: Verification.Skipped);
+ verifier.VerifyDiagnostics();
+ verifier.VerifyIL("S1..ctor", @"
+{
+ // Code size 20 (0x14)
+ .maxstack 2
+ IL_0000: ldarg.0
+ IL_0001: ldnull
+ IL_0002: stfld ""object S1.Y""
+ IL_0007: ldarg.0
+ IL_0008: ldc.i4.1
+ IL_0009: box ""int""
+ IL_000e: stfld ""object S1.X""
+ IL_0013: ret
+}");
- comp = CreateCompilation(new[] { source, IsExternalInitTypeDefinition });
- comp.VerifyDiagnostics(expectedDiagnostics);
+ verifier.VerifyIL("S2..ctor", @"
+{
+ // Code size 20 (0x14)
+ .maxstack 2
+ IL_0000: ldarg.0
+ IL_0001: ldnull
+ IL_0002: stfld ""object S2.k__BackingField""
+ IL_0007: ldarg.0
+ IL_0008: ldc.i4.2
+ IL_0009: box ""int""
+ IL_000e: stfld ""object S2.k__BackingField""
+ IL_0013: ret
+}");
+
+ verifier.VerifyIL("S3..ctor", @"
+{
+ // Code size 20 (0x14)
+ .maxstack 2
+ IL_0000: ldarg.0
+ IL_0001: ldnull
+ IL_0002: stfld ""object S3.k__BackingField""
+ IL_0007: ldarg.0
+ IL_0008: ldc.i4.3
+ IL_0009: box ""int""
+ IL_000e: stfld ""object S3.k__BackingField""
+ IL_0013: ret
+}");
}
[Fact]
public void FieldInitializers_10()
{
- var source =
-@"#pragma warning disable 649
+ var source = @"
+using System;
+
+#pragma warning disable 649
record struct S1(object X)
{
- internal object X = 1;
- internal object Y;
+ public object X = 1;
+ public object Y;
}
record struct S2(object X)
{
- internal object X { get; } = 2;
- internal object Y { get; }
+ public object X { get; } = 2;
+ public object Y { get; }
}
record struct S3(object Y)
{
- internal object X { get; init; }
- internal object Y { get; init; } = 3;
+ public object X { get; init; }
+ public object Y { get; init; } = 3;
+}
+
+class Program
+{
+ static void Main()
+ {
+ Console.WriteLine(new S1(""a""));
+ Console.WriteLine(new S2(""b""));
+ Console.WriteLine(new S3(""c""));
+ }
}
";
- var expectedDiagnostics = new[]
- {
- // (2,15): error CS0171: Field 'S1.Y' must be fully assigned before control is returned to the caller
+ var comp = CreateCompilation(new[] { source, IsExternalInitTypeDefinition }, parseOptions: TestOptions.Regular10);
+ comp.VerifyDiagnostics(
+ // (5,15): error CS0171: Field 'S1.Y' must be fully assigned before control is returned to the caller. Consider updating to language version 'preview' to auto-default the field.
// record struct S1(object X)
- Diagnostic(ErrorCode.ERR_UnassignedThis, "S1").WithArguments("S1.Y").WithLocation(2, 15),
- // (2,25): warning CS8907: Parameter 'X' is unread. Did you forget to use it to initialize the property with that name?
+ Diagnostic(ErrorCode.ERR_UnassignedThisUnsupportedVersion, "S1").WithArguments("S1.Y", "preview").WithLocation(5, 15),
+ // (5,25): warning CS8907: Parameter 'X' is unread. Did you forget to use it to initialize the property with that name?
// record struct S1(object X)
- Diagnostic(ErrorCode.WRN_UnreadRecordParameter, "X").WithArguments("X").WithLocation(2, 25),
- // (7,15): error CS0843: Auto-implemented property 'S2.Y' must be fully assigned before control is returned to the caller.
+ Diagnostic(ErrorCode.WRN_UnreadRecordParameter, "X").WithArguments("X").WithLocation(5, 25),
+ // (10,15): error CS0843: Auto-implemented property 'S2.Y' must be fully assigned before control is returned to the caller. Consider updating to language version 'preview' to auto-default the property.
// record struct S2(object X)
- Diagnostic(ErrorCode.ERR_UnassignedThisAutoProperty, "S2").WithArguments("S2.Y").WithLocation(7, 15),
- // (7,25): warning CS8907: Parameter 'X' is unread. Did you forget to use it to initialize the property with that name?
+ Diagnostic(ErrorCode.ERR_UnassignedThisAutoPropertyUnsupportedVersion, "S2").WithArguments("S2.Y", "preview").WithLocation(10, 15),
+ // (10,25): warning CS8907: Parameter 'X' is unread. Did you forget to use it to initialize the property with that name?
// record struct S2(object X)
- Diagnostic(ErrorCode.WRN_UnreadRecordParameter, "X").WithArguments("X").WithLocation(7, 25),
- // (12,15): error CS0843: Auto-implemented property 'S3.X' must be fully assigned before control is returned to the caller.
+ Diagnostic(ErrorCode.WRN_UnreadRecordParameter, "X").WithArguments("X").WithLocation(10, 25),
+ // (15,15): error CS0843: Auto-implemented property 'S3.X' must be fully assigned before control is returned to the caller. Consider updating to language version 'preview' to auto-default the property.
// record struct S3(object Y)
- Diagnostic(ErrorCode.ERR_UnassignedThisAutoProperty, "S3").WithArguments("S3.X").WithLocation(12, 15),
- // (12,25): warning CS8907: Parameter 'Y' is unread. Did you forget to use it to initialize the property with that name?
+ Diagnostic(ErrorCode.ERR_UnassignedThisAutoPropertyUnsupportedVersion, "S3").WithArguments("S3.X", "preview").WithLocation(15, 15),
+ // (15,25): warning CS8907: Parameter 'Y' is unread. Did you forget to use it to initialize the property with that name?
// record struct S3(object Y)
- Diagnostic(ErrorCode.WRN_UnreadRecordParameter, "Y").WithArguments("Y").WithLocation(12, 25)
- };
-
- var comp = CreateCompilation(new[] { source, IsExternalInitTypeDefinition }, parseOptions: TestOptions.Regular10);
- comp.VerifyDiagnostics(expectedDiagnostics);
-
- comp = CreateCompilation(new[] { source, IsExternalInitTypeDefinition });
- comp.VerifyDiagnostics(expectedDiagnostics);
+ Diagnostic(ErrorCode.WRN_UnreadRecordParameter, "Y").WithArguments("Y").WithLocation(15, 25));
+
+ var verifier = CompileAndVerify(new[] { source, IsExternalInitTypeDefinition }, parseOptions: TestOptions.RegularNext, expectedOutput:
+@"S1 { X = 1, Y = }
+S2 { X = 2, Y = }
+S3 { X = , Y = 3 }
+", verify: Verification.Skipped);
+ verifier.VerifyDiagnostics(
+ // (5,25): warning CS8907: Parameter 'X' is unread. Did you forget to use it to initialize the property with that name?
+ // record struct S1(object X)
+ Diagnostic(ErrorCode.WRN_UnreadRecordParameter, "X").WithArguments("X").WithLocation(5, 25),
+ // (10,25): warning CS8907: Parameter 'X' is unread. Did you forget to use it to initialize the property with that name?
+ // record struct S2(object X)
+ Diagnostic(ErrorCode.WRN_UnreadRecordParameter, "X").WithArguments("X").WithLocation(10, 25),
+ // (15,25): warning CS8907: Parameter 'Y' is unread. Did you forget to use it to initialize the property with that name?
+ // record struct S3(object Y)
+ Diagnostic(ErrorCode.WRN_UnreadRecordParameter, "Y").WithArguments("Y").WithLocation(15, 25));
}
[Fact]
public void FieldInitializers_11()
{
- var source =
-@"#pragma warning disable 649
+ var source = @"
+using System;
+
+#pragma warning disable 649
record struct S1(object X)
{
- internal object X;
- internal object Y = 1;
+ public object X;
+ public object Y = 1;
}
record struct S2(object X)
{
- internal object X { get; }
- internal object Y { get; } = 2;
+ public object X { get; }
+ public object Y { get; } = 2;
}
record struct S3(object Y)
{
- internal object X { get; init; } = 3;
- internal object Y { get; init; }
+ public object X { get; init; } = 3;
+ public object Y { get; init; }
}
-";
- var expectedDiagnostics = new[]
- {
- // (2,15): error CS0171: Field 'S1.X' must be fully assigned before control is returned to the caller
+class Program
+{
+ static void Main()
+ {
+ Console.WriteLine(new S1(""a""));
+ Console.WriteLine(new S2(""b""));
+ Console.WriteLine(new S3(""c""));
+ }
+}
+";
+ var comp = CreateCompilation(new[] { source, IsExternalInitTypeDefinition }, parseOptions: TestOptions.Regular10);
+ comp.VerifyDiagnostics(
+ // (5,15): error CS0171: Field 'S1.X' must be fully assigned before control is returned to the caller. Consider updating to language version 'preview' to auto-default the field.
// record struct S1(object X)
- Diagnostic(ErrorCode.ERR_UnassignedThis, "S1").WithArguments("S1.X").WithLocation(2, 15),
- // (2,25): warning CS8907: Parameter 'X' is unread. Did you forget to use it to initialize the property with that name?
+ Diagnostic(ErrorCode.ERR_UnassignedThisUnsupportedVersion, "S1").WithArguments("S1.X", "preview").WithLocation(5, 15),
+ // (5,25): warning CS8907: Parameter 'X' is unread. Did you forget to use it to initialize the property with that name?
// record struct S1(object X)
- Diagnostic(ErrorCode.WRN_UnreadRecordParameter, "X").WithArguments("X").WithLocation(2, 25),
- // (7,15): error CS0843: Auto-implemented property 'S2.X' must be fully assigned before control is returned to the caller.
+ Diagnostic(ErrorCode.WRN_UnreadRecordParameter, "X").WithArguments("X").WithLocation(5, 25),
+ // (10,15): error CS0843: Auto-implemented property 'S2.X' must be fully assigned before control is returned to the caller. Consider updating to language version 'preview' to auto-default the property.
// record struct S2(object X)
- Diagnostic(ErrorCode.ERR_UnassignedThisAutoProperty, "S2").WithArguments("S2.X").WithLocation(7, 15),
- // (7,25): warning CS8907: Parameter 'X' is unread. Did you forget to use it to initialize the property with that name?
+ Diagnostic(ErrorCode.ERR_UnassignedThisAutoPropertyUnsupportedVersion, "S2").WithArguments("S2.X", "preview").WithLocation(10, 15),
+ // (10,25): warning CS8907: Parameter 'X' is unread. Did you forget to use it to initialize the property with that name?
// record struct S2(object X)
- Diagnostic(ErrorCode.WRN_UnreadRecordParameter, "X").WithArguments("X").WithLocation(7, 25),
- // (12,15): error CS0843: Auto-implemented property 'S3.Y' must be fully assigned before control is returned to the caller.
+ Diagnostic(ErrorCode.WRN_UnreadRecordParameter, "X").WithArguments("X").WithLocation(10, 25),
+ // (15,15): error CS0843: Auto-implemented property 'S3.Y' must be fully assigned before control is returned to the caller. Consider updating to language version 'preview' to auto-default the property.
// record struct S3(object Y)
- Diagnostic(ErrorCode.ERR_UnassignedThisAutoProperty, "S3").WithArguments("S3.Y").WithLocation(12, 15),
- // (12,25): warning CS8907: Parameter 'Y' is unread. Did you forget to use it to initialize the property with that name?
+ Diagnostic(ErrorCode.ERR_UnassignedThisAutoPropertyUnsupportedVersion, "S3").WithArguments("S3.Y", "preview").WithLocation(15, 15),
+ // (15,25): warning CS8907: Parameter 'Y' is unread. Did you forget to use it to initialize the property with that name?
// record struct S3(object Y)
- Diagnostic(ErrorCode.WRN_UnreadRecordParameter, "Y").WithArguments("Y").WithLocation(12, 25)
- };
-
- var comp = CreateCompilation(new[] { source, IsExternalInitTypeDefinition }, parseOptions: TestOptions.Regular10);
- comp.VerifyDiagnostics(expectedDiagnostics);
-
- comp = CreateCompilation(new[] { source, IsExternalInitTypeDefinition });
- comp.VerifyDiagnostics(expectedDiagnostics);
+ Diagnostic(ErrorCode.WRN_UnreadRecordParameter, "Y").WithArguments("Y").WithLocation(15, 25));
+
+ var verifier = CompileAndVerify(new[] { source, IsExternalInitTypeDefinition }, parseOptions: TestOptions.RegularNext, expectedOutput:
+@"S1 { X = , Y = 1 }
+S2 { X = , Y = 2 }
+S3 { X = 3, Y = }", verify: Verification.Skipped);
+ verifier.VerifyDiagnostics(
+ // (5,25): warning CS8907: Parameter 'X' is unread. Did you forget to use it to initialize the property with that name?
+ // record struct S1(object X)
+ Diagnostic(ErrorCode.WRN_UnreadRecordParameter, "X").WithArguments("X").WithLocation(5, 25),
+ // (10,25): warning CS8907: Parameter 'X' is unread. Did you forget to use it to initialize the property with that name?
+ // record struct S2(object X)
+ Diagnostic(ErrorCode.WRN_UnreadRecordParameter, "X").WithArguments("X").WithLocation(10, 25),
+ // (15,25): warning CS8907: Parameter 'Y' is unread. Did you forget to use it to initialize the property with that name?
+ // record struct S3(object Y)
+ Diagnostic(ErrorCode.WRN_UnreadRecordParameter, "Y").WithArguments("Y").WithLocation(15, 25));
}
[Fact]
@@ -2027,20 +2115,35 @@ struct S3
public S3() { F3 = GetValue(); }
static object? GetValue() => null;
}";
- var comp = CreateCompilation(source);
+ var comp = CreateCompilation(source, parseOptions: TestOptions.Regular10);
comp.VerifyDiagnostics(
// (10,12): warning CS8618: Non-nullable field 'F1' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.
// public S1() { }
Diagnostic(ErrorCode.WRN_UninitializedNonNullableField, "S1").WithArguments("field", "F1").WithLocation(10, 12),
- // (10,12): error CS0171: Field 'S1.F1' must be fully assigned before control is returned to the caller
+ // (10,12): error CS0171: Field 'S1.F1' must be fully assigned before control is returned to the caller. Consider updating to language version 'preview' to auto-default the field.
// public S1() { }
- Diagnostic(ErrorCode.ERR_UnassignedThis, "S1").WithArguments("S1.F1").WithLocation(10, 12),
+ Diagnostic(ErrorCode.ERR_UnassignedThisUnsupportedVersion, "S1").WithArguments("S1.F1", "preview").WithLocation(10, 12),
// (16,5): warning CS8618: Non-nullable field 'F2' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.
// S2(object? obj) { }
Diagnostic(ErrorCode.WRN_UninitializedNonNullableField, "S2").WithArguments("field", "F2").WithLocation(16, 5),
- // (16,5): error CS0171: Field 'S2.F2' must be fully assigned before control is returned to the caller
+ // (16,5): error CS0171: Field 'S2.F2' must be fully assigned before control is returned to the caller. Consider updating to language version 'preview' to auto-default the field.
// S2(object? obj) { }
- Diagnostic(ErrorCode.ERR_UnassignedThis, "S2").WithArguments("S2.F2").WithLocation(16, 5),
+ Diagnostic(ErrorCode.ERR_UnassignedThisUnsupportedVersion, "S2").WithArguments("S2.F2", "preview").WithLocation(16, 5),
+ // (21,12): warning CS8618: Non-nullable field 'F3' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.
+ // public S3() { F3 = GetValue(); }
+ Diagnostic(ErrorCode.WRN_UninitializedNonNullableField, "S3").WithArguments("field", "F3").WithLocation(21, 12),
+ // (21,24): warning CS8601: Possible null reference assignment.
+ // public S3() { F3 = GetValue(); }
+ Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "GetValue()").WithLocation(21, 24));
+
+ comp = CreateCompilation(source, parseOptions: TestOptions.RegularNext);
+ comp.VerifyDiagnostics(
+ // (10,12): warning CS8618: Non-nullable field 'F1' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.
+ // public S1() { }
+ Diagnostic(ErrorCode.WRN_UninitializedNonNullableField, "S1").WithArguments("field", "F1").WithLocation(10, 12),
+ // (16,5): warning CS8618: Non-nullable field 'F2' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.
+ // S2(object? obj) { }
+ Diagnostic(ErrorCode.WRN_UninitializedNonNullableField, "S2").WithArguments("field", "F2").WithLocation(16, 5),
// (21,12): warning CS8618: Non-nullable field 'F3' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.
// public S3() { F3 = GetValue(); }
Diagnostic(ErrorCode.WRN_UninitializedNonNullableField, "S3").WithArguments("field", "F3").WithLocation(21, 12),
@@ -2142,17 +2245,27 @@ unsafe struct S5
public S5() { X = 5; }
}";
- var comp = CreateCompilation(source, options: TestOptions.UnsafeReleaseDll);
+ var comp = CreateCompilation(source, options: TestOptions.UnsafeReleaseDll, parseOptions: TestOptions.Regular10);
comp.VerifyDiagnostics(
- // (20,12): error CS0171: Field 'S3.X' must be fully assigned before control is returned to the caller
+ // (20,12): error CS0171: Field 'S3.X' must be fully assigned before control is returned to the caller. Consider updating to language version 'preview' to auto-default the field.
// public S3() { }
- Diagnostic(ErrorCode.ERR_UnassignedThis, "S3").WithArguments("S3.X").WithLocation(20, 12),
+ Diagnostic(ErrorCode.ERR_UnassignedThisUnsupportedVersion, "S3").WithArguments("S3.X", "preview").WithLocation(20, 12),
// (22,15): error CS8983: A 'struct' with field initializers must include an explicitly declared constructor.
// unsafe struct S4
Diagnostic(ErrorCode.ERR_StructHasInitializersAndNoDeclaredConstructor, "S4").WithLocation(22, 15),
// (29,9): warning CS0414: The field 'S5.X' is assigned but its value is never used
// int X;
Diagnostic(ErrorCode.WRN_UnreferencedFieldAssg, "X").WithArguments("S5.X").WithLocation(29, 9));
+
+ comp = CreateCompilation(source, options: TestOptions.UnsafeReleaseDll, parseOptions: TestOptions.RegularNext);
+ comp.VerifyDiagnostics(
+ // (22,15): error CS8983: A 'struct' with field initializers must include an explicitly declared constructor.
+ // unsafe struct S4
+ Diagnostic(ErrorCode.ERR_StructHasInitializersAndNoDeclaredConstructor, "S4").WithLocation(22, 15),
+ // (29,9): warning CS0414: The field 'S5.X' is assigned but its value is never used
+ // int X;
+ Diagnostic(ErrorCode.WRN_UnreferencedFieldAssg, "X").WithArguments("S5.X").WithLocation(29, 9)
+ );
}
[Fact]
@@ -2408,5 +2521,287 @@ static void M(I i)
// var s1 = i.F1();
Diagnostic(ErrorCode.ERR_InteropStructContainsMethods, "i.F1()").WithArguments("S1").WithLocation(6, 18));
}
+
+ [Fact]
+ public void ImplicitlyInitializedField_Simple()
+ {
+ var source = @"
+public struct S
+{
+ public int x;
+ public S() { }
+}";
+ CreateCompilation(source, parseOptions: TestOptions.Regular10)
+ .VerifyDiagnostics(
+ // (5,12): error CS0171: Field 'S.x' must be fully assigned before control is returned to the caller. Consider updating to language version 'preview' to auto-default the field.
+ // public S() { }
+ Diagnostic(ErrorCode.ERR_UnassignedThisUnsupportedVersion, "S").WithArguments("S.x", "preview").WithLocation(5, 12));
+
+ CreateCompilation(source, options: TestOptions.DebugDll.WithSpecificDiagnosticOptions(ReportStructInitializationWarnings), parseOptions: TestOptions.RegularNext)
+ .VerifyDiagnostics(
+ // (5,12): warning CS9021: Control is returned to caller before field 'S.x' is explicitly assigned, causing a preceding implicit assignment of 'default'.
+ // public S() { }
+ Diagnostic(ErrorCode.WRN_UnassignedThisSupportedVersion, "S").WithArguments("S.x").WithLocation(5, 12));
+
+ CreateCompilation(source, options: TestOptions.DebugDll.WithSpecificDiagnosticOptions(GetIdForErrorCode(ErrorCode.WRN_UnassignedThisSupportedVersion), ReportDiagnostic.Error), parseOptions: TestOptions.RegularNext)
+ .VerifyDiagnostics(
+ // (5,12): error CS9021: Control is returned to caller before field 'S.x' is explicitly assigned, causing a preceding implicit assignment of 'default'.
+ // public S() { }
+ Diagnostic(ErrorCode.WRN_UnassignedThisSupportedVersion, "S").WithArguments("S.x").WithLocation(5, 12).WithWarningAsError(true));
+
+ var verifier = CompileAndVerify(source, parseOptions: TestOptions.RegularNext);
+ verifier.VerifyDiagnostics();
+
+ verifier.VerifyIL("S..ctor()", @"
+{
+ // Code size 8 (0x8)
+ .maxstack 2
+ IL_0000: ldarg.0
+ IL_0001: ldc.i4.0
+ IL_0002: stfld ""int S.x""
+ IL_0007: ret
+}
+");
+ }
+
+ [Fact]
+ public void ImplicitlyInitializedField_NotOtherStruct()
+ {
+ var source = @"
+public struct S
+{
+ public int x;
+ public S() // 1
+ {
+ S other;
+ other.x.ToString(); // 2
+
+ S other2;
+ other2.ToString(); // 3
+ }
+}";
+ CreateCompilation(source, parseOptions: TestOptions.Regular10)
+ .VerifyDiagnostics(
+ // (5,12): error CS0171: Field 'S.x' must be fully assigned before control is returned to the caller. Consider updating to language version 'preview' to auto-default the field.
+ // public S() // 1
+ Diagnostic(ErrorCode.ERR_UnassignedThisUnsupportedVersion, "S").WithArguments("S.x", "preview").WithLocation(5, 12),
+ // (8,9): error CS0170: Use of possibly unassigned field 'x'
+ // other.x.ToString(); // 2
+ Diagnostic(ErrorCode.ERR_UseDefViolationField, "other.x").WithArguments("x").WithLocation(8, 9),
+ // (11,9): error CS0165: Use of unassigned local variable 'other2'
+ // other2.ToString(); // 3
+ Diagnostic(ErrorCode.ERR_UseDefViolation, "other2").WithArguments("other2").WithLocation(11, 9));
+
+ CreateCompilation(source, options: TestOptions.DebugDll.WithSpecificDiagnosticOptions(ReportStructInitializationWarnings), parseOptions: TestOptions.RegularNext)
+ .VerifyDiagnostics(
+ // (5,12): warning CS9021: Control is returned to caller before field 'S.x' is explicitly assigned, causing a preceding implicit assignment of 'default'.
+ // public S() // 1
+ Diagnostic(ErrorCode.WRN_UnassignedThisSupportedVersion, "S").WithArguments("S.x").WithLocation(5, 12),
+ // (8,9): error CS0170: Use of possibly unassigned field 'x'
+ // other.x.ToString(); // 2
+ Diagnostic(ErrorCode.ERR_UseDefViolationField, "other.x").WithArguments("x").WithLocation(8, 9),
+ // (11,9): error CS0165: Use of unassigned local variable 'other2'
+ // other2.ToString(); // 3
+ Diagnostic(ErrorCode.ERR_UseDefViolation, "other2").WithArguments("other2").WithLocation(11, 9));
+ }
+
+ [Fact]
+ public void ImplicitlyInitializedField_ExplicitReturn()
+ {
+ var source = @"
+public struct S
+{
+ public int x;
+ public S()
+ {
+ return;
+ }
+}";
+ var comp = CreateCompilation(source, parseOptions: TestOptions.Regular10);
+ comp.VerifyDiagnostics(
+ // (7,9): error CS0171: Field 'S.x' must be fully assigned before control is returned to the caller. Consider updating to language version 'preview' to auto-default the field.
+ // return;
+ Diagnostic(ErrorCode.ERR_UnassignedThisUnsupportedVersion, "return;").WithArguments("S.x", "preview").WithLocation(7, 9));
+
+ var verifier = CompileAndVerify(source, options: TestOptions.DebugDll.WithSpecificDiagnosticOptions(ReportStructInitializationWarnings), parseOptions: TestOptions.RegularNext);
+ verifier.VerifyDiagnostics(
+ // (7,9): warning CS9021: Control is returned to caller before field 'S.x' is explicitly assigned, causing a preceding implicit assignment of 'default'.
+ // return;
+ Diagnostic(ErrorCode.WRN_UnassignedThisSupportedVersion, "return;").WithArguments("S.x").WithLocation(7, 9));
+
+ verifier.VerifyIL("S..ctor", @"
+{
+ // Code size 11 (0xb)
+ .maxstack 2
+ IL_0000: ldarg.0
+ IL_0001: ldc.i4.0
+ IL_0002: stfld ""int S.x""
+ IL_0007: nop
+ IL_0008: br.s IL_000a
+ IL_000a: ret
+}
+");
+ }
+
+ [Fact]
+ public void ImplicitlyInitializedField_FieldLikeEvent()
+ {
+ var source = @"
+using System;
+
+public struct S
+{
+ public event Action E;
+ public S()
+ {
+ E?.Invoke();
+ }
+}";
+ var comp = CreateCompilation(source, parseOptions: TestOptions.Regular10);
+ comp.VerifyDiagnostics(
+ // (7,12): error CS0171: Field 'S.E' must be fully assigned before control is returned to the caller. Consider updating to language version 'preview' to auto-default the field.
+ // public S()
+ Diagnostic(ErrorCode.ERR_UnassignedThisUnsupportedVersion, "S").WithArguments("S.E", "preview").WithLocation(7, 12),
+ // (9,9): error CS9014: Use of possibly unassigned field 'E'. Consider updating to language version 'preview' to auto-default the field.
+ // E?.Invoke();
+ Diagnostic(ErrorCode.ERR_UseDefViolationFieldUnsupportedVersion, "E").WithArguments("E", "preview").WithLocation(9, 9));
+
+ var verifier = CompileAndVerify(source, options: TestOptions.DebugDll.WithSpecificDiagnosticOptions(ReportStructInitializationWarnings), parseOptions: TestOptions.RegularNext);
+ verifier.VerifyDiagnostics(
+ // (7,12): warning CS9021: Control is returned to caller before field 'S.E' is explicitly assigned, causing a preceding implicit assignment of 'default'.
+ // public S()
+ Diagnostic(ErrorCode.WRN_UnassignedThisSupportedVersion, "S").WithArguments("S.E").WithLocation(7, 12),
+ // (9,9): warning CS9018: Field 'E' is read before being explicitly assigned, causing a preceding implicit assignment of 'default'.
+ // E?.Invoke();
+ Diagnostic(ErrorCode.WRN_UseDefViolationFieldSupportedVersion, "E").WithArguments("E").WithLocation(9, 9));
+
+ verifier.VerifyIL("S..ctor", @"
+{
+ // Code size 27 (0x1b)
+ .maxstack 2
+ IL_0000: ldarg.0
+ IL_0001: ldnull
+ IL_0002: stfld ""System.Action S.E""
+ IL_0007: nop
+ IL_0008: ldarg.0
+ IL_0009: ldfld ""System.Action S.E""
+ IL_000e: dup
+ IL_000f: brtrue.s IL_0014
+ IL_0011: pop
+ IL_0012: br.s IL_001a
+ IL_0014: callvirt ""void System.Action.Invoke()""
+ IL_0019: nop
+ IL_001a: ret
+}
+");
+ }
+
+ [Fact]
+ public void NonNullableReferenceTypeField()
+ {
+ var source =
+@"public struct S
+{
+ public string Item;
+ public S(bool unused)
+ {
+ }
+}";
+ var comp = CreateCompilation(new[] { source }, options: WithNullableEnable(), parseOptions: TestOptions.Regular10);
+ comp.VerifyDiagnostics(
+ // (4,12): warning CS8618: Non-nullable field 'Item' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.
+ // public S(bool unused)
+ Diagnostic(ErrorCode.WRN_UninitializedNonNullableField, "S").WithArguments("field", "Item").WithLocation(4, 12),
+ // (4,12): error CS0171: Field 'S.Item' must be fully assigned before control is returned to the caller. Consider updating to language version 'preview' to auto-default the field.
+ // public S(bool unused)
+ Diagnostic(ErrorCode.ERR_UnassignedThisUnsupportedVersion, "S").WithArguments("S.Item", "preview").WithLocation(4, 12)
+ );
+
+ var verifier = CompileAndVerify(new[] { source }, options: WithNullableEnable(), parseOptions: TestOptions.RegularNext);
+ verifier.VerifyDiagnostics(
+ // (4,12): warning CS8618: Non-nullable field 'Item' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.
+ // public S(bool unused)
+ Diagnostic(ErrorCode.WRN_UninitializedNonNullableField, "S").WithArguments("field", "Item").WithLocation(4, 12));
+ verifier.VerifyIL("S..ctor", @"
+{
+ // Code size 8 (0x8)
+ .maxstack 2
+ IL_0000: ldarg.0
+ IL_0001: ldnull
+ IL_0002: stfld ""string S.Item""
+ IL_0007: ret
+}
+");
+ }
+
+ [Theory]
+ [InlineData(LanguageVersion.CSharp10)]
+ [InlineData(LanguageVersionFacts.CSharpNext)]
+ public void Struct_ExplicitThisConstructorInitializer_01(LanguageVersion languageVersion)
+ {
+ var source =
+@"public struct S
+{
+ public string Item;
+ public S(bool unused) : this()
+ {
+ }
+}";
+ var verifier = CompileAndVerify(new[] { source }, options: WithNullableEnable(), parseOptions: TestOptions.Regular.WithLanguageVersion(languageVersion));
+ verifier.VerifyDiagnostics(
+ // (4,12): warning CS8618: Non-nullable field 'Item' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.
+ // public S(bool unused)
+ Diagnostic(ErrorCode.WRN_UninitializedNonNullableField, "S").WithArguments("field", "Item").WithLocation(4, 12)
+ );
+
+ verifier.VerifyIL("S..ctor", @"
+{
+ // Code size 8 (0x8)
+ .maxstack 1
+ IL_0000: ldarg.0
+ IL_0001: initobj ""S""
+ IL_0007: ret
+}
+");
+ }
+
+ [Theory]
+ [InlineData(LanguageVersion.CSharp10)]
+ [InlineData(LanguageVersionFacts.CSharpNext)]
+ public void Struct_ExplicitThisConstructorInitializer_02(LanguageVersion languageVersion)
+ {
+ var source =
+@"public struct S
+{
+ public string Item;
+ public S(bool unused) : this()
+ {
+ }
+ public S() { Item = ""a""; }
+}";
+ var verifier = CompileAndVerify(new[] { source }, options: WithNullableEnable(), parseOptions: TestOptions.Regular.WithLanguageVersion(languageVersion));
+ verifier.VerifyDiagnostics();
+
+ verifier.VerifyIL("S..ctor(bool)", @"
+{
+ // Code size 7 (0x7)
+ .maxstack 1
+ IL_0000: ldarg.0
+ IL_0001: call ""S..ctor()""
+ IL_0006: ret
+}
+");
+
+ verifier.VerifyIL("S..ctor()", @"
+{
+ // Code size 12 (0xc)
+ .maxstack 2
+ IL_0000: ldarg.0
+ IL_0001: ldstr ""a""
+ IL_0006: stfld ""string S.Item""
+ IL_000b: ret
+}
+");
+ }
}
}
diff --git a/src/Compilers/CSharp/Test/Semantic/Semantics/UninitializedNonNullableFieldTests.cs b/src/Compilers/CSharp/Test/Semantic/Semantics/UninitializedNonNullableFieldTests.cs
index de70e1f4ea21f..6d5516d56cb9e 100644
--- a/src/Compilers/CSharp/Test/Semantic/Semantics/UninitializedNonNullableFieldTests.cs
+++ b/src/Compilers/CSharp/Test/Semantic/Semantics/UninitializedNonNullableFieldTests.cs
@@ -853,23 +853,75 @@ public S1(string s1, string s2) : this(s1)
}
";
- var comp = CreateCompilation(source, options: WithNullableEnable());
+ var comp = CreateCompilation(source, options: WithNullableEnable(), parseOptions: TestOptions.Regular10);
comp.VerifyDiagnostics(
- // (5,12): error CS0843: Auto-implemented property 'S1.Prop' must be fully assigned before control is returned to the caller.
+ // (5,12): error CS0843: Auto-implemented property 'S1.Prop' must be fully assigned before control is returned to the caller. Consider updating to language version 'preview' to auto-default the property.
// public S1(string s) // 1
- Diagnostic(ErrorCode.ERR_UnassignedThisAutoProperty, "S1").WithArguments("S1.Prop").WithLocation(5, 12),
+ Diagnostic(ErrorCode.ERR_UnassignedThisAutoPropertyUnsupportedVersion, "S1").WithArguments("S1.Prop", "preview").WithLocation(5, 12),
// (7,9): warning CS8602: Dereference of a possibly null reference.
// Prop.ToString(); // 2
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "Prop").WithLocation(7, 9),
- // (7,9): error CS8079: Use of possibly unassigned auto-implemented property 'Prop'
+ // (7,9): error CS9013: Use of possibly unassigned auto-implemented property 'Prop'. Consider updating to language version 'preview' to auto-default the property.
+ // Prop.ToString(); // 2
+ Diagnostic(ErrorCode.ERR_UseDefViolationPropertyUnsupportedVersion, "Prop").WithArguments("Prop", "preview").WithLocation(7, 9),
+ // (12,9): warning CS8602: Dereference of a possibly null reference.
+ // Prop.ToString(); // 3
+ Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "Prop").WithLocation(12, 9),
+ // (15,12): warning CS8618: Non-nullable property 'Prop' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
+ // public S1(object obj1, object obj2) : this() // 4
+ Diagnostic(ErrorCode.WRN_UninitializedNonNullableField, "S1").WithArguments("property", "Prop").WithLocation(15, 12));
+
+ var verifier = CompileAndVerify(source, options: WithNullableEnable(), parseOptions: TestOptions.RegularNext);
+ verifier.VerifyDiagnostics(
+ // (7,9): warning CS8602: Dereference of a possibly null reference.
// Prop.ToString(); // 2
- Diagnostic(ErrorCode.ERR_UseDefViolationProperty, "Prop").WithArguments("Prop").WithLocation(7, 9),
+ Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "Prop").WithLocation(7, 9),
// (12,9): warning CS8602: Dereference of a possibly null reference.
// Prop.ToString(); // 3
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "Prop").WithLocation(12, 9),
// (15,12): warning CS8618: Non-nullable property 'Prop' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
// public S1(object obj1, object obj2) : this() // 4
Diagnostic(ErrorCode.WRN_UninitializedNonNullableField, "S1").WithArguments("property", "Prop").WithLocation(15, 12));
+
+ verifier.VerifyIL("S1..ctor(string)", @"
+{
+ // Code size 20 (0x14)
+ .maxstack 2
+ IL_0000: ldarg.0
+ IL_0001: ldnull
+ IL_0002: stfld ""string S1.k__BackingField""
+ IL_0007: ldarg.0
+ IL_0008: call ""readonly string S1.Prop.get""
+ IL_000d: callvirt ""string object.ToString()""
+ IL_0012: pop
+ IL_0013: ret
+}
+");
+
+ verifier.VerifyIL("S1..ctor(object, object)", @"
+{
+ // Code size 8 (0x8)
+ .maxstack 1
+ IL_0000: ldarg.0
+ IL_0001: initobj ""S1""
+ IL_0007: ret
+}
+");
+
+ verifier.VerifyIL("S1..ctor(string, string)", @"
+{
+ // Code size 20 (0x14)
+ .maxstack 2
+ IL_0000: ldarg.0
+ IL_0001: ldarg.1
+ IL_0002: call ""S1..ctor(string)""
+ IL_0007: ldarg.0
+ IL_0008: call ""readonly string S1.Prop.get""
+ IL_000d: callvirt ""string object.ToString()""
+ IL_0012: pop
+ IL_0013: ret
+}
+");
}
[Fact, WorkItem(48574, "https://github.com/dotnet/roslyn/issues/48574")]
@@ -907,6 +959,34 @@ public S1(string s1, string s2) : this(s1)
Diagnostic(ErrorCode.WRN_UninitializedNonNullableField, "S1").WithArguments("field", "field").WithLocation(13, 12));
}
+ [Fact, WorkItem(48574, "https://github.com/dotnet/roslyn/issues/48574")]
+ public void StructConstructorInitializer_NestedUninitializedField()
+ {
+ var source = @"
+#nullable enable
+public struct S1
+{
+ public object F1;
+ public S2 S2;
+
+ public S1()
+ {
+ F1.ToString(); // 1
+ S2.F2.ToString(); // missing warning: https://github.com/dotnet/roslyn/issues/60038
+ }
+}
+public struct S2
+{
+ public object F2;
+}
+";
+ var comp = CreateCompilation(source, parseOptions: TestOptions.RegularNext);
+ comp.VerifyDiagnostics(
+ // (10,9): warning CS8602: Dereference of a possibly null reference.
+ // F1.ToString(); // 1
+ Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "F1").WithLocation(10, 9));
+ }
+
[Fact, WorkItem(48574, "https://github.com/dotnet/roslyn/issues/48574")]
public void StructConstructorInitializer_InitializedFieldViaParameterlessConstructor()
{
@@ -969,18 +1049,50 @@ public S1(string s) // 1, 2
}
}
";
- var comp = CreateCompilation(source);
+ var comp = CreateCompilation(source, parseOptions: TestOptions.Regular10);
comp.VerifyDiagnostics(
// (13,12): warning CS8618: Non-nullable field 'field' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.
// public S1(string s) // 1, 2
Diagnostic(ErrorCode.WRN_UninitializedNonNullableField, "S1").WithArguments("field", "field").WithLocation(13, 12),
- // (13,12): error CS0171: Field 'S1.field' must be fully assigned before control is returned to the caller
+ // (13,12): error CS0171: Field 'S1.field' must be fully assigned before control is returned to the caller. Consider updating to language version 'preview' to auto-default the field.
// public S1(string s) // 1, 2
- Diagnostic(ErrorCode.ERR_UnassignedThis, "S1").WithArguments("S1.field").WithLocation(13, 12),
- // (15,30): error CS0170: Use of possibly unassigned field 'field'
+ Diagnostic(ErrorCode.ERR_UnassignedThisUnsupportedVersion, "S1").WithArguments("S1.field", "preview").WithLocation(13, 12),
+ // (15,30): error CS9014: Use of possibly unassigned field 'field'. Consider updating to language version 'preview' to auto-default the field.
// System.Console.Write(field); // 3
- Diagnostic(ErrorCode.ERR_UseDefViolationField, "field").WithArguments("field").WithLocation(15, 30)
+ Diagnostic(ErrorCode.ERR_UseDefViolationFieldUnsupportedVersion, "field").WithArguments("field", "preview").WithLocation(15, 30)
);
+
+ var verifier = CompileAndVerify(source, parseOptions: TestOptions.RegularNext);
+ verifier.VerifyDiagnostics(
+ // (13,12): warning CS8618: Non-nullable field 'field' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.
+ // public S1(string s) // 1, 2
+ Diagnostic(ErrorCode.WRN_UninitializedNonNullableField, "S1").WithArguments("field", "field").WithLocation(13, 12)
+ );
+
+ verifier.VerifyIL("S1..ctor()", @"
+{
+ // Code size 12 (0xc)
+ .maxstack 2
+ IL_0000: ldarg.0
+ IL_0001: ldstr ""ok ""
+ IL_0006: stfld ""string S1.field""
+ IL_000b: ret
+}
+");
+
+ verifier.VerifyIL("S1..ctor(string)", @"
+{
+ // Code size 19 (0x13)
+ .maxstack 2
+ IL_0000: ldarg.0
+ IL_0001: ldnull
+ IL_0002: stfld ""string S1.field""
+ IL_0007: ldarg.0
+ IL_0008: ldfld ""string S1.field""
+ IL_000d: call ""void System.Console.Write(string)""
+ IL_0012: ret
+}
+");
}
[Fact, WorkItem(48574, "https://github.com/dotnet/roslyn/issues/48574")]
@@ -1675,12 +1787,50 @@ internal S(string s)
// (6,14): warning CS8618: Non-nullable field 'F' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.
// internal S(string s)
Diagnostic(ErrorCode.WRN_UninitializedNonNullableField, "S").WithArguments("field", "F").WithLocation(6, 14),
- // (6,14): error CS0843: Auto-implemented property 'S.P' must be fully assigned before control is returned to the caller.
+ // (6,14): error CS0843: Auto-implemented property 'S.P' must be fully assigned before control is returned to the caller. Consider updating to language version 'preview' to auto-default the property.
// internal S(string s)
- Diagnostic(ErrorCode.ERR_UnassignedThisAutoProperty, "S").WithArguments("S.P").WithLocation(6, 14),
- // (6,14): error CS0171: Field 'S.F' must be fully assigned before control is returned to the caller
+ Diagnostic(ErrorCode.ERR_UnassignedThisAutoPropertyUnsupportedVersion, "S").WithArguments("S.P", "preview").WithLocation(6, 14),
+ // (6,14): error CS0171: Field 'S.F' must be fully assigned before control is returned to the caller. Consider updating to language version 'preview' to auto-default the field.
// internal S(string s)
- Diagnostic(ErrorCode.ERR_UnassignedThis, "S").WithArguments("S.F").WithLocation(6, 14));
+ Diagnostic(ErrorCode.ERR_UnassignedThisUnsupportedVersion, "S").WithArguments("S.F", "preview").WithLocation(6, 14));
+
+ var verifier = CompileAndVerify(new[] { source }, options: WithNullableEnable(), parseOptions: TestOptions.RegularNext);
+ verifier.VerifyDiagnostics(
+ // (6,14): warning CS8618: Non-nullable property 'P' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
+ // internal S(string s)
+ Diagnostic(ErrorCode.WRN_UninitializedNonNullableField, "S").WithArguments("property", "P").WithLocation(6, 14),
+ // (6,14): warning CS8618: Non-nullable field 'F' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.
+ // internal S(string s)
+ Diagnostic(ErrorCode.WRN_UninitializedNonNullableField, "S").WithArguments("field", "F").WithLocation(6, 14));
+ verifier.VerifyIL("S..ctor", @"
+{
+ // Code size 48 (0x30)
+ .maxstack 5
+ IL_0000: ldarg.0
+ IL_0001: ldnull
+ IL_0002: stfld ""string S.F""
+ IL_0007: ldarg.0
+ IL_0008: ldnull
+ IL_0009: stfld ""string[] S.k__BackingField""
+ IL_000e: ldarg.1
+ IL_000f: callvirt ""int string.Length.get""
+ IL_0014: ldc.i4.0
+ IL_0015: ble.s IL_001f
+ IL_0017: ldarg.0
+ IL_0018: ldarg.1
+ IL_0019: stfld ""string S.F""
+ IL_001e: ret
+ IL_001f: ldarg.0
+ IL_0020: ldc.i4.1
+ IL_0021: newarr ""string""
+ IL_0026: dup
+ IL_0027: ldc.i4.0
+ IL_0028: ldarg.1
+ IL_0029: stelem.ref
+ IL_002a: call ""void S.P.set""
+ IL_002f: ret
+}
+");
}
[Fact]
diff --git a/src/Compilers/CSharp/Test/Semantic/Semantics/WarningVersionTests.cs b/src/Compilers/CSharp/Test/Semantic/Semantics/WarningVersionTests.cs
index 9a0a81a379c1b..cc9d27ebaf279 100644
--- a/src/Compilers/CSharp/Test/Semantic/Semantics/WarningVersionTests.cs
+++ b/src/Compilers/CSharp/Test/Semantic/Semantics/WarningVersionTests.cs
@@ -129,7 +129,7 @@ public struct Struct
}
";
var comp1 = CreateCompilation(source, options: TestOptions.DebugModule);
- var moduleReference = comp1.EmitToImageReference();
+ var moduleReference = new[] { comp1.EmitToImageReference() };
var source2 =
@"public struct Program
@@ -139,13 +139,62 @@ public Program(int dummy)
{
}
}";
- CreateCompilation(source2, references: new MetadataReference[] { moduleReference }, options: TestOptions.ReleaseDll.WithWarningLevel(CodeAnalysis.Diagnostic.DefaultWarningLevel)).VerifyDiagnostics(
- );
- CreateCompilation(source2, references: new MetadataReference[] { moduleReference }, options: TestOptions.ReleaseDll.WithWarningLevel(5)).VerifyDiagnostics(
- // (4,12): warning CS8822: Auto-implemented property 'Program.Property' must be fully assigned before control is returned to the caller.
+ var expectedIL = @"
+{
+ // Code size 14 (0xe)
+ .maxstack 1
+ IL_0000: ldarg.0
+ IL_0001: ldflda ""Struct Program.k__BackingField""
+ IL_0006: initobj ""Struct""
+ IL_000c: nop
+ IL_000d: ret
+}
+";
+ // C# 10
+ var verifier = CompileAndVerify(
+ source2,
+ references: moduleReference,
+ options: TestOptions.DebugDll.WithWarningLevel(CodeAnalysis.Diagnostic.DefaultWarningLevel),
+ parseOptions: TestOptions.Regular10,
+ verify: Verification.Skipped);
+ verifier.VerifyDiagnostics();
+ verifier.VerifyIL("Program..ctor", expectedIL);
+
+ verifier = CompileAndVerify(
+ source2,
+ references: moduleReference,
+ options: TestOptions.DebugDll.WithWarningLevel(5),
+ parseOptions: TestOptions.Regular10,
+ verify: Verification.Skipped);
+ verifier.VerifyDiagnostics(
+ // (4,12): warning CS8880: Auto-implemented property 'Program.Property' must be fully assigned before control is returned to the caller. Consider updating to language version 'preview' to auto-default the property.
// public Program(int dummy)
- Diagnostic(ErrorCode.WRN_UnassignedThisAutoProperty, "Program").WithArguments("Program.Property").WithLocation(4, 12)
- );
+ Diagnostic(ErrorCode.WRN_UnassignedThisAutoPropertyUnsupportedVersion, "Program").WithArguments("Program.Property", "preview").WithLocation(4, 12));
+ verifier.VerifyIL("Program..ctor", expectedIL);
+
+ // C# 11+
+ verifier = CompileAndVerify(
+ source2,
+ references: moduleReference,
+ options: TestOptions.DebugDll.WithWarningLevel(5),
+ parseOptions: TestOptions.RegularNext,
+ verify: Verification.Skipped);
+ verifier.VerifyDiagnostics();
+ verifier.VerifyIL("Program..ctor", expectedIL);
+
+ verifier = CompileAndVerify(
+ source2,
+ references: moduleReference,
+ options: TestOptions.DebugDll
+ .WithWarningLevel(5)
+ .WithSpecificDiagnosticOptions(ReportStructInitializationWarnings),
+ parseOptions: TestOptions.RegularNext,
+ verify: Verification.Skipped);
+ verifier.VerifyDiagnostics(
+ // (4,12): warning CS9020: Control is returned to caller before auto-implemented property 'Program.Property' is explicitly assigned, causing a preceding implicit assignment of 'default'.
+ // public Program(int dummy)
+ Diagnostic(ErrorCode.WRN_UnassignedThisAutoPropertySupportedVersion, "Program").WithArguments("Program.Property").WithLocation(4, 12));
+ verifier.VerifyIL("Program..ctor", expectedIL);
}
[Fact]
@@ -158,7 +207,7 @@ public struct Struct
}
";
var comp1 = CreateCompilation(source, options: TestOptions.DebugModule);
- var moduleReference = comp1.EmitToImageReference();
+ var moduleReference = new[] { comp1.EmitToImageReference() };
var source2 =
@"public struct Program
@@ -168,13 +217,157 @@ public Program(int dummy)
{
}
}";
- CreateCompilation(source2, references: new MetadataReference[] { moduleReference }, options: TestOptions.ReleaseDll.WithWarningLevel(CodeAnalysis.Diagnostic.DefaultWarningLevel)).VerifyDiagnostics(
- );
- CreateCompilation(source2, references: new MetadataReference[] { moduleReference }, options: TestOptions.ReleaseDll.WithWarningLevel(5)).VerifyDiagnostics(
- // (4,12): warning CS8823: Field 'Program.Field' must be fully assigned before control is returned to the caller
+ var expectedIL = @"
+{
+ // Code size 14 (0xe)
+ .maxstack 1
+ IL_0000: ldarg.0
+ IL_0001: ldflda ""Struct Program.Field""
+ IL_0006: initobj ""Struct""
+ IL_000c: nop
+ IL_000d: ret
+}
+";
+ // C# 10
+ var verifier = CompileAndVerify(
+ source2,
+ references: moduleReference,
+ options: TestOptions.DebugDll.WithWarningLevel(CodeAnalysis.Diagnostic.DefaultWarningLevel),
+ parseOptions: TestOptions.Regular10,
+ verify: Verification.Skipped);
+ verifier.VerifyDiagnostics();
+ verifier.VerifyIL("Program..ctor", expectedIL);
+
+ verifier = CompileAndVerify(
+ source2,
+ references: moduleReference,
+ options: TestOptions.DebugDll.WithWarningLevel(5),
+ parseOptions: TestOptions.Regular10,
+ verify: Verification.Skipped);
+ verifier.VerifyDiagnostics(
+ // (4,12): warning CS8881: Field 'Program.Field' must be fully assigned before control is returned to the caller. Consider updating to language version 'preview' to auto-default the field.
// public Program(int dummy)
- Diagnostic(ErrorCode.WRN_UnassignedThis, "Program").WithArguments("Program.Field").WithLocation(4, 12)
- );
+ Diagnostic(ErrorCode.WRN_UnassignedThisUnsupportedVersion, "Program").WithArguments("Program.Field", "preview").WithLocation(4, 12));
+ verifier.VerifyIL("Program..ctor", expectedIL);
+
+ // C# 11+
+ verifier = CompileAndVerify(
+ source2,
+ references: moduleReference,
+ options: TestOptions.DebugDll
+ .WithWarningLevel(5),
+ parseOptions: TestOptions.RegularNext,
+ verify: Verification.Skipped);
+ verifier.VerifyDiagnostics();
+ verifier.VerifyIL("Program..ctor", expectedIL);
+
+ verifier = CompileAndVerify(
+ source2,
+ references: moduleReference,
+ options: TestOptions.DebugDll
+ .WithWarningLevel(5)
+ .WithSpecificDiagnosticOptions(ReportStructInitializationWarnings),
+ parseOptions: TestOptions.RegularNext,
+ verify: Verification.Skipped);
+ verifier.VerifyDiagnostics(
+ // (4,12): warning CS9021: Control is returned to caller before field 'Program.Field' is explicitly assigned, causing a preceding implicit assignment of 'default'.
+ // public Program(int dummy)
+ Diagnostic(ErrorCode.WRN_UnassignedThisSupportedVersion, "Program").WithArguments("Program.Field").WithLocation(4, 12));
+ verifier.VerifyIL("Program..ctor", expectedIL);
+ }
+
+ [Fact]
+ public void UnassignedThisField_And_UnassignedLocal()
+ {
+ var source = @"
+public struct Struct
+{
+ private string data;
+}
+";
+ var comp1 = CreateCompilation(source, options: TestOptions.DebugModule);
+ var moduleReference = new[] { comp1.EmitToImageReference() };
+
+ var source2 =
+@"public struct Program
+{
+ public Struct Field;
+ public Program(int dummy)
+ {
+ Struct s;
+ s.ToString();
+ }
+}";
+ var expectedIL = @"
+{
+ // Code size 28 (0x1c)
+ .maxstack 1
+ .locals init (Struct V_0) //s
+ IL_0000: ldarg.0
+ IL_0001: ldflda ""Struct Program.Field""
+ IL_0006: initobj ""Struct""
+ IL_000c: nop
+ IL_000d: ldloca.s V_0
+ IL_000f: constrained. ""Struct""
+ IL_0015: callvirt ""string object.ToString()""
+ IL_001a: pop
+ IL_001b: ret
+}
+";
+ // C# 10
+ var verifier = CompileAndVerify(
+ source2,
+ references: moduleReference,
+ options: TestOptions.DebugDll.WithWarningLevel(CodeAnalysis.Diagnostic.DefaultWarningLevel),
+ parseOptions: TestOptions.Regular10,
+ verify: Verification.Skipped);
+ verifier.VerifyDiagnostics();
+ verifier.VerifyIL("Program..ctor", expectedIL);
+
+ verifier = CompileAndVerify(
+ source2,
+ references: moduleReference,
+ options: TestOptions.DebugDll.WithWarningLevel(5),
+ parseOptions: TestOptions.Regular10,
+ verify: Verification.Skipped);
+ verifier.VerifyDiagnostics(
+ // (4,12): warning CS8881: Field 'Program.Field' must be fully assigned before control is returned to the caller. Consider updating to language version 'preview' to auto-default the field.
+ // public Program(int dummy)
+ Diagnostic(ErrorCode.WRN_UnassignedThisUnsupportedVersion, "Program").WithArguments("Program.Field", "preview").WithLocation(4, 12),
+ // (7,9): warning CS8887: Use of unassigned local variable 's'
+ // s.ToString();
+ Diagnostic(ErrorCode.WRN_UseDefViolation, "s").WithArguments("s").WithLocation(7, 9));
+ verifier.VerifyIL("Program..ctor", expectedIL);
+
+ // C# 11+
+ verifier = CompileAndVerify(
+ source2,
+ references: moduleReference,
+ options: TestOptions.DebugDll.WithWarningLevel(5),
+ parseOptions: TestOptions.RegularNext,
+ verify: Verification.Skipped);
+ verifier.VerifyDiagnostics(
+ // (7,9): warning CS8887: Use of unassigned local variable 's'
+ // s.ToString();
+ Diagnostic(ErrorCode.WRN_UseDefViolation, "s").WithArguments("s").WithLocation(7, 9));
+ verifier.VerifyIL("Program..ctor", expectedIL);
+
+ verifier = CompileAndVerify(
+ source2,
+ references: moduleReference,
+ options: TestOptions.DebugDll
+ .WithWarningLevel(5)
+ .WithSpecificDiagnosticOptions(ReportStructInitializationWarnings),
+ parseOptions: TestOptions.RegularNext,
+ verify: Verification.Skipped);
+ verifier.VerifyDiagnostics(
+ // (4,12): warning CS9021: Control is returned to caller before field 'Program.Field' is explicitly assigned, causing a preceding implicit assignment of 'default'.
+ // public Program(int dummy)
+ Diagnostic(ErrorCode.WRN_UnassignedThisSupportedVersion, "Program").WithArguments("Program.Field").WithLocation(4, 12),
+ // (7,9): warning CS8887: Use of unassigned local variable 's'
+ // s.ToString();
+ Diagnostic(ErrorCode.WRN_UseDefViolation, "s").WithArguments("s").WithLocation(7, 9));
+ verifier.VerifyIL("Program..ctor", expectedIL);
}
[Fact]
@@ -215,7 +408,7 @@ public struct Struct
}
";
var comp1 = CreateCompilation(source, options: TestOptions.DebugModule);
- var moduleReference = comp1.EmitToImageReference();
+ var moduleReference = new[] { comp1.EmitToImageReference() };
var source2 =
@"public struct Program
@@ -227,13 +420,69 @@ public struct Struct
Property = default;
}
}";
- CreateCompilation(source2, references: new MetadataReference[] { moduleReference }, options: TestOptions.ReleaseDll.WithWarningLevel(CodeAnalysis.Diagnostic.DefaultWarningLevel)).VerifyDiagnostics(
- );
- CreateCompilation(source2, references: new MetadataReference[] { moduleReference }, options: TestOptions.ReleaseDll.WithWarningLevel(5)).VerifyDiagnostics(
- // (6,21): warning CS8825: Use of possibly unassigned auto-implemented property 'Property'
+ var expectedIL = @"
+{
+ // Code size 33 (0x21)
+ .maxstack 1
+ .locals init (Struct V_0) //v2
+ IL_0000: ldarg.0
+ IL_0001: ldflda ""Struct Program.k__BackingField""
+ IL_0006: initobj ""Struct""
+ IL_000c: nop
+ IL_000d: ldarg.0
+ IL_000e: call ""readonly Struct Program.Property.get""
+ IL_0013: stloc.0
+ IL_0014: ldarg.0
+ IL_0015: ldflda ""Struct Program.k__BackingField""
+ IL_001a: initobj ""Struct""
+ IL_0020: ret
+}
+";
+ // C# 10
+ var verifier = CompileAndVerify(
+ source2,
+ references: moduleReference,
+ options: TestOptions.DebugDll.WithWarningLevel(CodeAnalysis.Diagnostic.DefaultWarningLevel),
+ parseOptions: TestOptions.Regular10,
+ verify: Verification.Skipped);
+ verifier.VerifyDiagnostics();
+ verifier.VerifyIL("Program..ctor", expectedIL);
+
+ verifier = CompileAndVerify(
+ source2,
+ references: moduleReference,
+ options: TestOptions.DebugDll.WithWarningLevel(5),
+ parseOptions: TestOptions.Regular10,
+ verify: Verification.Skipped);
+ verifier.VerifyDiagnostics(
+ // (6,21): warning CS9015: Use of possibly unassigned auto-implemented property 'Property'. Consider updating to language version 'preview' to auto-default the property.
// Struct v2 = Property;
- Diagnostic(ErrorCode.WRN_UseDefViolationProperty, "Property").WithArguments("Property").WithLocation(6, 21)
- );
+ Diagnostic(ErrorCode.WRN_UseDefViolationPropertyUnsupportedVersion, "Property").WithArguments("Property", "preview").WithLocation(6, 21));
+ verifier.VerifyIL("Program..ctor", expectedIL);
+
+ // C# 11+
+ verifier = CompileAndVerify(
+ source2,
+ references: moduleReference,
+ options: TestOptions.DebugDll.WithWarningLevel(5),
+ parseOptions: TestOptions.RegularNext,
+ verify: Verification.Skipped);
+ verifier.VerifyDiagnostics();
+ verifier.VerifyIL("Program..ctor", expectedIL);
+
+ verifier = CompileAndVerify(
+ source2,
+ references: moduleReference,
+ options: TestOptions.DebugDll
+ .WithWarningLevel(5)
+ .WithSpecificDiagnosticOptions(ReportStructInitializationWarnings),
+ parseOptions: TestOptions.RegularNext,
+ verify: Verification.Skipped);
+ verifier.VerifyDiagnostics(
+ // (6,21): warning CS9014: Use of possibly unassigned auto-implemented property 'Property'
+ // Struct v2 = Property;
+ Diagnostic(ErrorCode.WRN_UseDefViolationPropertySupportedVersion, "Property").WithArguments("Property").WithLocation(6, 21));
+ verifier.VerifyIL("Program..ctor", expectedIL);
}
[Fact]
@@ -246,7 +495,7 @@ public struct Struct
}
";
var comp1 = CreateCompilation(source, options: TestOptions.DebugModule);
- var moduleReference = comp1.EmitToImageReference();
+ var moduleReference = new[] { comp1.EmitToImageReference() };
var source2 =
@"public struct Program
@@ -258,13 +507,69 @@ public struct Struct
Field = default;
}
}";
- CreateCompilation(source2, references: new MetadataReference[] { moduleReference }, options: TestOptions.ReleaseDll.WithWarningLevel(CodeAnalysis.Diagnostic.DefaultWarningLevel)).VerifyDiagnostics(
- );
- CreateCompilation(source2, references: new MetadataReference[] { moduleReference }, options: TestOptions.ReleaseDll.WithWarningLevel(5)).VerifyDiagnostics(
- // (6,21): warning CS8826: Use of possibly unassigned field 'Field'
+ var expectedIL = @"
+{
+ // Code size 33 (0x21)
+ .maxstack 1
+ .locals init (Struct V_0) //v2
+ IL_0000: ldarg.0
+ IL_0001: ldflda ""Struct Program.Field""
+ IL_0006: initobj ""Struct""
+ IL_000c: nop
+ IL_000d: ldarg.0
+ IL_000e: ldfld ""Struct Program.Field""
+ IL_0013: stloc.0
+ IL_0014: ldarg.0
+ IL_0015: ldflda ""Struct Program.Field""
+ IL_001a: initobj ""Struct""
+ IL_0020: ret
+}
+";
+ // C# 10
+ var verifier = CompileAndVerify(
+ source2,
+ references: moduleReference,
+ options: TestOptions.DebugDll.WithWarningLevel(CodeAnalysis.Diagnostic.DefaultWarningLevel),
+ parseOptions: TestOptions.Regular10,
+ verify: Verification.Skipped);
+ verifier.VerifyDiagnostics();
+ verifier.VerifyIL("Program..ctor", expectedIL);
+
+ verifier = CompileAndVerify(
+ source2,
+ references: moduleReference,
+ options: TestOptions.DebugDll.WithWarningLevel(5),
+ parseOptions: TestOptions.Regular10,
+ verify: Verification.Skipped);
+ verifier.VerifyDiagnostics(
+ // (6,21): warning CS9016: Use of possibly unassigned field 'Field'. Consider updating to language version 'preview' to auto-default the field.
// Struct v2 = Field;
- Diagnostic(ErrorCode.WRN_UseDefViolationField, "Field").WithArguments("Field").WithLocation(6, 21)
- );
+ Diagnostic(ErrorCode.WRN_UseDefViolationFieldUnsupportedVersion, "Field").WithArguments("Field", "preview").WithLocation(6, 21));
+ verifier.VerifyIL("Program..ctor", expectedIL);
+
+ // C# 11+
+ verifier = CompileAndVerify(
+ source2,
+ references: moduleReference,
+ options: TestOptions.DebugDll.WithWarningLevel(5),
+ parseOptions: TestOptions.RegularNext,
+ verify: Verification.Skipped);
+ verifier.VerifyDiagnostics();
+
+ verifier.VerifyIL("Program..ctor", expectedIL);
+ verifier = CompileAndVerify(
+ source2,
+ references: moduleReference,
+ options: TestOptions.DebugDll
+ .WithWarningLevel(5)
+ .WithSpecificDiagnosticOptions(ReportStructInitializationWarnings),
+ parseOptions: TestOptions.RegularNext,
+ verify: Verification.Skipped);
+ verifier.VerifyDiagnostics(
+ // (6,21): warning CS9014: Use of possibly unassigned field 'Field'
+ // Struct v2 = Field;
+ Diagnostic(ErrorCode.WRN_UseDefViolationFieldSupportedVersion, "Field").WithArguments("Field").WithLocation(6, 21));
+ verifier.VerifyIL("Program..ctor", expectedIL);
}
[Fact]
@@ -277,7 +582,7 @@ public struct Struct
}
";
var comp1 = CreateCompilation(source, options: TestOptions.DebugModule);
- var moduleReference = comp1.EmitToImageReference();
+ var moduleReference = new[] { comp1.EmitToImageReference() };
var source2 =
@"public struct Program
@@ -289,13 +594,69 @@ public struct Struct
this.Field = default;
}
}";
- CreateCompilation(source2, references: new MetadataReference[] { moduleReference }, options: TestOptions.ReleaseDll.WithWarningLevel(CodeAnalysis.Diagnostic.DefaultWarningLevel)).VerifyDiagnostics(
- );
- CreateCompilation(source2, references: new MetadataReference[] { moduleReference }, options: TestOptions.ReleaseDll.WithWarningLevel(5)).VerifyDiagnostics(
- // (6,22): warning CS8827: The 'this' object cannot be used before all of its fields have been assigned
+ var expectedIL = @"
+{
+ // Code size 33 (0x21)
+ .maxstack 1
+ .locals init (Program V_0) //p2
+ IL_0000: ldarg.0
+ IL_0001: ldflda ""Struct Program.Field""
+ IL_0006: initobj ""Struct""
+ IL_000c: nop
+ IL_000d: ldarg.0
+ IL_000e: ldobj ""Program""
+ IL_0013: stloc.0
+ IL_0014: ldarg.0
+ IL_0015: ldflda ""Struct Program.Field""
+ IL_001a: initobj ""Struct""
+ IL_0020: ret
+}
+";
+ // C# 10
+ var verifier = CompileAndVerify(
+ source2,
+ references: moduleReference,
+ options: TestOptions.DebugDll.WithWarningLevel(CodeAnalysis.Diagnostic.DefaultWarningLevel),
+ parseOptions: TestOptions.Regular10,
+ verify: Verification.Skipped);
+ verifier.VerifyDiagnostics();
+ verifier.VerifyIL("Program..ctor", expectedIL);
+
+ verifier = CompileAndVerify(
+ source2,
+ references: moduleReference,
+ options: TestOptions.DebugDll.WithWarningLevel(5),
+ parseOptions: TestOptions.Regular10,
+ verify: Verification.Skipped);
+ verifier.VerifyDiagnostics(
+ // (6,22): warning CS8885: The 'this' object cannot be used before all of its fields have been assigned. Consider updating to language version 'this' to auto-default the unassigned fields.
// Program p2 = this;
- Diagnostic(ErrorCode.WRN_UseDefViolationThis, "this").WithArguments("this").WithLocation(6, 22)
- );
+ Diagnostic(ErrorCode.WRN_UseDefViolationThisUnsupportedVersion, "this").WithArguments("this", "preview").WithLocation(6, 22));
+ verifier.VerifyIL("Program..ctor", expectedIL);
+
+ // C# 11+
+ verifier = CompileAndVerify(
+ source2,
+ references: moduleReference,
+ options: TestOptions.DebugDll.WithWarningLevel(5),
+ parseOptions: TestOptions.RegularNext,
+ verify: Verification.Skipped);
+ verifier.VerifyDiagnostics();
+ verifier.VerifyIL("Program..ctor", expectedIL);
+
+ verifier = CompileAndVerify(
+ source2,
+ references: moduleReference,
+ options: TestOptions.DebugDll
+ .WithWarningLevel(5)
+ .WithSpecificDiagnosticOptions(ReportStructInitializationWarnings),
+ parseOptions: TestOptions.RegularNext,
+ verify: Verification.Skipped);
+ verifier.VerifyDiagnostics(
+ // (6,22): warning CS9019: The 'this' object cannot be used before all of its fields have been assigned, causing preceding implicit assignments of 'default' to non-explicitly assigned fields.
+ // Program p2 = this;
+ Diagnostic(ErrorCode.WRN_UseDefViolationThisSupportedVersion, "this").WithArguments("this").WithLocation(6, 22));
+ verifier.VerifyIL("Program..ctor", expectedIL);
}
[Fact]
diff --git a/src/Compilers/CSharp/Test/Symbol/Symbols/Source/EventTests.cs b/src/Compilers/CSharp/Test/Symbol/Symbols/Source/EventTests.cs
index 4c6b66371c085..d7fdef0e1feef 100644
--- a/src/Compilers/CSharp/Test/Symbol/Symbols/Source/EventTests.cs
+++ b/src/Compilers/CSharp/Test/Symbol/Symbols/Source/EventTests.cs
@@ -1333,7 +1333,7 @@ event System.Action F { add { } remove { } }
S(int unused1, int unused2)
{
- // CS0171: E not initialized
+ // CS0171: E not initialized before C# 11
// No error for F
}
@@ -1346,13 +1346,18 @@ void Method(S s)
}
}
";
- CreateCompilation(text).VerifyDiagnostics(
- // (11,5): error CS0171: Field 'S.E' must be fully assigned before control is returned to the caller
+ CreateCompilation(text, parseOptions: TestOptions.Regular10).VerifyDiagnostics(
+ // (11,5): error CS0171: Field 'S.E' must be fully assigned before control is returned to the caller. Consider updating to language version 'preview' to auto-default the field.
// S(int unused1, int unused2)
- Diagnostic(ErrorCode.ERR_UnassignedThis, "S").WithArguments("S.E"),
- // (21,9): error CS1612: Cannot modify the return value of 'S.This' because it is not a variable
+ Diagnostic(ErrorCode.ERR_UnassignedThisUnsupportedVersion, "S").WithArguments("S.E", "preview").WithLocation(11, 5),
+ // (22,9): error CS1612: Cannot modify the return value of 'S.This' because it is not a variable
+ // This.E = null; //CS1612: receiver is not a variable
+ Diagnostic(ErrorCode.ERR_ReturnNotLValue, "This").WithArguments("S.This").WithLocation(22, 9));
+
+ CreateCompilation(text, parseOptions: TestOptions.RegularNext).VerifyDiagnostics(
+ // (22,9): error CS1612: Cannot modify the return value of 'S.This' because it is not a variable
// This.E = null; //CS1612: receiver is not a variable
- Diagnostic(ErrorCode.ERR_ReturnNotLValue, "This").WithArguments("S.This"));
+ Diagnostic(ErrorCode.ERR_ReturnNotLValue, "This").WithArguments("S.This").WithLocation(22, 9));
}
[WorkItem(546356, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/546356")]
diff --git a/src/Compilers/CSharp/Test/Symbol/Symbols/Source/NullablePublicAPITests.cs b/src/Compilers/CSharp/Test/Symbol/Symbols/Source/NullablePublicAPITests.cs
index fe4dd2aabd837..43002d02ece83 100644
--- a/src/Compilers/CSharp/Test/Symbol/Symbols/Source/NullablePublicAPITests.cs
+++ b/src/Compilers/CSharp/Test/Symbol/Symbols/Source/NullablePublicAPITests.cs
@@ -4971,15 +4971,6 @@ public ValueTuple(T1 item1, T2 item2)
// (7,39): error CS8128: Member 'Item1' was not found on type '(T1, T2)' from assembly 'comp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
// System.Console.WriteLine($"{x.a}");
Diagnostic(ErrorCode.ERR_PredefinedTypeMemberNotFoundInAssembly, "a").WithArguments("Item1", "(T1, T2)", "comp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null").WithLocation(7, 39),
- // (17,16): error CS0171: Field '(T1, T2).Item2' must be fully assigned before control is returned to the caller
- // public ValueTuple(T1 item1, T2 item2)
- Diagnostic(ErrorCode.ERR_UnassignedThis, "ValueTuple").WithArguments("(T1, T2).Item2").WithLocation(17, 16),
- // (17,16): error CS0171: Field '(T1, T2).Item1' must be fully assigned before control is returned to the caller
- // public ValueTuple(T1 item1, T2 item2)
- Diagnostic(ErrorCode.ERR_UnassignedThis, "ValueTuple").WithArguments("(T1, T2).Item1").WithLocation(17, 16),
- // (17,16): error CS0171: Field '(T1, T2).Item2' must be fully assigned before control is returned to the caller
- // public ValueTuple(T1 item1, T2 item2)
- Diagnostic(ErrorCode.ERR_UnassignedThis, "ValueTuple").WithArguments("(T1, T2).Item2").WithLocation(17, 16),
// (19,18): error CS0229: Ambiguity between '(T1, T2).Item2' and '(T1, T2).Item2'
// this.Item2 = 2;
Diagnostic(ErrorCode.ERR_AmbigMember, "Item2").WithArguments("(T1, T2).Item2", "(T1, T2).Item2").WithLocation(19, 18)
diff --git a/src/Compilers/CSharp/Test/Syntax/Diagnostics/DiagnosticTest.cs b/src/Compilers/CSharp/Test/Syntax/Diagnostics/DiagnosticTest.cs
index ffc6e4ad098c9..fa8c75eb718a3 100644
--- a/src/Compilers/CSharp/Test/Syntax/Diagnostics/DiagnosticTest.cs
+++ b/src/Compilers/CSharp/Test/Syntax/Diagnostics/DiagnosticTest.cs
@@ -349,6 +349,11 @@ public void WarningLevel_2()
case ErrorCode.WRN_InterpolatedStringHandlerArgumentAttributeIgnoredOnLambdaParameters:
case ErrorCode.WRN_CompileTimeCheckedOverflow:
case ErrorCode.WRN_MethGrpToNonDel:
+ case ErrorCode.WRN_UnassignedThisAutoPropertySupportedVersion:
+ case ErrorCode.WRN_UnassignedThisSupportedVersion:
+ case ErrorCode.WRN_UseDefViolationPropertySupportedVersion:
+ case ErrorCode.WRN_UseDefViolationFieldSupportedVersion:
+ case ErrorCode.WRN_UseDefViolationThisSupportedVersion:
Assert.Equal(1, ErrorFacts.GetWarningLevel(errorCode));
break;
case ErrorCode.WRN_InvalidVersionFormat:
@@ -357,12 +362,14 @@ public void WarningLevel_2()
case ErrorCode.WRN_NubExprIsConstBool2:
case ErrorCode.WRN_StaticInAsOrIs:
case ErrorCode.WRN_PrecedenceInversion:
- case ErrorCode.WRN_UnassignedThisAutoProperty:
- case ErrorCode.WRN_UnassignedThis:
+ case ErrorCode.WRN_UnassignedThisAutoPropertyUnsupportedVersion:
+ case ErrorCode.WRN_UnassignedThisUnsupportedVersion:
case ErrorCode.WRN_ParamUnassigned:
case ErrorCode.WRN_UseDefViolationProperty:
case ErrorCode.WRN_UseDefViolationField:
- case ErrorCode.WRN_UseDefViolationThis:
+ case ErrorCode.WRN_UseDefViolationPropertyUnsupportedVersion:
+ case ErrorCode.WRN_UseDefViolationFieldUnsupportedVersion:
+ case ErrorCode.WRN_UseDefViolationThisUnsupportedVersion:
case ErrorCode.WRN_UseDefViolationOut:
case ErrorCode.WRN_UseDefViolation:
case ErrorCode.WRN_SyncAndAsyncEntryPoints:
@@ -425,12 +432,12 @@ public void NullableWarnings()
ErrorCode.WRN_ConstOutOfRangeChecked,
ErrorCode.WRN_SwitchExpressionNotExhaustiveWithWhen,
ErrorCode.WRN_PrecedenceInversion,
- ErrorCode.WRN_UnassignedThisAutoProperty,
- ErrorCode.WRN_UnassignedThis,
+ ErrorCode.WRN_UnassignedThisAutoPropertyUnsupportedVersion,
+ ErrorCode.WRN_UnassignedThisUnsupportedVersion,
ErrorCode.WRN_ParamUnassigned,
ErrorCode.WRN_UseDefViolationProperty,
ErrorCode.WRN_UseDefViolationField,
- ErrorCode.WRN_UseDefViolationThis,
+ ErrorCode.WRN_UseDefViolationThisUnsupportedVersion,
ErrorCode.WRN_UseDefViolationOut,
ErrorCode.WRN_UseDefViolation,
ErrorCode.WRN_SyncAndAsyncEntryPoints,
@@ -2756,6 +2763,11 @@ public override string GetErrorDisplayString(ISymbol symbol)
{
return MessageProvider.Instance.GetErrorDisplayString(symbol);
}
+
+ public override bool GetIsEnabledByDefault(int code)
+ {
+ return true;
+ }
}
#endregion
diff --git a/src/Compilers/Core/Portable/Diagnostic/CommonMessageProvider.cs b/src/Compilers/Core/Portable/Diagnostic/CommonMessageProvider.cs
index 732780f5d5c7e..8a49a6741599d 100644
--- a/src/Compilers/Core/Portable/Diagnostic/CommonMessageProvider.cs
+++ b/src/Compilers/Core/Portable/Diagnostic/CommonMessageProvider.cs
@@ -103,6 +103,8 @@ public Diagnostic CreateDiagnostic(int code, Location location)
///
public abstract string GetErrorDisplayString(ISymbol symbol);
+ public abstract bool GetIsEnabledByDefault(int code);
+
///
/// Given an error code (like 1234) return the identifier (CS1234 or BC1234).
///
diff --git a/src/Compilers/Core/Portable/Diagnostic/DiagnosticInfo.cs b/src/Compilers/Core/Portable/Diagnostic/DiagnosticInfo.cs
index 0f271f2372989..433e23d507e5b 100644
--- a/src/Compilers/Core/Portable/Diagnostic/DiagnosticInfo.cs
+++ b/src/Compilers/Core/Portable/Diagnostic/DiagnosticInfo.cs
@@ -91,7 +91,7 @@ private static DiagnosticDescriptor CreateDescriptor(int errorCode, DiagnosticSe
var category = messageProvider.GetCategory(errorCode);
var customTags = GetCustomTags(defaultSeverity);
return new DiagnosticDescriptor(id, title, messageFormat, category, defaultSeverity,
- isEnabledByDefault: true, description: description, helpLinkUri: helpLink, customTags: customTags);
+ isEnabledByDefault: messageProvider.GetIsEnabledByDefault(errorCode), description: description, helpLinkUri: helpLink, customTags: customTags);
}
[Conditional("DEBUG")]
diff --git a/src/Compilers/Core/Portable/Diagnostic/DiagnosticWithInfo.cs b/src/Compilers/Core/Portable/Diagnostic/DiagnosticWithInfo.cs
index f7de304d402de..9c9a13b31efe8 100644
--- a/src/Compilers/Core/Portable/Diagnostic/DiagnosticWithInfo.cs
+++ b/src/Compilers/Core/Portable/Diagnostic/DiagnosticWithInfo.cs
@@ -84,8 +84,7 @@ public sealed override DiagnosticSeverity DefaultSeverity
internal sealed override bool IsEnabledByDefault
{
- // All compiler errors and warnings are enabled by default.
- get { return true; }
+ get { return this.Info.Descriptor.IsEnabledByDefault; }
}
public override bool IsSuppressed
diff --git a/src/Compilers/Test/Utilities/CSharp/CSharpTestBase.cs b/src/Compilers/Test/Utilities/CSharp/CSharpTestBase.cs
index f0d111f21babb..98db689e6d490 100644
--- a/src/Compilers/Test/Utilities/CSharp/CSharpTestBase.cs
+++ b/src/Compilers/Test/Utilities/CSharp/CSharpTestBase.cs
@@ -2411,6 +2411,22 @@ protected static CSharpCompilation CreateCompilationWithSpanAndMemoryExtensions(
parseOptions: parseOptions);
}
}
+
+ internal static string GetIdForErrorCode(ErrorCode code)
+ {
+ return MessageProvider.Instance.GetIdForErrorCode((int)code);
+ }
+
+ internal static ImmutableDictionary ReportStructInitializationWarnings { get; } = ImmutableDictionary.CreateRange(
+ new[]
+ {
+ KeyValuePairUtil.Create(GetIdForErrorCode(ErrorCode.WRN_UseDefViolationPropertySupportedVersion), ReportDiagnostic.Warn),
+ KeyValuePairUtil.Create(GetIdForErrorCode(ErrorCode.WRN_UseDefViolationFieldSupportedVersion), ReportDiagnostic.Warn),
+ KeyValuePairUtil.Create(GetIdForErrorCode(ErrorCode.WRN_UseDefViolationThisSupportedVersion), ReportDiagnostic.Warn),
+ KeyValuePairUtil.Create(GetIdForErrorCode(ErrorCode.WRN_UnassignedThisAutoPropertySupportedVersion), ReportDiagnostic.Warn),
+ KeyValuePairUtil.Create(GetIdForErrorCode(ErrorCode.WRN_UnassignedThisSupportedVersion), ReportDiagnostic.Warn),
+ });
+
#endregion
#region Interpolated string handlers
diff --git a/src/Compilers/VisualBasic/Portable/Errors/MessageProvider.vb b/src/Compilers/VisualBasic/Portable/Errors/MessageProvider.vb
index 28685c2fc9496..be62b4a41e8e6 100644
--- a/src/Compilers/VisualBasic/Portable/Errors/MessageProvider.vb
+++ b/src/Compilers/VisualBasic/Portable/Errors/MessageProvider.vb
@@ -117,6 +117,10 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
Return SymbolDisplay.ToDisplayString(symbol, SymbolDisplayFormat.VisualBasicShortErrorMessageFormat)
End Function
+ Public Overrides Function GetIsEnabledByDefault(code As Integer) As Boolean
+ Return True
+ End Function
+
' Given a message identifier (e.g., CS0219), severity, warning as error and a culture,
' get the entire prefix (e.g., "error BC42024:" for VB) used on error messages.
Public Overrides Function GetMessagePrefix(id As String, severity As DiagnosticSeverity, isWarningAsError As Boolean, culture As CultureInfo) As String
diff --git a/src/Compilers/VisualBasic/Test/Syntax/Syntax/GeneratedTests.vb b/src/Compilers/VisualBasic/Test/Syntax/Syntax/GeneratedTests.vb
index 1a089907f6e1b..e8ff76e2f4a47 100644
--- a/src/Compilers/VisualBasic/Test/Syntax/Syntax/GeneratedTests.vb
+++ b/src/Compilers/VisualBasic/Test/Syntax/Syntax/GeneratedTests.vb
@@ -86,6 +86,10 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.UnitTests
Public Overrides Function GetErrorDisplayString(symbol As ISymbol) As String
Return MessageProvider.Instance.GetErrorDisplayString(symbol)
End Function
+
+ Public Overrides Function GetIsEnabledByDefault(code As Integer) As Boolean
+ Return True
+ End Function
End Class
Friend Class RedIdentityRewriter
diff --git a/src/Compilers/VisualBasic/Test/Syntax/TestSyntaxNodes.vb b/src/Compilers/VisualBasic/Test/Syntax/TestSyntaxNodes.vb
index 397672b35ad93..585ebc41b336a 100644
--- a/src/Compilers/VisualBasic/Test/Syntax/TestSyntaxNodes.vb
+++ b/src/Compilers/VisualBasic/Test/Syntax/TestSyntaxNodes.vb
@@ -1105,6 +1105,10 @@ End Class
Public Overrides Function GetErrorDisplayString(symbol As ISymbol) As String
Return MessageProvider.Instance.GetErrorDisplayString(symbol)
End Function
+
+ Public Overrides Function GetIsEnabledByDefault(code As Integer) As Boolean
+ Return True
+ End Function
End Class
' A test rewriting visitor
diff --git a/src/EditorFeatures/CSharpTest/ConvertTupleToStruct/ConvertTupleToStructTests.cs b/src/EditorFeatures/CSharpTest/ConvertTupleToStruct/ConvertTupleToStructTests.cs
index b00df5aa1aadd..9ad2925b2719e 100644
--- a/src/EditorFeatures/CSharpTest/ConvertTupleToStruct/ConvertTupleToStructTests.cs
+++ b/src/EditorFeatures/CSharpTest/ConvertTupleToStruct/ConvertTupleToStructTests.cs
@@ -2321,10 +2321,10 @@ public static implicit operator NewStruct((int a, int a) value)
DiagnosticResult.CompilerError("CS7036").WithSpan(6, 22, 6, 31).WithArguments("a", "NewStruct.NewStruct(int, int)"),
// /0/Test0.cs(13,16): error CS0102: The type 'NewStruct' already contains a definition for 'a'
DiagnosticResult.CompilerError("CS0102").WithSpan(13, 16, 13, 17).WithArguments("NewStruct", "a"),
- // /0/Test0.cs(15,12): error CS0171: Field 'NewStruct.a' must be fully assigned before control is returned to the caller
- DiagnosticResult.CompilerError("CS0171").WithSpan(15, 12, 15, 21).WithArguments("NewStruct.a"),
- // /0/Test0.cs(15,12): error CS0171: Field 'NewStruct.a' must be fully assigned before control is returned to the caller
- DiagnosticResult.CompilerError("CS0171").WithSpan(15, 12, 15, 21).WithArguments("NewStruct.a"),
+ // /0/Test0.cs(15,12): error CS0171: Field 'NewStruct.a' must be fully assigned before control is returned to the caller. Consider updating to language version 'preview' to auto-default the field.
+ DiagnosticResult.CompilerError("CS0171").WithSpan(15, 12, 15, 21).WithArguments("NewStruct.a", "preview"),
+ // /0/Test0.cs(15,12): error CS0171: Field 'NewStruct.a' must be fully assigned before control is returned to the caller. Consider updating to language version 'preview' to auto-default the field.
+ DiagnosticResult.CompilerError("CS0171").WithSpan(15, 12, 15, 21).WithArguments("NewStruct.a", "preview"),
// /0/Test0.cs(15,33): error CS0100: The parameter name 'a' is a duplicate
DiagnosticResult.CompilerError("CS0100").WithSpan(15, 33, 15, 34).WithArguments("a"),
// /0/Test0.cs(17,14): error CS0229: Ambiguity between 'NewStruct.a' and 'NewStruct.a'