Skip to content

Commit 0a8b81e

Browse files
Ensure that we never try to monomorphize the upcasting of impossible dyn types
1 parent 7f36543 commit 0a8b81e

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed

Diff for: compiler/rustc_trait_selection/src/traits/vtable.rs

+21-5
Original file line numberDiff line numberDiff line change
@@ -318,9 +318,17 @@ pub(crate) fn first_method_vtable_slot<'tcx>(tcx: TyCtxt<'tcx>, key: ty::TraitRe
318318
bug!();
319319
};
320320
let source_principal = tcx.instantiate_bound_regions_with_erased(
321-
source.principal().unwrap().with_self_ty(tcx, tcx.types.trait_object_dummy_self),
321+
source.principal().unwrap().with_self_ty(tcx, key.self_ty()),
322322
);
323323

324+
// We're monomorphizing a call to a dyn trait object that can never be constructed.
325+
if tcx.instantiate_and_check_impossible_predicates((
326+
source_principal.def_id,
327+
source_principal.args,
328+
)) {
329+
return 0;
330+
}
331+
324332
let target_principal = ty::ExistentialTraitRef::erase_self_ty(tcx, key);
325333

326334
let vtable_segment_callback = {
@@ -373,19 +381,27 @@ pub(crate) fn supertrait_vtable_slot<'tcx>(
373381
let (source, target) = key;
374382

375383
// If the target principal is `None`, we can just return `None`.
376-
let ty::Dynamic(target, _, _) = *target.kind() else {
384+
let ty::Dynamic(target_data, _, _) = *target.kind() else {
377385
bug!();
378386
};
379-
let target_principal = tcx.instantiate_bound_regions_with_erased(target.principal()?);
387+
let target_principal = tcx.instantiate_bound_regions_with_erased(target_data.principal()?);
380388

381389
// Given that we have a target principal, it is a bug for there not to be a source principal.
382-
let ty::Dynamic(source, _, _) = *source.kind() else {
390+
let ty::Dynamic(source_data, _, _) = *source.kind() else {
383391
bug!();
384392
};
385393
let source_principal = tcx.instantiate_bound_regions_with_erased(
386-
source.principal().unwrap().with_self_ty(tcx, tcx.types.trait_object_dummy_self),
394+
source_data.principal().unwrap().with_self_ty(tcx, source),
387395
);
388396

397+
// We're monomorphizing a dyn trait object upcast that can never be constructed.
398+
if tcx.instantiate_and_check_impossible_predicates((
399+
source_principal.def_id,
400+
source_principal.args,
401+
)) {
402+
return None;
403+
}
404+
389405
let vtable_segment_callback = {
390406
let mut vptr_offset = 0;
391407
move |segment| {

Diff for: tests/ui/traits/trait-upcasting/mono-impossible.rs

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//@ build-pass
2+
3+
#![feature(trait_upcasting)]
4+
5+
trait Supertrait<T> {
6+
fn method(&self) {}
7+
}
8+
impl<T> Supertrait<T> for () {}
9+
10+
trait WithAssoc {
11+
type Assoc;
12+
}
13+
trait Trait<P: WithAssoc>: Supertrait<P::Assoc> + Supertrait<()> {}
14+
15+
fn upcast<P>(x: &dyn Trait<P>) -> &dyn Supertrait<()> {
16+
x
17+
}
18+
19+
fn call<P>(x: &dyn Trait<P>) {
20+
x.method();
21+
}
22+
23+
fn main() {
24+
println!("{:p}", upcast::<()> as *const ());
25+
println!("{:p}", call::<()> as *const ());
26+
}

0 commit comments

Comments
 (0)