Draft
Fix InvalidCastException in projectable constructors when a property name collides with a member-access selector name#221
Conversation
… collides with member access name Co-authored-by: PhenX <42170+PhenX@users.noreply.github.com>
Co-authored-by: PhenX <42170+PhenX@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix source generator failing with InvalidCastException
Fix InvalidCastException in projectable constructors when a property name collides with a member-access selector name
Sep 1, 2026
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.
Block-bodied
[Projectable]constructors that assign a property (e.g.Name) and later access a same-named member on an unrelated parameter (e.g.category.Name) caused the generator to throwInvalidCastExceptionand silently fail to emit source.Root cause
ConstructorBodyConverter's syntax substitutors (ParameterSubstitutor,LocalVariableSubstitutor,AssignedPropertySubstitutor) overrideVisitIdentifierNameto inline known parameters/locals/assigned-properties by name. None of them guarded against visiting theNamepart of aMemberAccessExpressionSyntax(e.g. theNameincategory.Name). OnceNamehad been assigned earlier in the body,AssignedPropertySubstitutorwould replace that.Nameselector with aParenthesizedExpressionSyntax— invalid where aSimpleNameSyntaxis required — triggering the cast exception when the tree was rebuilt.Fix
VisitMemberAccessExpressionoverrides to all three substitutor classes so they only ever rewrite the targetExpressionof a member access, never theNameselector.AssignedPropertySubstitutorstill handles its intentional@this.PropNameand bare-identifier substitution cases explicitly; only the unmatched fallback path was constrained.Tests
ProjectableConstructor_MemberAccessNameCollidesWithAssignedPropertyNamegenerator test reproducing the issue, with a verified snapshot confirmingName = item.Name, CategoryName = category.Nameis emitted correctly.