Skip to content

Fix 1158 ref extension property#1227

Merged
GrahamTheCoder merged 5 commits intoicsharpcode:masterfrom
GrahamTheCoder:fix-1158-ref-extension-property-5310240367378084052
Mar 11, 2026
Merged

Fix 1158 ref extension property#1227
GrahamTheCoder merged 5 commits intoicsharpcode:masterfrom
GrahamTheCoder:fix-1158-ref-extension-property-5310240367378084052

Conversation

@GrahamTheCoder
Copy link
Member

@GrahamTheCoder GrahamTheCoder commented Mar 10, 2026

Fixes #1158.

When a VB.NET extension method with a ByRef parameter is called on a property, the C# converter previously failed to hoist the property into a temporary variable, resulting in the invalid C# code this.Number.NegEx(); (CS0206: A property or indexer may not be passed as an out or ref parameter).

This PR generalizes ArgumentConverter.HoistByRefDeclaration so it can be applied to the this argument expression, and updates NameExpressionNodeVisitor to correctly hoist properties passed to extension methods. It also includes the requested unit test.


Jules task 5310240367378084052 started by @GrahamTheCoder


Gemini review

This pull request addresses a critical bug in the VB.NET to C# code converter where ByRef extension methods invoked on properties would generate invalid C# code. The changes ensure that such properties are correctly hoisted into temporary variables during conversion, resolving the compilation error and improving the robustness of the converter.

Highlights

  • Generalized ByRef Hoisting: The HoistByRefDeclaration method in ArgumentConverter.cs has been generalized to accept a broader VBSyntax.ExpressionSyntax type, allowing for more flexible handling of expressions that need to be hoisted into temporary variables.
  • Corrected Property Handling for Extension Methods: The NameExpressionNodeVisitor.cs now includes specific logic to detect when a VB.NET extension method with a ByRef parameter is called on a property. It correctly hoists the property into a temporary variable to avoid C# compilation errors (CS0206).
  • New Unit Test: A new unit test, ExtensionMethodRefPropertyTests.cs, has been added to validate the fix, ensuring that VB.NET code involving properties and ByRef extension methods converts correctly to C#.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • CodeConverter/CSharp/ArgumentConverter.cs
    • Modified the HoistByRefDeclaration method signature to accept VBSyntax.ExpressionSyntax for broader applicability.
    • Updated internal calls to GetTypeInfo and ShouldPreferExplicitType to use the generalized node parameter.
  • CodeConverter/CSharp/NameExpressionNodeVisitor.cs
    • Refactored the handling of reduced extension methods to include logic for ByRef parameter hoisting.
    • Implemented checks to determine if a property, field, or method requires hoisting based on its RefKind and RefConversion type.
    • Integrated the generalized HoistByRefDeclaration method for proper property hoisting in extension method calls.
  • Tests/VB/ExtensionMethodRefPropertyTests.cs
    • Added a new test file to verify the correct conversion of VB.NET extension methods with ByRef parameters when called on properties.
Activity
  • PR created automatically by Jules for task 5310240367378084052, started by @GrahamTheCoder.

google-labs-jules bot and others added 5 commits March 8, 2026 12:16
… ref

* Extract `HoistByRefDeclaration` in `ArgumentConverter` to accept `ExpressionSyntax` instead of `SimpleArgumentSyntax` so it can be reused for the `this` parameter of extension methods.
* Update `NameExpressionNodeVisitor` to apply ref-conversion hoisting rules to the `this` expression of extension methods using `ref` parameters.
* Add a unit test verifying that `Number.NegEx()` where `NegEx` takes `ByRef num as Integer` correctly generates a temporary variable assignment before and after the call.

Co-authored-by: GrahamTheCoder <2490482+GrahamTheCoder@users.noreply.github.com>
… ref

* Add `HoistByRefDeclaration` overload in `ArgumentConverter` accepting `ExpressionSyntax` instead of `SimpleArgumentSyntax` to support generic expression hoisting.
* Update `NameExpressionNodeVisitor` to correctly detect when the receiver expression of an extension method needs to be passed by reference (e.g. properties, fields), and reuse the variable hoisting logic to correctly generate temporary variables for property modification before and after the method call.
* Add comprehensive test `TestExtensionMethodRefPropertyAsync` to ensure the generated C# code successfully extracts the property to a temporary variable for method calls.

Co-authored-by: GrahamTheCoder <2490482+GrahamTheCoder@users.noreply.github.com>
… ref

* Generalize `HoistByRefDeclaration` in `ArgumentConverter` to accept `ExpressionSyntax`.
* Update `NameExpressionNodeVisitor` to correctly process extension method receiver ref-conversions, hoisting the receiver expression (e.g., properties) into a temporary variable when required by `ByRef`.
* Introduce `TestExtensionMethodRefPropertyAsync` to explicitly verify property ref assignments to extension methods generate a valid C# state.

Co-authored-by: GrahamTheCoder <2490482+GrahamTheCoder@users.noreply.github.com>
@GrahamTheCoder GrahamTheCoder changed the title Fix 1158 ref extension property 5310240367378084052 Fix 1158 ref extension property Mar 11, 2026
@GrahamTheCoder GrahamTheCoder marked this pull request as ready for review March 11, 2026 00:48
@GrahamTheCoder GrahamTheCoder merged commit 2df8e04 into icsharpcode:master Mar 11, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

VB -> C#: Properties not handled correctly for ref extension methods

1 participant