Skip to content

Allow box_this pattern on composite r2r#124519

Open
BrzVlad wants to merge 1 commit intodotnet:mainfrom
BrzVlad:fix-r2r-boxthis
Open

Allow box_this pattern on composite r2r#124519
BrzVlad wants to merge 1 commit intodotnet:mainfrom
BrzVlad:fix-r2r-boxthis

Conversation

@BrzVlad
Copy link
Member

@BrzVlad BrzVlad commented Feb 17, 2026

For the following code:

struct MyStruct
{
   public int Value;
}

static string CallToString<T>(ref T val) where T : struct
{
   // constrained. T callvirt ToString()
   return val.ToString();
}

var s = new MyStruct { Value = 42 };
Console.WriteLine(CallToString(ref s));

The CallToString method would be skipped by r2r because, in this state, if user would change MyStruct to contain an override for ToString then the generated code for CallToString would be incorrect since it should no longer do any boxing. For composite r2r however, this is not a problem if the valuetype (MyStruct) and the compiled method (CallToString) are in the same version bubble. The fix adds a check that these are in the same version bubble.

For the following code:
```
struct MyStruct
{
   public int Value;
}

static string CallToString<T>(ref T val) where T : struct
{
   // constrained. T callvirt ToString()
   return val.ToString();
}

var s = new MyStruct { Value = 42 };
Console.WriteLine(CallToString(ref s));
```

The CallToString method would be skipped by r2r because, in this state, if user would change MyStruct to contain an override for `ToString` then the generated code for CallToString would be incorrect since it should no longer do any boxing. For composite r2r however, this is not a problem if the valuetype (MyStruct) and the compiled method (CallToString) are in the same version bubble. The fix adds a check that these are in the same version bubble.
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant