Skip to content

Fix InvalidCastException in projectable constructors when a property name collides with a member-access selector name - #221

Draft
PhenX with Copilot wants to merge 3 commits into
masterfrom
copilot/fix-source-generator-invalid-cast
Draft

Fix InvalidCastException in projectable constructors when a property name collides with a member-access selector name#221
PhenX with Copilot wants to merge 3 commits into
masterfrom
copilot/fix-source-generator-invalid-cast

Conversation

Copilot AI commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

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 throw InvalidCastException and silently fail to emit source.

[Projectable]
public InventoryEntry(Item item, ItemCategory category) {
    Name = item.Name;
    CategoryName = category.Name; // crashed the generator
}

Root cause

ConstructorBodyConverter's syntax substitutors (ParameterSubstitutor, LocalVariableSubstitutor, AssignedPropertySubstitutor) override VisitIdentifierName to inline known parameters/locals/assigned-properties by name. None of them guarded against visiting the Name part of a MemberAccessExpressionSyntax (e.g. the Name in category.Name). Once Name had been assigned earlier in the body, AssignedPropertySubstitutor would replace that .Name selector with a ParenthesizedExpressionSyntax — invalid where a SimpleNameSyntax is required — triggering the cast exception when the tree was rebuilt.

Fix

  • Added VisitMemberAccessExpression overrides to all three substitutor classes so they only ever rewrite the target Expression of a member access, never the Name selector.
  • AssignedPropertySubstitutor still handles its intentional @this.PropName and bare-identifier substitution cases explicitly; only the unmatched fallback path was constrained.

Tests

  • Added ProjectableConstructor_MemberAccessNameCollidesWithAssignedPropertyName generator test reproducing the issue, with a verified snapshot confirming Name = item.Name, CategoryName = category.Name is emitted correctly.

Copilot AI linked an issue Sep 1, 2026 that may be closed by this pull request
Copilot AI and others added 2 commits September 1, 2026 19:44
… 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
Copilot AI requested a review from PhenX September 1, 2026 19:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Source generator fails with InvalidCastException

2 participants