Skip to content

Conversation

EraYaN
Copy link
Contributor

@EraYaN EraYaN commented Jul 28, 2025

support ref keyword in user defined methods for the target parameter

Description

This will emit a ref keyword and supporting lines so pass by value types can actually be updated by the existing target methods (immutable records, array etc.) when the user defined methods has a ref keyword.

Fixes #1892

Checklist

  • The existing code style is followed
  • The commit message follows our guidelines
  • Performed a self-review of my code
  • Hard-to-understand areas of my code are commented
  • The documentation is updated (as applicable)
  • Unit tests are added/updated
  • Integration tests are added/updated (as applicable, especially if feature/bug depends on roslyn or framework version in use)

This is an initial attempt at solving this issue, does this agree with how the project would expect solving this issue?I has to extend the MethodParameter struct and pass it down a bit further to get the extra required context to the point where the Arguments are made. Not sure if there is some clean up that is possible to unify some of the other code paths.

@EraYaN EraYaN force-pushed the ref-keyword-support branch from 8ba3ff6 to 3146755 Compare July 28, 2025 08:47
@latonz
Copy link
Contributor

latonz commented Jul 28, 2025

Thank you for this contribution, whats the reason for introducing the temporary field in the generated code?

@EraYaN
Copy link
Contributor Author

EraYaN commented Jul 28, 2025

If the member access is a property, you can not pass it as a reference. Since it's really a function call. So it need to take the reference and then assign it back.

var a = new ClassWithProperty();

// this
FunctionWithRefParam(ref a.Property);
// becomes this (kind of)
FunctionWithRefParam(ref a.get_Property());

Copy link
Contributor

@latonz latonz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for this contribution 😊 I added my feedback.

@latonz latonz added the enhancement New feature or request label Jul 29, 2025
@EraYaN EraYaN force-pushed the ref-keyword-support branch from 5a91078 to 423c6e3 Compare July 29, 2025 08:21
@EraYaN
Copy link
Contributor Author

EraYaN commented Jul 29, 2025

I've also rebased on latest main.

@EraYaN EraYaN force-pushed the ref-keyword-support branch from 423c6e3 to 1a7d7ed Compare July 29, 2025 08:59
@EraYaN
Copy link
Contributor Author

EraYaN commented Jul 29, 2025

That last failing test, I hope it's fixed now, but no idea why it broke for .NET 4.8.

@latonz
Copy link
Contributor

latonz commented Jul 29, 2025

The tests are failing due to the non-nullable not initialized array field in the integration tests: https://github.com/riok/mapperly/actions/runs/16592052238/job/46930700287?pr=1895#step:4:22

The integration test for the .net framework is failing since we generate slightly different code for the .net framework. Instead of #nullable enable we use #nullable enable annotations there. So your [VersionedSnapshot(Versions.NET6_0)] was correct, sorry about that. The test should pass if you add [VersionedSnapshot(Versions.NET6_0)], copy the snapshot UseUserMethodWithRefTest.SnapshotGeneratedSource.verified.cs as UseUserMethodWithRefTest.SnapshotGeneratedSource_NET6_0.verified.cs and replace #nullable enable with #nullable enable annotations in UseUserMethodWithRefTest.SnapshotGeneratedSource.verified.cs.

@EraYaN EraYaN force-pushed the ref-keyword-support branch from aacdb38 to 2436787 Compare July 29, 2025 09:42
@EraYaN
Copy link
Contributor Author

EraYaN commented Jul 29, 2025

Alright that should then fix it, I mirrored the nullable annotation from UseExternalMapperTest.SnapshotGeneratedSource_NET6_0.verified.cs

@EraYaN EraYaN force-pushed the ref-keyword-support branch from 2436787 to 52c965a Compare July 29, 2025 10:04
@EraYaN
Copy link
Contributor Author

EraYaN commented Jul 29, 2025

I understand the issue now, it needs both snapshots.

@latonz latonz merged commit 787c6aa into riok:main Jul 29, 2025
19 checks passed
@latonz
Copy link
Contributor

latonz commented Jul 29, 2025

Thank you for this great contribution! 🥳

@EraYaN EraYaN deleted the ref-keyword-support branch July 30, 2025 11:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Copy ref keyword if UserMapping function has it when function is used

2 participants