Skip to content

Commit

Permalink
Rollup merge of #104856 - luqmana:associated-const-bad-suggestion, r=…
Browse files Browse the repository at this point in the history
…compiler-errors

Don't suggest associated function call for associated const.

Fixes #104801.

r? `@compiler-errors`
  • Loading branch information
matthiaskrgr authored Dec 4, 2022
2 parents fd02567 + 7054287 commit 785b47d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
20 changes: 17 additions & 3 deletions compiler/rustc_hir_typeck/src/method/probe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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()
}
}
}
Expand Down
8 changes: 1 addition & 7 deletions src/test/ui/suggestions/dont-suggest-ufcs-for-const.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 785b47d

Please sign in to comment.