Skip to content

Commit c78e411

Browse files
committed
Ensure type is monomorphic enough before evaluating size_of / align_of.
1 parent 2c0f486 commit c78e411

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

compiler/rustc_const_eval/src/interpret/intrinsics.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
187187
}
188188
sym::size_of => {
189189
let tp_ty = instance.args.type_at(0);
190+
ensure_monomorphic_enough(tcx, tp_ty)?;
190191
let layout = self.layout_of(tp_ty)?;
191192
if !layout.is_sized() {
192193
span_bug!(self.cur_span(), "unsized type for `size_of`");
@@ -196,6 +197,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
196197
}
197198
sym::align_of => {
198199
let tp_ty = instance.args.type_at(0);
200+
ensure_monomorphic_enough(tcx, tp_ty)?;
199201
let layout = self.layout_of(tp_ty)?;
200202
if !layout.is_sized() {
201203
span_bug!(self.cur_span(), "unsized type for `align_of`");
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Regression test for #149081. Check that an attempt to evaluate size_of / align_of intrinsics
2+
// during an optimization, when type parameter is not monomorphic enough, doesn't lead to a
3+
// compilation failure.
4+
//
5+
//@compile-flags: --crate-type=lib -O
6+
//@build-pass
7+
8+
pub fn size_align_of<T: WithAssoc<Assoc = U>, U>() -> (usize, usize) {
9+
let a = const { std::mem::size_of::<Wrapper<T>>() };
10+
let b = const { std::mem::align_of::<Wrapper<T>>() };
11+
(a, b)
12+
}
13+
14+
pub struct Wrapper<T: WithAssoc> {
15+
pub assoc2: <T::Assoc as WithAssoc>::Assoc,
16+
pub value: T,
17+
}
18+
19+
pub trait WithAssoc {
20+
type Assoc: WithAssoc;
21+
}

0 commit comments

Comments
 (0)