Skip to content

Commit eb75d20

Browse files
Relax a debug assertion in codegen
1 parent c3ce4e6 commit eb75d20

File tree

5 files changed

+49
-20
lines changed

5 files changed

+49
-20
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -3448,6 +3448,7 @@ dependencies = [
34483448
"rustc_span",
34493449
"rustc_symbol_mangling",
34503450
"rustc_target",
3451+
"rustc_trait_selection",
34513452
"rustc_type_ir",
34523453
"serde_json",
34533454
"smallvec",

compiler/rustc_codegen_cranelift/src/unsize.rs

+3-14
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
//!
33
//! [`PointerCoercion::Unsize`]: `rustc_middle::ty::adjustment::PointerCoercion::Unsize`
44
5+
use rustc_codegen_ssa::base::validate_trivial_unsize;
56
use rustc_middle::ty::print::{with_no_trimmed_paths, with_no_visible_paths};
67

78
use crate::base::codegen_panic_nounwind;
@@ -34,20 +35,8 @@ pub(crate) fn unsized_info<'tcx>(
3435
let old_info =
3536
old_info.expect("unsized_info: missing old info for trait upcasting coercion");
3637
if data_a.principal_def_id() == data_b.principal_def_id() {
37-
// Codegen takes advantage of the additional assumption, where if the
38-
// principal trait def id of what's being casted doesn't change,
39-
// then we don't need to adjust the vtable at all. This
40-
// corresponds to the fact that `dyn Tr<A>: Unsize<dyn Tr<B>>`
41-
// requires that `A = B`; we don't allow *upcasting* objects
42-
// between the same trait with different args. If we, for
43-
// some reason, were to relax the `Unsize` trait, it could become
44-
// unsound, so let's assert here that the trait refs are *equal*.
45-
//
46-
// We can use `assert_eq` because the binders should have been anonymized,
47-
// and because higher-ranked equality now requires the binders are equal.
48-
debug_assert_eq!(
49-
data_a.principal(),
50-
data_b.principal(),
38+
debug_assert!(
39+
validate_trivial_unsize(fx.tcx, data_a, data_b),
5140
"NOP unsize vtable changed principal trait ref: {data_a} -> {data_b}"
5241
);
5342
return old_info;

compiler/rustc_codegen_ssa/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ rustc_session = { path = "../rustc_session" }
3434
rustc_span = { path = "../rustc_span" }
3535
rustc_symbol_mangling = { path = "../rustc_symbol_mangling" }
3636
rustc_target = { path = "../rustc_target" }
37+
rustc_trait_selection = { path = "../rustc_trait_selection" }
3738
rustc_type_ir = { path = "../rustc_type_ir" }
3839
serde_json = "1.0.59"
3940
smallvec = { version = "1.8.1", features = ["union", "may_dangle"] }

compiler/rustc_codegen_ssa/src/base.rs

+36-6
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ use rustc_session::config::{self, CrateType, EntryFnType, OptLevel, OutputType};
2727
use rustc_span::symbol::sym;
2828
use rustc_span::{DUMMY_SP, Symbol};
2929
use rustc_target::abi::FIRST_VARIANT;
30+
use rustc_trait_selection::infer::TyCtxtInferExt;
31+
use rustc_trait_selection::traits::{ObligationCause, ObligationCtxt};
3032
use tracing::{debug, info};
3133

3234
use crate::assert_module_sources::CguReuse;
@@ -101,6 +103,38 @@ pub fn compare_simd_types<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
101103
bx.sext(cmp, ret_ty)
102104
}
103105

106+
/// Codegen takes advantage of the additional assumption, where if the
107+
/// principal trait def id of what's being casted doesn't change,
108+
/// then we don't need to adjust the vtable at all. This
109+
/// corresponds to the fact that `dyn Tr<A>: Unsize<dyn Tr<B>>`
110+
/// requires that `A = B`; we don't allow *upcasting* objects
111+
/// between the same trait with different args. If we, for
112+
/// some reason, were to relax the `Unsize` trait, it could become
113+
/// unsound, so let's validate here that the trait refs are subtypes.
114+
pub fn validate_trivial_unsize<'tcx>(
115+
tcx: TyCtxt<'tcx>,
116+
data_a: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
117+
data_b: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
118+
) -> bool {
119+
match (data_a.principal(), data_b.principal()) {
120+
(Some(principal_a), Some(principal_b)) => {
121+
let infcx = tcx.infer_ctxt().build();
122+
let ocx = ObligationCtxt::new(&infcx);
123+
let Ok(()) = ocx.sub(
124+
&ObligationCause::dummy(),
125+
ty::ParamEnv::reveal_all(),
126+
principal_a,
127+
principal_b,
128+
) else {
129+
return false;
130+
};
131+
ocx.select_all_or_error().is_empty()
132+
}
133+
(None, None) => true,
134+
_ => false,
135+
}
136+
}
137+
104138
/// Retrieves the information we are losing (making dynamic) in an unsizing
105139
/// adjustment.
106140
///
@@ -133,12 +167,8 @@ fn unsized_info<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
133167
// between the same trait with different args. If we, for
134168
// some reason, were to relax the `Unsize` trait, it could become
135169
// unsound, so let's assert here that the trait refs are *equal*.
136-
//
137-
// We can use `assert_eq` because the binders should have been anonymized,
138-
// and because higher-ranked equality now requires the binders are equal.
139-
debug_assert_eq!(
140-
data_a.principal(),
141-
data_b.principal(),
170+
debug_assert!(
171+
validate_trivial_unsize(cx.tcx(), data_a, data_b),
142172
"NOP unsize vtable changed principal trait ref: {data_a} -> {data_b}"
143173
);
144174

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//@ build-pass
2+
3+
// Regression test for an overly aggressive assertion in #130855.
4+
5+
fn main() {
6+
let subtype: &(dyn for<'a> Fn(&'a i32) -> &'a i32) = &|x| x;
7+
let supertype: &(dyn Fn(&'static i32) -> &'static i32) = subtype;
8+
}

0 commit comments

Comments
 (0)