未验证 提交 7e89bce3 编写于 作者: A AlekseyTs 提交者: GitHub

A copy constructor shouldn't prevent an automatic addition of a default...

A copy constructor shouldn't prevent an automatic addition of a default instance constructor for a record. (#47850)

https://github.com/dotnet/csharplang/pull/3913
上级 d171b0b4
......@@ -3507,7 +3507,7 @@ private void AddSynthesizedConstructorsIfNecessary(ArrayBuilder<Symbol> 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;
......
......@@ -844,7 +844,6 @@ .maxstack 1
");
var comp = (CSharpCompilation)verifier.Compilation;
comp.VerifyEmitDiagnostics();
var actualMembers = comp.GetMember<NamedTypeSymbol>("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()
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册