Skip to content

Commit fd95d12

Browse files
Hack around compiler builtins' Float/Int traits shadowing built-in methods
1 parent 6467f73 commit fd95d12

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

compiler/rustc_hir_typeck/src/method/probe.rs

+24
Original file line numberDiff line numberDiff line change
@@ -1254,6 +1254,10 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
12541254
for (kind, candidates) in
12551255
[("inherent", &self.inherent_candidates), ("extension", &self.extension_candidates)]
12561256
{
1257+
if kind == "inherent" && self.should_skip_shadowable_inherent_numerical_methods() {
1258+
continue;
1259+
}
1260+
12571261
debug!("searching {} candidates", kind);
12581262
let res = self.consider_candidates(
12591263
self_ty,
@@ -1690,6 +1694,26 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
16901694
})
16911695
}
16921696

1697+
fn should_skip_shadowable_inherent_numerical_methods(&self) -> bool {
1698+
let res = self.inherent_candidates.len() > 1
1699+
&& self.extension_candidates.iter().any(|cand| match cand.kind {
1700+
TraitCandidate(trait_ref) => {
1701+
let trait_def_id = trait_ref.def_id();
1702+
self.tcx.crate_name(trait_def_id.krate) == sym::compiler_builtins
1703+
&& [sym::Float, sym::Int].contains(&self.tcx.item_name(trait_def_id))
1704+
}
1705+
InherentImplCandidate(_) | ObjectCandidate(_) | WhereClauseCandidate(_) => false,
1706+
})
1707+
&& self.inherent_candidates.iter().all(|cand| match cand.kind {
1708+
InherentImplCandidate(def_id) => {
1709+
self.tcx.type_of(def_id).skip_binder().is_numeric()
1710+
}
1711+
ObjectCandidate(_) | TraitCandidate(_) | WhereClauseCandidate(_) => false,
1712+
});
1713+
1714+
res
1715+
}
1716+
16931717
/// Sometimes we get in a situation where we have multiple probes that are all impls of the
16941718
/// same trait, but we don't know which impl to use. In this case, since in all cases the
16951719
/// external interface of the method can be determined from the trait, it's ok not to decide.

compiler/rustc_span/src/symbol.rs

+2
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ symbols! {
211211
Error,
212212
File,
213213
FileType,
214+
Float,
214215
Fn,
215216
FnMut,
216217
FnOnce,
@@ -233,6 +234,7 @@ symbols! {
233234
IndexOutput,
234235
Input,
235236
Instant,
237+
Int,
236238
Into,
237239
IntoFuture,
238240
IntoIterator,

0 commit comments

Comments
 (0)