Skip to content

Commit 16a4138

Browse files
authored
Rollup merge of rust-lang#107803 - eggyal:do_not_bring_trait_alias_supertraits_into_scope, r=compiler-errors
Do not bring trait alias supertraits into scope Fixes rust-lang#107747 cc rust-lang#41517
2 parents 39ba110 + 38ec810 commit 16a4138

File tree

2 files changed

+52
-17
lines changed

2 files changed

+52
-17
lines changed

compiler/rustc_hir_typeck/src/method/probe.rs

+31-17
Original file line numberDiff line numberDiff line change
@@ -951,24 +951,38 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
951951
let trait_ref = self.tcx.mk_trait_ref(trait_def_id, trait_substs);
952952

953953
if self.tcx.is_trait_alias(trait_def_id) {
954-
// For trait aliases, assume all supertraits are relevant.
955-
let bounds = iter::once(ty::Binder::dummy(trait_ref));
956-
self.elaborate_bounds(bounds, |this, new_trait_ref, item| {
957-
let new_trait_ref = this.erase_late_bound_regions(new_trait_ref);
954+
// For trait aliases, recursively assume all explicitly named traits are relevant
955+
for expansion in traits::expand_trait_aliases(
956+
self.tcx,
957+
iter::once((ty::Binder::dummy(trait_ref), self.span)),
958+
) {
959+
let bound_trait_ref = expansion.trait_ref();
960+
for item in self.impl_or_trait_item(bound_trait_ref.def_id()) {
961+
if !self.has_applicable_self(&item) {
962+
self.record_static_candidate(CandidateSource::Trait(
963+
bound_trait_ref.def_id(),
964+
));
965+
} else {
966+
let new_trait_ref = self.erase_late_bound_regions(bound_trait_ref);
958967

959-
let (xform_self_ty, xform_ret_ty) =
960-
this.xform_self_ty(&item, new_trait_ref.self_ty(), new_trait_ref.substs);
961-
this.push_candidate(
962-
Candidate {
963-
xform_self_ty,
964-
xform_ret_ty,
965-
item,
966-
import_ids: import_ids.clone(),
967-
kind: TraitCandidate(new_trait_ref),
968-
},
969-
false,
970-
);
971-
});
968+
let (xform_self_ty, xform_ret_ty) = self.xform_self_ty(
969+
&item,
970+
new_trait_ref.self_ty(),
971+
new_trait_ref.substs,
972+
);
973+
self.push_candidate(
974+
Candidate {
975+
xform_self_ty,
976+
xform_ret_ty,
977+
item,
978+
import_ids: import_ids.clone(),
979+
kind: TraitCandidate(new_trait_ref),
980+
},
981+
false,
982+
);
983+
}
984+
}
985+
}
972986
} else {
973987
debug_assert!(self.tcx.is_trait(trait_def_id));
974988
if self.tcx.trait_is_auto(trait_def_id) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Regression test for #107747: methods from trait alias supertraits were brought into scope
2+
//
3+
// check-pass
4+
5+
#![feature(trait_alias)]
6+
7+
use std::fmt;
8+
9+
trait Foo: fmt::Debug {}
10+
trait Bar = Foo;
11+
12+
#[derive(Debug)]
13+
struct Qux(bool);
14+
15+
impl fmt::Display for Qux {
16+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
17+
self.0.fmt(f)
18+
}
19+
}
20+
21+
fn main() {}

0 commit comments

Comments
 (0)