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..6610848af4371 100644 --- a/tests/ui/consts/const-eval/generic-slice.rs +++ b/tests/ui/consts/const-eval/generic-slice.rs @@ -7,11 +7,13 @@ 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] = &[]; + //~^ ERROR destructor of `[T; 0]` cannot be evaluated at compile-time x }; const EMPTY_SLICE_REF: &'a &'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 }; @@ -19,11 +21,13 @@ impl<'a, T: 'static> Generic<'a, T> { 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..5af1197ff36c1 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 @@ -13,8 +31,26 @@ LL | LL | }; | - `x` dropped here while still borrowed +error[E0493]: destructor of `[RefCell>; 0]` cannot be evaluated at compile-time + --> $DIR/generic-slice.rs:23: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:29: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:31: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`.