Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit cf8986d

Browse filesBrowse files
committed
Auto merge of rust-lang#136311 - compiler-errors:vtable-fixes-2, r=<try>
Ensure that we never try to monomorphize the upcasting or vtable calls of impossible dyn types Check for impossible obligations in the `dyn Trait` type we're trying to compute its the vtable upcasting and method call slots. r? lcnr
2 parents 820bfff + 0a8b81e commit cf8986d
Copy full SHA for cf8986d

File tree

Expand file treeCollapse file tree

2 files changed

+47
-5
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+47
-5
lines changed

‎compiler/rustc_trait_selection/src/traits/vtable.rs

Copy file name to clipboardExpand all lines: compiler/rustc_trait_selection/src/traits/vtable.rs
+21-5Lines changed: 21 additions & 5 deletions
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| {
+26Lines changed: 26 additions & 0 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.