Skip to content

Commit 66ba810

Browse files
committed
Auto merge of #85994 - tmiasko:monomorphic-needs-drop, r=RalfJung
Disallow non-monomorphic calls to `needs_drop` in interpreter otherwise evaluation could change after further substitutions.
2 parents 68aa6b2 + 07a03b0 commit 66ba810

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

compiler/rustc_mir/src/interpret/intrinsics.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,12 @@ crate fn eval_nullary_intrinsic<'tcx>(
5656
let alloc = type_name::alloc_type_name(tcx, tp_ty);
5757
ConstValue::Slice { data: alloc, start: 0, end: alloc.len() }
5858
}
59-
sym::needs_drop => ConstValue::from_bool(tp_ty.needs_drop(tcx, param_env)),
59+
sym::needs_drop => {
60+
ensure_monomorphic_enough(tcx, tp_ty)?;
61+
ConstValue::from_bool(tp_ty.needs_drop(tcx, param_env))
62+
}
6063
sym::min_align_of | sym::pref_align_of => {
64+
// Correctly handles non-monomorphic calls, so there is no need for ensure_monomorphic_enough.
6165
let layout = tcx.layout_of(param_env.and(tp_ty)).map_err(|e| err_inval!(Layout(e)))?;
6266
let n = match name {
6367
sym::pref_align_of => layout.align.pref.bytes(),
@@ -71,6 +75,7 @@ crate fn eval_nullary_intrinsic<'tcx>(
7175
ConstValue::from_u64(tcx.type_id_hash(tp_ty))
7276
}
7377
sym::variant_count => match tp_ty.kind() {
78+
// Correctly handles non-monomorphic calls, so there is no need for ensure_monomorphic_enough.
7479
ty::Adt(ref adt, _) => ConstValue::from_machine_usize(adt.variants.len() as u64, &tcx),
7580
ty::Projection(_)
7681
| ty::Opaque(_, _)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Check that evaluation of needs_drop<T> fails when T is not monomorphic.
2+
#![feature(const_generics)]
3+
#![allow(const_evaluatable_unchecked)]
4+
#![allow(incomplete_features)]
5+
6+
struct Bool<const B: bool> {}
7+
impl Bool<true> {
8+
fn assert() {}
9+
}
10+
fn f<T>() {
11+
Bool::<{ std::mem::needs_drop::<T>() }>::assert();
12+
//~^ ERROR no function or associated item named `assert` found
13+
//~| ERROR constant expression depends on a generic parameter
14+
}
15+
fn main() {
16+
f::<u32>();
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error[E0599]: no function or associated item named `assert` found for struct `Bool<{ std::mem::needs_drop::<T>() }>` in the current scope
2+
--> $DIR/const-needs_drop-monomorphic.rs:11:46
3+
|
4+
LL | struct Bool<const B: bool> {}
5+
| -------------------------- function or associated item `assert` not found for this
6+
...
7+
LL | Bool::<{ std::mem::needs_drop::<T>() }>::assert();
8+
| ^^^^^^ function or associated item cannot be called on `Bool<{ std::mem::needs_drop::<T>() }>` due to unsatisfied trait bounds
9+
10+
error: constant expression depends on a generic parameter
11+
--> $DIR/const-needs_drop-monomorphic.rs:11:5
12+
|
13+
LL | Bool::<{ std::mem::needs_drop::<T>() }>::assert();
14+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
15+
|
16+
= note: this may fail depending on what value the parameter takes
17+
18+
error: aborting due to 2 previous errors
19+
20+
For more information about this error, try `rustc --explain E0599`.

0 commit comments

Comments
 (0)