Skip to content

Commit

Permalink
Rollup merge of rust-lang#107803 - eggyal:do_not_bring_trait_alias_su…
Browse files Browse the repository at this point in the history
…pertraits_into_scope, r=compiler-errors

Do not bring trait alias supertraits into scope

Fixes rust-lang#107747
cc rust-lang#41517
  • Loading branch information
Dylan-DPC authored Feb 9, 2023
2 parents 39ba110 + 38ec810 commit 16a4138
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 17 deletions.
48 changes: 31 additions & 17 deletions compiler/rustc_hir_typeck/src/method/probe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -951,24 +951,38 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
let trait_ref = self.tcx.mk_trait_ref(trait_def_id, trait_substs);

if self.tcx.is_trait_alias(trait_def_id) {
// For trait aliases, assume all supertraits are relevant.
let bounds = iter::once(ty::Binder::dummy(trait_ref));
self.elaborate_bounds(bounds, |this, new_trait_ref, item| {
let new_trait_ref = this.erase_late_bound_regions(new_trait_ref);
// For trait aliases, recursively assume all explicitly named traits are relevant
for expansion in traits::expand_trait_aliases(
self.tcx,
iter::once((ty::Binder::dummy(trait_ref), self.span)),
) {
let bound_trait_ref = expansion.trait_ref();
for item in self.impl_or_trait_item(bound_trait_ref.def_id()) {
if !self.has_applicable_self(&item) {
self.record_static_candidate(CandidateSource::Trait(
bound_trait_ref.def_id(),
));
} else {
let new_trait_ref = self.erase_late_bound_regions(bound_trait_ref);

let (xform_self_ty, xform_ret_ty) =
this.xform_self_ty(&item, new_trait_ref.self_ty(), new_trait_ref.substs);
this.push_candidate(
Candidate {
xform_self_ty,
xform_ret_ty,
item,
import_ids: import_ids.clone(),
kind: TraitCandidate(new_trait_ref),
},
false,
);
});
let (xform_self_ty, xform_ret_ty) = self.xform_self_ty(
&item,
new_trait_ref.self_ty(),
new_trait_ref.substs,
);
self.push_candidate(
Candidate {
xform_self_ty,
xform_ret_ty,
item,
import_ids: import_ids.clone(),
kind: TraitCandidate(new_trait_ref),
},
false,
);
}
}
}
} else {
debug_assert!(self.tcx.is_trait(trait_def_id));
if self.tcx.trait_is_auto(trait_def_id) {
Expand Down
21 changes: 21 additions & 0 deletions tests/ui/traits/alias/issue-107747-do-not-assemble-supertraits.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Regression test for #107747: methods from trait alias supertraits were brought into scope
//
// check-pass

#![feature(trait_alias)]

use std::fmt;

trait Foo: fmt::Debug {}
trait Bar = Foo;

#[derive(Debug)]
struct Qux(bool);

impl fmt::Display for Qux {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}

fn main() {}

0 comments on commit 16a4138

Please sign in to comment.