Don't suggest associated function call for associated const.

This commit is contained in:
Luqman Aden 2022-11-24 21:15:46 -05:00
parent 5dfb4b0afa
commit 2582e36da3
2 changed files with 14 additions and 10 deletions

View File

@ -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()
}
}
}

View File

@ -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