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 041296d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
26 changes: 22 additions & 4 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 @@ -316,6 +321,11 @@ pub(crate) fn first_method_vtable_slot<'tcx>(tcx: TyCtxt<'tcx>, key: ty::TraitRe
"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((key.def_id, key.args)) {
return 0;
}

let ty::Dynamic(source, _, _) = *key.self_ty().kind() else {
bug!();
};
Expand Down Expand Up @@ -375,19 +385,27 @@ pub(crate) fn supertrait_vtable_slot<'tcx>(
let (source, target) = key;

// If the target principal is `None`, we can just return `None`.
let ty::Dynamic(target, _, _) = *target.kind() else {
let ty::Dynamic(target_data, _, _) = *target.kind() else {
bug!();
};
let target_principal = tcx.instantiate_bound_regions_with_erased(target.principal()?);
let target_principal = tcx.instantiate_bound_regions_with_erased(target_data.principal()?);

// Given that we have a target principal, it is a bug for there not to be a source principal.
let ty::Dynamic(source, _, _) = *source.kind() else {
let ty::Dynamic(source_data, _, _) = *source.kind() else {
bug!();
};
let source_principal = tcx.instantiate_bound_regions_with_erased(
source.principal().unwrap().with_self_ty(tcx, tcx.types.trait_object_dummy_self),
source_data.principal().unwrap().with_self_ty(tcx, source),
);

// 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 041296d

Please sign in to comment.