Skip to content

Commit 39c4312

Browse files
committed
Add test file
Add test file Add changes & test results Add stderr reference file Move break to correct position. Failing test added from tests in ui Update compiler/rustc_hir_typeck/src/method/suggest.rs Co-authored-by: Esteban Kuber <estebank@users.noreply.github.com> Update compiler/rustc_hir_typeck/src/method/suggest.rs Co-authored-by: Esteban Kuber <estebank@users.noreply.github.com> Match to find Error type: BadReturnType Print info whitespace fmt Use probe_name_for_many Revert probe.rs Update .stderr Update compiler/rustc_hir_typeck/src/method/suggest.rs Add span.note - this pottlues other tests span_labels add span_label Ignore candidtes beyond the scope of current context Move check to end of function to filter out methods with better context errors Whiteline Format Remove mutation check Whitespace Fmt again
1 parent c3ae470 commit 39c4312

File tree

4 files changed

+69
-2
lines changed

4 files changed

+69
-2
lines changed

compiler/rustc_hir_typeck/src/method/suggest.rs

+25-1
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
483483
);
484484
probe.is_ok()
485485
});
486-
487486
self.note_internal_mutation_in_method(
488487
&mut err,
489488
rcvr_expr,
@@ -1240,7 +1239,32 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12401239
}
12411240
}
12421241
}
1242+
// Only if an appropriate error source is not found, check method chain for possible candiates
1243+
let error_source_not_found = unsatisfied_predicates.is_empty();
1244+
if error_source_not_found && let Mode::MethodCall = mode && let SelfSource::MethodCall(mut rcvr_expr) = source {
1245+
while let hir::ExprKind::MethodCall(_path_segment, parent_expr, _args, method_span) =
1246+
rcvr_expr.kind
1247+
{
1248+
let prev_ty = self.resolve_vars_if_possible(
1249+
self.typeck_results
1250+
.borrow()
1251+
.expr_ty_adjusted_opt(parent_expr)
1252+
.unwrap_or(Ty::new_misc_error(self.tcx)),);
1253+
1254+
for _matched_method in self.probe_for_name_many(
1255+
Mode::MethodCall,
1256+
item_name,
1257+
None,
1258+
IsSuggestion(true),
1259+
prev_ty,
1260+
rcvr_expr.hir_id,
1261+
ProbeScope::TraitsInScope,) {
1262+
err.span_label(method_span, format!("{item_kind} `{item_name}` is available on `{prev_ty}`"));
1263+
}
1264+
rcvr_expr = parent_expr;
12431265

1266+
}
1267+
}
12441268
self.note_derefed_ty_has_method(&mut err, source, rcvr_ty, item_name, expected);
12451269
return Some(err);
12461270
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
struct A;
2+
struct B;
3+
struct C;
4+
struct D;
5+
6+
impl A {
7+
fn b(&self) -> B { B }
8+
}
9+
10+
impl B {
11+
fn c(&self) -> C { C }
12+
fn foo(&self) {}
13+
}
14+
15+
impl C {
16+
fn d(&self) -> D { D }
17+
}
18+
19+
impl D {
20+
fn e(&self) {}
21+
}
22+
23+
fn main() {
24+
A.b().c().d().foo();
25+
//~^ ERROR no method named `foo` found for struct `D` in the current scope
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0599]: no method named `foo` found for struct `D` in the current scope
2+
--> $DIR/method-chain-expression-failure.rs:24:19
3+
|
4+
LL | struct D;
5+
| -------- method `foo` not found for this struct
6+
...
7+
LL | A.b().c().d().foo();
8+
| --- ^^^ method not found in `D`
9+
| |
10+
| method `foo` is available on `&B`
11+
12+
error: aborting due to previous error
13+
14+
For more information about this error, try `rustc --explain E0599`.

tests/ui/suggestions/chain-method-call-mutation-in-place.stderr

+4-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ error[E0599]: no method named `sort` found for unit type `()` in the current sco
1818
--> $DIR/chain-method-call-mutation-in-place.rs:3:72
1919
|
2020
LL | vec![1, 2, 3].into_iter().collect::<Vec<i32>>().sort_by_key(|i| i).sort();
21-
| ^^^^ method not found in `()`
21+
| ----------- ------------------ ^^^^ method not found in `()`
22+
| | |
23+
| | method `sort` is available on `&mut [i32]`
24+
| method `sort` is available on `Vec<i32>`
2225
|
2326
note: method `sort_by_key` modifies its receiver in-place, it is not meant to be used in method chains.
2427
--> $DIR/chain-method-call-mutation-in-place.rs:3:53

0 commit comments

Comments
 (0)