diff options
| author | Matthias Krüger <matthias.krueger@famsik.de> | 2022-12-04 11:38:50 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-12-04 11:38:50 +0100 |
| commit | 785b47d92a0519e4bcc1a9dd2c0c28067e082ff1 (patch) | |
| tree | c3abc8e9fb92006c704befa9b63c8dfe048871a9 | |
| parent | fd02567705326884ddb94535d2ab230a9595dc2a (diff) | |
| parent | 7054287b0d52cd1be5dc50e6a11fa7dcace8029f (diff) | |
| download | rust-785b47d92a0519e4bcc1a9dd2c0c28067e082ff1.tar.gz rust-785b47d92a0519e4bcc1a9dd2c0c28067e082ff1.zip | |
Rollup merge of #104856 - luqmana:associated-const-bad-suggestion, r=compiler-errors
Don't suggest associated function call for associated const. Fixes #104801. r? `@compiler-errors`
| -rw-r--r-- | compiler/rustc_hir_typeck/src/method/probe.rs | 20 | ||||
| -rw-r--r-- | src/test/ui/suggestions/dont-suggest-ufcs-for-const.stderr | 8 |
2 files changed, 18 insertions, 10 deletions
diff --git a/compiler/rustc_hir_typeck/src/method/probe.rs b/compiler/rustc_hir_typeck/src/method/probe.rs index c78a32c29dc..ae299cc9d13 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}; @@ -1876,6 +1875,15 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> { self.tcx.erase_late_bound_regions(value) } + /// Determine if the given associated item type is relevant in the current context. + fn is_relevant_kind_for_mode(&self, kind: ty::AssocKind) -> bool { + match (self.mode, kind) { + (Mode::MethodCall, ty::AssocKind::Fn) => true, + (Mode::Path, ty::AssocKind::Const | ty::AssocKind::Fn) => true, + _ => false, + } + } + /// Finds the method with the appropriate name (or return type, as the case may be). If /// `allow_similar_names` is set, find methods with close-matching names. // The length of the returned iterator is nearly always 0 or 1 and this @@ -1888,7 +1896,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> { .associated_items(def_id) .in_definition_order() .filter(|x| { - if x.kind.namespace() != Namespace::ValueNS { + if !self.is_relevant_kind_for_mode(x.kind) { return false; } match lev_distance_with_substrings(name.as_str(), x.name.as_str(), max_dist) @@ -1902,10 +1910,16 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> { } else { self.fcx .associated_value(def_id, name) + .filter(|x| self.is_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| self.is_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 |
