Fix misattributed type inference error span for index expressions#156885
Open
Dnreikronos wants to merge 1 commit into
Open
Fix misattributed type inference error span for index expressions#156885Dnreikronos wants to merge 1 commit into
Dnreikronos wants to merge 1 commit into
Conversation
This comment has been minimized.
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.
09f69f4 to
d061dcb
Compare
Contributor
|
r? compiler |
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.
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:
.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