diff --git a/src/Compilers/CSharp/Portable/Symbols/Source/SourceMemberContainerSymbol.cs b/src/Compilers/CSharp/Portable/Symbols/Source/SourceMemberContainerSymbol.cs index 424f293909470dfafd05124c44167aa8ce19885a..8d1ffdb8b4e95186d2dddca483ed3bb4ecfa69a4 100644 --- a/src/Compilers/CSharp/Portable/Symbols/Source/SourceMemberContainerSymbol.cs +++ b/src/Compilers/CSharp/Portable/Symbols/Source/SourceMemberContainerSymbol.cs @@ -3507,7 +3507,7 @@ private void AddSynthesizedConstructorsIfNecessary(ArrayBuilder members, { case MethodKind.Constructor: // Ignore the record copy constructor - if (!(method is SynthesizedRecordCopyCtor)) + if (!IsRecord || !SynthesizedRecordCopyCtor.HasCopyConstructorSignature(method)) { hasInstanceConstructor = true; hasParameterlessInstanceConstructor = hasParameterlessInstanceConstructor || method.ParameterCount == 0; diff --git a/src/Compilers/CSharp/Test/Semantic/Semantics/RecordTests.cs b/src/Compilers/CSharp/Test/Semantic/Semantics/RecordTests.cs index 7d75c6aaaa2e63565be33c6ea8b9a022db0419f8..a01a569053cee990eb1080f523632a80a3f1fc03 100644 --- a/src/Compilers/CSharp/Test/Semantic/Semantics/RecordTests.cs +++ b/src/Compilers/CSharp/Test/Semantic/Semantics/RecordTests.cs @@ -844,7 +844,6 @@ .maxstack 1 "); var comp = (CSharpCompilation)verifier.Compilation; - comp.VerifyEmitDiagnostics(); var actualMembers = comp.GetMember("C").GetMembers().ToTestDisplayStrings(); var expectedMembers = new[] @@ -25631,6 +25630,187 @@ public static void Main() CompileAndVerify(comp, expectedOutput: "4243", verify: Verification.Skipped /* init-only */); } + [Fact] + public void DefaultCtor_01() + { + var src = @" +record C +{ +} + +class Program +{ + static void Main() + { + var x = new C(); + var y = new C(); + System.Console.WriteLine(x == y); + } +} +"; + + var verifier = CompileAndVerify(src, expectedOutput: "True"); + verifier.VerifyDiagnostics(); + + verifier.VerifyIL("C..ctor()", @" +{ + // Code size 7 (0x7) + .maxstack 1 + IL_0000: ldarg.0 + IL_0001: call ""object..ctor()"" + IL_0006: ret +} +"); + } + + [Fact] + public void DefaultCtor_02() + { + var src = @" +record B(int x); + +record C : B +{ +} +"; + + var comp = CreateCompilation(src); + comp.VerifyEmitDiagnostics( + // (4,8): error CS1729: 'B' does not contain a constructor that takes 0 arguments + // record C : B + Diagnostic(ErrorCode.ERR_BadCtorArgCount, "C").WithArguments("B", "0").WithLocation(4, 8) + ); + } + + [Fact] + public void DefaultCtor_03() + { + var src = @" +record C +{ + public C(C c){} +} + +class Program +{ + static void Main() + { + var x = new C(); + var y = new C(); + System.Console.WriteLine(x == y); + } +} +"; + + var verifier = CompileAndVerify(src, expectedOutput: "True"); + verifier.VerifyDiagnostics(); + + verifier.VerifyIL("C..ctor()", @" +{ + // Code size 7 (0x7) + .maxstack 1 + IL_0000: ldarg.0 + IL_0001: call ""object..ctor()"" + IL_0006: ret +} +"); + } + + [Fact] + public void DefaultCtor_04() + { + var src = @" +record B(int x); + +record C : B +{ + public C(C c) : base(c) {} +} +"; + + var comp = CreateCompilation(src); + comp.VerifyEmitDiagnostics( + // (4,8): error CS1729: 'B' does not contain a constructor that takes 0 arguments + // record C : B + Diagnostic(ErrorCode.ERR_BadCtorArgCount, "C").WithArguments("B", "0").WithLocation(4, 8) + ); + } + + [Fact] + public void DefaultCtor_05() + { + var src = @" +record C(int x); + +class Program +{ + static void Main() + { + _ = new C(); + } +} +"; + + var comp = CreateCompilation(src); + comp.VerifyEmitDiagnostics( + // (8,17): error CS7036: There is no argument given that corresponds to the required formal parameter 'x' of 'C.C(int)' + // _ = new C(); + Diagnostic(ErrorCode.ERR_NoCorrespondingArgument, "C").WithArguments("x", "C.C(int)").WithLocation(8, 17) + ); + } + + [Fact] + public void DefaultCtor_06() + { + var src = @" +record C +{ + C(int x) {} +} + +class Program +{ + static void Main() + { + _ = new C(); + } +} +"; + + var comp = CreateCompilation(src); + comp.VerifyEmitDiagnostics( + // (11,17): error CS1729: 'C' does not contain a constructor that takes 0 arguments + // _ = new C(); + Diagnostic(ErrorCode.ERR_BadCtorArgCount, "C").WithArguments("C", "0").WithLocation(11, 17) + ); + } + + [Fact] + public void DefaultCtor_07() + { + var src = @" +class C +{ + C(C x) {} +} + +class Program +{ + static void Main() + { + _ = new C(); + } +} +"; + + var comp = CreateCompilation(src); + comp.VerifyEmitDiagnostics( + // (11,17): error CS1729: 'C' does not contain a constructor that takes 0 arguments + // _ = new C(); + Diagnostic(ErrorCode.ERR_BadCtorArgCount, "C").WithArguments("C", "0").WithLocation(11, 17) + ); + } + [Fact] [WorkItem(47867, "https://github.com/dotnet/roslyn/issues/47867")] public void ToString_RecordWithStaticMembers()