Fix #3824: fold a hoisted argument null-guard into the chained ctor call#3829
Open
sailro wants to merge 1 commit into
Open
Fix #3824: fold a hoisted argument null-guard into the chained ctor call#3829sailro wants to merge 1 commit into
sailro wants to merge 1 commit into
Conversation
…ined ctor call When an initializer argument contains an argument-validation null-check (e.g. `arg ?? throw new ArgumentNullException(...)`) and the argument is used more than once, the compiler hoists the null-check in front of the chained constructor call. MoveConstructorInitializer only inspected the first body statement, so the guard defeated it and the chained call was left as an illegal in-body `this..ctor(...)` / `base..ctor(...)`. Skip leading argument null-guards (if (param == null) throw ...;) when locating the chained call, and fold each guard back into the first argument that uses the parameter as `param ?? throw ...`. If a guard cannot be folded, the constructor is left unchanged. Assisted-by: Copilot:claude-opus-4.8:GitHub Copilot CLI
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 #3824.
When a constructor initializer argument contains argument-validation that throws
(e.g.
arg ?? throw new ArgumentNullException(...)) and the argument is used bymore than one parameter, the compiler hoists the null-check in front of the
chained constructor call.
MoveConstructorInitializeronly inspected the firstbody statement, so the hoisted guard defeated it and the chained call was left as
an illegal in-body
this..ctor(...)/base..ctor(...)statement (a parse error).Fix
When locating the chained call, skip leading argument null-guards
(
if (param == null) throw ...;) and fold each one back into the first argumentthat uses the parameter, as
param ?? throw .... If a guard cannot be folded (itsparameter is not used by any argument), the constructor is left unchanged.
Before:
After:
Test
Pretty/ConstructorInitializersgains a#if CS70case where the null-check ishoisted because the parameter is used by more than one argument; it round-trips to
the folded initializer form.
Real-world occurrence: IbanNet
PatternToken(string)(net462).