Skip to content

Commit

Permalink
Handle generic types in method deduplication
Browse files Browse the repository at this point in the history
This fixes an error where methods with the same parameters but different _generic type parameters_ were being treated as identical during deduplication, resulting in the loss of interface methods.

The fix is to use `genericsOptions: SymbolDisplayGenericsOptions.IncludeTypeParameters` when constucting the `SymbolDisplayFormat` used for deduplication.

Before:
`public void AMethod(Func<int> getValue) {}` produced a deduplication signature of `AMethod(Func)`
After:
`public void AMethod(Func<int> getValue) {}` produced a deduplication signature of `AMethod(Func<Int32>)`
  • Loading branch information
simonmckenzie committed Aug 14, 2024
1 parent d8d8fc4 commit e7224f2
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
1 change: 1 addition & 0 deletions AutomaticInterface/AutomaticInterface/Builder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public static class Builder

private static readonly SymbolDisplayFormat MethodSignatureDisplayFormat =
new(
genericsOptions: SymbolDisplayGenericsOptions.IncludeTypeParameters,
memberOptions: SymbolDisplayMemberOptions.IncludeParameters,
parameterOptions: SymbolDisplayParameterOptions.IncludeType
| SymbolDisplayParameterOptions.IncludeParamsRefOut
Expand Down
49 changes: 49 additions & 0 deletions AutomaticInterface/Tests/GeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2031,6 +2031,55 @@ public partial interface IDemoClass
GenerateCode(code).Should().Be(expected);
}

[Fact]
public void WorksWithGenericParameterOverloads()
{
const string code = """
using AutomaticInterface;
namespace AutomaticInterfaceExample;
[GenerateAutomaticInterface]
public class DemoClass
{
public void AMethod(Func<int> getValue) {}
public void AMethod(Func<float> getValue) {}
}
""";

const string expected = """
//--------------------------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
// </auto-generated>
//--------------------------------------------------------------------------------------------------
using System.CodeDom.Compiler;
using AutomaticInterface;
namespace AutomaticInterfaceExample
{
[GeneratedCode("AutomaticInterface", "")]
public partial interface IDemoClass
{
/// <inheritdoc />
void AMethod(Func<int> getValue);
/// <inheritdoc />
void AMethod(Func<float> getValue);
}
}
""";
GenerateCode(code).Should().Be(expected);
}

[Fact]
public void WorksWithEventOverrides()
{
Expand Down

0 comments on commit e7224f2

Please sign in to comment.