Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve diag when calling method on result of index op #125985

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion compiler/rustc_hir_typeck/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::errors::{
FieldMultiplySpecifiedInInitializer, FunctionalRecordUpdateOnNonStruct, HelpUseLatestEdition,
YieldExprOutsideOfCoroutine,
};
use crate::expr_use_visitor::TypeInformationCtxt;
use crate::fatally_break_rust;
use crate::type_error_struct;
use crate::CoroutineTypes;
Expand Down Expand Up @@ -1329,7 +1330,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
) -> Ty<'tcx> {
let rcvr_t = self.check_expr(rcvr);
// no need to check for bot/err -- callee does that
let rcvr_t = self.structurally_resolve_type(rcvr.span, rcvr_t);
let rcvr_t = if let ExprKind::Index(_, index, _) = rcvr.kind
&& self.typeck_results().expr_ty(index).is_ty_var()
{
self.structurally_resolve_type(index.span, rcvr_t)
Copy link
Member

@compiler-errors compiler-errors Jun 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not correct in case the ambiguity is not due to the index expression:

fn main() {
    let x = vec![];
    x[0usize].method();
}
error[E0282]: type annotations needed for `Vec<_>`
 --> /home/michael/test.rs:2:9
  |
2 |     let x = vec![];
  |         ^
3 |     x[0usize].method();
  |       ------ type must be known at this point

See above how after this PR, we now underline the 0usize even though it is not ambiguous, and there is a single implementation that satisfies the index operator.

Again, I really don't think we should make this change in such a special case.

Systematically determining why the method receiver is ambiguous due to inference is difficult, and I'd rather not patch a single case since it doesn't seem to me more special than any other example.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about the new change?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not correct for cases where the key's ambiguity doesn't affect the value's ambiguity. Consider HashMap<i32, _>.

} else {
self.structurally_resolve_type(rcvr.span, rcvr_t)
};

let method = match self.lookup_method(rcvr_t, segment, segment.ident.span, expr, rcvr, args)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
struct Foo;

impl Foo {
fn foo(&self) {}
}

fn _bar() {
let x = vec![]; //~ ERROR type annotations needed for `Vec<_>`
x[0usize].foo();
}

fn main() {
let thing: Vec<Foo> = vec![];

let foo = |i| {
thing[i].foo(); //~ ERROR type annotations needed
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
error[E0282]: type annotations needed for `Vec<_>`
--> $DIR/calling-method-on-result-of-index-op-issue-125924.rs:8:9
|
LL | let x = vec![];
| ^
LL | x[0usize].foo();
| --------- type must be known at this point
|
help: consider giving `x` an explicit type, where the placeholders `_` are specified
|
LL | let x: Vec<_> = vec![];
| ++++++++

error[E0282]: type annotations needed
--> $DIR/calling-method-on-result-of-index-op-issue-125924.rs:16:15
|
LL | thing[i].foo();
| ^ cannot infer type

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0282`.
Loading