Fix #3821: keep ref-struct conditional as if/return, not ?. / ?? (CS8978)#3823
Merged
siegfriedpammer merged 1 commit intoJun 29, 2026
Merged
Conversation
…. / ?? (CS8978) NullPropagationTransform rewrote `c != null ? c.AccessChain : default` to `c?.AccessChain ?? default` whenever the access-chain result was a non-nullable value type. For a by-ref-like type (a ref struct such as Span<T>) that form does not compile: a ref struct cannot be wrapped in Nullable<T> (CS8978). Exclude by-ref-like return types from the null-coalescing rewrite. Assisted-by: Copilot:claude-opus-4.8:GitHub Copilot CLI
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes an invalid null-propagation rewrite in NullPropagationTransform that produced uncompilable C# when the access-chain returns a by-ref-like value type (e.g., a ref struct such as Span<T>), addressing #3821.
Changes:
- Exclude by-ref-like return types from the
?./??null-coalescing rewrite that relies on nullable-lifting. - Add a correctness test case (guarded by
#if CS72) that returns aref structfromholder != null ? holder.Get() : defaultto ensure the decompiled output remains recompilable.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| ICSharpCode.Decompiler/IL/Transforms/NullPropagationTransform.cs | Adds a guard (!returnType.IsByRefLike) to prevent emitting ?./?? for by-ref-like return types that cannot be made nullable. |
| ICSharpCode.Decompiler.Tests/TestCases/Correctness/NullPropagation.cs | Adds a C# 7.2+ test case covering ref struct return values to prevent regressions. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Member
|
Thank you for your contribution! |
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 #3821.
NullPropagationTransformrewrotetestedVar != null ? testedVar.AccessChain : defaultto
testedVar?.AccessChain ?? defaultwhenever the access-chain result was anon-nullable value type. For a by-ref-like type (a ref struct such as
Span<T>)this does not compile: a ref struct cannot be wrapped in
Nullable<T>(CS8978).This excludes by-ref-like return types from the null-coalescing rewrite, leaving
the conditional as an
if/return, which compiles.Test
A
Correctness/NullPropagationcase returns a ref struct from aholder != null ? holder.Get() : defaultconditional (guarded#if CS72).Without the fix the decompiled
holder?.Get() ?? defaultfails to recompile(CS8978); with it the decompiled output recompiles and behaves identically.
Verified against a build with this change by decompiling a minimal assembly:
h != null ? h.GetSpan() : defaultnow decompiles to anif/return instead ofh?.GetSpan() ?? default(Span<byte>).