Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disallow non-monomorphic calls to needs_drop in interpreter #85994

Merged
merged 2 commits into from
Jun 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion compiler/rustc_mir/src/interpret/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,12 @@ 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 => {
// Correctly handles non-monomorphic calls, so there is no need for ensure_monomorphic_enough.
let layout = tcx.layout_of(param_env.and(tp_ty)).map_err(|e| err_inval!(Layout(e)))?;
let n = match name {
sym::pref_align_of => layout.align.pref.bytes(),
Expand All @@ -71,6 +75,7 @@ crate fn eval_nullary_intrinsic<'tcx>(
ConstValue::from_u64(tcx.type_id_hash(tp_ty))
}
sym::variant_count => match tp_ty.kind() {
// Correctly handles non-monomorphic calls, so there is no need for ensure_monomorphic_enough.
ty::Adt(ref adt, _) => ConstValue::from_machine_usize(adt.variants.len() as u64, &tcx),
ty::Projection(_)
| ty::Opaque(_, _)
Expand Down
17 changes: 17 additions & 0 deletions src/test/ui/consts/const-needs_drop-monomorphic.rs
Original file line number Diff line number Diff line change
@@ -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>();
}
20 changes: 20 additions & 0 deletions src/test/ui/consts/const-needs_drop-monomorphic.stderr
Original file line number Diff line number Diff line change
@@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This error is useless - it would be good to suppress it. Is there already an issue for this kind of error?

--> $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`.