From 2582e36da3bb1ac57fe0472f6d424da40520dc51 Mon Sep 17 00:00:00 2001 From: Luqman Aden Date: Thu, 24 Nov 2022 21:15:46 -0500 Subject: [PATCH] Don't suggest associated function call for associated const. --- compiler/rustc_hir_typeck/src/method/probe.rs | 16 +++++++++++++--- .../dont-suggest-ufcs-for-const.stderr | 8 +------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/compiler/rustc_hir_typeck/src/method/probe.rs b/compiler/rustc_hir_typeck/src/method/probe.rs index 44c3edf06a8..979e7e25996 100644 --- a/compiler/rustc_hir_typeck/src/method/probe.rs +++ b/compiler/rustc_hir_typeck/src/method/probe.rs @@ -9,7 +9,6 @@ use rustc_data_structures::fx::FxHashSet; use rustc_errors::Applicability; use rustc_hir as hir; use rustc_hir::def::DefKind; -use rustc_hir::def::Namespace; use rustc_infer::infer::canonical::OriginalQueryValues; use rustc_infer::infer::canonical::{Canonical, QueryResponse}; use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind}; @@ -1881,6 +1880,11 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> { // The length of the returned iterator is nearly always 0 or 1 and this // method is fairly hot. fn impl_or_trait_item(&self, def_id: DefId) -> SmallVec<[ty::AssocItem; 1]> { + let relevant_kind_for_mode = |kind| match (self.mode, kind) { + (Mode::MethodCall, ty::AssocKind::Fn) => true, + (Mode::Path, ty::AssocKind::Const | ty::AssocKind::Fn) => true, + _ => false, + }; if let Some(name) = self.method_name { if self.allow_similar_names { let max_dist = max(name.as_str().len(), 3) / 3; @@ -1888,7 +1892,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> { .associated_items(def_id) .in_definition_order() .filter(|x| { - if x.kind.namespace() != Namespace::ValueNS { + if !relevant_kind_for_mode(x.kind) { return false; } match lev_distance_with_substrings(name.as_str(), x.name.as_str(), max_dist) @@ -1902,10 +1906,16 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> { } else { self.fcx .associated_value(def_id, name) + .filter(|x| relevant_kind_for_mode(x.kind)) .map_or_else(SmallVec::new, |x| SmallVec::from_buf([x])) } } else { - self.tcx.associated_items(def_id).in_definition_order().copied().collect() + self.tcx + .associated_items(def_id) + .in_definition_order() + .filter(|x| relevant_kind_for_mode(x.kind)) + .copied() + .collect() } } } diff --git a/src/test/ui/suggestions/dont-suggest-ufcs-for-const.stderr b/src/test/ui/suggestions/dont-suggest-ufcs-for-const.stderr index 04e0511d788..0d9543e0b8f 100644 --- a/src/test/ui/suggestions/dont-suggest-ufcs-for-const.stderr +++ b/src/test/ui/suggestions/dont-suggest-ufcs-for-const.stderr @@ -2,13 +2,7 @@ error[E0599]: no method named `MAX` found for type `u32` in the current scope --> $DIR/dont-suggest-ufcs-for-const.rs:2:11 | LL | 1_u32.MAX(); - | ------^^^-- - | | | - | | this is an associated function, not a method - | help: use associated function syntax instead: `u32::MAX()` - | - = note: found the following associated functions; to be used as methods, functions must have a `self` parameter - = note: the candidate is defined in an impl for the type `u32` + | ^^^ method not found in `u32` error: aborting due to previous error