Skip to content

Commit

Permalink
Ensure that we never try to monomorphize the upcasting of impossible …
Browse files Browse the repository at this point in the history
…dyn types
  • Loading branch information
compiler-errors committed Jan 18, 2025
1 parent 8a460d3 commit 83fb0ff
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
21 changes: 21 additions & 0 deletions compiler/rustc_trait_selection/src/traits/vtable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,11 @@ fn vtable_entries<'tcx>(
"vtable trait ref should be normalized"
);

// We're monomorphizing a dyn trait object that can never be constructed.
if tcx.instantiate_and_check_impossible_predicates((trait_ref.def_id, trait_ref.args)) {
return TyCtxt::COMMON_VTABLE_ENTRIES;
}

debug!("vtable_entries({:?})", trait_ref);

let mut entries = vec![];
Expand Down Expand Up @@ -323,6 +328,14 @@ pub(crate) fn first_method_vtable_slot<'tcx>(tcx: TyCtxt<'tcx>, key: ty::TraitRe
source.principal().unwrap().with_self_ty(tcx, tcx.types.trait_object_dummy_self),
);

// We're monomorphizing a dyn trait object that can never be constructed.
if tcx.instantiate_and_check_impossible_predicates((
source_principal.def_id,
source_principal.args,
)) {
return 0;
}

let target_principal = ty::ExistentialTraitRef::erase_self_ty(tcx, key);

let vtable_segment_callback = {
Expand Down Expand Up @@ -388,6 +401,14 @@ pub(crate) fn supertrait_vtable_slot<'tcx>(
source.principal().unwrap().with_self_ty(tcx, tcx.types.trait_object_dummy_self),
);

// We're monomorphizing a dyn trait object that can never be constructed.
if tcx.instantiate_and_check_impossible_predicates((
source_principal.def_id,
source_principal.args,
)) {
return None;
}

let vtable_segment_callback = {
let mut vptr_offset = 0;
move |segment| {
Expand Down
24 changes: 24 additions & 0 deletions tests/ui/traits/trait-upcasting/mono-impossible.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#![feature(trait_upcasting)]

trait Supertrait<T> {
fn method(&self) {}
}
impl<T> Supertrait<T> for () {}

trait WithAssoc {
type Assoc;
}
trait Trait<P: WithAssoc>: Supertrait<P::Assoc> + Supertrait<()> {}

fn upcast<P>(x: &dyn Trait<P>) -> &dyn Supertrait<()> {
x
}

fn call<P>(x: &dyn Trait<P>) {
x.method();
}

fn main() {
println!("{:p}", upcast::<()> as *const ());
println!("{:p}", call::<()> as *const ());
}

0 comments on commit 83fb0ff

Please sign in to comment.