Don't give method suggestions when method probe fails due to bad impl of Deref

This commit is contained in:
Michael Goulet 2024-09-29 11:56:06 -04:00
parent d194948e50
commit 40d413f9fe
5 changed files with 33 additions and 8 deletions

View File

@ -18,8 +18,8 @@
self, GenericArgs, GenericArgsRef, GenericParamDefKind, Ty, TypeVisitableExt, self, GenericArgs, GenericArgsRef, GenericParamDefKind, Ty, TypeVisitableExt,
}; };
use rustc_middle::{bug, span_bug}; use rustc_middle::{bug, span_bug};
use rustc_span::Span;
use rustc_span::symbol::Ident; use rustc_span::symbol::Ident;
use rustc_span::{ErrorGuaranteed, Span};
use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt; use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt;
use rustc_trait_selection::traits::{self, NormalizeExt}; use rustc_trait_selection::traits::{self, NormalizeExt};
use tracing::{debug, instrument}; use tracing::{debug, instrument};
@ -66,6 +66,9 @@ pub(crate) enum MethodError<'tcx> {
// Found a match, but the return type is wrong // Found a match, but the return type is wrong
BadReturnType, BadReturnType,
// Error has already been emitted, no need to emit another one.
ErrorReported(ErrorGuaranteed),
} }
// Contains a list of static methods that may apply, a list of unsatisfied trait predicates which // Contains a list of static methods that may apply, a list of unsatisfied trait predicates which
@ -120,6 +123,7 @@ pub fn method_exists_for_diagnostic(
Err(PrivateMatch(..)) => false, Err(PrivateMatch(..)) => false,
Err(IllegalSizedBound { .. }) => true, Err(IllegalSizedBound { .. }) => true,
Err(BadReturnType) => false, Err(BadReturnType) => false,
Err(ErrorReported(_)) => false,
} }
} }

View File

@ -446,13 +446,7 @@ pub(crate) fn probe_op<OP, R>(
_ => bug!("unexpected bad final type in method autoderef"), _ => bug!("unexpected bad final type in method autoderef"),
}; };
self.demand_eqtype(span, ty, Ty::new_error(self.tcx, guar)); self.demand_eqtype(span, ty, Ty::new_error(self.tcx, guar));
return Err(MethodError::NoMatch(NoMatchData { return Err(MethodError::ErrorReported(guar));
static_candidates: Vec::new(),
unsatisfied_predicates: Vec::new(),
out_of_scope_traits: Vec::new(),
similar_candidate: None,
mode,
}));
} }
} }

View File

@ -386,6 +386,8 @@ trait `{}` provides `{item_name}` is implemented but not reachable",
return err.emit(); return err.emit();
} }
MethodError::ErrorReported(guar) => guar,
MethodError::BadReturnType => bug!("no return type expectations but got BadReturnType"), MethodError::BadReturnType => bug!("no return type expectations but got BadReturnType"),
} }
} }

View File

@ -0,0 +1,13 @@
use std::clone::Clone;
use std::ops::Deref;
#[derive(Clone)]
pub struct Foo {}
impl Deref for Foo {}
//~^ ERROR not all trait items implemented
pub fn main() {
let f = Foo {};
let _ = f.clone();
}

View File

@ -0,0 +1,12 @@
error[E0046]: not all trait items implemented, missing: `Target`, `deref`
--> $DIR/dont-suggest-import-on-deref-err.rs:7:1
|
LL | impl Deref for Foo {}
| ^^^^^^^^^^^^^^^^^^ missing `Target`, `deref` in implementation
|
= help: implement the missing item: `type Target = /* Type */;`
= help: implement the missing item: `fn deref(&self) -> &<Self as Deref>::Target { todo!() }`
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0046`.