Skip to content

Commit 785b47d

Browse files
authored
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`
2 parents fd02567 + 7054287 commit 785b47d

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

compiler/rustc_hir_typeck/src/method/probe.rs

+17-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};
@@ -1876,6 +1875,15 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
18761875
self.tcx.erase_late_bound_regions(value)
18771876
}
18781877

1878+
/// Determine if the given associated item type is relevant in the current context.
1879+
fn is_relevant_kind_for_mode(&self, kind: ty::AssocKind) -> bool {
1880+
match (self.mode, kind) {
1881+
(Mode::MethodCall, ty::AssocKind::Fn) => true,
1882+
(Mode::Path, ty::AssocKind::Const | ty::AssocKind::Fn) => true,
1883+
_ => false,
1884+
}
1885+
}
1886+
18791887
/// Finds the method with the appropriate name (or return type, as the case may be). If
18801888
/// `allow_similar_names` is set, find methods with close-matching names.
18811889
// The length of the returned iterator is nearly always 0 or 1 and this
@@ -1888,7 +1896,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
18881896
.associated_items(def_id)
18891897
.in_definition_order()
18901898
.filter(|x| {
1891-
if x.kind.namespace() != Namespace::ValueNS {
1899+
if !self.is_relevant_kind_for_mode(x.kind) {
18921900
return false;
18931901
}
18941902
match lev_distance_with_substrings(name.as_str(), x.name.as_str(), max_dist)
@@ -1902,10 +1910,16 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
19021910
} else {
19031911
self.fcx
19041912
.associated_value(def_id, name)
1913+
.filter(|x| self.is_relevant_kind_for_mode(x.kind))
19051914
.map_or_else(SmallVec::new, |x| SmallVec::from_buf([x]))
19061915
}
19071916
} else {
1908-
self.tcx.associated_items(def_id).in_definition_order().copied().collect()
1917+
self.tcx
1918+
.associated_items(def_id)
1919+
.in_definition_order()
1920+
.filter(|x| self.is_relevant_kind_for_mode(x.kind))
1921+
.copied()
1922+
.collect()
19091923
}
19101924
}
19111925
}

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)