-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Emit defensive copy for constrained call on in
parameter
#66189
Conversation
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); | ||
}"; | ||
var verifier = CompileAndVerify(source, expectedOutput: "00"); | ||
// Note: we use a temp instead of directly doing a constrained call on `in` parameter |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we use a temp even when the target method is marked as readonly
? #Closed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not possible to have exactly this scenario with the only modification being an additional readonly
because this scenario uses an interface and readonly
is only allowed in struct
types (not in interfaces).
Prior to the code change, we'd produce a direct constrained call, so the comment calls out what is important to observe in the IL.
That said, it should be possible to emit a pattern-based On second thought, that scenario isn't so interesting, as it will not cover the modified test (it'll go down a different branch of the same method, see line 1600).GetEnumerator
call directly on the struct (ie. foreach
without IEnumerable
interface). I'll add a test for that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI, amended my earlier comment
@AlekseyTs @dotnet/roslyn-compiler Please take a look. Thanks |
@@ -1626,15 +1626,15 @@ private void EmitInstanceCallExpression(BoundCall call, UseKind useKind) | |||
} | |||
else | |||
{ | |||
// calling a method defined in a base class. | |||
// calling a method defined in a base class or interface. | |||
|
|||
// When calling a method that is virtual in metadata on a struct receiver, | |||
// we use a constrained virtual call. If possible, it will skip boxing. | |||
if (method.IsMetadataVirtual()) | |||
{ | |||
// NB: all methods that a struct could inherit from bases are non-mutating |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have a test for that (InvokeStructToStringOverrideOnInParameter
) and we have existing coverage as well (CodeGenReadOnlyStructTests.ReadOnlyMethod_CallBaseMethod
for non-override scenario, and other related ones). In that case we fall into the condition above (at line 1600), not here.
I'd be okay always using AddressKind.Writeable
here. It's definitely safe, but a bit less optimal for ToString
scenarios. Let me know what you think.
|
||
// When calling a method that is virtual in metadata on a struct receiver, | ||
// we use a constrained virtual call. If possible, it will skip boxing. | ||
if (method.IsMetadataVirtual()) | ||
{ | ||
// NB: all methods that a struct could inherit from bases are non-mutating | ||
// treat receiver as ReadOnly | ||
tempOpt = EmitReceiverRef(receiver, AddressKind.ReadOnly); | ||
tempOpt = EmitReceiverRef(receiver, methodContainingType.IsInterface ? AddressKind.Writeable : AddressKind.ReadOnly); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks much for suggesting the scenario where an override is added to the struct.
That showed that the assumption was broken. Removed it.
|
||
// When calling a method that is virtual in metadata on a struct receiver, | ||
// we use a constrained virtual call. If possible, it will skip boxing. | ||
if (method.IsMetadataVirtual()) | ||
{ | ||
// NB: all methods that a struct could inherit from bases are non-mutating | ||
// treat receiver as ReadOnly | ||
tempOpt = EmitReceiverRef(receiver, AddressKind.ReadOnly); | ||
tempOpt = EmitReceiverRef(receiver, methodContainingType.IsInterface ? AddressKind.Writeable : AddressKind.ReadOnly); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Readonly fields are not affected by this change. Here we're only dealing with calls to a method.
We clarified offline. The question is about using a readonly field as the receiver. If C# is affected, maybe VB is too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did some investigation. It is indeed possible to hit this with a readonly field as well.
On the VB side, it's a bit confusing because the AddressKind
enum values have different meaning than the C# AddressKind
s of the same name, but as far as I could tell we didn't have the same problem. Added tests.
It's worth noting that in VB, the For Each
generates a conversion (boxing) instead of a constrained call, so I couldn't get exactly the same scenario that was broken in C#.
@AlekseyTs This is ready for another look. Thanks |
1 similar comment
@AlekseyTs This is ready for another look. Thanks |
Looking |
This scenario is actually interesting. Perhaps we could optimize cases like this as long as consumer and the consumed type are in the same module. Refers to: src/Compilers/CSharp/Test/Emit/CodeGen/CodeGenReadonlyStructTests.cs:507 in c711d2b. [](commit_id = c711d2b, deletion_comment = False) |
{ | ||
public static string M(in S s) | ||
{ | ||
return s.ToString(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done with review pass (commit 2) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM (commit 4)
/azp run |
Azure Pipelines successfully started running 2 pipeline(s). |
This change seems to cause defensive copies to be emitted when they don't need to: The IL shows that a defensive copy is being emitted when it is not necessary. It is not necessary because the implementation that |
@hamarb123 Interface members cannot be marked |
Why can they be marked |
I think by "they" you mean private interface implementations, such as Tagging @RikkiGibson (dev on readonly struct members) in case anything to add. |
I don't recall specifically. #32911 does indicate it was intentional though 😊
|
Fixes #66135