From 4bc41b91d7e299b6d0651a8ba48122304beb6820 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Thu, 23 May 2024 18:47:32 -0400 Subject: [PATCH] Don't continue probing for method if in suggestion and autoderef hits ambiguity --- compiler/rustc_hir_typeck/src/method/probe.rs | 11 +++++++++-- .../suggest-method-on-call-for-ambig-receiver.rs | 16 ++++++++++++++++ ...gest-method-on-call-for-ambig-receiver.stderr | 9 +++++++++ 3 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 tests/ui/methods/suggest-method-on-call-for-ambig-receiver.rs create mode 100644 tests/ui/methods/suggest-method-on-call-for-ambig-receiver.stderr diff --git a/compiler/rustc_hir_typeck/src/method/probe.rs b/compiler/rustc_hir_typeck/src/method/probe.rs index e0a60337c3b..820fe97afb5 100644 --- a/compiler/rustc_hir_typeck/src/method/probe.rs +++ b/compiler/rustc_hir_typeck/src/method/probe.rs @@ -395,8 +395,15 @@ pub(crate) fn probe_op( // ambiguous. if let Some(bad_ty) = &steps.opt_bad_ty { if is_suggestion.0 { - // Ambiguity was encountered during a suggestion. Just keep going. - debug!("ProbeContext: encountered ambiguity in suggestion"); + // Ambiguity was encountered during a suggestion. There's really + // not much use in suggesting methods in this case. + return Err(MethodError::NoMatch(NoMatchData { + static_candidates: Vec::new(), + unsatisfied_predicates: Vec::new(), + out_of_scope_traits: Vec::new(), + similar_candidate: None, + mode, + })); } else if bad_ty.reached_raw_pointer && !self.tcx.features().arbitrary_self_types && !self.tcx.sess.at_least_rust_2018() diff --git a/tests/ui/methods/suggest-method-on-call-for-ambig-receiver.rs b/tests/ui/methods/suggest-method-on-call-for-ambig-receiver.rs new file mode 100644 index 00000000000..fc2c15ee8c6 --- /dev/null +++ b/tests/ui/methods/suggest-method-on-call-for-ambig-receiver.rs @@ -0,0 +1,16 @@ +// Fix for . + +fn separate_arms() { + let mut x = None; + match x { + None => { + x = Some(0); + } + Some(right) => { + consume(right); + //~^ ERROR cannot find function `consume` in this scope + } + } +} + +fn main() {} diff --git a/tests/ui/methods/suggest-method-on-call-for-ambig-receiver.stderr b/tests/ui/methods/suggest-method-on-call-for-ambig-receiver.stderr new file mode 100644 index 00000000000..40d8301c24e --- /dev/null +++ b/tests/ui/methods/suggest-method-on-call-for-ambig-receiver.stderr @@ -0,0 +1,9 @@ +error[E0425]: cannot find function `consume` in this scope + --> $DIR/suggest-method-on-call-for-ambig-receiver.rs:10:13 + | +LL | consume(right); + | ^^^^^^^ not found in this scope + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0425`.