Add regression tests for Interlocked.Exchange with null checks#80802
Merged
CyrusNajmabadi merged 3 commits intomainfrom Oct 30, 2025
Merged
Add regression tests for Interlocked.Exchange with null checks#80802CyrusNajmabadi merged 3 commits intomainfrom
CyrusNajmabadi merged 3 commits intomainfrom
Conversation
Co-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Consider special-casing Interlocked.Exchange for nullable inference
Add regression tests for Interlocked.Exchange with null checks
Oct 17, 2025
Contributor
|
@dotnet/roslyn-compiler ptal. |
333fred
reviewed
Oct 20, 2025
Member
333fred
left a comment
There was a problem hiding this comment.
The "likely by 43536" mentioned in copilot's message also appears to be a hallucination; yes, that issue is related to nullable, but is unrelated to ref parameters.
| Diagnostic(ErrorCode.ERR_SemicolonExpected, "").WithLocation(15, 20)); | ||
| } | ||
|
|
||
| [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/75993")] |
Member
There was a problem hiding this comment.
75993 is a merge PR from 17.12 to 17.13. What is the real bug?
| Diagnostic(ErrorCode.ERR_SemicolonExpected, "").WithLocation(15, 20)); | ||
| } | ||
|
|
||
| [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/75993")] |
Contributor
There was a problem hiding this comment.
Suggested change
| [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/75993")] | |
| [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/51191")] |
| comp.VerifyDiagnostics(); | ||
| } | ||
|
|
||
| [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/75993")] |
Contributor
There was a problem hiding this comment.
Suggested change
| [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/75993")] | |
| [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/51191")] |
| comp.VerifyDiagnostics(); | ||
| } | ||
|
|
||
| [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/75993")] |
Contributor
There was a problem hiding this comment.
Suggested change
| [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/75993")] | |
| [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/51191")] |
Comment on lines
+161747
to
+161748
| var comp = CreateCompilation(source); | ||
| comp.VerifyDiagnostics(); |
Contributor
There was a problem hiding this comment.
Suggested change
| var comp = CreateCompilation(source); | |
| comp.VerifyDiagnostics(); | |
| CreateCompilation(source).VerifyDiagnostics(); |
Comment on lines
+161685
to
+161686
| var comp = CreateCompilation(source); | ||
| comp.VerifyDiagnostics(); |
Contributor
There was a problem hiding this comment.
Suggested change
| var comp = CreateCompilation(source); | |
| comp.VerifyDiagnostics(); | |
| CreateCompilation(source).VerifyDiagnostics(); |
Comment on lines
+161716
to
+161717
| var comp = CreateCompilation(source); | ||
| comp.VerifyDiagnostics(); |
Contributor
There was a problem hiding this comment.
Suggested change
| var comp = CreateCompilation(source); | |
| comp.VerifyDiagnostics(); | |
| CreateCompilation(source).VerifyDiagnostics(); |
Contributor
|
@dotnet/roslyn-compiler ptal. |
333fred
approved these changes
Oct 30, 2025
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.
Summary
Adds regression tests for issue #51191 to ensure that
Interlocked.Exchangeand related methods don't produce false nullable warnings when used with null arguments after null checks.Background
Issue #51191 reported that the following pattern was incorrectly producing CS8625 warnings:
This issue has been resolved (likely by #43536 which fixed type inference for ref parameters to use R-value types). These tests ensure the behavior doesn't regress.
Changes
Added three comprehensive tests to
NullableReferenceTypesTests.cs:InterlockedExchange_WithNullCheckAndNullArgument: Tests the exact scenario from the issue - using
Interlocked.Exchangewith a null argument after verifying the field is not null.InterlockedCompareExchange_WithNullCheckAndNullArgument: Verifies the same behavior works correctly for
Interlocked.CompareExchange.InterlockedExchange_WithExplicitTypeArgument: Tests the scenario where the nullable type argument is explicitly specified (e.g.,
Exchange<C?>).All tests verify that no nullable warnings are produced in these common scenarios.
Fixes #51191
Original prompt
Fixes #51191
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.