JasonBock / Rocks

A mocking library based on the Compiler APIs (Roslyn + Mocks)
MIT License
263 stars 20 forks source link

Virtual Members Using the `new` Keyword Breaks Base Invocations #334

Closed JasonBock closed 3 months ago

JasonBock commented 3 months ago

To Reproduce

#nullable enable

using Rocks;
using System;

[assembly: Rock(typeof(NewNameUsage), BuildType.Create)]

public class NewNameUsage
{
  public virtual void Work(string @new) { }
}

Expected behavior The mock is created with no issues.

Actual behavior Errors occur:

Error - Id: CS1003, Description: Rocks\Rocks.RockGenerator\NewNameUsage_Rock_Create.g.cs(146,18): error CS1003: Syntax error, ',' expected
Error - Id: CS1003, Description: Rocks\Rocks.RockGenerator\NewNameUsage_Rock_Create.g.cs(146,20): error CS1003: Syntax error, ',' expected
Error - Id: CS1526, Description: Rocks\Rocks.RockGenerator\NewNameUsage_Rock_Create.g.cs(146,18): error CS1526: A new expression requires an argument list or (), [], or {} after type

Additional context The problem is that the method's parameter uses new for the name and is virtual. Within the override, Rocks puts in a call to the base class method, but it'll do this:

base.Work(new: @new);

What it should do is:

base.Work(@new: @new);

In general, any base invocation should always put @ in front of the parameter name.

This was found on Microsoft.EntityFrameworkCore.Query.Internal.ExpressionTreeFuncletizer