Disallow non-monomorphic calls to needs_drop in interpreter

otherwise evaluation could change after further substitutions.
This commit is contained in:
Tomasz Miąsko 2021-06-04 00:00:00 +00:00
parent 5ea19239d9
commit 894b42c861
3 changed files with 41 additions and 1 deletions

View File

@ -56,7 +56,10 @@ crate fn eval_nullary_intrinsic<'tcx>(
let alloc = type_name::alloc_type_name(tcx, tp_ty);
ConstValue::Slice { data: alloc, start: 0, end: alloc.len() }
}
sym::needs_drop => ConstValue::from_bool(tp_ty.needs_drop(tcx, param_env)),
sym::needs_drop => {
ensure_monomorphic_enough(tcx, tp_ty)?;
ConstValue::from_bool(tp_ty.needs_drop(tcx, param_env))
}
sym::min_align_of | sym::pref_align_of => {
let layout = tcx.layout_of(param_env.and(tp_ty)).map_err(|e| err_inval!(Layout(e)))?;
let n = match name {

View File

@ -0,0 +1,17 @@
// Check that evaluation of needs_drop<T> fails when T is not monomorphic.
#![feature(const_generics)]
#![allow(const_evaluatable_unchecked)]
#![allow(incomplete_features)]
struct Bool<const B: bool> {}
impl Bool<true> {
fn assert() {}
}
fn f<T>() {
Bool::<{ std::mem::needs_drop::<T>() }>::assert();
//~^ ERROR no function or associated item named `assert` found
//~| ERROR constant expression depends on a generic parameter
}
fn main() {
f::<u32>();
}

View File

@ -0,0 +1,20 @@
error[E0599]: no function or associated item named `assert` found for struct `Bool<{ std::mem::needs_drop::<T>() }>` in the current scope
--> $DIR/const-needs_drop-monomorphic.rs:11:46
|
LL | struct Bool<const B: bool> {}
| -------------------------- function or associated item `assert` not found for this
...
LL | Bool::<{ std::mem::needs_drop::<T>() }>::assert();
| ^^^^^^ function or associated item cannot be called on `Bool<{ std::mem::needs_drop::<T>() }>` due to unsatisfied trait bounds
error: constant expression depends on a generic parameter
--> $DIR/const-needs_drop-monomorphic.rs:11:5
|
LL | Bool::<{ std::mem::needs_drop::<T>() }>::assert();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: this may fail depending on what value the parameter takes
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0599`.