Skip to content

Commit

Permalink
Fix #3237: Use ref readonly locals for readonly.ldelema
Browse files Browse the repository at this point in the history
  • Loading branch information
dgrunwald committed Jul 21, 2024
1 parent 5a66518 commit aa91405
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
3 changes: 3 additions & 0 deletions ICSharpCode.Decompiler/IL/Transforms/ILInlining.cs
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,9 @@ internal static ExpressionClassification ClassifyExpression(ILInstruction inst)
}
}

/// <summary>
/// Gets whether the ILInstruction will turn into a C# expresion that is considered readonly by the C# compiler.
/// </summary>
internal static bool IsReadonlyReference(ILInstruction addr)
{
switch (addr)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,18 @@ bool IsUsedAsRefReadonly(ILVariable variable)
{
foreach (var store in variable.StoreInstructions.OfType<StLoc>())
{
// Check if C# requires that the local is ref-readonly in order to allow the store:
if (ILInlining.IsReadonlyReference(store.Value))
return true;
// Check whether the local needs to be ref-readonly to avoid changing the semantics of
// a readonly.ldelema:
ILInstruction val = store.Value;
while (val is LdFlda ldflda)
{
val = ldflda.Target;
}
if (val is LdElema { IsReadOnly: true })
return true;
}
return false;
}
Expand Down

0 comments on commit aa91405

Please sign in to comment.