Skip to content

Duplicate method names when inheriting interfaces require explicit interface implementation #29

@StevenTCramer

Description

@StevenTCramer

Bug Description

When a class implements multiple interfaces that share method names (like IEnumerable<T> and IEnumerable), the generator creates duplicate public methods with the same name, causing a compilation error. C# requires one of these to use explicit interface implementation.

Steps to Reproduce

  1. Create a class that implements IList<T> via delegation (which inherits from both IEnumerable<T> and IEnumerable):
using TimeWarp.SourceGenerators;

public partial class NuruAppBuilder : IList<ServiceDescriptor>
{
  [Implements]
  private ServiceCollection? ServiceCollection;
}
  1. Build the project

Expected Behavior

The generator should detect when multiple interfaces define methods with the same name and use explicit interface implementation for the non-generic version:

// Public generic version
public IEnumerator<ServiceDescriptor> GetEnumerator()
{
    return ServiceCollection.GetEnumerator();
}

// Explicit interface implementation for non-generic version
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
    return GetEnumerator();
}

Actual Behavior

The generator creates two public methods with the same name:

public System.Collections.Generic.IEnumerator<ServiceDescriptor> GetEnumerator()
{
    return ServiceCollection.GetEnumerator();
}

public System.Collections.IEnumerator GetEnumerator()  // ERROR: Duplicate method name
{
    return ServiceCollection.GetEnumerator();
}

Compiler Error

error CS0111: Type 'NuruAppBuilder' already defines a member called 'GetEnumerator' with the same parameter types

Environment

  • TimeWarp.SourceGenerators: 1.0.0-beta.7
  • .NET: 10.0
  • OS: Linux (WSL2)

Reproduction Repository

Branch: https://github.com/TimeWarpEngineering/timewarp-nuru/tree/Cramer/2025-10-16/readme-restructure
File: Source/TimeWarp.Nuru/NuruAppBuilder.cs (lines 10, 14-15)
Generated: Source/TimeWarp.Nuru/generated/timewarp-source-generators/TimeWarp.SourceGenerators.InterfaceDelegationGenerator/NuruAppBuilder.implements.g.cs (lines 45, 50)

Impact

This prevents using interface delegation for collection interfaces (IList<T>, IDictionary<K,V>, ISet<T>, etc.) because they all inherit from both generic and non-generic IEnumerable.

Related

Beta.7 successfully generates all inherited interface members, but hits this C# language constraint with duplicate method names.

Suggested Fix

When generating interface members, detect when multiple interfaces define methods with the same signature (same name and parameters). Make the more-derived/generic version public, and use explicit interface implementation for the base/non-generic version.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions