Keep instance-dependent assignments in constructors - #3980
Open
sailro wants to merge 2 commits into
Open
Conversation
A moved field initializer cannot read another instance member. Reject primary-constructor conversion for that case and preserve the original constructor. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 0dd407b6-9410-48df-add5-761ca4a8dec0
The guard added here reads the target of a member access to decide whether an assignment may move into a field initializer, and it recognises the current instance as a ThisResolveResult. Only one of the two spellings produces that. An unqualified `A` is resolved through CSharpResolver.LookInCurrentType, which synthesizes the target as a this-reference; an explicit `this.A` is built by ExpressionBuilder, whose TranslateTarget hands back whatever ConvertVariable produced - and `this` is a parameter like any other there, so the target is an ILVariableResolveResult. The guard saw the first and missed the second. Which spelling appears is decided by RequiresQualifier, for reasons unrelated to the question being asked: a constructor parameter that shadows the field forces the qualifier, and AlwaysQualifyMemberReferences forces it everywhere. So the transform hoisted `b = this.value + 1` into a field initializer, where naming the instance is CS0027 and the output does not compile. TranslateTarget already builds a ThisResolveResult for `base`, one branch above. Doing the same for `this` leaves the guard untouched and makes it see both spellings, and spares every future consumer the same trap. The type is carried over from the previous resolve result, so nothing downstream observes a different one - the this/base keyword links read exactly this node. Fixes icsharpcode#3984. Assisted-by: Claude:claude-opus-5:Claude Code
siegfriedpammer
force-pushed
the
fix-primary-constructor-field-dependency
branch
from
August 12, 2026 07:24
c06a8ab to
13c6cc6
Compare
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.
tl;dr: Do not introduce a primary constructor when a moved initializer depends on the instance.
Fixes #3979
Problem
TransformFieldAndConstructorInitializerscan promote a constructor even when a later assignment reads an instance member initialized by an earlier assignment.Moving both assignments to field initializers produces CS0236.
Solution
Reject primary-constructor conversion when a candidate initializer references
thisor a non-generated instance member throughthis.