Skip to content

Commit

Permalink
Only record class can have a copy constructor. (#68442)
Browse files Browse the repository at this point in the history
Fixes #68345.
  • Loading branch information
AlekseyTs authored Jun 7, 2023
1 parent 397a7c6 commit 61853f2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ internal override void AddSynthesizedAttributes(PEModuleBuilder moduleBuilder, r

internal static bool IsCopyConstructor(Symbol member)
{
if (member is MethodSymbol { ContainingType.IsRecordStruct: false, MethodKind: MethodKind.Constructor } method)
if (member is MethodSymbol { ContainingType.IsRecord: true, MethodKind: MethodKind.Constructor } method)
{
return HasCopyConstructorSignature(method);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,27 @@ public C() // overload
}
}

[Theory]
[CombinatorialData]
[WorkItem("https://github.com/dotnet/roslyn/issues/68345")]
public void ConstructorOverloading_03([CombinatorialValues("class ", "struct")] string keyword)
{
var comp = CreateCompilation(@"
#pragma warning disable CS" + UnreadParameterWarning() + @" // Parameter 'x' is unread.
" + keyword + @" C(int x, string y)
{
internal C(C other) // overload
{
}
}");

comp.VerifyDiagnostics(
// (5,14): error CS8862: A constructor declared in a type with parameter list must have 'this' constructor initializer.
// internal C(C other) // overload
Diagnostic(ErrorCode.ERR_UnexpectedOrMissingConstructorInitializerInRecord, "C").WithLocation(5, 14)
);
}

[Theory]
[CombinatorialData]
public void Members_01([CombinatorialValues("class", "struct")] string keyword)
Expand Down

0 comments on commit 61853f2

Please sign in to comment.