Repository navigation
[Compiler] Fix NullReferenceException in SemanticModel queries on compound assignments - #2096
Merged
RobertvanderHulst merged 1 commit intoOct 6, 2026
Conversation
…pound assignments Two X#-specific blocks in the binder enumerate diagnostics.DiagnosticBag without a null check. When a public SemanticModel query binds, diagnostics is BindingDiagnosticBag.Discarded, whose DiagnosticBag is null. - Binder_Operators.cs, BindCompoundAssignment: the "clear 9020" block threw for every compound assignment (+=, -=, ...). - Binder_Expressions.cs, CheckVOIndexedValue: foreach over DiagnosticBag?.AsEnumerable() enumerated null (hardening, not observed). Both guards use diagnostics.AccumulatesDiagnostics. The guarded code only filters diagnostics, so xsc output is unchanged. In CheckVOIndexedValue the CheckValue binding result still runs; only the diagnostic filter is guarded. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
Two X#-specific blocks in the binder enumerate
diagnostics.DiagnosticBagwithout a null check. When a publicSemanticModelquery binds a member,diagnosticsisBindingDiagnosticBag.Discarded, whoseDiagnosticBagisnull.Binder_Operators.cs,BindCompoundAssignment: the "clear 9020" block throws. Result:GetSymbolInfo/GetTypeInfothrowNullReferenceExceptionfor the nodes of any statement that contains a compound assignment (+=,-=, ...). Measured downstream in a tool that queries the semantic model of a large VO solution: 36,421 failed semantic-model queries before, 94 after.Binder_Expressions.cs,CheckVOIndexedValue:foreach (... in diagnostics.DiagnosticBag?.AsEnumerable())enumeratesnull. Same pattern; not seen in practice, fixed for consistency (hardening).Both guards use
diagnostics.AccumulatesDiagnostics, the idiom already used inBinder_Operators.cs. The guarded code only filters diagnostics. With a real bag (everyxsccompile) the behavior is unchanged. InCheckVOIndexedValueonly the diagnostic filter is guarded; theCheckValuebinding result above it still runs.xscoutput is not affected. Analyzers, IDE features and other tools that use theSemanticModelAPI are.No existing issue found (searched for
SemanticModel NullReferenceException,Discarded DiagnosticBag,BindCompoundAssignment).Repro
There is no maintained test project that drives the public
SemanticModelAPI (XSTestCodeAnalysisis not part of any solution), so the repro is included here instead of as a unit test.Testing
Based on
dev@ae0ed9fdb3. Release build viasrc\buildcompiler.cmd Release, tests viaRunCompilertests.cmd.The 38 failing fixed tests fail on
devalready; the set of failing test IDs is identical before and after.🤖 Generated with Claude Code