Skip to content

Commit 2e4c90c

Browse files
Don't do post-method-probe error reporting steps if we're in a suggestion
1 parent 3458211 commit 2e4c90c

File tree

6 files changed

+37
-9
lines changed

6 files changed

+37
-9
lines changed

compiler/rustc_hir_typeck/src/demand.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -938,14 +938,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
938938
);
939939
}
940940

941-
pub fn get_conversion_methods(
941+
pub fn get_conversion_methods_for_diagnostic(
942942
&self,
943943
span: Span,
944944
expected: Ty<'tcx>,
945945
checked_ty: Ty<'tcx>,
946946
hir_id: hir::HirId,
947947
) -> Vec<AssocItem> {
948-
let methods = self.probe_for_return_type(
948+
let methods = self.probe_for_return_type_for_diagnostic(
949949
span,
950950
probe::Mode::MethodCall,
951951
expected,

compiler/rustc_hir_typeck/src/expr.rs

+7-2
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

+2-1
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

+2-2
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ pub enum CandidateSource {
9191
impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
9292
/// Determines whether the type `self_ty` supports a visible method named `method_name` or not.
9393
#[instrument(level = "debug", skip(self))]
94-
pub fn method_exists(
94+
pub fn method_exists_for_diagnostic(
9595
&self,
9696
method_name: Ident,
9797
self_ty: Ty<'tcx>,
@@ -102,7 +102,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
102102
probe::Mode::MethodCall,
103103
method_name,
104104
return_type,
105-
IsSuggestion(false),
105+
IsSuggestion(true),
106106
self_ty,
107107
call_expr_id,
108108
ProbeScope::TraitsInScope,

compiler/rustc_hir_typeck/src/method/probe.rs

+22-1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ pub(crate) struct ProbeContext<'a, 'tcx> {
9090
>,
9191

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

95100
impl<'a, 'tcx> Deref for ProbeContext<'a, 'tcx> {
@@ -220,7 +225,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
220225
/// would use to decide if a method is a plausible fit for
221226
/// ambiguity purposes).
222227
#[instrument(level = "debug", skip(self, candidate_filter))]
223-
pub fn probe_for_return_type(
228+
pub fn probe_for_return_type_for_diagnostic(
224229
&self,
225230
span: Span,
226231
mode: Mode,
@@ -459,6 +464,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
459464
&orig_values,
460465
steps.steps,
461466
scope_expr_id,
467+
is_suggestion,
462468
);
463469

464470
probe_cx.assemble_inherent_candidates();
@@ -553,6 +559,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
553559
orig_steps_var_values: &'a OriginalQueryValues<'tcx>,
554560
steps: &'tcx [CandidateStep<'tcx>],
555561
scope_expr_id: HirId,
562+
is_suggestion: IsSuggestion,
556563
) -> ProbeContext<'a, 'tcx> {
557564
ProbeContext {
558565
fcx,
@@ -570,6 +577,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
570577
static_candidates: RefCell::new(Vec::new()),
571578
unsatisfied_predicates: RefCell::new(Vec::new()),
572579
scope_expr_id,
580+
is_suggestion,
573581
}
574582
}
575583

@@ -944,6 +952,18 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
944952
return r;
945953
}
946954

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

949969
let static_candidates = std::mem::take(self.static_candidates.get_mut());
@@ -1631,6 +1651,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
16311651
self.orig_steps_var_values,
16321652
self.steps,
16331653
self.scope_expr_id,
1654+
IsSuggestion(true),
16341655
);
16351656
pcx.allow_similar_names = true;
16361657
pcx.assemble_inherent_candidates();

compiler/rustc_hir_typeck/src/method/suggest.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -2814,7 +2814,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
28142814
Some(output_ty) => self.resolve_vars_if_possible(output_ty),
28152815
_ => return,
28162816
};
2817-
let method_exists = self.method_exists(item_name, output_ty, call.hir_id, return_type);
2817+
let method_exists =
2818+
self.method_exists_for_diagnostic(item_name, output_ty, call.hir_id, return_type);
28182819
debug!("suggest_await_before_method: is_method_exist={}", method_exists);
28192820
if method_exists {
28202821
err.span_suggestion_verbose(

0 commit comments

Comments
 (0)