未验证 提交 1fd21f84 编写于 作者: C Charles Stoner 提交者: GitHub

Make Inheritance_* tests contiguous (#45233)

上级 64b18bef
......@@ -3689,369 +3689,797 @@ .maxstack 3
}");
}
[Theory, WorkItem(44902, "https://github.com/dotnet/roslyn/issues/44902")]
[InlineData(false)]
[InlineData(true)]
public void CopyCtor(bool useCompilationReference)
[Fact]
public void Inheritance_22()
{
var sourceA =
@"public record B(object N1, object N2)
var source =
@"record A
{
}";
var compA = CreateCompilation(sourceA);
var verifierA = CompileAndVerify(compA, verify: ExecutionConditionUtil.IsCoreClr ? Verification.Skipped : Verification.Fails);
verifierA.VerifyIL("B..ctor(B)", @"
public ref object P1 => throw null;
public object P2 => throw null;
}
record B : A
{
// Code size 31 (0x1f)
.maxstack 2
IL_0000: ldarg.0
IL_0001: call ""object..ctor()""
IL_0006: ldarg.0
IL_0007: ldarg.1
IL_0008: ldfld ""object B.<N1>k__BackingField""
IL_000d: stfld ""object B.<N1>k__BackingField""
IL_0012: ldarg.0
IL_0013: ldarg.1
IL_0014: ldfld ""object B.<N2>k__BackingField""
IL_0019: stfld ""object B.<N2>k__BackingField""
IL_001e: ret
}");
var refA = useCompilationReference ? compA.ToMetadataReference() : compA.EmitToImageReference();
var sourceB =
@"record C(object P1, object P2) : B(3, 4)
public new object P1 => throw null;
public new ref object P2 => throw null;
}
record C(object P1, object P2) : B
{
static void Main()
{
var c1 = new C(1, 2);
System.Console.Write((c1.P1, c1.P2, c1.N1, c1.N2));
System.Console.Write("" "");
var c2 = new C(c1);
System.Console.Write((c2.P1, c2.P2, c2.N1, c2.N2));
System.Console.Write("" "");
var c3 = c1 with { P1 = 10, N1 = 30 };
System.Console.Write((c3.P1, c3.P2, c3.N1, c3.N2));
}
}";
var compB = CreateCompilation(sourceB, references: new[] { refA }, parseOptions: TestOptions.RegularPreview, options: TestOptions.ReleaseExe);
compB.VerifyDiagnostics();
var verifierB = CompileAndVerify(compB, expectedOutput: "(1, 2, 3, 4) (1, 2, 3, 4) (10, 2, 30, 4)", verify: ExecutionConditionUtil.IsCoreClr ? Verification.Skipped : Verification.Fails);
// call base copy constructor B..ctor(B)
verifierB.VerifyIL("C..ctor(C)", @"
{
// Code size 32 (0x20)
.maxstack 2
IL_0000: ldarg.0
IL_0001: ldarg.1
IL_0002: call ""B..ctor(B)""
IL_0007: ldarg.0
IL_0008: ldarg.1
IL_0009: ldfld ""object C.<P1>k__BackingField""
IL_000e: stfld ""object C.<P1>k__BackingField""
IL_0013: ldarg.0
IL_0014: ldarg.1
IL_0015: ldfld ""object C.<P2>k__BackingField""
IL_001a: stfld ""object C.<P2>k__BackingField""
IL_001f: ret
}");
var comp = CreateCompilation(source);
comp.VerifyDiagnostics();
var actualMembers = GetProperties(comp, "C").ToTestDisplayStrings();
AssertEx.Equal(new[] { "System.Type C.EqualityContract { get; }" }, actualMembers);
}
[Fact, WorkItem(44902, "https://github.com/dotnet/roslyn/issues/44902")]
public void CopyCtor_WithOtherOverload()
[Fact]
public void Inheritance_23()
{
var source =
@"public record B(object N1, object N2)
@"record A
{
public B(C c) : this(30, 40) => throw null;
public static object P1 { get; }
public object P2 { get; }
}
public record C(object P1, object P2) : B(3, 4)
record B : A
{
static void Main()
{
var c1 = new C(1, 2);
System.Console.Write((c1.P1, c1.P2, c1.N1, c1.N2));
System.Console.Write("" "");
var c2 = c1 with { P1 = 10, P2 = 20, N1 = 30, N2 = 40 };
System.Console.Write((c2.P1, c2.P2, c2.N1, c2.N2));
}
}";
var comp = CreateCompilation(new[] { source, IsExternalInitTypeDefinition }, parseOptions: TestOptions.RegularPreview, options: TestOptions.ReleaseExe);
comp.VerifyDiagnostics();
var verifier = CompileAndVerify(comp, expectedOutput: "(1, 2, 3, 4) (10, 20, 30, 40)", verify: ExecutionConditionUtil.IsCoreClr ? Verification.Skipped : Verification.Fails);
// call base copy constructor B..ctor(B)
verifier.VerifyIL("C..ctor(C)", @"
public new object P1 { get; }
public new static object P2 { get; }
}
record C(object P1, object P2) : B
{
// Code size 32 (0x20)
.maxstack 2
IL_0000: ldarg.0
IL_0001: ldarg.1
IL_0002: call ""B..ctor(B)""
IL_0007: ldarg.0
IL_0008: ldarg.1
IL_0009: ldfld ""object C.<P1>k__BackingField""
IL_000e: stfld ""object C.<P1>k__BackingField""
IL_0013: ldarg.0
IL_0014: ldarg.1
IL_0015: ldfld ""object C.<P2>k__BackingField""
IL_001a: stfld ""object C.<P2>k__BackingField""
IL_001f: ret
}");
}";
var comp = CreateCompilation(source);
comp.VerifyDiagnostics(
// (11,28): error CS8866: Record member 'B.P2' must be a readable instance property of type 'object' to match positional parameter 'P2'.
// record C(object P1, object P2) : B
Diagnostic(ErrorCode.ERR_BadRecordMemberForPositionalParameter, "P2").WithArguments("B.P2", "object", "P2").WithLocation(11, 28));
var actualMembers = GetProperties(comp, "C").ToTestDisplayStrings();
AssertEx.Equal(new[] { "System.Type C.EqualityContract { get; }" }, actualMembers);
}
[Fact, WorkItem(44902, "https://github.com/dotnet/roslyn/issues/44902")]
public void CopyCtor_WithObsoleteCopyConstructor()
[Fact]
public void Inheritance_24()
{
var source =
@"public record B(object N1, object N2)
@"record A
{
[System.Obsolete(""Obsolete"", true)]
public B(B b) { }
public object get_P() => null;
public object set_Q() => null;
}
public record C(object P1, object P2) : B(3, 4) { }
";
var comp = CreateCompilation(new[] { source, IsExternalInitTypeDefinition }, parseOptions: TestOptions.RegularPreview);
comp.VerifyDiagnostics();
}
[Fact, WorkItem(44902, "https://github.com/dotnet/roslyn/issues/44902")]
public void CopyCtor_WithParamsCopyConstructor()
{
var source =
@"public record B(object N1, object N2)
record B(object P, object Q) : A
{
public B(B b, params int[] i) : this(30, 40) { }
}
public record C(object P1, object P2) : B(3, 4) { }
";
record C(object P)
{
public object get_P() => null;
public object set_Q() => null;
}";
var comp = CreateCompilation(source);
comp.VerifyDiagnostics();
comp.VerifyDiagnostics(
// (9,17): error CS0082: Type 'C' already reserves a member called 'get_P' with the same parameter types
// record C(object P)
Diagnostic(ErrorCode.ERR_MemberReserved, "P").WithArguments("get_P", "C").WithLocation(9, 17));
var actualMembers = comp.GetMember<NamedTypeSymbol>("B").GetMembers().Where(m => m.Name == ".ctor").ToTestDisplayStrings();
var expectedMembers = new[]
{
"B..ctor(System.Object N1, System.Object N2)",
"B..ctor(B b, params System.Int32[] i)",
"B..ctor(B )"
"A B.<>Clone()",
"System.Type B.EqualityContract.get",
"System.Type B.EqualityContract { get; }",
"B..ctor(System.Object P, System.Object Q)",
"System.Object B.<P>k__BackingField",
"System.Object B.P.get",
"void modreq(System.Runtime.CompilerServices.IsExternalInit) B.P.init",
"System.Object B.P { get; init; }",
"System.Object B.<Q>k__BackingField",
"System.Object B.Q.get",
"void modreq(System.Runtime.CompilerServices.IsExternalInit) B.Q.init",
"System.Object B.Q { get; init; }",
"System.Int32 B.GetHashCode()",
"System.Boolean B.Equals(System.Object? )",
"System.Boolean B.Equals(A? )",
"System.Boolean B.Equals(B? )",
"B..ctor(B )",
"void B.Deconstruct(out System.Object P, out System.Object Q)"
};
AssertEx.Equal(expectedMembers, actualMembers);
AssertEx.Equal(expectedMembers, comp.GetMember<NamedTypeSymbol>("B").GetMembers().ToTestDisplayStrings());
var verifier = CompileAndVerify(comp, verify: ExecutionConditionUtil.IsCoreClr ? Verification.Skipped : Verification.Fails);
verifier.VerifyIL("C..ctor(C)", @"
{
// Code size 32 (0x20)
.maxstack 2
IL_0000: ldarg.0
IL_0001: ldarg.1
IL_0002: call ""B..ctor(B)""
IL_0007: ldarg.0
IL_0008: ldarg.1
IL_0009: ldfld ""object C.<P1>k__BackingField""
IL_000e: stfld ""object C.<P1>k__BackingField""
IL_0013: ldarg.0
IL_0014: ldarg.1
IL_0015: ldfld ""object C.<P2>k__BackingField""
IL_001a: stfld ""object C.<P2>k__BackingField""
IL_001f: ret
}
");
expectedMembers = new[]
{
"C C.<>Clone()",
"System.Type C.EqualityContract.get",
"System.Type C.EqualityContract { get; }",
"C..ctor(System.Object P)",
"System.Object C.<P>k__BackingField",
"System.Object C.P.get",
"void modreq(System.Runtime.CompilerServices.IsExternalInit) C.P.init",
"System.Object C.P { get; init; }",
"System.Object C.get_P()",
"System.Object C.set_Q()",
"System.Int32 C.GetHashCode()",
"System.Boolean C.Equals(System.Object? )",
"System.Boolean C.Equals(C? )",
"C..ctor(C )",
"void C.Deconstruct(out System.Object P)"
};
AssertEx.Equal(expectedMembers, comp.GetMember<NamedTypeSymbol>("C").GetMembers().ToTestDisplayStrings());
}
[Fact, WorkItem(44902, "https://github.com/dotnet/roslyn/issues/44902")]
public void CopyCtor_WithInitializers()
[Fact]
public void Inheritance_25()
{
var source =
@"public record C(object N1, object N2)
var sourceA =
@"public record A
{
private int field = 42;
public int Property = 43;
public class P1 { }
internal object P2 = 2;
public int P3(object o) => 3;
internal int P4<T>(T t) => 4;
}";
var comp = CreateCompilation(source);
comp.VerifyDiagnostics();
var verifier = CompileAndVerify(comp, verify: ExecutionConditionUtil.IsCoreClr ? Verification.Skipped : Verification.Fails);
verifier.VerifyIL("C..ctor(C)", @"
var sourceB =
@"record B(object P1, object P2, object P3, object P4) : A
{
// Code size 55 (0x37)
.maxstack 2
IL_0000: ldarg.0
IL_0001: call ""object..ctor()""
IL_0006: ldarg.0
IL_0007: ldarg.1
IL_0008: ldfld ""object C.<N1>k__BackingField""
IL_000d: stfld ""object C.<N1>k__BackingField""
IL_0012: ldarg.0
IL_0013: ldarg.1
IL_0014: ldfld ""object C.<N2>k__BackingField""
IL_0019: stfld ""object C.<N2>k__BackingField""
IL_001e: ldarg.0
IL_001f: ldarg.1
IL_0020: ldfld ""int C.field""
IL_0025: stfld ""int C.field""
IL_002a: ldarg.0
IL_002b: ldarg.1
IL_002c: ldfld ""int C.Property""
IL_0031: stfld ""int C.Property""
IL_0036: ret
}");
}";
var comp = CreateCompilation(new[] { sourceA, sourceB });
comp.VerifyDiagnostics(
// (1,17): error CS8866: Record member 'A.P1' must be a readable instance property of type 'object' to match positional parameter 'P1'.
// record B(object P1, object P2, object P3, object P4) : A
Diagnostic(ErrorCode.ERR_BadRecordMemberForPositionalParameter, "P1").WithArguments("A.P1", "object", "P1").WithLocation(1, 17),
// (1,28): error CS8866: Record member 'A.P2' must be a readable instance property of type 'object' to match positional parameter 'P2'.
// record B(object P1, object P2, object P3, object P4) : A
Diagnostic(ErrorCode.ERR_BadRecordMemberForPositionalParameter, "P2").WithArguments("A.P2", "object", "P2").WithLocation(1, 28),
// (1,39): error CS8866: Record member 'A.P3' must be a readable instance property of type 'object' to match positional parameter 'P3'.
// record B(object P1, object P2, object P3, object P4) : A
Diagnostic(ErrorCode.ERR_BadRecordMemberForPositionalParameter, "P3").WithArguments("A.P3", "object", "P3").WithLocation(1, 39));
var actualMembers = GetProperties(comp, "B").ToTestDisplayStrings();
var expectedMembers = new[]
{
"System.Type B.EqualityContract { get; }",
"System.Object B.P4 { get; init; }",
};
AssertEx.Equal(expectedMembers, actualMembers);
comp = CreateCompilation(sourceA);
var refA = comp.EmitToImageReference();
comp = CreateCompilation(sourceB, references: new[] { refA }, parseOptions: TestOptions.RegularPreview);
comp.VerifyDiagnostics(
// (1,17): error CS8866: Record member 'A.P1' must be a readable instance property of type 'object' to match positional parameter 'P1'.
// record B(object P1, object P2, object P3, object P4) : A
Diagnostic(ErrorCode.ERR_BadRecordMemberForPositionalParameter, "P1").WithArguments("A.P1", "object", "P1").WithLocation(1, 17),
// (1,39): error CS8866: Record member 'A.P3' must be a readable instance property of type 'object' to match positional parameter 'P3'.
// record B(object P1, object P2, object P3, object P4) : A
Diagnostic(ErrorCode.ERR_BadRecordMemberForPositionalParameter, "P3").WithArguments("A.P3", "object", "P3").WithLocation(1, 39));
actualMembers = GetProperties(comp, "B").ToTestDisplayStrings();
expectedMembers = new[]
{
"System.Type B.EqualityContract { get; }",
"System.Object B.P2 { get; init; }",
"System.Object B.P4 { get; init; }",
};
AssertEx.Equal(expectedMembers, actualMembers);
}
[Fact, WorkItem(44902, "https://github.com/dotnet/roslyn/issues/44902")]
public void CopyCtor_UserDefinedButDoesNotDelegateToBaseCopyCtor()
[Fact]
public void Inheritance_26()
{
var source =
@"public record B(object N1, object N2)
var sourceA =
@"public record A
{
}
public record C(object P1, object P2) : B(0, 1)
internal const int P = 4;
}";
var sourceB =
@"record B(object P) : A
{
public C(C c) // 1, 2
{
}
}
";
var comp = CreateCompilation(source);
}";
var comp = CreateCompilation(new[] { sourceA, sourceB });
comp.VerifyDiagnostics(
// (6,12): error CS1729: 'B' does not contain a constructor that takes 0 arguments
// public C(C c) // 1, 2
Diagnostic(ErrorCode.ERR_BadCtorArgCount, "C").WithArguments("B", "0").WithLocation(6, 12),
// (6,12): error CS8868: A copy constructor in a record must call a copy constructor of the base, or a parameterless object constructor if the record inherits from object.
// public C(C c) // 1, 2
Diagnostic(ErrorCode.ERR_CopyConstructorMustInvokeBaseCopyConstructor, "C").WithLocation(6, 12)
);
// (1,17): error CS8866: Record member 'A.P' must be a readable instance property of type 'object' to match positional parameter 'P'.
// record B(object P) : A
Diagnostic(ErrorCode.ERR_BadRecordMemberForPositionalParameter, "P").WithArguments("A.P", "object", "P").WithLocation(1, 17));
AssertEx.Equal(new[] { "System.Type B.EqualityContract { get; }" }, GetProperties(comp, "B").ToTestDisplayStrings());
comp = CreateCompilation(sourceA);
var refA = comp.EmitToImageReference();
comp = CreateCompilation(sourceB, references: new[] { refA }, parseOptions: TestOptions.RegularPreview);
comp.VerifyDiagnostics();
AssertEx.Equal(new[] { "System.Type B.EqualityContract { get; }", "System.Object B.P { get; init; }" }, GetProperties(comp, "B").ToTestDisplayStrings());
}
[Fact, WorkItem(44902, "https://github.com/dotnet/roslyn/issues/44902")]
public void CopyCtor_UserDefinedButDoesNotDelegateToBaseCopyCtor_DerivesFromObject()
[Fact]
public void Inheritance_27()
{
var source =
@"public record C(int I)
@"record A
{
public int I { get; set; } = 42;
public C(C c)
{
}
public static void Main()
{
var c = new C(1);
c.I = 2;
var c2 = new C(c);
System.Console.Write((c.I, c2.I));
}
public object P { get; }
public object Q { get; set; }
}
";
var comp = CreateCompilation(new[] { source, IsExternalInitTypeDefinition }, parseOptions: TestOptions.RegularPreview, options: TestOptions.DebugExe);
comp.VerifyDiagnostics();
var verifier = CompileAndVerify(comp, expectedOutput: "(2, 0)");
verifier.VerifyIL("C..ctor(C)", @"
record B(object get_P, object set_Q) : A
{
// Code size 9 (0x9)
.maxstack 1
IL_0000: ldarg.0
IL_0001: call ""object..ctor()""
IL_0006: nop
IL_0007: nop
IL_0008: ret
}
");
}";
var comp = CreateCompilation(source);
comp.VerifyDiagnostics();
var actualMembers = GetProperties(comp, "B").ToTestDisplayStrings();
var expectedMembers = new[]
{
"System.Type B.EqualityContract { get; }",
"System.Object B.get_P { get; init; }",
"System.Object B.set_Q { get; init; }",
};
AssertEx.Equal(expectedMembers, actualMembers);
}
[Fact, WorkItem(44902, "https://github.com/dotnet/roslyn/issues/44902")]
public void CopyCtor_UserDefinedButDoesNotDelegateToBaseCopyCtor_DerivesFromObject_WithFieldInitializer()
[Fact]
public void Inheritance_28()
{
var source =
@"public record C(int I)
@"interface I
{
public int I { get; set; } = 42;
public int field = 43;
public C(C c)
{
System.Console.Write("" RAN "");
}
public static void Main()
{
var c = new C(1);
c.I = 2;
c.field = 100;
System.Console.Write((c.I, c.field));
var c2 = new C(c);
System.Console.Write((c2.I, c2.field));
}
object P { get; }
}
";
var comp = CreateCompilation(new[] { source, IsExternalInitTypeDefinition }, parseOptions: TestOptions.RegularPreview, options: TestOptions.DebugExe);
comp.VerifyDiagnostics();
var verifier = CompileAndVerify(comp, expectedOutput: "(2, 100) RAN (0, 0)");
verifier.VerifyIL("C..ctor(C)", @"
record A : I
{
// Code size 20 (0x14)
.maxstack 1
IL_0000: ldarg.0
IL_0001: call ""object..ctor()""
IL_0006: nop
IL_0007: nop
IL_0008: ldstr "" RAN ""
IL_000d: call ""void System.Console.Write(string)""
IL_0012: nop
IL_0013: ret
object I.P => null;
}
");
}
[Fact, WorkItem(44902, "https://github.com/dotnet/roslyn/issues/44902")]
public void CopyCtor_DerivesFromObject_GivesParameterToBase()
{
var source = @"
public record C(object I)
record B(object P) : A
{
public C(C c) : base(1) { }
}
";
record C(object P) : I
{
object I.P => null;
}";
var comp = CreateCompilation(source);
comp.VerifyDiagnostics(
// (4,21): error CS1729: 'object' does not contain a constructor that takes 1 arguments
// public C(C c) : base(1) { }
Diagnostic(ErrorCode.ERR_BadCtorArgCount, "base").WithArguments("object", "1").WithLocation(4, 21),
// (4,21): error CS8868: A copy constructor in a record must call a copy constructor of the base, or a parameterless object constructor if the record inherits from object.
// public C(C c) : base(1) { }
Diagnostic(ErrorCode.ERR_CopyConstructorMustInvokeBaseCopyConstructor, "base").WithLocation(4, 21)
);
comp.VerifyDiagnostics();
AssertEx.Equal(new[] { "System.Type B.EqualityContract { get; }", "System.Object B.P { get; init; }" }, GetProperties(comp, "B").ToTestDisplayStrings());
AssertEx.Equal(new[] { "System.Type C.EqualityContract { get; }", "System.Object C.P { get; init; }", "System.Object C.I.P { get; }" }, GetProperties(comp, "C").ToTestDisplayStrings());
}
[Fact, WorkItem(44902, "https://github.com/dotnet/roslyn/issues/44902")]
public void CopyCtor_DerivesFromObject_WithSomeOtherConstructor()
{
var source = @"
public record C(object I)
{
public C(int i) : this((object)null) { }
public static void Main()
[Fact]
public void Inheritance_29()
{
var c = new C((object)null);
var c2 = new C(1);
var c3 = new C(c);
System.Console.Write(""RAN"");
}
}
var sourceA =
@"Public Class A
Public Property P(o As Object) As Object
Get
Return Nothing
End Get
Set
End Set
End Property
Public Property Q(x As Object, y As Object) As Object
Get
Return Nothing
End Get
Set
End Set
End Property
End Class
";
var comp = CreateCompilation(new[] { source, IsExternalInitTypeDefinition }, parseOptions: TestOptions.RegularPreview, options: TestOptions.DebugExe);
comp.VerifyDiagnostics();
var verifier = CompileAndVerify(comp, expectedOutput: "RAN", verify: ExecutionConditionUtil.IsCoreClr ? Verification.Skipped : Verification.Fails);
verifier.VerifyIL("C..ctor(int)", @"
var compA = CreateVisualBasicCompilation(sourceA);
compA.VerifyDiagnostics();
var refA = compA.EmitToImageReference();
var sourceB =
@"record B(object P, object Q) : A
{
// Code size 10 (0xa)
.maxstack 2
IL_0000: ldarg.0
IL_0001: ldnull
IL_0002: call ""C..ctor(object)""
IL_0007: nop
object P { get; }
}";
var compB = CreateCompilation(new[] { sourceB, IsExternalInitTypeDefinition }, references: new[] { refA }, parseOptions: TestOptions.RegularPreview);
compB.VerifyDiagnostics(
// (1,8): error CS8867: No accessible copy constructor found in base type 'A'.
// record B(object P, object Q) : A
Diagnostic(ErrorCode.ERR_NoCopyConstructorInBaseType, "B").WithArguments("A").WithLocation(1, 8),
// (1,32): error CS8864: Records may only inherit from object or another record
// record B(object P, object Q) : A
Diagnostic(ErrorCode.ERR_BadRecordBase, "A").WithLocation(1, 32)
);
var actualMembers = GetProperties(compB, "B").ToTestDisplayStrings();
var expectedMembers = new[]
{
"System.Type B.EqualityContract { get; }",
"System.Object B.Q { get; init; }",
"System.Object B.P { get; }",
};
AssertEx.Equal(expectedMembers, actualMembers);
}
[Fact]
public void Inheritance_30()
{
var sourceA =
@"Public Class A
Public ReadOnly Overloads Property P() As Object
Get
Return Nothing
End Get
End Property
Public ReadOnly Overloads Property P(o As Object) As Object
Get
Return Nothing
End Get
End Property
Public Overloads Property Q(o As Object) As Object
Get
Return Nothing
End Get
Set
End Set
End Property
Public Overloads Property Q() As Object
Get
Return Nothing
End Get
Set
End Set
End Property
End Class
";
var compA = CreateVisualBasicCompilation(sourceA);
compA.VerifyDiagnostics();
var refA = compA.EmitToImageReference();
var sourceB =
@"record B(object P, object Q) : A
{
}";
var compB = CreateCompilation(new[] { sourceB, IsExternalInitTypeDefinition }, references: new[] { refA }, parseOptions: TestOptions.RegularPreview);
compB.VerifyDiagnostics(
// (1,8): error CS8867: No accessible copy constructor found in base type 'A'.
// record B(object P, object Q) : A
Diagnostic(ErrorCode.ERR_NoCopyConstructorInBaseType, "B").WithArguments("A").WithLocation(1, 8),
// (1,32): error CS8864: Records may only inherit from object or another record
// record B(object P, object Q) : A
Diagnostic(ErrorCode.ERR_BadRecordBase, "A").WithLocation(1, 32)
);
var actualMembers = GetProperties(compB, "B").ToTestDisplayStrings();
AssertEx.Equal(new[] { "System.Type B.EqualityContract { get; }" }, actualMembers);
}
[Fact]
public void Inheritance_31()
{
var sourceA =
@"Public Class A
Public ReadOnly Property P() As Object
Get
Return Nothing
End Get
End Property
Public Property Q(o As Object) As Object
Get
Return Nothing
End Get
Set
End Set
End Property
Public Property R(o As Object) As Object
Get
Return Nothing
End Get
Set
End Set
End Property
Public Sub New(a as A)
End Sub
End Class
Public Class B
Inherits A
Public ReadOnly Overloads Property P(o As Object) As Object
Get
Return Nothing
End Get
End Property
Public Overloads Property Q() As Object
Get
Return Nothing
End Get
Set
End Set
End Property
Public Overloads Property R(x As Object, y As Object) As Object
Get
Return Nothing
End Get
Set
End Set
End Property
Public Sub New(b as B)
MyBase.New(b)
End Sub
End Class
";
var compA = CreateVisualBasicCompilation(sourceA);
compA.VerifyDiagnostics();
var refA = compA.EmitToImageReference();
var sourceB =
@"record C(object P, object Q, object R) : B
{
}";
var compB = CreateCompilation(new[] { sourceB, IsExternalInitTypeDefinition }, references: new[] { refA }, parseOptions: TestOptions.RegularPreview);
compB.VerifyDiagnostics(
// (1,9): error CS7036: There is no argument given that corresponds to the required formal parameter 'b' of 'B.B(B)'
// record C(object P, object Q, object R) : B
Diagnostic(ErrorCode.ERR_NoCorrespondingArgument, "(object P, object Q, object R)").WithArguments("b", "B.B(B)").WithLocation(1, 9),
// (1,42): error CS8864: Records may only inherit from object or another record
// record C(object P, object Q, object R) : B
Diagnostic(ErrorCode.ERR_BadRecordBase, "B").WithLocation(1, 42)
);
var actualMembers = GetProperties(compB, "C").ToTestDisplayStrings();
var expectedMembers = new[]
{
"System.Type C.EqualityContract { get; }",
"System.Object C.R { get; init; }",
};
AssertEx.Equal(expectedMembers, actualMembers);
}
[Theory, WorkItem(44902, "https://github.com/dotnet/roslyn/issues/44902")]
[InlineData(false)]
[InlineData(true)]
public void CopyCtor(bool useCompilationReference)
{
var sourceA =
@"public record B(object N1, object N2)
{
}";
var compA = CreateCompilation(sourceA);
var verifierA = CompileAndVerify(compA, verify: ExecutionConditionUtil.IsCoreClr ? Verification.Skipped : Verification.Fails);
verifierA.VerifyIL("B..ctor(B)", @"
{
// Code size 31 (0x1f)
.maxstack 2
IL_0000: ldarg.0
IL_0001: call ""object..ctor()""
IL_0006: ldarg.0
IL_0007: ldarg.1
IL_0008: ldfld ""object B.<N1>k__BackingField""
IL_000d: stfld ""object B.<N1>k__BackingField""
IL_0012: ldarg.0
IL_0013: ldarg.1
IL_0014: ldfld ""object B.<N2>k__BackingField""
IL_0019: stfld ""object B.<N2>k__BackingField""
IL_001e: ret
}");
var refA = useCompilationReference ? compA.ToMetadataReference() : compA.EmitToImageReference();
var sourceB =
@"record C(object P1, object P2) : B(3, 4)
{
static void Main()
{
var c1 = new C(1, 2);
System.Console.Write((c1.P1, c1.P2, c1.N1, c1.N2));
System.Console.Write("" "");
var c2 = new C(c1);
System.Console.Write((c2.P1, c2.P2, c2.N1, c2.N2));
System.Console.Write("" "");
var c3 = c1 with { P1 = 10, N1 = 30 };
System.Console.Write((c3.P1, c3.P2, c3.N1, c3.N2));
}
}";
var compB = CreateCompilation(sourceB, references: new[] { refA }, parseOptions: TestOptions.RegularPreview, options: TestOptions.ReleaseExe);
compB.VerifyDiagnostics();
var verifierB = CompileAndVerify(compB, expectedOutput: "(1, 2, 3, 4) (1, 2, 3, 4) (10, 2, 30, 4)", verify: ExecutionConditionUtil.IsCoreClr ? Verification.Skipped : Verification.Fails);
// call base copy constructor B..ctor(B)
verifierB.VerifyIL("C..ctor(C)", @"
{
// Code size 32 (0x20)
.maxstack 2
IL_0000: ldarg.0
IL_0001: ldarg.1
IL_0002: call ""B..ctor(B)""
IL_0007: ldarg.0
IL_0008: ldarg.1
IL_0009: ldfld ""object C.<P1>k__BackingField""
IL_000e: stfld ""object C.<P1>k__BackingField""
IL_0013: ldarg.0
IL_0014: ldarg.1
IL_0015: ldfld ""object C.<P2>k__BackingField""
IL_001a: stfld ""object C.<P2>k__BackingField""
IL_001f: ret
}");
}
[Fact, WorkItem(44902, "https://github.com/dotnet/roslyn/issues/44902")]
public void CopyCtor_WithOtherOverload()
{
var source =
@"public record B(object N1, object N2)
{
public B(C c) : this(30, 40) => throw null;
}
public record C(object P1, object P2) : B(3, 4)
{
static void Main()
{
var c1 = new C(1, 2);
System.Console.Write((c1.P1, c1.P2, c1.N1, c1.N2));
System.Console.Write("" "");
var c2 = c1 with { P1 = 10, P2 = 20, N1 = 30, N2 = 40 };
System.Console.Write((c2.P1, c2.P2, c2.N1, c2.N2));
}
}";
var comp = CreateCompilation(new[] { source, IsExternalInitTypeDefinition }, parseOptions: TestOptions.RegularPreview, options: TestOptions.ReleaseExe);
comp.VerifyDiagnostics();
var verifier = CompileAndVerify(comp, expectedOutput: "(1, 2, 3, 4) (10, 20, 30, 40)", verify: ExecutionConditionUtil.IsCoreClr ? Verification.Skipped : Verification.Fails);
// call base copy constructor B..ctor(B)
verifier.VerifyIL("C..ctor(C)", @"
{
// Code size 32 (0x20)
.maxstack 2
IL_0000: ldarg.0
IL_0001: ldarg.1
IL_0002: call ""B..ctor(B)""
IL_0007: ldarg.0
IL_0008: ldarg.1
IL_0009: ldfld ""object C.<P1>k__BackingField""
IL_000e: stfld ""object C.<P1>k__BackingField""
IL_0013: ldarg.0
IL_0014: ldarg.1
IL_0015: ldfld ""object C.<P2>k__BackingField""
IL_001a: stfld ""object C.<P2>k__BackingField""
IL_001f: ret
}");
}
[Fact, WorkItem(44902, "https://github.com/dotnet/roslyn/issues/44902")]
public void CopyCtor_WithObsoleteCopyConstructor()
{
var source =
@"public record B(object N1, object N2)
{
[System.Obsolete(""Obsolete"", true)]
public B(B b) { }
}
public record C(object P1, object P2) : B(3, 4) { }
";
var comp = CreateCompilation(new[] { source, IsExternalInitTypeDefinition }, parseOptions: TestOptions.RegularPreview);
comp.VerifyDiagnostics();
}
[Fact, WorkItem(44902, "https://github.com/dotnet/roslyn/issues/44902")]
public void CopyCtor_WithParamsCopyConstructor()
{
var source =
@"public record B(object N1, object N2)
{
public B(B b, params int[] i) : this(30, 40) { }
}
public record C(object P1, object P2) : B(3, 4) { }
";
var comp = CreateCompilation(source);
comp.VerifyDiagnostics();
var actualMembers = comp.GetMember<NamedTypeSymbol>("B").GetMembers().Where(m => m.Name == ".ctor").ToTestDisplayStrings();
var expectedMembers = new[]
{
"B..ctor(System.Object N1, System.Object N2)",
"B..ctor(B b, params System.Int32[] i)",
"B..ctor(B )"
};
AssertEx.Equal(expectedMembers, actualMembers);
var verifier = CompileAndVerify(comp, verify: ExecutionConditionUtil.IsCoreClr ? Verification.Skipped : Verification.Fails);
verifier.VerifyIL("C..ctor(C)", @"
{
// Code size 32 (0x20)
.maxstack 2
IL_0000: ldarg.0
IL_0001: ldarg.1
IL_0002: call ""B..ctor(B)""
IL_0007: ldarg.0
IL_0008: ldarg.1
IL_0009: ldfld ""object C.<P1>k__BackingField""
IL_000e: stfld ""object C.<P1>k__BackingField""
IL_0013: ldarg.0
IL_0014: ldarg.1
IL_0015: ldfld ""object C.<P2>k__BackingField""
IL_001a: stfld ""object C.<P2>k__BackingField""
IL_001f: ret
}
");
}
[Fact, WorkItem(44902, "https://github.com/dotnet/roslyn/issues/44902")]
public void CopyCtor_WithInitializers()
{
var source =
@"public record C(object N1, object N2)
{
private int field = 42;
public int Property = 43;
}";
var comp = CreateCompilation(source);
comp.VerifyDiagnostics();
var verifier = CompileAndVerify(comp, verify: ExecutionConditionUtil.IsCoreClr ? Verification.Skipped : Verification.Fails);
verifier.VerifyIL("C..ctor(C)", @"
{
// Code size 55 (0x37)
.maxstack 2
IL_0000: ldarg.0
IL_0001: call ""object..ctor()""
IL_0006: ldarg.0
IL_0007: ldarg.1
IL_0008: ldfld ""object C.<N1>k__BackingField""
IL_000d: stfld ""object C.<N1>k__BackingField""
IL_0012: ldarg.0
IL_0013: ldarg.1
IL_0014: ldfld ""object C.<N2>k__BackingField""
IL_0019: stfld ""object C.<N2>k__BackingField""
IL_001e: ldarg.0
IL_001f: ldarg.1
IL_0020: ldfld ""int C.field""
IL_0025: stfld ""int C.field""
IL_002a: ldarg.0
IL_002b: ldarg.1
IL_002c: ldfld ""int C.Property""
IL_0031: stfld ""int C.Property""
IL_0036: ret
}");
}
[Fact, WorkItem(44902, "https://github.com/dotnet/roslyn/issues/44902")]
public void CopyCtor_UserDefinedButDoesNotDelegateToBaseCopyCtor()
{
var source =
@"public record B(object N1, object N2)
{
}
public record C(object P1, object P2) : B(0, 1)
{
public C(C c) // 1, 2
{
}
}
";
var comp = CreateCompilation(source);
comp.VerifyDiagnostics(
// (6,12): error CS1729: 'B' does not contain a constructor that takes 0 arguments
// public C(C c) // 1, 2
Diagnostic(ErrorCode.ERR_BadCtorArgCount, "C").WithArguments("B", "0").WithLocation(6, 12),
// (6,12): error CS8868: A copy constructor in a record must call a copy constructor of the base, or a parameterless object constructor if the record inherits from object.
// public C(C c) // 1, 2
Diagnostic(ErrorCode.ERR_CopyConstructorMustInvokeBaseCopyConstructor, "C").WithLocation(6, 12)
);
}
[Fact, WorkItem(44902, "https://github.com/dotnet/roslyn/issues/44902")]
public void CopyCtor_UserDefinedButDoesNotDelegateToBaseCopyCtor_DerivesFromObject()
{
var source =
@"public record C(int I)
{
public int I { get; set; } = 42;
public C(C c)
{
}
public static void Main()
{
var c = new C(1);
c.I = 2;
var c2 = new C(c);
System.Console.Write((c.I, c2.I));
}
}
";
var comp = CreateCompilation(new[] { source, IsExternalInitTypeDefinition }, parseOptions: TestOptions.RegularPreview, options: TestOptions.DebugExe);
comp.VerifyDiagnostics();
var verifier = CompileAndVerify(comp, expectedOutput: "(2, 0)");
verifier.VerifyIL("C..ctor(C)", @"
{
// Code size 9 (0x9)
.maxstack 1
IL_0000: ldarg.0
IL_0001: call ""object..ctor()""
IL_0006: nop
IL_0007: nop
IL_0008: ret
}
");
}
[Fact, WorkItem(44902, "https://github.com/dotnet/roslyn/issues/44902")]
public void CopyCtor_UserDefinedButDoesNotDelegateToBaseCopyCtor_DerivesFromObject_WithFieldInitializer()
{
var source =
@"public record C(int I)
{
public int I { get; set; } = 42;
public int field = 43;
public C(C c)
{
System.Console.Write("" RAN "");
}
public static void Main()
{
var c = new C(1);
c.I = 2;
c.field = 100;
System.Console.Write((c.I, c.field));
var c2 = new C(c);
System.Console.Write((c2.I, c2.field));
}
}
";
var comp = CreateCompilation(new[] { source, IsExternalInitTypeDefinition }, parseOptions: TestOptions.RegularPreview, options: TestOptions.DebugExe);
comp.VerifyDiagnostics();
var verifier = CompileAndVerify(comp, expectedOutput: "(2, 100) RAN (0, 0)");
verifier.VerifyIL("C..ctor(C)", @"
{
// Code size 20 (0x14)
.maxstack 1
IL_0000: ldarg.0
IL_0001: call ""object..ctor()""
IL_0006: nop
IL_0007: nop
IL_0008: ldstr "" RAN ""
IL_000d: call ""void System.Console.Write(string)""
IL_0012: nop
IL_0013: ret
}
");
}
[Fact, WorkItem(44902, "https://github.com/dotnet/roslyn/issues/44902")]
public void CopyCtor_DerivesFromObject_GivesParameterToBase()
{
var source = @"
public record C(object I)
{
public C(C c) : base(1) { }
}
";
var comp = CreateCompilation(source);
comp.VerifyDiagnostics(
// (4,21): error CS1729: 'object' does not contain a constructor that takes 1 arguments
// public C(C c) : base(1) { }
Diagnostic(ErrorCode.ERR_BadCtorArgCount, "base").WithArguments("object", "1").WithLocation(4, 21),
// (4,21): error CS8868: A copy constructor in a record must call a copy constructor of the base, or a parameterless object constructor if the record inherits from object.
// public C(C c) : base(1) { }
Diagnostic(ErrorCode.ERR_CopyConstructorMustInvokeBaseCopyConstructor, "base").WithLocation(4, 21)
);
}
[Fact, WorkItem(44902, "https://github.com/dotnet/roslyn/issues/44902")]
public void CopyCtor_DerivesFromObject_WithSomeOtherConstructor()
{
var source = @"
public record C(object I)
{
public C(int i) : this((object)null) { }
public static void Main()
{
var c = new C((object)null);
var c2 = new C(1);
var c3 = new C(c);
System.Console.Write(""RAN"");
}
}
";
var comp = CreateCompilation(new[] { source, IsExternalInitTypeDefinition }, parseOptions: TestOptions.RegularPreview, options: TestOptions.DebugExe);
comp.VerifyDiagnostics();
var verifier = CompileAndVerify(comp, expectedOutput: "RAN", verify: ExecutionConditionUtil.IsCoreClr ? Verification.Skipped : Verification.Fails);
verifier.VerifyIL("C..ctor(int)", @"
{
// Code size 10 (0xa)
.maxstack 2
IL_0000: ldarg.0
IL_0001: ldnull
IL_0002: call ""C..ctor(object)""
IL_0007: nop
IL_0008: nop
IL_0009: ret
}
......@@ -4507,117 +4935,42 @@ public record C(object P1, object P2) : B(3, 4)
private int field2 = 200;
public int GetField2() => field2;
static void Main()
{
var c1 = new C(1, 2);
var c2 = new C(c1);
System.Console.Write((c2.P1, c2.P2, c2.N1, c2.N2, c2.GetField1(), c2.GetField2()));
}
}";
var comp = CreateCompilation(new[] { source, IsExternalInitTypeDefinition }, parseOptions: TestOptions.RegularPreview, options: TestOptions.ReleaseExe);
comp.VerifyDiagnostics();
var verifier = CompileAndVerify(comp, expectedOutput: "(1, 2, 3, 4, 100, 200)", verify: ExecutionConditionUtil.IsCoreClr ? Verification.Skipped : Verification.Fails);
verifier.VerifyIL("C..ctor(C)", @"
{
// Code size 44 (0x2c)
.maxstack 2
IL_0000: ldarg.0
IL_0001: ldarg.1
IL_0002: call ""B..ctor(B)""
IL_0007: ldarg.0
IL_0008: ldarg.1
IL_0009: ldfld ""object C.<P1>k__BackingField""
IL_000e: stfld ""object C.<P1>k__BackingField""
IL_0013: ldarg.0
IL_0014: ldarg.1
IL_0015: ldfld ""object C.<P2>k__BackingField""
IL_001a: stfld ""object C.<P2>k__BackingField""
IL_001f: ldarg.0
IL_0020: ldarg.1
IL_0021: ldfld ""int C.field2""
IL_0026: stfld ""int C.field2""
IL_002b: ret
}");
}
[Fact, WorkItem(44902, "https://github.com/dotnet/roslyn/issues/44902")]
public void CopyCtor_MissingInMetadata()
{
// IL for `public record B { }`
var ilSource = @"
.class public auto ansi beforefieldinit B extends [mscorlib]System.Object
{
.method public hidebysig specialname newslot virtual instance class B '<>Clone' () cil managed
{
IL_0000: ldnull
IL_0001: throw
}
.method family hidebysig newslot virtual instance class [mscorlib]System.Type get_EqualityContract () cil managed
{
IL_0000: ldnull
IL_0001: throw
}
.method public hidebysig virtual instance int32 GetHashCode () cil managed
{
IL_0000: ldnull
IL_0001: throw
}
.method public hidebysig virtual instance bool Equals ( object '' ) cil managed
{
IL_0000: ldnull
IL_0001: throw
}
.method public newslot virtual instance bool Equals ( class B '' ) cil managed
{
IL_0000: ldnull
IL_0001: throw
}
// Removed copy constructor
//.method public hidebysig specialname rtspecialname instance void .ctor ( class B '' ) cil managed
.method public hidebysig specialname rtspecialname instance void .ctor () cil managed
{
IL_0000: ldnull
IL_0001: throw
}
.property instance class [mscorlib]System.Type EqualityContract()
static void Main()
{
.get instance class [mscorlib]System.Type B::get_EqualityContract()
var c1 = new C(1, 2);
var c2 = new C(c1);
System.Console.Write((c2.P1, c2.P2, c2.N1, c2.N2, c2.GetField1(), c2.GetField2()));
}
}
";
var source = @"
public record C : B {
}";
var comp = CreateCompilationWithIL(new[] { source, IsExternalInitTypeDefinition }, ilSource: ilSource, parseOptions: TestOptions.RegularPreview);
comp.VerifyDiagnostics(
// (2,15): error CS8867: No accessible copy constructor found in base type 'B'.
// public record C : B {
Diagnostic(ErrorCode.ERR_NoCopyConstructorInBaseType, "C").WithArguments("B").WithLocation(2, 15)
);
var comp = CreateCompilation(new[] { source, IsExternalInitTypeDefinition }, parseOptions: TestOptions.RegularPreview, options: TestOptions.ReleaseExe);
comp.VerifyDiagnostics();
var source2 = @"
public record C : B
var verifier = CompileAndVerify(comp, expectedOutput: "(1, 2, 3, 4, 100, 200)", verify: ExecutionConditionUtil.IsCoreClr ? Verification.Skipped : Verification.Fails);
verifier.VerifyIL("C..ctor(C)", @"
{
public C(C c) { }
}";
var comp2 = CreateCompilationWithIL(new[] { source2, IsExternalInitTypeDefinition }, ilSource: ilSource, parseOptions: TestOptions.RegularPreview);
comp2.VerifyDiagnostics(
// (4,12): error CS8868: A copy constructor in a record must call a copy constructor of the base, or a parameterless object constructor if the record inherits from object.
// public C(C c) { }
Diagnostic(ErrorCode.ERR_CopyConstructorMustInvokeBaseCopyConstructor, "C").WithLocation(4, 12)
);
// Code size 44 (0x2c)
.maxstack 2
IL_0000: ldarg.0
IL_0001: ldarg.1
IL_0002: call ""B..ctor(B)""
IL_0007: ldarg.0
IL_0008: ldarg.1
IL_0009: ldfld ""object C.<P1>k__BackingField""
IL_000e: stfld ""object C.<P1>k__BackingField""
IL_0013: ldarg.0
IL_0014: ldarg.1
IL_0015: ldfld ""object C.<P2>k__BackingField""
IL_001a: stfld ""object C.<P2>k__BackingField""
IL_001f: ldarg.0
IL_0020: ldarg.1
IL_0021: ldfld ""int C.field2""
IL_0026: stfld ""int C.field2""
IL_002b: ret
}");
}
[Fact, WorkItem(44902, "https://github.com/dotnet/roslyn/issues/44902")]
public void CopyCtor_InaccessibleInMetadata()
public void CopyCtor_MissingInMetadata()
{
// IL for `public record B { }`
var ilSource = @"
......@@ -4653,12 +5006,8 @@ public void CopyCtor_InaccessibleInMetadata()
IL_0001: throw
}
// Inaccessible copy constructor
.method private hidebysig specialname rtspecialname instance void .ctor ( class B '' ) cil managed
{
IL_0000: ldnull
IL_0001: throw
}
// Removed copy constructor
//.method public hidebysig specialname rtspecialname instance void .ctor ( class B '' ) cil managed
.method public hidebysig specialname rtspecialname instance void .ctor () cil managed
{
......@@ -4681,434 +5030,85 @@ public void CopyCtor_InaccessibleInMetadata()
// public record C : B {
Diagnostic(ErrorCode.ERR_NoCopyConstructorInBaseType, "C").WithArguments("B").WithLocation(2, 15)
);
}
[Fact]
public void Inheritance_22()
{
var source =
@"record A
{
public ref object P1 => throw null;
public object P2 => throw null;
}
record B : A
{
public new object P1 => throw null;
public new ref object P2 => throw null;
}
record C(object P1, object P2) : B
{
}";
var comp = CreateCompilation(source);
comp.VerifyDiagnostics();
var actualMembers = GetProperties(comp, "C").ToTestDisplayStrings();
AssertEx.Equal(new[] { "System.Type C.EqualityContract { get; }" }, actualMembers);
}
[Fact]
public void Inheritance_23()
{
var source =
@"record A
{
public static object P1 { get; }
public object P2 { get; }
}
record B : A
{
public new object P1 { get; }
public new static object P2 { get; }
}
record C(object P1, object P2) : B
{
}";
var comp = CreateCompilation(source);
comp.VerifyDiagnostics(
// (11,28): error CS8866: Record member 'B.P2' must be a readable instance property of type 'object' to match positional parameter 'P2'.
// record C(object P1, object P2) : B
Diagnostic(ErrorCode.ERR_BadRecordMemberForPositionalParameter, "P2").WithArguments("B.P2", "object", "P2").WithLocation(11, 28));
var actualMembers = GetProperties(comp, "C").ToTestDisplayStrings();
AssertEx.Equal(new[] { "System.Type C.EqualityContract { get; }" }, actualMembers);
}
[Fact]
public void Inheritance_24()
{
var source =
@"record A
{
public object get_P() => null;
public object set_Q() => null;
}
record B(object P, object Q) : A
{
}
record C(object P)
{
public object get_P() => null;
public object set_Q() => null;
}";
var comp = CreateCompilation(source);
comp.VerifyDiagnostics(
// (9,17): error CS0082: Type 'C' already reserves a member called 'get_P' with the same parameter types
// record C(object P)
Diagnostic(ErrorCode.ERR_MemberReserved, "P").WithArguments("get_P", "C").WithLocation(9, 17));
var expectedMembers = new[]
{
"A B.<>Clone()",
"System.Type B.EqualityContract.get",
"System.Type B.EqualityContract { get; }",
"B..ctor(System.Object P, System.Object Q)",
"System.Object B.<P>k__BackingField",
"System.Object B.P.get",
"void modreq(System.Runtime.CompilerServices.IsExternalInit) B.P.init",
"System.Object B.P { get; init; }",
"System.Object B.<Q>k__BackingField",
"System.Object B.Q.get",
"void modreq(System.Runtime.CompilerServices.IsExternalInit) B.Q.init",
"System.Object B.Q { get; init; }",
"System.Int32 B.GetHashCode()",
"System.Boolean B.Equals(System.Object? )",
"System.Boolean B.Equals(A? )",
"System.Boolean B.Equals(B? )",
"B..ctor(B )",
"void B.Deconstruct(out System.Object P, out System.Object Q)"
};
AssertEx.Equal(expectedMembers, comp.GetMember<NamedTypeSymbol>("B").GetMembers().ToTestDisplayStrings());
expectedMembers = new[]
{
"C C.<>Clone()",
"System.Type C.EqualityContract.get",
"System.Type C.EqualityContract { get; }",
"C..ctor(System.Object P)",
"System.Object C.<P>k__BackingField",
"System.Object C.P.get",
"void modreq(System.Runtime.CompilerServices.IsExternalInit) C.P.init",
"System.Object C.P { get; init; }",
"System.Object C.get_P()",
"System.Object C.set_Q()",
"System.Int32 C.GetHashCode()",
"System.Boolean C.Equals(System.Object? )",
"System.Boolean C.Equals(C? )",
"C..ctor(C )",
"void C.Deconstruct(out System.Object P)"
};
AssertEx.Equal(expectedMembers, comp.GetMember<NamedTypeSymbol>("C").GetMembers().ToTestDisplayStrings());
}
[Fact]
public void Inheritance_25()
{
var sourceA =
@"public record A
{
public class P1 { }
internal object P2 = 2;
public int P3(object o) => 3;
internal int P4<T>(T t) => 4;
}";
var sourceB =
@"record B(object P1, object P2, object P3, object P4) : A
{
}";
var comp = CreateCompilation(new[] { sourceA, sourceB });
comp.VerifyDiagnostics(
// (1,17): error CS8866: Record member 'A.P1' must be a readable instance property of type 'object' to match positional parameter 'P1'.
// record B(object P1, object P2, object P3, object P4) : A
Diagnostic(ErrorCode.ERR_BadRecordMemberForPositionalParameter, "P1").WithArguments("A.P1", "object", "P1").WithLocation(1, 17),
// (1,28): error CS8866: Record member 'A.P2' must be a readable instance property of type 'object' to match positional parameter 'P2'.
// record B(object P1, object P2, object P3, object P4) : A
Diagnostic(ErrorCode.ERR_BadRecordMemberForPositionalParameter, "P2").WithArguments("A.P2", "object", "P2").WithLocation(1, 28),
// (1,39): error CS8866: Record member 'A.P3' must be a readable instance property of type 'object' to match positional parameter 'P3'.
// record B(object P1, object P2, object P3, object P4) : A
Diagnostic(ErrorCode.ERR_BadRecordMemberForPositionalParameter, "P3").WithArguments("A.P3", "object", "P3").WithLocation(1, 39));
var actualMembers = GetProperties(comp, "B").ToTestDisplayStrings();
var expectedMembers = new[]
{
"System.Type B.EqualityContract { get; }",
"System.Object B.P4 { get; init; }",
};
AssertEx.Equal(expectedMembers, actualMembers);
comp = CreateCompilation(sourceA);
var refA = comp.EmitToImageReference();
comp = CreateCompilation(sourceB, references: new[] { refA }, parseOptions: TestOptions.RegularPreview);
comp.VerifyDiagnostics(
// (1,17): error CS8866: Record member 'A.P1' must be a readable instance property of type 'object' to match positional parameter 'P1'.
// record B(object P1, object P2, object P3, object P4) : A
Diagnostic(ErrorCode.ERR_BadRecordMemberForPositionalParameter, "P1").WithArguments("A.P1", "object", "P1").WithLocation(1, 17),
// (1,39): error CS8866: Record member 'A.P3' must be a readable instance property of type 'object' to match positional parameter 'P3'.
// record B(object P1, object P2, object P3, object P4) : A
Diagnostic(ErrorCode.ERR_BadRecordMemberForPositionalParameter, "P3").WithArguments("A.P3", "object", "P3").WithLocation(1, 39));
actualMembers = GetProperties(comp, "B").ToTestDisplayStrings();
expectedMembers = new[]
{
"System.Type B.EqualityContract { get; }",
"System.Object B.P2 { get; init; }",
"System.Object B.P4 { get; init; }",
};
AssertEx.Equal(expectedMembers, actualMembers);
}
[Fact]
public void Inheritance_26()
{
var sourceA =
@"public record A
{
internal const int P = 4;
}";
var sourceB =
@"record B(object P) : A
var source2 = @"
public record C : B
{
public C(C c) { }
}";
var comp = CreateCompilation(new[] { sourceA, sourceB });
comp.VerifyDiagnostics(
// (1,17): error CS8866: Record member 'A.P' must be a readable instance property of type 'object' to match positional parameter 'P'.
// record B(object P) : A
Diagnostic(ErrorCode.ERR_BadRecordMemberForPositionalParameter, "P").WithArguments("A.P", "object", "P").WithLocation(1, 17));
AssertEx.Equal(new[] { "System.Type B.EqualityContract { get; }" }, GetProperties(comp, "B").ToTestDisplayStrings());
comp = CreateCompilation(sourceA);
var refA = comp.EmitToImageReference();
comp = CreateCompilation(sourceB, references: new[] { refA }, parseOptions: TestOptions.RegularPreview);
comp.VerifyDiagnostics();
AssertEx.Equal(new[] { "System.Type B.EqualityContract { get; }", "System.Object B.P { get; init; }" }, GetProperties(comp, "B").ToTestDisplayStrings());
var comp2 = CreateCompilationWithIL(new[] { source2, IsExternalInitTypeDefinition }, ilSource: ilSource, parseOptions: TestOptions.RegularPreview);
comp2.VerifyDiagnostics(
// (4,12): error CS8868: A copy constructor in a record must call a copy constructor of the base, or a parameterless object constructor if the record inherits from object.
// public C(C c) { }
Diagnostic(ErrorCode.ERR_CopyConstructorMustInvokeBaseCopyConstructor, "C").WithLocation(4, 12)
);
}
[Fact]
public void Inheritance_27()
[Fact, WorkItem(44902, "https://github.com/dotnet/roslyn/issues/44902")]
public void CopyCtor_InaccessibleInMetadata()
{
var source =
@"record A
{
public object P { get; }
public object Q { get; set; }
}
record B(object get_P, object set_Q) : A
// IL for `public record B { }`
var ilSource = @"
.class public auto ansi beforefieldinit B extends [mscorlib]System.Object
{
}";
var comp = CreateCompilation(source);
comp.VerifyDiagnostics();
var actualMembers = GetProperties(comp, "B").ToTestDisplayStrings();
var expectedMembers = new[]
.method public hidebysig specialname newslot virtual instance class B '<>Clone' () cil managed
{
"System.Type B.EqualityContract { get; }",
"System.Object B.get_P { get; init; }",
"System.Object B.set_Q { get; init; }",
};
AssertEx.Equal(expectedMembers, actualMembers);
IL_0000: ldnull
IL_0001: throw
}
[Fact]
public void Inheritance_28()
.method family hidebysig newslot virtual instance class [mscorlib]System.Type get_EqualityContract () cil managed
{
var source =
@"interface I
{
object P { get; }
}
record A : I
{
object I.P => null;
}
record B(object P) : A
{
}
record C(object P) : I
{
object I.P => null;
}";
var comp = CreateCompilation(source);
comp.VerifyDiagnostics();
AssertEx.Equal(new[] { "System.Type B.EqualityContract { get; }", "System.Object B.P { get; init; }" }, GetProperties(comp, "B").ToTestDisplayStrings());
AssertEx.Equal(new[] { "System.Type C.EqualityContract { get; }", "System.Object C.P { get; init; }", "System.Object C.I.P { get; }" }, GetProperties(comp, "C").ToTestDisplayStrings());
IL_0000: ldnull
IL_0001: throw
}
[Fact]
public void Inheritance_29()
.method public hidebysig virtual instance int32 GetHashCode () cil managed
{
var sourceA =
@"Public Class A
Public Property P(o As Object) As Object
Get
Return Nothing
End Get
Set
End Set
End Property
Public Property Q(x As Object, y As Object) As Object
Get
Return Nothing
End Get
Set
End Set
End Property
End Class
";
var compA = CreateVisualBasicCompilation(sourceA);
compA.VerifyDiagnostics();
var refA = compA.EmitToImageReference();
var sourceB =
@"record B(object P, object Q) : A
{
object P { get; }
}";
var compB = CreateCompilation(new[] { sourceB, IsExternalInitTypeDefinition }, references: new[] { refA }, parseOptions: TestOptions.RegularPreview);
compB.VerifyDiagnostics(
// (1,8): error CS8867: No accessible copy constructor found in base type 'A'.
// record B(object P, object Q) : A
Diagnostic(ErrorCode.ERR_NoCopyConstructorInBaseType, "B").WithArguments("A").WithLocation(1, 8),
// (1,32): error CS8864: Records may only inherit from object or another record
// record B(object P, object Q) : A
Diagnostic(ErrorCode.ERR_BadRecordBase, "A").WithLocation(1, 32)
);
IL_0000: ldnull
IL_0001: throw
}
var actualMembers = GetProperties(compB, "B").ToTestDisplayStrings();
var expectedMembers = new[]
.method public hidebysig virtual instance bool Equals ( object '' ) cil managed
{
"System.Type B.EqualityContract { get; }",
"System.Object B.Q { get; init; }",
"System.Object B.P { get; }",
};
AssertEx.Equal(expectedMembers, actualMembers);
IL_0000: ldnull
IL_0001: throw
}
[Fact]
public void Inheritance_30()
.method public newslot virtual instance bool Equals ( class B '' ) cil managed
{
var sourceA =
@"Public Class A
Public ReadOnly Overloads Property P() As Object
Get
Return Nothing
End Get
End Property
Public ReadOnly Overloads Property P(o As Object) As Object
Get
Return Nothing
End Get
End Property
Public Overloads Property Q(o As Object) As Object
Get
Return Nothing
End Get
Set
End Set
End Property
Public Overloads Property Q() As Object
Get
Return Nothing
End Get
Set
End Set
End Property
End Class
";
var compA = CreateVisualBasicCompilation(sourceA);
compA.VerifyDiagnostics();
var refA = compA.EmitToImageReference();
IL_0000: ldnull
IL_0001: throw
}
var sourceB =
@"record B(object P, object Q) : A
{
}";
var compB = CreateCompilation(new[] { sourceB, IsExternalInitTypeDefinition }, references: new[] { refA }, parseOptions: TestOptions.RegularPreview);
compB.VerifyDiagnostics(
// (1,8): error CS8867: No accessible copy constructor found in base type 'A'.
// record B(object P, object Q) : A
Diagnostic(ErrorCode.ERR_NoCopyConstructorInBaseType, "B").WithArguments("A").WithLocation(1, 8),
// (1,32): error CS8864: Records may only inherit from object or another record
// record B(object P, object Q) : A
Diagnostic(ErrorCode.ERR_BadRecordBase, "A").WithLocation(1, 32)
);
// Inaccessible copy constructor
.method private hidebysig specialname rtspecialname instance void .ctor ( class B '' ) cil managed
{
IL_0000: ldnull
IL_0001: throw
}
var actualMembers = GetProperties(compB, "B").ToTestDisplayStrings();
AssertEx.Equal(new[] { "System.Type B.EqualityContract { get; }" }, actualMembers);
.method public hidebysig specialname rtspecialname instance void .ctor () cil managed
{
IL_0000: ldnull
IL_0001: throw
}
[Fact]
public void Inheritance_31()
.property instance class [mscorlib]System.Type EqualityContract()
{
var sourceA =
@"Public Class A
Public ReadOnly Property P() As Object
Get
Return Nothing
End Get
End Property
Public Property Q(o As Object) As Object
Get
Return Nothing
End Get
Set
End Set
End Property
Public Property R(o As Object) As Object
Get
Return Nothing
End Get
Set
End Set
End Property
Public Sub New(a as A)
End Sub
End Class
Public Class B
Inherits A
Public ReadOnly Overloads Property P(o As Object) As Object
Get
Return Nothing
End Get
End Property
Public Overloads Property Q() As Object
Get
Return Nothing
End Get
Set
End Set
End Property
Public Overloads Property R(x As Object, y As Object) As Object
Get
Return Nothing
End Get
Set
End Set
End Property
Public Sub New(b as B)
MyBase.New(b)
End Sub
End Class
.get instance class [mscorlib]System.Type B::get_EqualityContract()
}
}
";
var compA = CreateVisualBasicCompilation(sourceA);
compA.VerifyDiagnostics();
var refA = compA.EmitToImageReference();
var sourceB =
@"record C(object P, object Q, object R) : B
{
var source = @"
public record C : B {
}";
var compB = CreateCompilation(new[] { sourceB, IsExternalInitTypeDefinition }, references: new[] { refA }, parseOptions: TestOptions.RegularPreview);
compB.VerifyDiagnostics(
// (1,9): error CS7036: There is no argument given that corresponds to the required formal parameter 'b' of 'B.B(B)'
// record C(object P, object Q, object R) : B
Diagnostic(ErrorCode.ERR_NoCorrespondingArgument, "(object P, object Q, object R)").WithArguments("b", "B.B(B)").WithLocation(1, 9),
// (1,42): error CS8864: Records may only inherit from object or another record
// record C(object P, object Q, object R) : B
Diagnostic(ErrorCode.ERR_BadRecordBase, "B").WithLocation(1, 42)
var comp = CreateCompilationWithIL(new[] { source, IsExternalInitTypeDefinition }, ilSource: ilSource, parseOptions: TestOptions.RegularPreview);
comp.VerifyDiagnostics(
// (2,15): error CS8867: No accessible copy constructor found in base type 'B'.
// public record C : B {
Diagnostic(ErrorCode.ERR_NoCopyConstructorInBaseType, "C").WithArguments("B").WithLocation(2, 15)
);
var actualMembers = GetProperties(compB, "C").ToTestDisplayStrings();
var expectedMembers = new[]
{
"System.Type C.EqualityContract { get; }",
"System.Object C.R { get; init; }",
};
AssertEx.Equal(expectedMembers, actualMembers);
}
[Fact]
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册