Skip to content

Commit

Permalink
Rollup merge of #117655 - compiler-errors:method-tweaks, r=estebank
Browse files Browse the repository at this point in the history
Method suggestion code tweaks

I was rummaging around the method suggestion code after #117006 (comment) and saw a few things to simplify.

This is two unrelated commits, both in the same file. Review them separately, if you'd like.

r? estebank
matthiaskrgr authored Nov 7, 2023
2 parents 7552dd1 + 0add056 commit f72e974
Showing 12 changed files with 178 additions and 198 deletions.
6 changes: 3 additions & 3 deletions compiler/rustc_hir_typeck/src/expr.rs
Original file line number Diff line number Diff line change
@@ -13,7 +13,7 @@ use crate::errors::{
YieldExprOutsideOfCoroutine,
};
use crate::fatally_break_rust;
use crate::method::{MethodCallComponents, SelfSource};
use crate::method::SelfSource;
use crate::type_error_struct;
use crate::Expectation::{self, ExpectCastableToType, ExpectHasType, NoExpectation};
use crate::{
@@ -512,7 +512,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
) -> Ty<'tcx> {
let tcx = self.tcx;
let (res, opt_ty, segs) =
self.resolve_ty_and_res_fully_qualified_call(qpath, expr.hir_id, expr.span);
self.resolve_ty_and_res_fully_qualified_call(qpath, expr.hir_id, expr.span, Some(args));
let ty = match res {
Res::Err => {
self.suggest_assoc_method_call(segs);
@@ -1332,7 +1332,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
segment.ident,
SelfSource::MethodCall(rcvr),
error,
Some(MethodCallComponents { receiver: rcvr, args, full_expr: expr }),
Some(args),
expected,
false,
) {
3 changes: 2 additions & 1 deletion compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs
Original file line number Diff line number Diff line change
@@ -797,6 +797,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
qpath: &'tcx QPath<'tcx>,
hir_id: hir::HirId,
span: Span,
args: Option<&'tcx [hir::Expr<'tcx>]>,
) -> (Res, Option<RawTy<'tcx>>, &'tcx [hir::PathSegment<'tcx>]) {
debug!(
"resolve_ty_and_res_fully_qualified_call: qpath={:?} hir_id={:?} span={:?}",
@@ -898,7 +899,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
item_name,
SelfSource::QPath(qself),
error,
None,
args,
Expectation::NoExpectation,
trait_missing_method && span.edition().at_least_rust_2021(), // emits missing method for trait only after edition 2021
) {
2 changes: 1 addition & 1 deletion compiler/rustc_hir_typeck/src/method/mod.rs
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@ mod prelude2021;
pub mod probe;
mod suggest;

pub use self::suggest::{MethodCallComponents, SelfSource};
pub use self::suggest::SelfSource;
pub use self::MethodError::*;

use crate::errors::OpMethodGenericParams;
277 changes: 128 additions & 149 deletions compiler/rustc_hir_typeck/src/method/suggest.rs

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions compiler/rustc_hir_typeck/src/pat.rs
Original file line number Diff line number Diff line change
@@ -166,9 +166,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
fn check_pat(&self, pat: &'tcx Pat<'tcx>, expected: Ty<'tcx>, pat_info: PatInfo<'tcx, '_>) {
let PatInfo { binding_mode: def_bm, top_info: ti, .. } = pat_info;
let path_res = match &pat.kind {
PatKind::Path(qpath) => {
Some(self.resolve_ty_and_res_fully_qualified_call(qpath, pat.hir_id, pat.span))
}
PatKind::Path(qpath) => Some(
self.resolve_ty_and_res_fully_qualified_call(qpath, pat.hir_id, pat.span, None),
),
_ => None,
};
let adjust_mode = self.calc_adjust_mode(pat, path_res.map(|(res, ..)| res));
@@ -1060,7 +1060,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {

// Resolve the path and check the definition for errors.
let (res, opt_ty, segments) =
self.resolve_ty_and_res_fully_qualified_call(qpath, pat.hir_id, pat.span);
self.resolve_ty_and_res_fully_qualified_call(qpath, pat.hir_id, pat.span, None);
if res == Res::Err {
let e = tcx.sess.delay_span_bug(pat.span, "`Res::Err` but no error emitted");
self.set_tainted_by_errors(e);
12 changes: 6 additions & 6 deletions tests/ui/methods/disambiguate-multiple-blanket-impl.stderr
Original file line number Diff line number Diff line change
@@ -29,10 +29,10 @@ LL | fn foo(&self) {}
| ^^^^^^^^^^^^^
help: use fully-qualified syntax to disambiguate
|
LL | <T as A>::foo(&s);
| ~~~~~~~~~~
LL | <T as B>::foo(&s);
| ~~~~~~~~~~
LL | A::foo(&s);
| ~~~
LL | B::foo(&s);
| ~~~

error[E0034]: multiple applicable items in scope
--> $DIR/disambiguate-multiple-blanket-impl.rs:33:8
@@ -52,9 +52,9 @@ LL | const CONST: usize = 2;
| ^^^^^^^^^^^^^^^^^^
help: use fully-qualified syntax to disambiguate
|
LL | <T as A>::CONST;
LL | <S as A>::CONST;
| ~~~~~~~~~~
LL | <T as B>::CONST;
LL | <S as B>::CONST;
| ~~~~~~~~~~

error: aborting due to 3 previous errors
8 changes: 4 additions & 4 deletions tests/ui/methods/disambiguate-multiple-impl.stderr
Original file line number Diff line number Diff line change
@@ -29,10 +29,10 @@ LL | fn foo(&self) {}
| ^^^^^^^^^^^^^
help: use fully-qualified syntax to disambiguate
|
LL | <S as A>::foo(&s);
| ~~~~~~~~~~
LL | <S as B>::foo(&s);
| ~~~~~~~~~~
LL | A::foo(&s);
| ~~~
LL | B::foo(&s);
| ~~~

error[E0034]: multiple applicable items in scope
--> $DIR/disambiguate-multiple-impl.rs:34:16
28 changes: 14 additions & 14 deletions tests/ui/methods/disambiguate-multiple-trait-2.stderr
Original file line number Diff line number Diff line change
@@ -37,12 +37,12 @@ LL | fn foo(&self);
| ^^^^^^^^^^^^^^
help: disambiguate the method for candidate #1
|
LL | A::foo(t);
| ~~~~~~~~~
LL | A::foo(&t);
| ~~~~~~~~~~
help: disambiguate the method for candidate #2
|
LL | B::foo(t);
| ~~~~~~~~~
LL | B::foo(&t);
| ~~~~~~~~~~

error[E0034]: multiple applicable items in scope
--> $DIR/disambiguate-multiple-trait-2.rs:20:16
@@ -62,10 +62,10 @@ LL | const CONST: usize;
| ^^^^^^^^^^^^^^^^^^
help: use fully-qualified syntax to disambiguate
|
LL | let _ = A::CONST;
| ~~~
LL | let _ = B::CONST;
| ~~~
LL | let _ = <T as A>::CONST;
| ~~~~~~~~~~
LL | let _ = <T as B>::CONST;
| ~~~~~~~~~~

error[E0223]: ambiguous associated type
--> $DIR/disambiguate-multiple-trait-2.rs:52:12
@@ -98,10 +98,10 @@ LL | fn foo(&self) {}
| ^^^^^^^^^^^^^
help: use fully-qualified syntax to disambiguate
|
LL | <T as A>::foo(&s);
| ~~~~~~~~~~
LL | <T as B>::foo(&s);
| ~~~~~~~~~~
LL | A::foo(&s);
| ~~~
LL | B::foo(&s);
| ~~~

error[E0034]: multiple applicable items in scope
--> $DIR/disambiguate-multiple-trait-2.rs:49:16
@@ -121,9 +121,9 @@ LL | const CONST: usize = 1;
| ^^^^^^^^^^^^^^^^^^
help: use fully-qualified syntax to disambiguate
|
LL | let _ = <T as A>::CONST;
LL | let _ = <S as A>::CONST;
| ~~~~~~~~~~
LL | let _ = <T as B>::CONST;
LL | let _ = <S as B>::CONST;
| ~~~~~~~~~~

error: aborting due to 6 previous errors
12 changes: 6 additions & 6 deletions tests/ui/methods/disambiguate-multiple-trait.stderr
Original file line number Diff line number Diff line change
@@ -29,10 +29,10 @@ LL | fn foo(&self) {}
| ^^^^^^^^^^^^^
help: use fully-qualified syntax to disambiguate
|
LL | <T as A>::foo(&s);
| ~~~~~~~~~~
LL | <T as B>::foo(&s);
| ~~~~~~~~~~
LL | A::foo(&s);
| ~~~
LL | B::foo(&s);
| ~~~

error[E0034]: multiple applicable items in scope
--> $DIR/disambiguate-multiple-trait.rs:27:16
@@ -52,9 +52,9 @@ LL | const CONST: usize = 2;
| ^^^^^^^^^^^^^^^^^^
help: use fully-qualified syntax to disambiguate
|
LL | let _ = <T as A>::CONST;
LL | let _ = <S as A>::CONST;
| ~~~~~~~~~~
LL | let _ = <T as B>::CONST;
LL | let _ = <S as B>::CONST;
| ~~~~~~~~~~

error: aborting due to 3 previous errors
8 changes: 4 additions & 4 deletions tests/ui/methods/method-ambig-two-traits-from-bounds.stderr
Original file line number Diff line number Diff line change
@@ -16,12 +16,12 @@ LL | trait B { fn foo(&self); }
| ^^^^^^^^^^^^^^
help: disambiguate the method for candidate #1
|
LL | A::foo(t);
| ~~~~~~~~~
LL | A::foo(&t);
| ~~~~~~~~~~
help: disambiguate the method for candidate #2
|
LL | B::foo(t);
| ~~~~~~~~~
LL | B::foo(&t);
| ~~~~~~~~~~

error: aborting due to previous error

Original file line number Diff line number Diff line change
@@ -54,8 +54,8 @@ LL | let z = NuisanceFoo::foo(x);
| ~~~~~~~~~~~~~~~~~~~
help: disambiguate the method for candidate #3
|
LL | let z = FinalFoo::foo(x);
| ~~~~~~~~~~~~~~~~
LL | let z = FinalFoo::foo(&x);
| ~~~~~~~~~~~~~~~~~

error[E0308]: mismatched types
--> $DIR/method-deref-to-same-trait-object-with-separate-params.rs:139:24
8 changes: 4 additions & 4 deletions tests/ui/span/issue-37767.stderr
Original file line number Diff line number Diff line change
@@ -16,12 +16,12 @@ LL | fn foo(&mut self) {}
| ^^^^^^^^^^^^^^^^^
help: disambiguate the method for candidate #1
|
LL | A::foo(&a)
| ~~~~~~~~~~
LL | A::foo(&mut a)
| ~~~~~~~~~~~~~~
help: disambiguate the method for candidate #2
|
LL | B::foo(&a)
| ~~~~~~~~~~
LL | B::foo(&mut a)
| ~~~~~~~~~~~~~~

error[E0034]: multiple applicable items in scope
--> $DIR/issue-37767.rs:22:7

0 comments on commit f72e974

Please sign in to comment.