Browse Source

Fix #3237: Use `ref readonly` locals for `readonly.ldelema`

pull/3243/head
Daniel Grunwald 10 months ago
parent
commit
aa914058ce
  1. 3
      ICSharpCode.Decompiler/IL/Transforms/ILInlining.cs
  2. 10
      ICSharpCode.Decompiler/IL/Transforms/IntroduceRefReadOnlyModifierOnLocals.cs

3
ICSharpCode.Decompiler/IL/Transforms/ILInlining.cs

@ -537,6 +537,9 @@ namespace ICSharpCode.Decompiler.IL.Transforms @@ -537,6 +537,9 @@ namespace ICSharpCode.Decompiler.IL.Transforms
}
}
/// <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)

10
ICSharpCode.Decompiler/IL/Transforms/IntroduceRefReadOnlyModifierOnLocals.cs

@ -52,8 +52,18 @@ namespace ICSharpCode.Decompiler.IL @@ -52,8 +52,18 @@ namespace ICSharpCode.Decompiler.IL
{
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;
}

Loading…
Cancel
Save