Skip to content

fix: parenthesize upcast when inlining a Use= mapping in a projection#2341

Merged
latonz merged 2 commits into
riok:mainfrom
Demivan:fix/2340-projection-cast-elision
Jul 6, 2026
Merged

fix: parenthesize upcast when inlining a Use= mapping in a projection#2341
latonz merged 2 commits into
riok:mainfrom
Demivan:fix/2340-projection-cast-elision

Conversation

@Demivan

@Demivan Demivan commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

Parenthesize upcast when inlining a Use= mapping in a queryable projection

Description

A queryable projection that inlines a Use= helper whose parameter is a supertype (interface or base class) of the projected source element generated an upcast without parentheses:

// source: x => new Target { Result = MapResult((IBase)x) }, inlined to:
Result = (IBase)x.Value      // Parses as (IBase)(x.Value)

Because a parameter identifier binds tighter than a member-access receiver, dropping the helper's body inline reattaches the cast to the wrong subtree. The result either fails to compile or binds the wrong member.

The inliner now parenthesizes a substituted cast when it lands in a receiver position:

Result = ((IBase)x).Value    // Correct

Fixes #2340

Checklist

  • I did not use AI tools to generate this PR, or I have manually verified that the code is correct, optimal, and follows the project guidelines and architecture
  • I understand that low-quality, AI-generated PRs will be closed immediately without further explanation
  • The existing code style is followed
  • The commit message follows our guidelines
  • Performed a self-review of my code
  • Hard-to-understand areas of my code are commented
  • The documentation is updated (as applicable)
  • Unit tests are added/updated
  • Integration tests are added/updated (as applicable, especially if feature/bug depends on roslyn or framework version in use)

Comment thread test/Riok.Mapperly.IntegrationTests/SupertypeProjectionMapperTest.cs Outdated
@latonz latonz added the bug Something isn't working label Jul 6, 2026
@latonz

latonz commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

Thanks for this fix, I added my feedback 😊

@latonz

latonz commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

Could you rebase onto main?

@Demivan
Demivan force-pushed the fix/2340-projection-cast-elision branch from 26c3b0d to 2c2a525 Compare July 6, 2026 09:21
@Demivan

Demivan commented Jul 6, 2026

Copy link
Copy Markdown
Contributor Author

Rebased it using GitHub rebase button

@latonz

latonz commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

Thank you for this contribution!

@latonz
latonz enabled auto-merge (squash) July 6, 2026 09:24
@latonz
latonz merged commit 2d893f6 into riok:main Jul 6, 2026
17 checks passed
@Demivan
Demivan deleted the fix/2340-projection-cast-elision branch July 6, 2026 17:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Queryable projection inlines Use= helper with supertype parameter using an unparenthesized cast (CS0030)

2 participants