Skip to content

Commit 12a2489

Browse files
committed
disable needs_drop special-case for [T; 0]
1 parent 71ef9ec commit 12a2489

File tree

5 files changed

+84
-25
lines changed

5 files changed

+84
-25
lines changed

compiler/rustc_middle/src/ty/util.rs

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1328,22 +1328,8 @@ pub fn needs_drop_components<'tcx>(
13281328

13291329
ty::Dynamic(..) | ty::Error(_) => Err(AlwaysRequiresDrop),
13301330

1331-
ty::Slice(ty) => needs_drop_components(*ty, target_layout),
1332-
ty::Array(elem_ty, size) => {
1333-
match needs_drop_components(*elem_ty, target_layout) {
1334-
Ok(v) if v.is_empty() => Ok(v),
1335-
res => match size.kind().try_to_bits(target_layout.pointer_size) {
1336-
// Arrays of size zero don't need drop, even if their element
1337-
// type does.
1338-
Some(0) => Ok(SmallVec::new()),
1339-
Some(_) => res,
1340-
// We don't know which of the cases above we are in, so
1341-
// return the whole type and let the caller decide what to
1342-
// do.
1343-
None => Ok(smallvec![ty]),
1344-
},
1345-
}
1346-
}
1331+
&ty::Slice(ty) | &ty::Array(ty, _) => needs_drop_components(ty, target_layout),
1332+
13471333
// If any field needs drop, then the whole tuple does.
13481334
ty::Tuple(fields) => fields.iter().try_fold(SmallVec::new(), move |mut acc, elem| {
13491335
acc.extend(needs_drop_components(elem, target_layout)?);

tests/ui/consts/const-eval/generic-slice.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,28 @@ struct Generic<'a, T>(std::marker::PhantomData<&'a T>);
77
impl<'a, T: 'static> Generic<'a, T> {
88
const EMPTY_SLICE: &'a [T] = {
99
let x: &'a [T] = &[];
10-
x
10+
//~^ ERROR destructor of `[T; 0]` cannot be evaluated at compile-time
11+
x
1112
};
1213

1314
const EMPTY_SLICE_REF: &'a &'static [T] = {
14-
let x: &'static [T] = &[];
15+
let x: &'static [T] = &[];
16+
//~^ ERROR destructor of `[T; 0]` cannot be evaluated at compile-time
1517
&x
1618
//~^ ERROR `x` does not live long enough
19+
1720
};
1821
}
1922

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

2529
static mut INTERIOR_MUT_AND_DROP_REF: &'static &'static [std::cell::RefCell<Vec<i32>>] = {
2630
let x: &[_] = &[];
31+
//~^ ERROR destructor of `[RefCell<Vec<i32>>; 0]` cannot be evaluated at compile-time
2732
&x
2833
//~^ ERROR `x` does not live long enough
2934
};
Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
1+
error[E0493]: destructor of `[T; 0]` cannot be evaluated at compile-time
2+
--> $DIR/generic-slice.rs:9:27
3+
|
4+
LL | let x: &'a [T] = &[];
5+
| ^^ the destructor for this type cannot be evaluated in constants
6+
...
7+
LL | };
8+
| - value is dropped here
9+
10+
error[E0493]: destructor of `[T; 0]` cannot be evaluated at compile-time
11+
--> $DIR/generic-slice.rs:15:32
12+
|
13+
LL | let x: &'static [T] = &[];
14+
| ^^ the destructor for this type cannot be evaluated in constants
15+
...
16+
LL | };
17+
| - value is dropped here
18+
119
error[E0597]: `x` does not live long enough
2-
--> $DIR/generic-slice.rs:15:9
20+
--> $DIR/generic-slice.rs:17:9
321
|
422
LL | impl<'a, T: 'static> Generic<'a, T> {
523
| -- lifetime `'a` defined here
@@ -9,12 +27,30 @@ LL | &x
927
| |
1028
| borrowed value does not live long enough
1129
| using this value as a constant requires that `x` is borrowed for `'a`
12-
LL |
30+
...
1331
LL | };
1432
| - `x` dropped here while still borrowed
1533

34+
error[E0493]: destructor of `[RefCell<Vec<i32>>; 0]` cannot be evaluated at compile-time
35+
--> $DIR/generic-slice.rs:24:20
36+
|
37+
LL | let x: &[_] = &[];
38+
| ^^ the destructor for this type cannot be evaluated in statics
39+
...
40+
LL | };
41+
| - value is dropped here
42+
43+
error[E0493]: destructor of `[RefCell<Vec<i32>>; 0]` cannot be evaluated at compile-time
44+
--> $DIR/generic-slice.rs:30:20
45+
|
46+
LL | let x: &[_] = &[];
47+
| ^^ the destructor for this type cannot be evaluated in statics
48+
...
49+
LL | };
50+
| - value is dropped here
51+
1652
error[E0597]: `x` does not live long enough
17-
--> $DIR/generic-slice.rs:27:5
53+
--> $DIR/generic-slice.rs:32:5
1854
|
1955
LL | &x
2056
| ^^
@@ -25,6 +61,7 @@ LL |
2561
LL | };
2662
| - `x` dropped here while still borrowed
2763

28-
error: aborting due to 2 previous errors
64+
error: aborting due to 6 previous errors
2965

30-
For more information about this error, try `rustc --explain E0597`.
66+
Some errors have detailed explanations: E0493, E0597.
67+
For more information about an error, try `rustc --explain E0493`.

tests/ui/consts/issue-65348.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// check-pass
2-
31
struct Generic<T>(T);
42

53
impl<T> Generic<T> {
@@ -10,14 +8,17 @@ impl<T> Generic<T> {
108

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

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

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

2324
fn main() {}

tests/ui/consts/issue-65348.stderr

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
error[E0493]: destructor of `[T; 0]` cannot be evaluated at compile-time
2+
--> $DIR/issue-65348.rs:10:6
3+
|
4+
LL | &Generic::<T>::ARRAY[0]
5+
| ^^^^^^^^^^^^^^^^^^^ the destructor for this type cannot be evaluated in constant functions
6+
LL |
7+
LL | }
8+
| - value is dropped here
9+
10+
error[E0493]: destructor of `Generic<[T; 0]>` cannot be evaluated at compile-time
11+
--> $DIR/issue-65348.rs:15:6
12+
|
13+
LL | &Generic::<T>::NEWTYPE_ARRAY.0[0]
14+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ the destructor for this type cannot be evaluated in constant functions
15+
LL |
16+
LL | }
17+
| - value is dropped here
18+
19+
error[E0493]: destructor of `Generic<(i32, [T; 0])>` cannot be evaluated at compile-time
20+
--> $DIR/issue-65348.rs:20:7
21+
|
22+
LL | &(Generic::<T>::ARRAY_FIELD.0).1[0]
23+
| ^^^^^^^^^^^^^^^^^^^^^^^^^ the destructor for this type cannot be evaluated in constant functions
24+
LL |
25+
LL | }
26+
| - value is dropped here
27+
28+
error: aborting due to 3 previous errors
29+
30+
For more information about this error, try `rustc --explain E0493`.

0 commit comments

Comments
 (0)