Skip to content

Fix misattributed type inference error span for index expressions#156885

Open
Dnreikronos wants to merge 1 commit into
rust-lang:mainfrom
Dnreikronos:fix/index-expr-ambiguous-type-span
Open

Fix misattributed type inference error span for index expressions#156885
Dnreikronos wants to merge 1 commit into
rust-lang:mainfrom
Dnreikronos:fix/index-expr-ambiguous-type-span

Conversation

@Dnreikronos
Copy link
Copy Markdown

@Dnreikronos Dnreikronos commented May 24, 2026

If arr[idx.into()] appears inside a cast or binary op, the type inference error gets blamed on the wrong expression.

Before:

  • [1, 2, 3][bad_idx.into()] as i32 → error points at [1, 2, 3][bad_idx.into()] (the whole indexing expr)
  • 0 + [1, 2, 3][bad_idx.into()] → error points at +

After:

  • Both cases point at .into(), which is where the ambiguity actually is.

In cast.rs, before resolving the cast expression type, we check if it's an index with an unresolved index type. If so, resolve the index sub-expression first at its own span so the error lands there. If that already errored, skip the broader resolution to avoid duplicates.

In op.rs, after writing the method call for an overloaded binary op, if the return type still has inference variables and the RHS is an index expression, force resolution of the index type at its span.

Fixes #156738

r? compiler

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 24, 2026
@rustbot

This comment has been minimized.

When an index expression with an ambiguous type (e.g. `arr[idx.into()]`)
appears inside a cast or binary operation, the type inference error was
incorrectly attributed to the outer expression instead of the `.into()`
call. Resolve the index sub-expression type first so the error points at
the actual ambiguous site.
@Dnreikronos Dnreikronos force-pushed the fix/index-expr-ambiguous-type-span branch from 09f69f4 to d061dcb Compare May 24, 2026 17:22
@mejrs
Copy link
Copy Markdown
Contributor

mejrs commented May 24, 2026

r? compiler

@rustbot rustbot assigned wesleywiser and unassigned mejrs May 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Misattribution of type inference location surrounding slice indices

4 participants