Skip to content

Commit

Permalink
Only record class can have a copy constructor.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlekseyTs committed Jun 5, 2023
1 parent 43913e6 commit fa1ce73
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 fa1ce73

Please sign in to comment.