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

Duplicate .override method generation #73338

Open
viceroypenguin opened this issue May 4, 2024 · 2 comments
Open

Duplicate .override method generation #73338

viceroypenguin opened this issue May 4, 2024 · 2 comments

Comments

@viceroypenguin
Copy link
Contributor

Version Used:
Roslyn v4.9.2

Steps to Reproduce:

  1. Use a CRTP interface with a static abstract method
  2. Mark a partial nested class with the CRTP
  3. Use a source generator to also mark the class CRTP
  4. Use the source generator to implement the CRTP

Note

Using a source generator is an essential step. Copying the code into regular .cs files will not trigger this bug.

Repro: https://github.com/viceroypenguin/CompilerBug

Expected Behavior:
The generated assembly will be a valid legal assembly.

Actual Behavior:
The generated assembly is invalid and will fail when attempting to call Assembly.GetExportedTypes().

Specifically, in the linked repro, the generated assembly will have two .override method modifiers on the Validate method.

.method public hidebysig static 
	class [System.Collections]System.Collections.Generic.List`1<class [Immediate.Validations.Shared]Immediate.Validations.Shared.ValidationError> Validate (
		class Immediate.Validations.FunctionalTests.IntegrationTests.SaveRecord/Command target
	) cil managed 
{
	.custom instance void [System.Runtime]System.Runtime.CompilerServices.NullableContextAttribute::.ctor(uint8) = (
		01 00 00 00 00
	)
	.override method class [System.Collections]System.Collections.Generic.List`1<class [Immediate.Validations.Shared]Immediate.Validations.Shared.ValidationError> class [Immediate.Validations.Shared]Immediate.Validations.Shared.IValidationTarget`1<class Immediate.Validations.FunctionalTests.IntegrationTests.SaveRecord/Command>::Validate(!0)
	.override method class [System.Collections]System.Collections.Generic.List`1<class [Immediate.Validations.Shared]Immediate.Validations.Shared.ValidationError> class [Immediate.Validations.Shared]Immediate.Validations.Shared.IValidationTarget`1<class Immediate.Validations.FunctionalTests.IntegrationTests.SaveRecord/Command>::Validate(!0)

...
@dotnet-issue-labeler dotnet-issue-labeler bot added Area-IDE untriaged Issues and PRs which have not yet been triaged by a lead labels May 4, 2024
@viceroypenguin
Copy link
Contributor Author

build.zip

@jjonescz
Copy link
Contributor

jjonescz commented May 7, 2024

Looks like this is caused by different nullability contexts (generated files have different nullability context that's why you couldn't repro without source generators probably). Can reproduce with something as simple as:

#nullable enable
partial class C
    #nullable disable
    : IInterface<C>
    #nullable enable
{
}

partial class C : IInterface<C>
{
    public static void M() { }
}

interface IInterface<T>
{
    static abstract void M();
}

See SharpLab.

Results in this IL (notice the duplicate .override method):

.class private auto ansi beforefieldinit C
    extends [System.Runtime]System.Object
    implements class IInterface`1<class C>
{
    // Methods
    .method public hidebysig static 
        void M () cil managed 
    {
        .override method void class IInterface`1<class C>::M()
        .override method void class IInterface`1<class C>::M()
        // Method begins at RVA 0x2050
        // Code size 1 (0x1)
        .maxstack 8

        IL_0000: ret
    } // end of method C::M

Similar issue I've stumbled upon recently in razor: #40538 (via dotnet/razor#9837).

@jjonescz jjonescz added New Language Feature - Nullable Reference Types Nullable Reference Types and removed New Feature - Source Generators Source Generators labels May 7, 2024
@jaredpar jaredpar added Bug and removed untriaged Issues and PRs which have not yet been triaged by a lead labels May 7, 2024
@jaredpar jaredpar added this to the 17.11 milestone May 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants