Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Record copy constructor should leverage base copy constructor #44902

Closed
jcouv opened this issue Jun 5, 2020 · 1 comment · Fixed by #45022 or #45136
Closed

Record copy constructor should leverage base copy constructor #44902

jcouv opened this issue Jun 5, 2020 · 1 comment · Fixed by #45022 or #45136

Comments

@jcouv
Copy link
Member

jcouv commented Jun 5, 2020

        [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
@jcouv
Copy link
Member Author

jcouv commented Jun 15, 2020

Fix merged into 16.7p3. Closing this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
1 participant