Skip to content

Commit 1dea325

Browse files
authored
Unrolled build for rust-lang#131024
Rollup merge of rust-lang#131024 - compiler-errors:deref-sugg, r=estebank Don't give method suggestions when method probe fails due to bad implementation of `Deref` If we have a bad `Deref` impl, we used to bail with `MethodError::NoMatch`, which makes the error reporting code think that there was no applicable method (and thus try to suggest importing something, even if it's in scope). Suppress this error, which fixes rust-lang#131003.
2 parents 9ff5fc4 + 486440f commit 1dea325

File tree

5 files changed

+54
-31
lines changed

5 files changed

+54
-31
lines changed

compiler/rustc_hir_typeck/src/method/mod.rs

+11-7
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};
@@ -46,26 +46,29 @@ pub(crate) struct MethodCallee<'tcx> {
4646

4747
#[derive(Debug)]
4848
pub(crate) enum MethodError<'tcx> {
49-
// Did not find an applicable method, but we did find various near-misses that may work.
49+
/// Did not find an applicable method, but we did find various near-misses that may work.
5050
NoMatch(NoMatchData<'tcx>),
5151

52-
// Multiple methods might apply.
52+
/// Multiple methods might apply.
5353
Ambiguity(Vec<CandidateSource>),
5454

55-
// Found an applicable method, but it is not visible. The third argument contains a list of
56-
// not-in-scope traits which may work.
55+
/// Found an applicable method, but it is not visible. The third argument contains a list of
56+
/// not-in-scope traits which may work.
5757
PrivateMatch(DefKind, DefId, Vec<DefId>),
5858

59-
// Found a `Self: Sized` bound where `Self` is a trait object.
59+
/// Found a `Self: Sized` bound where `Self` is a trait object.
6060
IllegalSizedBound {
6161
candidates: Vec<DefId>,
6262
needs_mut: bool,
6363
bound_span: Span,
6464
self_expr: &'tcx hir::Expr<'tcx>,
6565
},
6666

67-
// Found a match, but the return type is wrong
67+
/// 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

+17-17
Original file line numberDiff line numberDiff line change
@@ -229,20 +229,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
229229
}
230230

231231
match error {
232-
MethodError::NoMatch(mut no_match_data) => {
233-
return self.report_no_match_method_error(
234-
span,
235-
rcvr_ty,
236-
item_name,
237-
call_id,
238-
source,
239-
args,
240-
sugg_span,
241-
&mut no_match_data,
242-
expected,
243-
trait_missing_method,
244-
);
245-
}
232+
MethodError::NoMatch(mut no_match_data) => self.report_no_match_method_error(
233+
span,
234+
rcvr_ty,
235+
item_name,
236+
call_id,
237+
source,
238+
args,
239+
sugg_span,
240+
&mut no_match_data,
241+
expected,
242+
trait_missing_method,
243+
),
246244

247245
MethodError::Ambiguity(mut sources) => {
248246
let mut err = struct_span_code_err!(
@@ -263,7 +261,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
263261
&mut sources,
264262
Some(sugg_span),
265263
);
266-
return err.emit();
264+
err.emit()
267265
}
268266

269267
MethodError::PrivateMatch(kind, def_id, out_of_scope_traits) => {
@@ -284,7 +282,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
284282
.unwrap_or_else(|| self.tcx.def_span(def_id));
285283
err.span_label(sp, format!("private {kind} defined here"));
286284
self.suggest_valid_traits(&mut err, item_name, out_of_scope_traits, true);
287-
return err.emit();
285+
err.emit()
288286
}
289287

290288
MethodError::IllegalSizedBound { candidates, needs_mut, bound_span, self_expr } => {
@@ -383,9 +381,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
383381
}
384382
}
385383
}
386-
return err.emit();
384+
err.emit()
387385
}
388386

387+
MethodError::ErrorReported(guar) => guar,
388+
389389
MethodError::BadReturnType => bug!("no return type expectations but got BadReturnType"),
390390
}
391391
}
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)