Skip to content

Commit e2fbd01

Browse files
committed
Compare picks via Self type and associated item id
1 parent d06aac1 commit e2fbd01

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

compiler/rustc_hir_typeck/src/method/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
209209
call_expr,
210210
ProbeScope::TraitsInScope,
211211
) {
212-
Ok(ref new_pick) if new_pick.self_ty != pick.self_ty => {
212+
Ok(ref new_pick) if pick.differs_from(new_pick) => {
213213
needs_mut = true;
214214
}
215215
_ => {}
@@ -220,7 +220,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
220220
let mut candidates =
221221
match self.lookup_probe(segment.ident, self_ty, call_expr, ProbeScope::AllTraits) {
222222
// If we find a different result the caller probably forgot to import a trait.
223-
Ok(ref new_pick) if new_pick.self_ty != pick.self_ty => {
223+
Ok(ref new_pick) if pick.differs_from(new_pick) => {
224224
vec![new_pick.item.container_id(self.tcx)]
225225
}
226226
Err(Ambiguity(ref sources)) => sources

compiler/rustc_hir_typeck/src/method/probe.rs

+26
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use rustc_infer::infer::{self, InferOk, TyCtxtInferExt};
1717
use rustc_middle::infer::unify_key::{ConstVariableOrigin, ConstVariableOriginKind};
1818
use rustc_middle::middle::stability;
1919
use rustc_middle::ty::fast_reject::{simplify_type, TreatParams};
20+
use rustc_middle::ty::AssocItem;
2021
use rustc_middle::ty::GenericParamDefKind;
2122
use rustc_middle::ty::{self, ParamEnvAnd, ToPredicate, Ty, TyCtxt, TypeFoldable, TypeVisitable};
2223
use rustc_middle::ty::{InternalSubsts, SubstsRef};
@@ -1331,6 +1332,31 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
13311332
}
13321333

13331334
impl<'tcx> Pick<'tcx> {
1335+
/// In case there were unstable name collisions, emit them as a lint.
1336+
/// Checks whether two picks do not refer to the same trait item for the same `Self` type.
1337+
/// Only useful for comparisons of picks in order to improve diagnostics.
1338+
/// Do not use for type checking.
1339+
pub fn differs_from(&self, other: &Self) -> bool {
1340+
let Self {
1341+
item:
1342+
AssocItem {
1343+
def_id,
1344+
name: _,
1345+
kind: _,
1346+
container: _,
1347+
trait_item_def_id: _,
1348+
fn_has_self_parameter: _,
1349+
},
1350+
kind: _,
1351+
import_ids: _,
1352+
autoderefs: _,
1353+
autoref_or_ptr_adjustment: _,
1354+
self_ty,
1355+
unstable_candidates: _,
1356+
} = *self;
1357+
self_ty != other.self_ty || def_id != other.item.def_id
1358+
}
1359+
13341360
/// In case there were unstable name collisions, emit them as a lint.
13351361
pub fn maybe_emit_unstable_name_collision_hint(
13361362
&self,

0 commit comments

Comments
 (0)