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

disable needs_drop special-case for [T; 0] #110322

Closed
wants to merge 2 commits into from
Closed
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
18 changes: 2 additions & 16 deletions compiler/rustc_middle/src/ty/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?);
Expand Down
4 changes: 4 additions & 0 deletions tests/ui/consts/const-eval/generic-slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,27 @@ 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
};
}

static mut INTERIOR_MUT_AND_DROP: &'static [std::cell::RefCell<Vec<i32>>] = {
let x: &[_] = &[];
//~^ ERROR destructor of `[RefCell<Vec<i32>>; 0]` cannot be evaluated at compile-time
x
};

static mut INTERIOR_MUT_AND_DROP_REF: &'static &'static [std::cell::RefCell<Vec<i32>>] = {
let x: &[_] = &[];
//~^ ERROR destructor of `[RefCell<Vec<i32>>; 0]` cannot be evaluated at compile-time
&x
//~^ ERROR `x` does not live long enough
};
Expand Down
45 changes: 41 additions & 4 deletions tests/ui/consts/const-eval/generic-slice.stderr
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -13,8 +31,26 @@ LL |
LL | };
| - `x` dropped here while still borrowed

error[E0493]: destructor of `[RefCell<Vec<i32>>; 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<Vec<i32>>; 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
| ^^
Expand All @@ -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`.
5 changes: 3 additions & 2 deletions tests/ui/consts/issue-65348.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// check-pass

struct Generic<T>(T);

impl<T> Generic<T> {
Expand All @@ -10,14 +8,17 @@ impl<T> Generic<T> {

pub const fn array<T>() -> &'static T {
&Generic::<T>::ARRAY[0]
//~^ ERROR destructor of `[T; 0]` cannot be evaluated at compile-time
}

pub const fn newtype_array<T>() -> &'static T {
&Generic::<T>::NEWTYPE_ARRAY.0[0]
//~^ ERROR destructor of `Generic<[T; 0]>` cannot be evaluated at compile-time
}

pub const fn array_field<T>() -> &'static T {
&(Generic::<T>::ARRAY_FIELD.0).1[0]
//~^ ERROR destructor of `Generic<(i32, [T; 0])>` cannot be evaluated at compile-time
}

fn main() {}
30 changes: 30 additions & 0 deletions tests/ui/consts/issue-65348.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
error[E0493]: destructor of `[T; 0]` cannot be evaluated at compile-time
--> $DIR/issue-65348.rs:10:6
|
LL | &Generic::<T>::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::<T>::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::<T>::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`.