Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 37301f5

Browse files
compiler-errorscuviper
authored andcommitted
Don't do post-method-probe error reporting steps if we're in a suggestion
(cherry picked from commit 2e4c90c)
1 parent 75e17c9 commit 37301f5

File tree

6 files changed

+37
-9
lines changed

6 files changed

+37
-9
lines changed

compiler/rustc_hir_typeck/src/demand.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -942,14 +942,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
942942
);
943943
}
944944

945-
pub fn get_conversion_methods(
945+
pub fn get_conversion_methods_for_diagnostic(
946946
&self,
947947
span: Span,
948948
expected: Ty<'tcx>,
949949
checked_ty: Ty<'tcx>,
950950
hir_id: hir::HirId,
951951
) -> Vec<AssocItem> {
952-
let methods = self.probe_for_return_type(
952+
let methods = self.probe_for_return_type_for_diagnostic(
953953
span,
954954
probe::Mode::MethodCall,
955955
expected,

compiler/rustc_hir_typeck/src/expr.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2414,7 +2414,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
24142414

24152415
let guar = if field.name == kw::Empty {
24162416
self.dcx().span_delayed_bug(field.span, "field name with no name")
2417-
} else if self.method_exists(field, base_ty, expr.hir_id, expected.only_has_type(self)) {
2417+
} else if self.method_exists_for_diagnostic(
2418+
field,
2419+
base_ty,
2420+
expr.hir_id,
2421+
expected.only_has_type(self),
2422+
) {
24182423
self.ban_take_value_of_method(expr, base_ty, field)
24192424
} else if !base_ty.is_primitive_ty() {
24202425
self.ban_nonexisting_field(field, base, expr, base_ty)
@@ -2600,7 +2605,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
26002605
let mut err = self.private_field_err(field, base_did);
26012606

26022607
// Also check if an accessible method exists, which is often what is meant.
2603-
if self.method_exists(field, expr_t, expr.hir_id, return_ty)
2608+
if self.method_exists_for_diagnostic(field, expr_t, expr.hir_id, return_ty)
26042609
&& !self.expr_in_place(expr.hir_id)
26052610
{
26062611
self.suggest_method_call(

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
290290
expected_ty_expr: Option<&'tcx hir::Expr<'tcx>>,
291291
) -> bool {
292292
let expr = expr.peel_blocks();
293-
let methods = self.get_conversion_methods(expr.span, expected, found, expr.hir_id);
293+
let methods =
294+
self.get_conversion_methods_for_diagnostic(expr.span, expected, found, expr.hir_id);
294295

295296
if let Some((suggestion, msg, applicability, verbose, annotation)) =
296297
self.suggest_deref_or_ref(expr, found, expected)

compiler/rustc_hir_typeck/src/method/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ pub enum CandidateSource {
9090
impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
9191
/// Determines whether the type `self_ty` supports a visible method named `method_name` or not.
9292
#[instrument(level = "debug", skip(self))]
93-
pub fn method_exists(
93+
pub fn method_exists_for_diagnostic(
9494
&self,
9595
method_name: Ident,
9696
self_ty: Ty<'tcx>,
@@ -101,7 +101,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
101101
probe::Mode::MethodCall,
102102
method_name,
103103
return_type,
104-
IsSuggestion(false),
104+
IsSuggestion(true),
105105
self_ty,
106106
call_expr_id,
107107
ProbeScope::TraitsInScope,

compiler/rustc_hir_typeck/src/method/probe.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ pub(crate) struct ProbeContext<'a, 'tcx> {
8888
>,
8989

9090
scope_expr_id: HirId,
91+
92+
/// Is this probe being done for a diagnostic? This will skip some error reporting
93+
/// machinery, since we don't particularly care about, for example, similarly named
94+
/// candidates if we're *reporting* similarly named candidates.
95+
is_suggestion: IsSuggestion,
9196
}
9297

9398
impl<'a, 'tcx> Deref for ProbeContext<'a, 'tcx> {
@@ -218,7 +223,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
218223
/// would use to decide if a method is a plausible fit for
219224
/// ambiguity purposes).
220225
#[instrument(level = "debug", skip(self, candidate_filter))]
221-
pub fn probe_for_return_type(
226+
pub fn probe_for_return_type_for_diagnostic(
222227
&self,
223228
span: Span,
224229
mode: Mode,
@@ -457,6 +462,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
457462
&orig_values,
458463
steps.steps,
459464
scope_expr_id,
465+
is_suggestion,
460466
);
461467

462468
probe_cx.assemble_inherent_candidates();
@@ -551,6 +557,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
551557
orig_steps_var_values: &'a OriginalQueryValues<'tcx>,
552558
steps: &'tcx [CandidateStep<'tcx>],
553559
scope_expr_id: HirId,
560+
is_suggestion: IsSuggestion,
554561
) -> ProbeContext<'a, 'tcx> {
555562
ProbeContext {
556563
fcx,
@@ -568,6 +575,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
568575
static_candidates: RefCell::new(Vec::new()),
569576
unsatisfied_predicates: RefCell::new(Vec::new()),
570577
scope_expr_id,
578+
is_suggestion,
571579
}
572580
}
573581

@@ -942,6 +950,18 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
942950
return r;
943951
}
944952

953+
// If it's a `lookup_probe_for_diagnostic`, then quit early. No need to
954+
// probe for other candidates.
955+
if self.is_suggestion.0 {
956+
return Err(MethodError::NoMatch(NoMatchData {
957+
static_candidates: vec![],
958+
unsatisfied_predicates: vec![],
959+
out_of_scope_traits: vec![],
960+
similar_candidate: None,
961+
mode: self.mode,
962+
}));
963+
}
964+
945965
debug!("pick: actual search failed, assemble diagnostics");
946966

947967
let static_candidates = std::mem::take(self.static_candidates.get_mut());
@@ -1633,6 +1653,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
16331653
self.orig_steps_var_values,
16341654
self.steps,
16351655
self.scope_expr_id,
1656+
IsSuggestion(true),
16361657
);
16371658
pcx.allow_similar_names = true;
16381659
pcx.assemble_inherent_candidates();

compiler/rustc_hir_typeck/src/method/suggest.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2835,7 +2835,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
28352835
Some(output_ty) => self.resolve_vars_if_possible(output_ty),
28362836
_ => return,
28372837
};
2838-
let method_exists = self.method_exists(item_name, output_ty, call.hir_id, return_type);
2838+
let method_exists =
2839+
self.method_exists_for_diagnostic(item_name, output_ty, call.hir_id, return_type);
28392840
debug!("suggest_await_before_method: is_method_exist={}", method_exists);
28402841
if method_exists {
28412842
err.span_suggestion_verbose(

0 commit comments

Comments
 (0)