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

Hiding Method in Derived Interface Generates Invalid Code #352

Open
JasonBock opened this issue Feb 28, 2025 · 0 comments
Open

Hiding Method in Derived Interface Generates Invalid Code #352

JasonBock opened this issue Feb 28, 2025 · 0 comments

Comments

@JasonBock
Copy link
Owner

To Reproduce

var code =
  """
  using Rocks;
  
  [assembly: Rock(typeof(IServiceDerived), BuildType.Create | BuildType.Make)]
  
  public interface IOutBase { }
  
  public interface IOutDerived : IOutBase { }
  
  public interface IServiceBase
  {
    void TryGet<T>(out T value) where T : IOutBase;
  }
  
  public interface IServiceDerived : IServiceBase
  {
    new void TryGet<T>(out T value) where T : IOutDerived;
  }
  """;

Expected behavior

Mock code is generated with errors.

Actual behavior
The following error occurs:

// Rocks\Rocks.RockGenerator\IServiceDerived_Rock_Create.g.cs(49,15): error CS0425: 
The constraints for type parameter 'T' of method 'IServiceDerivedCreateExpectations.Mock.TryGet<T>(out T)' must match the constraints for type parameter 'T' of interface method 'IServiceBase.TryGet<T>(out T)'. Consider using an explicit interface implementation instead.

Additional context

This can be fixed if the base type is generated explicitly:

public class SD : IServiceDerived
{
  public void TryGet<T>(out T value) where T : IOutDerived
  {
    throw new NotImplementedException();
  }

  void IServiceBase.TryGet<T>(out T value)
  {
    throw new NotImplementedException();
  }
}

This was found by trying to duplicate this issue in NSubstitute.

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

1 participant