Skip to content

Commit 2582e36

Browse files
committed
Don't suggest associated function call for associated const.
1 parent 5dfb4b0 commit 2582e36

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

compiler/rustc_hir_typeck/src/method/probe.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use rustc_data_structures::fx::FxHashSet;
99
use rustc_errors::Applicability;
1010
use rustc_hir as hir;
1111
use rustc_hir::def::DefKind;
12-
use rustc_hir::def::Namespace;
1312
use rustc_infer::infer::canonical::OriginalQueryValues;
1413
use rustc_infer::infer::canonical::{Canonical, QueryResponse};
1514
use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
@@ -1881,14 +1880,19 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
18811880
// The length of the returned iterator is nearly always 0 or 1 and this
18821881
// method is fairly hot.
18831882
fn impl_or_trait_item(&self, def_id: DefId) -> SmallVec<[ty::AssocItem; 1]> {
1883+
let relevant_kind_for_mode = |kind| match (self.mode, kind) {
1884+
(Mode::MethodCall, ty::AssocKind::Fn) => true,
1885+
(Mode::Path, ty::AssocKind::Const | ty::AssocKind::Fn) => true,
1886+
_ => false,
1887+
};
18841888
if let Some(name) = self.method_name {
18851889
if self.allow_similar_names {
18861890
let max_dist = max(name.as_str().len(), 3) / 3;
18871891
self.tcx
18881892
.associated_items(def_id)
18891893
.in_definition_order()
18901894
.filter(|x| {
1891-
if x.kind.namespace() != Namespace::ValueNS {
1895+
if !relevant_kind_for_mode(x.kind) {
18921896
return false;
18931897
}
18941898
match lev_distance_with_substrings(name.as_str(), x.name.as_str(), max_dist)
@@ -1902,10 +1906,16 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
19021906
} else {
19031907
self.fcx
19041908
.associated_value(def_id, name)
1909+
.filter(|x| relevant_kind_for_mode(x.kind))
19051910
.map_or_else(SmallVec::new, |x| SmallVec::from_buf([x]))
19061911
}
19071912
} else {
1908-
self.tcx.associated_items(def_id).in_definition_order().copied().collect()
1913+
self.tcx
1914+
.associated_items(def_id)
1915+
.in_definition_order()
1916+
.filter(|x| relevant_kind_for_mode(x.kind))
1917+
.copied()
1918+
.collect()
19091919
}
19101920
}
19111921
}

src/test/ui/suggestions/dont-suggest-ufcs-for-const.stderr

+1-7
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,7 @@ error[E0599]: no method named `MAX` found for type `u32` in the current scope
22
--> $DIR/dont-suggest-ufcs-for-const.rs:2:11
33
|
44
LL | 1_u32.MAX();
5-
| ------^^^--
6-
| | |
7-
| | this is an associated function, not a method
8-
| help: use associated function syntax instead: `u32::MAX()`
9-
|
10-
= note: found the following associated functions; to be used as methods, functions must have a `self` parameter
11-
= note: the candidate is defined in an impl for the type `u32`
5+
| ^^^ method not found in `u32`
126

137
error: aborting due to previous error
148

0 commit comments

Comments
 (0)