Fix #3850: recover ReadOnlySpan<T> array literals from the legacy lazy cache#3851
Merged
siegfriedpammer merged 1 commit intoJul 13, 2026
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR addresses a decompilation correctness issue on frameworks without RuntimeHelpers.CreateSpan by collapsing Roslyn’s legacy lazy static cache pattern used for ReadOnlySpan<T> array literals, preventing unexpressible/undeclared <PrivateImplementationDetails> references in the generated C#.
Changes:
- Add
CachedReadOnlySpanInitializationblock transform to collapse the legacy lazy-cache initialization pattern. - Register the new transform immediately after
CachedDelegateInitializationin the IL transform pipeline. - Add an ILPretty regression test (
CachedReadOnlySpanInitialization) with expected C# output.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| ICSharpCode.Decompiler/IL/Transforms/CachedReadOnlySpanInitialization.cs | New block transform that recognizes and collapses the legacy lazy cache pattern for ReadOnlySpan<T> array literals. |
| ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs | Inserts the new transform after CachedDelegateInitialization in the per-block transform pipeline. |
| ICSharpCode.Decompiler.Tests/TestCases/ILPretty/CachedReadOnlySpanInitialization.il | New ILPretty input reproducing the legacy cached-array pattern (using <PrivateImplementationDetails>). |
| ICSharpCode.Decompiler.Tests/TestCases/ILPretty/CachedReadOnlySpanInitialization.cs | Expected decompiled C# output fixture for the new ILPretty test. |
| ICSharpCode.Decompiler.Tests/ILPrettyTestRunner.cs | Adds a new NUnit test entry to run the new ILPretty case. |
| ICSharpCode.Decompiler.Tests/ICSharpCode.Decompiler.Tests.csproj | Includes the new .il test input in the project. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
siegfriedpammer
requested changes
Jul 6, 2026
Member
|
@sailro I think I have to apologize to you, after some investigation I noticed that the legacy pattern with the cache field was in-fact the correct choice of test. I have re-added a test. Sorry again. |
… legacy lazy cache
Roslyn caches a ReadOnlySpan<T> created from an array literal in a
<PrivateImplementationDetails> field on target frameworks without
RuntimeHelpers.CreateSpan (e.g. .NET Framework / netstandard2.0 + System.Memory):
object obj = <PrivateImplementationDetails>.cache;
if (obj == null) {
obj = new char[] { '\r', '\n' };
<PrivateImplementationDetails>.cache = (char[])obj;
}
... new ReadOnlySpan<char>((char[])obj) ...
The decompiled output referenced the compiler-synthesized
<PrivateImplementationDetails> type, whose escaped name is not expressible in C#
and is never declared, so the output failed to recompile (CS0400).
The modern RuntimeHelpers.CreateSpan form was already handled
(TransformRuntimeHelpersCreateSpanInitialization); this adds the analogous
handling for the legacy lazy-cache form, mirroring CachedDelegateInitialization
(which collapses the same lazy-static-field cache for anonymous-method delegates).
Once the cache is collapsed, the existing array-initializer transforms recover
the array literal, so the <PrivateImplementationDetails> reference disappears.
Test: ILPretty/CachedReadOnlySpanInitialization.
siegfriedpammer
force-pushed
the
fix-readonlyspan-privateimpldetails-cache
branch
from
July 13, 2026 07:19
5b2b4cd to
29f68aa
Compare
siegfriedpammer
approved these changes
Jul 13, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #3850.
On target frameworks without
RuntimeHelpers.CreateSpan(.NET Framework, or netstandard2.0 + System.Memory), Roslyn caches the backing array of aReadOnlySpan<T>built from an array literal in a lazy<PrivateImplementationDetails>static field:The decompiled output referenced the compiler-synthesized
<PrivateImplementationDetails>type, which is never declared and whose name is not expressible in C#, so the output failed to recompile (CS0400 in whole-project output).Fix
A new
CachedReadOnlySpanInitializationblock transform collapses the lazy cache, mirroringCachedDelegateInitialization(which collapses the same lazy-static-field cache for anonymous-method delegates) and running right after it. Once the cache is collapsed, the existing array-initializer transforms recover the array literal, so the<PrivateImplementationDetails>reference disappears - the same result the modernRuntimeHelpers.CreateSpanpath (net7+) already produces.Before (does not recompile):
After:
The transform only fires on a compiler-generated static cache field with this exact lazy-init shape, so it does not touch the existing
<PrivateImplementationDetails>switch-on-string cache (covered by the existingCS1xSwitch_Debugtest) or user code.Test
ILPretty/CachedReadOnlySpanInitializationexercises the legacy lazy-cache IL and checks the recovered output.Real-world occurrence: Markdig
CodeInlineParser(net462).