Skip to content

Fix misattributed type inference error span for index expressions#156885

Merged
rust-bors[bot] merged 2 commits into
rust-lang:mainfrom
Dnreikronos:fix/index-expr-ambiguous-type-span
Jun 25, 2026
Merged

Fix misattributed type inference error span for index expressions#156885
rust-bors[bot] merged 2 commits into
rust-lang:mainfrom
Dnreikronos:fix/index-expr-ambiguous-type-span

Conversation

@Dnreikronos

@Dnreikronos Dnreikronos commented May 24, 2026

Copy link
Copy Markdown
Contributor

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

mejrs commented May 24, 2026

Copy link
Copy Markdown
Contributor

r? compiler

@rustbot rustbot assigned wesleywiser and unassigned mejrs May 24, 2026
@Enselic

Enselic commented Jun 13, 2026

Copy link
Copy Markdown
Member

which is where the ambiguity actually is

Can you explain how you reached that conclusion please? It will make it easier to review this PR. Thanks!

@Dnreikronos

Copy link
Copy Markdown
Contributor Author

sure. afaict everything that's unresolved here traces back to one variable: the return type of .into().

Into::into is just fn into(self) -> T, so bad_idx.into() spins up a fresh ?T plus the obligation u8: Into<?T>. nothing pins ?T down. that's the ambiguity, and it's exactly what the standalone case prints: the type must implement From<u8> / required for u8 to implement Into<_>.

that bare ?T is still the index expr's type by the time the cast/binop code runs, which is the whole reason the patch keys on idx_ty.is_ty_var() rather than the looser has_infer() on the outer expression. the var is the cause. everything around it is only unresolved because of it.

what actually convinced me it's the right span: plain [1, 2, 3][bad_idx.into()], no cast and no +, already points at .into() today. the cast and binop cases bottom out at the same stuck ?T, they just trip a wider-span resolution first that fires before we reach the index. fwiw the existing suggestion already agrees with this, it rewrites bad_idx.into() into <u8 as Into<T>>::into(bad_idx).

one wrinkle: forcing the index span through structurally_resolve_type always comes out as E0282, so cast/binop land on E0282 while standalone keeps its E0283 and the From<u8> note. same span now, different code. can look at lining those up if it bugs you.

@wesleywiser

Copy link
Copy Markdown
Member

r? compiler

@rustbot rustbot assigned BoxyUwU and unassigned wesleywiser Jun 15, 2026
@BoxyUwU

BoxyUwU commented Jun 19, 2026

Copy link
Copy Markdown
Member

This change feels somewhat dubious to me, you're introducing additional calls to structurally_resolve_type in places that previously would have succeeded fine. I expect this would actually cause inference to stop succeeding in some weird edge cases which seems undesirable 🤔

I'm not sure what the right way to do this diagnostic improvement is though

@Dnreikronos

Dnreikronos commented Jun 19, 2026

Copy link
Copy Markdown
Contributor Author

@BoxyUwU

yeah, i think you're right. i was looking at this as mostly span routing, but structurally_resolve_type is not passive. it can select obligations, emit e0282, and then relate the unresolved var to an error type, so this can change inference behavior and not just where the diagnostic points.

the binop bit is the one i like least, tbh. it adds a new force point while overloaded binop return handling is still going. the cast case is less bad since cast checks already run after fallback, but it still changes the order by resolving the index operand before the existing cast operand resolve.

imo i should back this version out and avoid resolving idx_ty directly. i'd rather keep the existing resolution points and only adjust the cause/span when the unresolved expression is an index and the index operand is the ambiguous part. the current indexing fulfillment-error span adjustment looks like the first place i'd poke at.

i'll try that direction. also going to see if i can minimize a case that would have kept compiling without the early index resolve, since idk how convincing this is without a regression test.

Adjust ambiguous index diagnostics without adding new index operand resolution points. Keep invalid operator and mismatched operand diagnostics on the operator while still pointing valid ambiguous index cases at the index operand.
@Dnreikronos

Dnreikronos commented Jun 19, 2026

Copy link
Copy Markdown
Contributor Author

@BoxyUwU pushed an update.

i kept the same basic reasoning from my earlier comment, but changed the implementation after your point about structurally_resolve_type.

the patch no longer force-resolves the index operand type. in cast.rs, it only changes the span used by the existing cast-expression resolution when the outer index expr and the index operand are both still plain inference vars.

for binops, i moved this into the final fulfillment-error adjustment path instead of doing anything during operator lookup. so it only changes the diagnostic span after we already have an ambiguity. it also checks the surrounding operator shape before blaming .into().

i added a few regression cases too, mostly because this was where the first pass got too broad. the plain 0 + [1, 2, 3][bad_idx.into()] case still lands on .into(), but bad operator shapes like true + ... and 0u64 + [1i32, ...][bad_idx.into()] stay on the operator. shifts are covered as well with 1u32 << [0u8][bad_idx.into()], since the rhs does not have to be the same type there. i also kept a String + &str-ish case as a sanity check.

one tradeoff is that the cast/binop cases do not keep the old fq-path help from the force-resolution version. imo that is better than adding a new resolution point just to get a nicer suggestion.

@BoxyUwU

BoxyUwU commented Jun 23, 2026

Copy link
Copy Markdown
Member

@bors r+

thanks :D

@rust-bors

rust-bors Bot commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

📌 Commit a09fc01 has been approved by BoxyUwU

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 23, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Jun 23, 2026
…us-type-span, r=BoxyUwU

Fix misattributed type inference error span for index expressions

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 rust-lang#156738

r? compiler
rust-bors Bot pushed a commit that referenced this pull request Jun 23, 2026
…uwer

Rollup of 16 pull requests

Successful merges:

 - #156885 (Fix misattributed type inference error span for index expressions)
 - #157271 (simplify some `proc_macro` things)
 - #157883 (Remove strict invariant node_type on hir_type during ty privacy visit)
 - #157921 (trait solver: Resolve region vars in max universe)
 - #157960 (delegation: add support for infers in generics)
 - #158105 (Extract all instance shim variants into new `ShimKind` enum)
 - #158207 (Resolver: local/external split of  `resolve_ident_in_module_non_globs_unadjusted` )
 - #158279 (Follow goto and drop when linting unreachable code)
 - #157807 (don't ice on non-lifetime binders under `-Zassumptions-on-binders`)
 - #158020 (Update mingw-w64 C toolchain)
 - #158222 (format: ignore println newline in foreign format hints)
 - #158223 (Move target checking for #[lang] to the attribute parser)
 - #158252 (Use `cfg_select` in `std::os`)
 - #158257 ( fix escaping placeholder check in next solver normalization folder)
 - #158274 (triagebot: Stop pinging myself)
 - #158282 (slice_split_once: bounds check optimization note)

Failed merges:

 - #158256 (Avoid parser panics bubbling out to proc macros)
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Jun 23, 2026
…us-type-span, r=BoxyUwU

Fix misattributed type inference error span for index expressions

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 rust-lang#156738

r? compiler
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Jun 23, 2026
…us-type-span, r=BoxyUwU

Fix misattributed type inference error span for index expressions

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 rust-lang#156738

r? compiler
rust-bors Bot pushed a commit that referenced this pull request Jun 23, 2026
…uwer

Rollup of 23 pull requests

Successful merges:

 - #158315 (`rust-analyzer` subtree update)
 - #155739 (Add temporary scope to assert_eq and assert_ne)
 - #156885 (Fix misattributed type inference error span for index expressions)
 - #157271 (simplify some `proc_macro` things)
 - #157883 (Remove strict invariant node_type on hir_type during ty privacy visit)
 - #157921 (trait solver: Resolve region vars in max universe)
 - #157960 (delegation: add support for infers in generics)
 - #157983 (Lift the same-signature restriction for `extern "tail"`)
 - #158105 (Extract all instance shim variants into new `ShimKind` enum)
 - #158207 (Resolver: local/external split of  `resolve_ident_in_module_non_globs_unadjusted` )
 - #158279 (Follow goto and drop when linting unreachable code)
 - #157527 (Move derive tests into their dedicated folder)
 - #157807 (don't ice on non-lifetime binders under `-Zassumptions-on-binders`)
 - #158020 (Update mingw-w64 C toolchain)
 - #158222 (format: ignore println newline in foreign format hints)
 - #158223 (Move target checking for #[lang] to the attribute parser)
 - #158252 (Use `cfg_select` in `std::os`)
 - #158257 ( fix escaping placeholder check in next solver normalization folder)
 - #158263 (Only load the feature list once in the entire resolver)
 - #158274 (triagebot: Stop pinging myself)
 - #158282 (slice_split_once: bounds check optimization note)
 - #158300 (Improve unknown crate_type diagnostic suggestions)
 - #158304 (mailmap: update mu001999)

Failed merges:

 - #158256 (Avoid parser panics bubbling out to proc macros)
jhpratt added a commit to jhpratt/rust that referenced this pull request Jun 24, 2026
…us-type-span, r=BoxyUwU

Fix misattributed type inference error span for index expressions

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 rust-lang#156738

r? compiler
rust-bors Bot pushed a commit that referenced this pull request Jun 24, 2026
Rollup of 27 pull requests

Successful merges:

 - #158315 (`rust-analyzer` subtree update)
 - #155739 (Add temporary scope to assert_eq and assert_ne)
 - #156885 (Fix misattributed type inference error span for index expressions)
 - #157271 (simplify some `proc_macro` things)
 - #157883 (Remove strict invariant node_type on hir_type during ty privacy visit)
 - #157921 (trait solver: Resolve region vars in max universe)
 - #157960 (delegation: add support for infers in generics)
 - #157983 (Lift the same-signature restriction for `extern "tail"`)
 - #158053 (Optimize network address parser)
 - #158105 (Extract all instance shim variants into new `ShimKind` enum)
 - #158207 (Resolver: local/external split of  `resolve_ident_in_module_non_globs_unadjusted` )
 - #158279 (Follow goto and drop when linting unreachable code)
 - #157527 (Move derive tests into their dedicated folder)
 - #157807 (don't ice on non-lifetime binders under `-Zassumptions-on-binders`)
 - #158020 (Update mingw-w64 C toolchain)
 - #158039 (c-variadic: test that we use equality up to free lifetimes)
 - #158222 (format: ignore println newline in foreign format hints)
 - #158223 (Move target checking for #[lang] to the attribute parser)
 - #158252 (Use `cfg_select` in `std::os`)
 - #158257 ( fix escaping placeholder check in next solver normalization folder)
 - #158263 (Only load the feature list once in the entire resolver)
 - #158267 (FromUtf8Error::into_utf8_lossy better example and suggest use)
 - #158274 (triagebot: Stop pinging myself)
 - #158282 (slice_split_once: bounds check optimization note)
 - #158300 (Improve unknown crate_type diagnostic suggestions)
 - #158304 (mailmap: update mu001999)
 - #158309 (Update `rustc-literal-escaper` version to `0.0.8`)

Failed merges:

 - #158256 (Avoid parser panics bubbling out to proc macros)
jhpratt added a commit to jhpratt/rust that referenced this pull request Jun 24, 2026
…us-type-span, r=BoxyUwU

Fix misattributed type inference error span for index expressions

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 rust-lang#156738

r? compiler
rust-bors Bot pushed a commit that referenced this pull request Jun 24, 2026
Rollup of 35 pull requests

Successful merges:

 - #158315 (`rust-analyzer` subtree update)
 - #158336 (Stop excluding `stdarch` test crates from `rust-src`)
 - #155739 (Add temporary scope to assert_eq and assert_ne)
 - #156885 (Fix misattributed type inference error span for index expressions)
 - #157271 (simplify some `proc_macro` things)
 - #157419 (move rustc_type_ir Term things to term_kind.rs)
 - #157883 (Remove strict invariant node_type on hir_type during ty privacy visit)
 - #157921 (trait solver: Resolve region vars in max universe)
 - #157960 (delegation: add support for infers in generics)
 - #157983 (Lift the same-signature restriction for `extern "tail"`)
 - #158053 (Optimize network address parser)
 - #158105 (Extract all instance shim variants into new `ShimKind` enum)
 - #158207 (Resolver: local/external split of  `resolve_ident_in_module_non_globs_unadjusted` )
 - #158279 (Follow goto and drop when linting unreachable code)
 - #157527 (Move derive tests into their dedicated folder)
 - #157807 (don't ice on non-lifetime binders under `-Zassumptions-on-binders`)
 - #157939 (Reorganize `tests/ui/issues` [8/N])
 - #157946 (Make `char::is_private_use` and `char::is_assigned` unstably public)
 - #158003 (Reorganize `tests/ui/issues` [9/N])
 - #158020 (Update mingw-w64 C toolchain)
 - #158039 (c-variadic: test that we use equality up to free lifetimes)
 - #158060 (Reorganize `tests/ui/issues` [10/N])
 - #158222 (format: ignore println newline in foreign format hints)
 - #158223 (Move target checking for #[lang] to the attribute parser)
 - #158252 (Use `cfg_select` in `std::os`)
 - #158263 (Only load the feature list once in the entire resolver)
 - #158267 (FromUtf8Error::into_utf8_lossy better example and suggest use)
 - #158272 (Reorganize `tests/ui/issues` [13/N])
 - #158274 (triagebot: Stop pinging myself)
 - #158282 (slice_split_once: bounds check optimization note)
 - #158300 (Improve unknown crate_type diagnostic suggestions)
 - #158304 (mailmap: update mu001999)
 - #158309 (Update `rustc-literal-escaper` version to `0.0.8`)
 - #158314 (Fix incorrect unsafe debug assertion in unchecked_div_exact)
 - #158326 (Add `io::ErrorKind::TooManyOpenFiles`)
rust-bors Bot pushed a commit that referenced this pull request Jun 24, 2026
Rollup of 35 pull requests

Successful merges:

 - #158315 (`rust-analyzer` subtree update)
 - #158336 (Stop excluding `stdarch` test crates from `rust-src`)
 - #155739 (Add temporary scope to assert_eq and assert_ne)
 - #156885 (Fix misattributed type inference error span for index expressions)
 - #157271 (simplify some `proc_macro` things)
 - #157419 (move rustc_type_ir Term things to term_kind.rs)
 - #157883 (Remove strict invariant node_type on hir_type during ty privacy visit)
 - #157921 (trait solver: Resolve region vars in max universe)
 - #157960 (delegation: add support for infers in generics)
 - #157983 (Lift the same-signature restriction for `extern "tail"`)
 - #158053 (Optimize network address parser)
 - #158105 (Extract all instance shim variants into new `ShimKind` enum)
 - #158207 (Resolver: local/external split of  `resolve_ident_in_module_non_globs_unadjusted` )
 - #158279 (Follow goto and drop when linting unreachable code)
 - #157527 (Move derive tests into their dedicated folder)
 - #157807 (don't ice on non-lifetime binders under `-Zassumptions-on-binders`)
 - #157939 (Reorganize `tests/ui/issues` [8/N])
 - #157946 (Make `char::is_private_use` and `char::is_assigned` unstably public)
 - #158003 (Reorganize `tests/ui/issues` [9/N])
 - #158020 (Update mingw-w64 C toolchain)
 - #158039 (c-variadic: test that we use equality up to free lifetimes)
 - #158060 (Reorganize `tests/ui/issues` [10/N])
 - #158222 (format: ignore println newline in foreign format hints)
 - #158223 (Move target checking for #[lang] to the attribute parser)
 - #158252 (Use `cfg_select` in `std::os`)
 - #158263 (Only load the feature list once in the entire resolver)
 - #158267 (FromUtf8Error::into_utf8_lossy better example and suggest use)
 - #158272 (Reorganize `tests/ui/issues` [13/N])
 - #158274 (triagebot: Stop pinging myself)
 - #158282 (slice_split_once: bounds check optimization note)
 - #158300 (Improve unknown crate_type diagnostic suggestions)
 - #158304 (mailmap: update mu001999)
 - #158309 (Update `rustc-literal-escaper` version to `0.0.8`)
 - #158314 (Fix incorrect unsafe debug assertion in unchecked_div_exact)
 - #158326 (Add `io::ErrorKind::TooManyOpenFiles`)
rust-bors Bot pushed a commit that referenced this pull request Jun 24, 2026
Rollup of 35 pull requests

Successful merges:

 - #158315 (`rust-analyzer` subtree update)
 - #158336 (Stop excluding `stdarch` test crates from `rust-src`)
 - #155739 (Add temporary scope to assert_eq and assert_ne)
 - #156885 (Fix misattributed type inference error span for index expressions)
 - #157271 (simplify some `proc_macro` things)
 - #157419 (move rustc_type_ir Term things to term_kind.rs)
 - #157883 (Remove strict invariant node_type on hir_type during ty privacy visit)
 - #157921 (trait solver: Resolve region vars in max universe)
 - #157960 (delegation: add support for infers in generics)
 - #157983 (Lift the same-signature restriction for `extern "tail"`)
 - #158053 (Optimize network address parser)
 - #158105 (Extract all instance shim variants into new `ShimKind` enum)
 - #158207 (Resolver: local/external split of  `resolve_ident_in_module_non_globs_unadjusted` )
 - #158279 (Follow goto and drop when linting unreachable code)
 - #157527 (Move derive tests into their dedicated folder)
 - #157807 (don't ice on non-lifetime binders under `-Zassumptions-on-binders`)
 - #157939 (Reorganize `tests/ui/issues` [8/N])
 - #157946 (Make `char::is_private_use` and `char::is_assigned` unstably public)
 - #158003 (Reorganize `tests/ui/issues` [9/N])
 - #158020 (Update mingw-w64 C toolchain)
 - #158039 (c-variadic: test that we use equality up to free lifetimes)
 - #158060 (Reorganize `tests/ui/issues` [10/N])
 - #158222 (format: ignore println newline in foreign format hints)
 - #158223 (Move target checking for #[lang] to the attribute parser)
 - #158252 (Use `cfg_select` in `std::os`)
 - #158263 (Only load the feature list once in the entire resolver)
 - #158267 (FromUtf8Error::into_utf8_lossy better example and suggest use)
 - #158272 (Reorganize `tests/ui/issues` [13/N])
 - #158274 (triagebot: Stop pinging myself)
 - #158282 (slice_split_once: bounds check optimization note)
 - #158300 (Improve unknown crate_type diagnostic suggestions)
 - #158304 (mailmap: update mu001999)
 - #158309 (Update `rustc-literal-escaper` version to `0.0.8`)
 - #158314 (Fix incorrect unsafe debug assertion in unchecked_div_exact)
 - #158326 (Add `io::ErrorKind::TooManyOpenFiles`)
rust-bors Bot pushed a commit that referenced this pull request Jun 24, 2026
Rollup of 35 pull requests

Successful merges:

 - #158315 (`rust-analyzer` subtree update)
 - #158336 (Stop excluding `stdarch` test crates from `rust-src`)
 - #155739 (Add temporary scope to assert_eq and assert_ne)
 - #156885 (Fix misattributed type inference error span for index expressions)
 - #157271 (simplify some `proc_macro` things)
 - #157419 (move rustc_type_ir Term things to term_kind.rs)
 - #157883 (Remove strict invariant node_type on hir_type during ty privacy visit)
 - #157921 (trait solver: Resolve region vars in max universe)
 - #157960 (delegation: add support for infers in generics)
 - #157983 (Lift the same-signature restriction for `extern "tail"`)
 - #158053 (Optimize network address parser)
 - #158105 (Extract all instance shim variants into new `ShimKind` enum)
 - #158207 (Resolver: local/external split of  `resolve_ident_in_module_non_globs_unadjusted` )
 - #158279 (Follow goto and drop when linting unreachable code)
 - #157527 (Move derive tests into their dedicated folder)
 - #157807 (don't ice on non-lifetime binders under `-Zassumptions-on-binders`)
 - #157939 (Reorganize `tests/ui/issues` [8/N])
 - #157946 (Make `char::is_private_use` and `char::is_assigned` unstably public)
 - #158003 (Reorganize `tests/ui/issues` [9/N])
 - #158020 (Update mingw-w64 C toolchain)
 - #158039 (c-variadic: test that we use equality up to free lifetimes)
 - #158060 (Reorganize `tests/ui/issues` [10/N])
 - #158222 (format: ignore println newline in foreign format hints)
 - #158223 (Move target checking for #[lang] to the attribute parser)
 - #158252 (Use `cfg_select` in `std::os`)
 - #158263 (Only load the feature list once in the entire resolver)
 - #158267 (FromUtf8Error::into_utf8_lossy better example and suggest use)
 - #158272 (Reorganize `tests/ui/issues` [13/N])
 - #158274 (triagebot: Stop pinging myself)
 - #158282 (slice_split_once: bounds check optimization note)
 - #158300 (Improve unknown crate_type diagnostic suggestions)
 - #158304 (mailmap: update mu001999)
 - #158309 (Update `rustc-literal-escaper` version to `0.0.8`)
 - #158314 (Fix incorrect unsafe debug assertion in unchecked_div_exact)
 - #158326 (Add `io::ErrorKind::TooManyOpenFiles`)
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Jun 24, 2026
…us-type-span, r=BoxyUwU

Fix misattributed type inference error span for index expressions

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 rust-lang#156738

r? compiler
rust-bors Bot pushed a commit that referenced this pull request Jun 24, 2026
Rollup of 35 pull requests



Successful merges:

 - #158315 (`rust-analyzer` subtree update)
 - #158336 (Stop excluding `stdarch` test crates from `rust-src`)
 - #155739 (Add temporary scope to assert_eq and assert_ne)
 - #156885 (Fix misattributed type inference error span for index expressions)
 - #157271 (simplify some `proc_macro` things)
 - #157419 (move rustc_type_ir Term things to term_kind.rs)
 - #157883 (Remove strict invariant node_type on hir_type during ty privacy visit)
 - #157921 (trait solver: Resolve region vars in max universe)
 - #157960 (delegation: add support for infers in generics)
 - #157983 (Lift the same-signature restriction for `extern "tail"`)
 - #158053 (Optimize network address parser)
 - #158105 (Extract all instance shim variants into new `ShimKind` enum)
 - #158207 (Resolver: local/external split of  `resolve_ident_in_module_non_globs_unadjusted` )
 - #158279 (Follow goto and drop when linting unreachable code)
 - #157527 (Move derive tests into their dedicated folder)
 - #157807 (don't ice on non-lifetime binders under `-Zassumptions-on-binders`)
 - #157939 (Reorganize `tests/ui/issues` [8/N])
 - #157946 (Make `char::is_private_use` and `char::is_assigned` unstably public)
 - #158003 (Reorganize `tests/ui/issues` [9/N])
 - #158020 (Update mingw-w64 C toolchain)
 - #158039 (c-variadic: test that we use equality up to free lifetimes)
 - #158060 (Reorganize `tests/ui/issues` [10/N])
 - #158222 (format: ignore println newline in foreign format hints)
 - #158223 (Move target checking for #[lang] to the attribute parser)
 - #158252 (Use `cfg_select` in `std::os`)
 - #158263 (Only load the feature list once in the entire resolver)
 - #158267 (FromUtf8Error::into_utf8_lossy better example and suggest use)
 - #158272 (Reorganize `tests/ui/issues` [13/N])
 - #158274 (triagebot: Stop pinging myself)
 - #158282 (slice_split_once: bounds check optimization note)
 - #158300 (Improve unknown crate_type diagnostic suggestions)
 - #158304 (mailmap: update mu001999)
 - #158309 (Update `rustc-literal-escaper` version to `0.0.8`)
 - #158314 (Fix incorrect unsafe debug assertion in unchecked_div_exact)
 - #158326 (Add `io::ErrorKind::TooManyOpenFiles`)
rust-bors Bot pushed a commit that referenced this pull request Jun 25, 2026
Rollup of 35 pull requests



Successful merges:

 - #158315 (`rust-analyzer` subtree update)
 - #158336 (Stop excluding `stdarch` test crates from `rust-src`)
 - #155739 (Add temporary scope to assert_eq and assert_ne)
 - #156885 (Fix misattributed type inference error span for index expressions)
 - #157271 (simplify some `proc_macro` things)
 - #157419 (move rustc_type_ir Term things to term_kind.rs)
 - #157883 (Remove strict invariant node_type on hir_type during ty privacy visit)
 - #157921 (trait solver: Resolve region vars in max universe)
 - #157960 (delegation: add support for infers in generics)
 - #157983 (Lift the same-signature restriction for `extern "tail"`)
 - #158053 (Optimize network address parser)
 - #158105 (Extract all instance shim variants into new `ShimKind` enum)
 - #158207 (Resolver: local/external split of  `resolve_ident_in_module_non_globs_unadjusted` )
 - #158279 (Follow goto and drop when linting unreachable code)
 - #157527 (Move derive tests into their dedicated folder)
 - #157807 (don't ice on non-lifetime binders under `-Zassumptions-on-binders`)
 - #157939 (Reorganize `tests/ui/issues` [8/N])
 - #157946 (Make `char::is_private_use` and `char::is_assigned` unstably public)
 - #158003 (Reorganize `tests/ui/issues` [9/N])
 - #158020 (Update mingw-w64 C toolchain)
 - #158039 (c-variadic: test that we use equality up to free lifetimes)
 - #158060 (Reorganize `tests/ui/issues` [10/N])
 - #158222 (format: ignore println newline in foreign format hints)
 - #158223 (Move target checking for #[lang] to the attribute parser)
 - #158252 (Use `cfg_select` in `std::os`)
 - #158263 (Only load the feature list once in the entire resolver)
 - #158267 (FromUtf8Error::into_utf8_lossy better example and suggest use)
 - #158272 (Reorganize `tests/ui/issues` [13/N])
 - #158274 (triagebot: Stop pinging myself)
 - #158282 (slice_split_once: bounds check optimization note)
 - #158300 (Improve unknown crate_type diagnostic suggestions)
 - #158304 (mailmap: update mu001999)
 - #158309 (Update `rustc-literal-escaper` version to `0.0.8`)
 - #158314 (Fix incorrect unsafe debug assertion in unchecked_div_exact)
 - #158326 (Add `io::ErrorKind::TooManyOpenFiles`)
rust-bors Bot pushed a commit that referenced this pull request Jun 25, 2026
Rollup of 35 pull requests



Successful merges:

 - #158315 (`rust-analyzer` subtree update)
 - #158336 (Stop excluding `stdarch` test crates from `rust-src`)
 - #155739 (Add temporary scope to assert_eq and assert_ne)
 - #156885 (Fix misattributed type inference error span for index expressions)
 - #157271 (simplify some `proc_macro` things)
 - #157419 (move rustc_type_ir Term things to term_kind.rs)
 - #157883 (Remove strict invariant node_type on hir_type during ty privacy visit)
 - #157921 (trait solver: Resolve region vars in max universe)
 - #157960 (delegation: add support for infers in generics)
 - #157983 (Lift the same-signature restriction for `extern "tail"`)
 - #158053 (Optimize network address parser)
 - #158105 (Extract all instance shim variants into new `ShimKind` enum)
 - #158207 (Resolver: local/external split of  `resolve_ident_in_module_non_globs_unadjusted` )
 - #158279 (Follow goto and drop when linting unreachable code)
 - #157527 (Move derive tests into their dedicated folder)
 - #157807 (don't ice on non-lifetime binders under `-Zassumptions-on-binders`)
 - #157939 (Reorganize `tests/ui/issues` [8/N])
 - #157946 (Make `char::is_private_use` and `char::is_assigned` unstably public)
 - #158003 (Reorganize `tests/ui/issues` [9/N])
 - #158020 (Update mingw-w64 C toolchain)
 - #158039 (c-variadic: test that we use equality up to free lifetimes)
 - #158060 (Reorganize `tests/ui/issues` [10/N])
 - #158222 (format: ignore println newline in foreign format hints)
 - #158223 (Move target checking for #[lang] to the attribute parser)
 - #158252 (Use `cfg_select` in `std::os`)
 - #158263 (Only load the feature list once in the entire resolver)
 - #158267 (FromUtf8Error::into_utf8_lossy better example and suggest use)
 - #158272 (Reorganize `tests/ui/issues` [13/N])
 - #158274 (triagebot: Stop pinging myself)
 - #158282 (slice_split_once: bounds check optimization note)
 - #158300 (Improve unknown crate_type diagnostic suggestions)
 - #158304 (mailmap: update mu001999)
 - #158309 (Update `rustc-literal-escaper` version to `0.0.8`)
 - #158314 (Fix incorrect unsafe debug assertion in unchecked_div_exact)
 - #158326 (Add `io::ErrorKind::TooManyOpenFiles`)
@rust-bors rust-bors Bot merged commit f7ba114 into rust-lang:main Jun 25, 2026
13 checks passed
@rustbot rustbot added this to the 1.98.0 milestone Jun 25, 2026
rust-timer added a commit that referenced this pull request Jun 25, 2026
Rollup merge of #156885 - Dnreikronos:fix/index-expr-ambiguous-type-span, r=BoxyUwU

Fix misattributed type inference error span for index expressions

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
@JonathanBrouwer

JonathanBrouwer commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

@rust-timer build 1b9799c
Perf run for #158342

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (1b9799c): comparison URL.

Overall result: ❌✅ regressions and improvements - no action needed

Benchmarking means the PR may be perf-sensitive. Consider adding rollup=never if this change is not fit for rolling up.

@rustbot label: -S-waiting-on-perf -perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
0.3% [0.3%, 0.3%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-0.2% [-0.2%, -0.2%] 1
All ❌✅ (primary) - - 0

Max RSS (memory usage)

Results (primary 4.1%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
4.1% [4.1%, 4.1%] 1
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 4.1% [4.1%, 4.1%] 1

Cycles

Results (primary 1.1%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
2.9% [2.9%, 2.9%] 1
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-0.7% [-0.7%, -0.7%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 1.1% [-0.7%, 2.9%] 2

Binary size

This perf run didn't have relevant results for this metric.

Bootstrap: 503.101s -> 505.385s (0.45%)
Artifact size: 353.51 MiB -> 353.11 MiB (-0.11%)

pull Bot pushed a commit to xtqqczze/rust-lang-miri that referenced this pull request Jun 26, 2026
Rollup of 35 pull requests



Successful merges:

 - rust-lang/rust#158315 (`rust-analyzer` subtree update)
 - rust-lang/rust#158336 (Stop excluding `stdarch` test crates from `rust-src`)
 - rust-lang/rust#155739 (Add temporary scope to assert_eq and assert_ne)
 - rust-lang/rust#156885 (Fix misattributed type inference error span for index expressions)
 - rust-lang/rust#157271 (simplify some `proc_macro` things)
 - rust-lang/rust#157419 (move rustc_type_ir Term things to term_kind.rs)
 - rust-lang/rust#157883 (Remove strict invariant node_type on hir_type during ty privacy visit)
 - rust-lang/rust#157921 (trait solver: Resolve region vars in max universe)
 - rust-lang/rust#157960 (delegation: add support for infers in generics)
 - rust-lang/rust#157983 (Lift the same-signature restriction for `extern "tail"`)
 - rust-lang/rust#158053 (Optimize network address parser)
 - rust-lang/rust#158105 (Extract all instance shim variants into new `ShimKind` enum)
 - rust-lang/rust#158207 (Resolver: local/external split of  `resolve_ident_in_module_non_globs_unadjusted` )
 - rust-lang/rust#158279 (Follow goto and drop when linting unreachable code)
 - rust-lang/rust#157527 (Move derive tests into their dedicated folder)
 - rust-lang/rust#157807 (don't ice on non-lifetime binders under `-Zassumptions-on-binders`)
 - rust-lang/rust#157939 (Reorganize `tests/ui/issues` [8/N])
 - rust-lang/rust#157946 (Make `char::is_private_use` and `char::is_assigned` unstably public)
 - rust-lang/rust#158003 (Reorganize `tests/ui/issues` [9/N])
 - rust-lang/rust#158020 (Update mingw-w64 C toolchain)
 - rust-lang/rust#158039 (c-variadic: test that we use equality up to free lifetimes)
 - rust-lang/rust#158060 (Reorganize `tests/ui/issues` [10/N])
 - rust-lang/rust#158222 (format: ignore println newline in foreign format hints)
 - rust-lang/rust#158223 (Move target checking for #[lang] to the attribute parser)
 - rust-lang/rust#158252 (Use `cfg_select` in `std::os`)
 - rust-lang/rust#158263 (Only load the feature list once in the entire resolver)
 - rust-lang/rust#158267 (FromUtf8Error::into_utf8_lossy better example and suggest use)
 - rust-lang/rust#158272 (Reorganize `tests/ui/issues` [13/N])
 - rust-lang/rust#158274 (triagebot: Stop pinging myself)
 - rust-lang/rust#158282 (slice_split_once: bounds check optimization note)
 - rust-lang/rust#158300 (Improve unknown crate_type diagnostic suggestions)
 - rust-lang/rust#158304 (mailmap: update mu001999)
 - rust-lang/rust#158309 (Update `rustc-literal-escaper` version to `0.0.8`)
 - rust-lang/rust#158314 (Fix incorrect unsafe debug assertion in unchecked_div_exact)
 - rust-lang/rust#158326 (Add `io::ErrorKind::TooManyOpenFiles`)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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

8 participants