From 12a2489de5e103af0e4a9542c2295faaccd12959 Mon Sep 17 00:00:00 2001 From: lcnr Date: Fri, 14 Apr 2023 14:35:11 +0200 Subject: [PATCH 1/2] disable `needs_drop` special-case for `[T; 0]` --- compiler/rustc_middle/src/ty/util.rs | 18 +------ tests/ui/consts/const-eval/generic-slice.rs | 9 +++- .../ui/consts/const-eval/generic-slice.stderr | 47 +++++++++++++++++-- tests/ui/consts/issue-65348.rs | 5 +- tests/ui/consts/issue-65348.stderr | 30 ++++++++++++ 5 files changed, 84 insertions(+), 25 deletions(-) create mode 100644 tests/ui/consts/issue-65348.stderr diff --git a/compiler/rustc_middle/src/ty/util.rs b/compiler/rustc_middle/src/ty/util.rs index c8a78ec03d947..7e67fe7471175 100644 --- a/compiler/rustc_middle/src/ty/util.rs +++ b/compiler/rustc_middle/src/ty/util.rs @@ -1328,22 +1328,8 @@ pub fn needs_drop_components<'tcx>( ty::Dynamic(..) | ty::Error(_) => Err(AlwaysRequiresDrop), - ty::Slice(ty) => needs_drop_components(*ty, target_layout), - ty::Array(elem_ty, size) => { - match needs_drop_components(*elem_ty, target_layout) { - Ok(v) if v.is_empty() => Ok(v), - res => match size.kind().try_to_bits(target_layout.pointer_size) { - // Arrays of size zero don't need drop, even if their element - // type does. - Some(0) => Ok(SmallVec::new()), - Some(_) => res, - // We don't know which of the cases above we are in, so - // return the whole type and let the caller decide what to - // do. - None => Ok(smallvec![ty]), - }, - } - } + &ty::Slice(ty) | &ty::Array(ty, _) => needs_drop_components(ty, target_layout), + // If any field needs drop, then the whole tuple does. ty::Tuple(fields) => fields.iter().try_fold(SmallVec::new(), move |mut acc, elem| { acc.extend(needs_drop_components(elem, target_layout)?); diff --git a/tests/ui/consts/const-eval/generic-slice.rs b/tests/ui/consts/const-eval/generic-slice.rs index 21360a1c471f6..f0c3145fff13d 100644 --- a/tests/ui/consts/const-eval/generic-slice.rs +++ b/tests/ui/consts/const-eval/generic-slice.rs @@ -7,23 +7,28 @@ struct Generic<'a, T>(std::marker::PhantomData<&'a T>); impl<'a, T: 'static> Generic<'a, T> { const EMPTY_SLICE: &'a [T] = { let x: &'a [T] = &[]; - x + //~^ ERROR destructor of `[T; 0]` cannot be evaluated at compile-time + x }; const EMPTY_SLICE_REF: &'a &'static [T] = { - let x: &'static [T] = &[]; + let x: &'static [T] = &[]; + //~^ ERROR destructor of `[T; 0]` cannot be evaluated at compile-time &x //~^ ERROR `x` does not live long enough + }; } static mut INTERIOR_MUT_AND_DROP: &'static [std::cell::RefCell>] = { let x: &[_] = &[]; + //~^ ERROR destructor of `[RefCell>; 0]` cannot be evaluated at compile-time x }; static mut INTERIOR_MUT_AND_DROP_REF: &'static &'static [std::cell::RefCell>] = { let x: &[_] = &[]; + //~^ ERROR destructor of `[RefCell>; 0]` cannot be evaluated at compile-time &x //~^ ERROR `x` does not live long enough }; diff --git a/tests/ui/consts/const-eval/generic-slice.stderr b/tests/ui/consts/const-eval/generic-slice.stderr index c38088df4d8e6..699c1c1e8c23a 100644 --- a/tests/ui/consts/const-eval/generic-slice.stderr +++ b/tests/ui/consts/const-eval/generic-slice.stderr @@ -1,5 +1,23 @@ +error[E0493]: destructor of `[T; 0]` cannot be evaluated at compile-time + --> $DIR/generic-slice.rs:9:27 + | +LL | let x: &'a [T] = &[]; + | ^^ the destructor for this type cannot be evaluated in constants +... +LL | }; + | - value is dropped here + +error[E0493]: destructor of `[T; 0]` cannot be evaluated at compile-time + --> $DIR/generic-slice.rs:15:32 + | +LL | let x: &'static [T] = &[]; + | ^^ the destructor for this type cannot be evaluated in constants +... +LL | }; + | - value is dropped here + error[E0597]: `x` does not live long enough - --> $DIR/generic-slice.rs:15:9 + --> $DIR/generic-slice.rs:17:9 | LL | impl<'a, T: 'static> Generic<'a, T> { | -- lifetime `'a` defined here @@ -9,12 +27,30 @@ LL | &x | | | borrowed value does not live long enough | using this value as a constant requires that `x` is borrowed for `'a` -LL | +... LL | }; | - `x` dropped here while still borrowed +error[E0493]: destructor of `[RefCell>; 0]` cannot be evaluated at compile-time + --> $DIR/generic-slice.rs:24:20 + | +LL | let x: &[_] = &[]; + | ^^ the destructor for this type cannot be evaluated in statics +... +LL | }; + | - value is dropped here + +error[E0493]: destructor of `[RefCell>; 0]` cannot be evaluated at compile-time + --> $DIR/generic-slice.rs:30:20 + | +LL | let x: &[_] = &[]; + | ^^ the destructor for this type cannot be evaluated in statics +... +LL | }; + | - value is dropped here + error[E0597]: `x` does not live long enough - --> $DIR/generic-slice.rs:27:5 + --> $DIR/generic-slice.rs:32:5 | LL | &x | ^^ @@ -25,6 +61,7 @@ LL | LL | }; | - `x` dropped here while still borrowed -error: aborting due to 2 previous errors +error: aborting due to 6 previous errors -For more information about this error, try `rustc --explain E0597`. +Some errors have detailed explanations: E0493, E0597. +For more information about an error, try `rustc --explain E0493`. diff --git a/tests/ui/consts/issue-65348.rs b/tests/ui/consts/issue-65348.rs index 5eafa831d6317..1f6bea6962967 100644 --- a/tests/ui/consts/issue-65348.rs +++ b/tests/ui/consts/issue-65348.rs @@ -1,5 +1,3 @@ -// check-pass - struct Generic(T); impl Generic { @@ -10,14 +8,17 @@ impl Generic { pub const fn array() -> &'static T { &Generic::::ARRAY[0] + //~^ ERROR destructor of `[T; 0]` cannot be evaluated at compile-time } pub const fn newtype_array() -> &'static T { &Generic::::NEWTYPE_ARRAY.0[0] + //~^ ERROR destructor of `Generic<[T; 0]>` cannot be evaluated at compile-time } pub const fn array_field() -> &'static T { &(Generic::::ARRAY_FIELD.0).1[0] + //~^ ERROR destructor of `Generic<(i32, [T; 0])>` cannot be evaluated at compile-time } fn main() {} diff --git a/tests/ui/consts/issue-65348.stderr b/tests/ui/consts/issue-65348.stderr new file mode 100644 index 0000000000000..6fd1bff29f80f --- /dev/null +++ b/tests/ui/consts/issue-65348.stderr @@ -0,0 +1,30 @@ +error[E0493]: destructor of `[T; 0]` cannot be evaluated at compile-time + --> $DIR/issue-65348.rs:10:6 + | +LL | &Generic::::ARRAY[0] + | ^^^^^^^^^^^^^^^^^^^ the destructor for this type cannot be evaluated in constant functions +LL | +LL | } + | - value is dropped here + +error[E0493]: destructor of `Generic<[T; 0]>` cannot be evaluated at compile-time + --> $DIR/issue-65348.rs:15:6 + | +LL | &Generic::::NEWTYPE_ARRAY.0[0] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ the destructor for this type cannot be evaluated in constant functions +LL | +LL | } + | - value is dropped here + +error[E0493]: destructor of `Generic<(i32, [T; 0])>` cannot be evaluated at compile-time + --> $DIR/issue-65348.rs:20:7 + | +LL | &(Generic::::ARRAY_FIELD.0).1[0] + | ^^^^^^^^^^^^^^^^^^^^^^^^^ the destructor for this type cannot be evaluated in constant functions +LL | +LL | } + | - value is dropped here + +error: aborting due to 3 previous errors + +For more information about this error, try `rustc --explain E0493`. From 9fb09b0a92fd05af563e5e7c0df41a77f82fdd4d Mon Sep 17 00:00:00 2001 From: lcnr Date: Fri, 14 Apr 2023 15:01:18 +0200 Subject: [PATCH 2/2] tidy --- tests/ui/consts/const-eval/generic-slice.rs | 5 ++--- tests/ui/consts/const-eval/generic-slice.stderr | 10 +++++----- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/tests/ui/consts/const-eval/generic-slice.rs b/tests/ui/consts/const-eval/generic-slice.rs index f0c3145fff13d..6610848af4371 100644 --- a/tests/ui/consts/const-eval/generic-slice.rs +++ b/tests/ui/consts/const-eval/generic-slice.rs @@ -8,15 +8,14 @@ impl<'a, T: 'static> Generic<'a, T> { const EMPTY_SLICE: &'a [T] = { let x: &'a [T] = &[]; //~^ ERROR destructor of `[T; 0]` cannot be evaluated at compile-time - x + x }; const EMPTY_SLICE_REF: &'a &'static [T] = { - let x: &'static [T] = &[]; + let x: &'static [T] = &[]; //~^ ERROR destructor of `[T; 0]` cannot be evaluated at compile-time &x //~^ ERROR `x` does not live long enough - }; } diff --git a/tests/ui/consts/const-eval/generic-slice.stderr b/tests/ui/consts/const-eval/generic-slice.stderr index 699c1c1e8c23a..5af1197ff36c1 100644 --- a/tests/ui/consts/const-eval/generic-slice.stderr +++ b/tests/ui/consts/const-eval/generic-slice.stderr @@ -10,7 +10,7 @@ LL | }; error[E0493]: destructor of `[T; 0]` cannot be evaluated at compile-time --> $DIR/generic-slice.rs:15:32 | -LL | let x: &'static [T] = &[]; +LL | let x: &'static [T] = &[]; | ^^ the destructor for this type cannot be evaluated in constants ... LL | }; @@ -27,12 +27,12 @@ LL | &x | | | borrowed value does not live long enough | using this value as a constant requires that `x` is borrowed for `'a` -... +LL | LL | }; | - `x` dropped here while still borrowed error[E0493]: destructor of `[RefCell>; 0]` cannot be evaluated at compile-time - --> $DIR/generic-slice.rs:24:20 + --> $DIR/generic-slice.rs:23:20 | LL | let x: &[_] = &[]; | ^^ the destructor for this type cannot be evaluated in statics @@ -41,7 +41,7 @@ LL | }; | - value is dropped here error[E0493]: destructor of `[RefCell>; 0]` cannot be evaluated at compile-time - --> $DIR/generic-slice.rs:30:20 + --> $DIR/generic-slice.rs:29:20 | LL | let x: &[_] = &[]; | ^^ the destructor for this type cannot be evaluated in statics @@ -50,7 +50,7 @@ LL | }; | - value is dropped here error[E0597]: `x` does not live long enough - --> $DIR/generic-slice.rs:32:5 + --> $DIR/generic-slice.rs:31:5 | LL | &x | ^^