Skip to content

Commit 40d413f

Browse files
Don't give method suggestions when method probe fails due to bad impl of Deref
1 parent d194948 commit 40d413f

File tree

5 files changed

+33
-8
lines changed

5 files changed

+33
-8
lines changed

compiler/rustc_hir_typeck/src/method/mod.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ use rustc_middle::ty::{
1818
self, GenericArgs, GenericArgsRef, GenericParamDefKind, Ty, TypeVisitableExt,
1919
};
2020
use rustc_middle::{bug, span_bug};
21-
use rustc_span::Span;
2221
use rustc_span::symbol::Ident;
22+
use rustc_span::{ErrorGuaranteed, Span};
2323
use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt;
2424
use rustc_trait_selection::traits::{self, NormalizeExt};
2525
use tracing::{debug, instrument};
@@ -66,6 +66,9 @@ pub(crate) enum MethodError<'tcx> {
6666

6767
// Found a match, but the return type is wrong
6868
BadReturnType,
69+
70+
// Error has already been emitted, no need to emit another one.
71+
ErrorReported(ErrorGuaranteed),
6972
}
7073

7174
// Contains a list of static methods that may apply, a list of unsatisfied trait predicates which
@@ -120,6 +123,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
120123
Err(PrivateMatch(..)) => false,
121124
Err(IllegalSizedBound { .. }) => true,
122125
Err(BadReturnType) => false,
126+
Err(ErrorReported(_)) => false,
123127
}
124128
}
125129

compiler/rustc_hir_typeck/src/method/probe.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -446,13 +446,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
446446
_ => bug!("unexpected bad final type in method autoderef"),
447447
};
448448
self.demand_eqtype(span, ty, Ty::new_error(self.tcx, guar));
449-
return Err(MethodError::NoMatch(NoMatchData {
450-
static_candidates: Vec::new(),
451-
unsatisfied_predicates: Vec::new(),
452-
out_of_scope_traits: Vec::new(),
453-
similar_candidate: None,
454-
mode,
455-
}));
449+
return Err(MethodError::ErrorReported(guar));
456450
}
457451
}
458452

compiler/rustc_hir_typeck/src/method/suggest.rs

+2
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
386386
return err.emit();
387387
}
388388

389+
MethodError::ErrorReported(guar) => guar,
390+
389391
MethodError::BadReturnType => bug!("no return type expectations but got BadReturnType"),
390392
}
391393
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use std::clone::Clone;
2+
use std::ops::Deref;
3+
4+
#[derive(Clone)]
5+
pub struct Foo {}
6+
7+
impl Deref for Foo {}
8+
//~^ ERROR not all trait items implemented
9+
10+
pub fn main() {
11+
let f = Foo {};
12+
let _ = f.clone();
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0046]: not all trait items implemented, missing: `Target`, `deref`
2+
--> $DIR/dont-suggest-import-on-deref-err.rs:7:1
3+
|
4+
LL | impl Deref for Foo {}
5+
| ^^^^^^^^^^^^^^^^^^ missing `Target`, `deref` in implementation
6+
|
7+
= help: implement the missing item: `type Target = /* Type */;`
8+
= help: implement the missing item: `fn deref(&self) -> &<Self as Deref>::Target { todo!() }`
9+
10+
error: aborting due to 1 previous error
11+
12+
For more information about this error, try `rustc --explain E0046`.

0 commit comments

Comments
 (0)