Skip to content

Record copy constructor should leverage base copy constructor #44902

@jcouv

Description

@jcouv
        [Fact]
        public void TODO()
        {
            var src = @"

public record Human(int age) { }
public record Person(string First, string Last) : Human(0) { }
";

            var comp = CreateCompilation(src);
            comp.VerifyDiagnostics(
                // (4,15): error CS1729: 'Human' does not contain a constructor that takes 0 arguments
                // public record Person(string First, string Last) : Human(0) 
                Diagnostic(ErrorCode.ERR_BadCtorArgCount, "Person").WithArguments("Human", "0").WithLocation(4, 15)
                );
        }

In a scenario that compiles, the copy constructor for Person looks like this:

    .method public hidebysig specialname rtspecialname 
        instance void .ctor (
            class Person ''
        ) cil managed 
    {
        // Method begins at RVA 0x214d
        // Code size 32 (0x20)
        .maxstack 8

        IL_0000: ldarg.0
        IL_0001: call instance void Human::.ctor()
        IL_0006: nop
        IL_0007: ldarg.0
        IL_0008: ldarg.1
        IL_0009: ldfld string Person::'<First>k__BackingField'
        IL_000e: stfld string Person::'<First>k__BackingField'
        IL_0013: ldarg.0
        IL_0014: ldarg.1
        IL_0015: ldfld string Person::'<Last>k__BackingField'
        IL_001a: stfld string Person::'<Last>k__BackingField'
        IL_001f: ret
    } // end of method Person::.ctor

Conclusions from offline group for copy constructors:

  • don't include initializers
  • require delegate to base copy constructor (user defined)
  • delegate to base copy constructor, except for object (synthesized)
  • error if not accessible during synthesis

Metadata

Metadata

Assignees

Type

No type

Projects

Status

Active/Investigating

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions