Skip to content

Commit 334542e

Browse files
committed
constify vec![1, 2, 3] macro
1 parent da80ed0 commit 334542e

6 files changed

Lines changed: 96 additions & 137 deletions

File tree

‎library/alloc/src/boxed.rs‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,8 @@ pub struct Box<
246246
#[rustc_no_mir_inline]
247247
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
248248
#[cfg(not(no_global_oom_handling))]
249-
fn box_new_uninit(layout: Layout) -> *mut u8 {
249+
#[rustc_const_unstable(feature = "const_heap", issue = "79597")]
250+
const fn box_new_uninit(layout: Layout) -> *mut u8 {
250251
match Global.allocate(layout) {
251252
Ok(ptr) => ptr.as_mut_ptr(),
252253
Err(_) => handle_alloc_error(layout),
@@ -258,10 +259,11 @@ fn box_new_uninit(layout: Layout) -> *mut u8 {
258259
/// This is unsafe, but has to be marked as safe or else we couldn't use it in `vec!`.
259260
#[doc(hidden)]
260261
#[unstable(feature = "liballoc_internals", issue = "none")]
262+
#[rustc_const_unstable(feature = "const_heap", issue = "79597")]
261263
#[inline(always)]
262264
#[cfg(not(no_global_oom_handling))]
263265
#[rustc_diagnostic_item = "box_assume_init_into_vec_unsafe"]
264-
pub fn box_assume_init_into_vec_unsafe<T, const N: usize>(
266+
pub const fn box_assume_init_into_vec_unsafe<T, const N: usize>(
265267
b: Box<MaybeUninit<[T; N]>>,
266268
) -> crate::vec::Vec<T> {
267269
unsafe { (b.assume_init() as Box<[T]>).into_vec() }
@@ -307,10 +309,11 @@ impl<T> Box<T> {
307309
/// ```
308310
#[cfg(not(no_global_oom_handling))]
309311
#[stable(feature = "new_uninit", since = "1.82.0")]
312+
#[rustc_const_unstable(feature = "const_heap", issue = "79597")]
310313
#[must_use]
311314
#[inline(always)]
312315
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
313-
pub fn new_uninit() -> Box<mem::MaybeUninit<T>> {
316+
pub const fn new_uninit() -> Box<mem::MaybeUninit<T>> {
314317
// This is the same as `Self::new_uninit_in(Global)`, but manually inlined (just like
315318
// `Box::new`).
316319

@@ -1197,8 +1200,9 @@ impl<T, A: Allocator> Box<mem::MaybeUninit<T>, A> {
11971200
/// assert_eq!(*five, 5)
11981201
/// ```
11991202
#[stable(feature = "new_uninit", since = "1.82.0")]
1203+
#[rustc_const_unstable(feature = "const_heap", issue = "79597")]
12001204
#[inline(always)]
1201-
pub unsafe fn assume_init(self) -> Box<T, A> {
1205+
pub const unsafe fn assume_init(self) -> Box<T, A> {
12021206
// This is used in the `vec!` macro, so we optimize for minimal IR generation
12031207
// even in debug builds.
12041208
// SAFETY: `Box<T>` and `Box<MaybeUninit<T>>` have the same layout.
@@ -1668,8 +1672,9 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
16681672
/// [memory layout]: self#memory-layout
16691673
#[must_use = "losing the pointer will leak memory"]
16701674
#[unstable(feature = "allocator_api", issue = "32838")]
1675+
#[rustc_const_unstable(feature = "const_heap", issue = "79597")]
16711676
#[inline]
1672-
pub fn into_raw_with_allocator(b: Self) -> (*mut T, A) {
1677+
pub const fn into_raw_with_allocator(b: Self) -> (*mut T, A) {
16731678
let mut b = mem::ManuallyDrop::new(b);
16741679
// We carefully get the raw pointer out in a way that Miri's aliasing model understands what
16751680
// is happening: using the primitive "deref" of `Box`. In case `A` is *not* `Global`, we

‎library/alloc/src/slice.rs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,8 +476,9 @@ impl<T> [T] {
476476
/// ```
477477
#[rustc_allow_incoherent_impl]
478478
#[stable(feature = "rust1", since = "1.0.0")]
479+
#[rustc_const_unstable(feature = "const_heap", issue = "79597")]
479480
#[inline]
480-
pub fn into_vec<A: Allocator>(self: Box<Self, A>) -> Vec<T, A> {
481+
pub const fn into_vec<A: Allocator>(self: Box<Self, A>) -> Vec<T, A> {
481482
unsafe {
482483
let len = self.len();
483484
let (b, alloc) = Box::into_raw_with_allocator(self);
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
const fn foo(a: i32) -> Vec<i32> {
1+
fn non_const() -> Vec<i32> {
22
vec![1, 2, 3]
3-
//~^ ERROR cannot call non-const
4-
//~| ERROR cannot call non-const
3+
}
4+
5+
const fn foo(a: i32) -> Vec<i32> {
6+
non_const()
7+
//~^ ERROR cannot call non-const function
58
}
69

710
fn main() {}
Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
1-
error[E0015]: cannot call non-const associated function `Box::<[i32; 3]>::new_uninit` in constant functions
2-
--> $DIR/bad_const_fn_body_ice.rs:2:5
1+
error[E0015]: cannot call non-const function `non_const` in constant functions
2+
--> $DIR/bad_const_fn_body_ice.rs:6:5
33
|
4-
LL | vec![1, 2, 3]
5-
| ^^^^^^^^^^^^^
4+
LL | non_const()
5+
| ^^^^^^^^^^^
66
|
7-
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
8-
9-
error[E0015]: cannot call non-const function `std::boxed::box_assume_init_into_vec_unsafe::<i32, 3>` in constant functions
10-
--> $DIR/bad_const_fn_body_ice.rs:2:5
11-
|
12-
LL | vec![1, 2, 3]
13-
| ^^^^^^^^^^^^^
7+
note: function `non_const` is not const
8+
--> $DIR/bad_const_fn_body_ice.rs:1:1
149
|
15-
note: function `box_assume_init_into_vec_unsafe` is not const
16-
--> $SRC_DIR/alloc/src/boxed.rs:LL:COL
10+
LL | fn non_const() -> Vec<i32> {
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
1712
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
1813

19-
error: aborting due to 2 previous errors
14+
error: aborting due to 1 previous error
2015

2116
For more information about this error, try `rustc --explain E0015`.

‎tests/ui/statics/check-values-constraints.rs‎

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ static STATIC9: SafeStruct = SafeStruct {
6868
}
6969
};
7070

71+
fn non_const<T>(value: T) -> Vec<T> {
72+
vec![value]
73+
}
74+
7175
struct UnsafeStruct;
7276

7377
impl Drop for UnsafeStruct {
@@ -78,9 +82,8 @@ static STATIC10: UnsafeStruct = UnsafeStruct;
7882

7983
struct MyOwned;
8084

81-
static STATIC11: Vec<MyOwned> = vec![MyOwned];
85+
static STATIC11: Vec<MyOwned> = non_const(MyOwned);
8286
//~^ ERROR cannot call non-const function
83-
//~| ERROR cannot call non-const
8487

8588
static mut STATIC12: UnsafeStruct = UnsafeStruct;
8689

@@ -93,29 +96,23 @@ static mut STATIC14: SafeStruct = SafeStruct {
9396
};
9497

9598
static STATIC15: &'static [Vec<MyOwned>] = &[
96-
vec![MyOwned], //~ ERROR cannot call non-const function
97-
//~| ERROR cannot call non-const
98-
vec![MyOwned], //~ ERROR cannot call non-const function
99-
//~| ERROR cannot call non-const
99+
non_const(MyOwned), //~ ERROR cannot call non-const function
100+
non_const(MyOwned), //~ ERROR cannot call non-const function
100101
];
101102

102103
static STATIC16: (&'static Vec<MyOwned>, &'static Vec<MyOwned>) = (
103-
&vec![MyOwned], //~ ERROR cannot call non-const function
104-
//~| ERROR cannot call non-const
105-
&vec![MyOwned], //~ ERROR cannot call non-const function
106-
//~| ERROR cannot call non-const
104+
&non_const(MyOwned), //~ ERROR cannot call non-const function
105+
&non_const(MyOwned), //~ ERROR cannot call non-const function
107106
);
108107

109108
static mut STATIC17: SafeEnum = SafeEnum::Variant1;
110109

111-
static STATIC19: Vec<isize> = vec![3];
110+
static STATIC19: Vec<isize> = non_const(3);
112111
//~^ ERROR cannot call non-const function
113-
//~| ERROR cannot call non-const
114112

115113
pub fn main() {
116114
let y = {
117-
static x: Vec<isize> = vec![3]; //~ ERROR cannot call non-const function
118-
//~| ERROR cannot call non-const
115+
static x: Vec<isize> = non_const(3); //~ ERROR cannot call non-const function
119116
x
120117
//~^ ERROR cannot move out of static
121118
};

‎tests/ui/statics/check-values-constraints.stderr‎

Lines changed: 58 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,22 @@ LL | | }
1111
LL | };
1212
| - value is dropped here
1313

14-
error[E0015]: cannot call non-const associated function `Box::<[MyOwned; 1]>::new_uninit` in statics
15-
--> $DIR/check-values-constraints.rs:81:33
14+
error[E0015]: cannot call non-const function `non_const::<MyOwned>` in statics
15+
--> $DIR/check-values-constraints.rs:85:33
1616
|
17-
LL | static STATIC11: Vec<MyOwned> = vec![MyOwned];
18-
| ^^^^^^^^^^^^^
17+
LL | static STATIC11: Vec<MyOwned> = non_const(MyOwned);
18+
| ^^^^^^^^^^^^^^^^^^
1919
|
20-
= note: calls in statics are limited to constant functions, tuple structs and tuple variants
21-
= note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)`
22-
23-
error[E0015]: cannot call non-const function `std::boxed::box_assume_init_into_vec_unsafe::<MyOwned, 1>` in statics
24-
--> $DIR/check-values-constraints.rs:81:33
25-
|
26-
LL | static STATIC11: Vec<MyOwned> = vec![MyOwned];
27-
| ^^^^^^^^^^^^^
20+
note: function `non_const` is not const
21+
--> $DIR/check-values-constraints.rs:71:1
2822
|
29-
note: function `box_assume_init_into_vec_unsafe` is not const
30-
--> $SRC_DIR/alloc/src/boxed.rs:LL:COL
23+
LL | fn non_const<T>(value: T) -> Vec<T> {
24+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3125
= note: calls in statics are limited to constant functions, tuple structs and tuple variants
3226
= note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)`
3327

3428
error[E0015]: cannot call non-const method `<str as ToString>::to_string` in statics
35-
--> $DIR/check-values-constraints.rs:92:38
29+
--> $DIR/check-values-constraints.rs:95:38
3630
|
3731
LL | field2: SafeEnum::Variant4("str".to_string()),
3832
| ^^^^^^^^^^^
@@ -47,128 +41,92 @@ note: method `to_string` is not const because trait `ToString` is not const
4741
= note: calls in statics are limited to constant functions, tuple structs and tuple variants
4842
= note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)`
4943

50-
error[E0015]: cannot call non-const associated function `Box::<[MyOwned; 1]>::new_uninit` in statics
51-
--> $DIR/check-values-constraints.rs:96:5
44+
error[E0015]: cannot call non-const function `non_const::<MyOwned>` in statics
45+
--> $DIR/check-values-constraints.rs:99:5
5246
|
53-
LL | vec![MyOwned],
54-
| ^^^^^^^^^^^^^
55-
|
56-
= note: calls in statics are limited to constant functions, tuple structs and tuple variants
57-
= note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)`
58-
59-
error[E0015]: cannot call non-const function `std::boxed::box_assume_init_into_vec_unsafe::<MyOwned, 1>` in statics
60-
--> $DIR/check-values-constraints.rs:96:5
47+
LL | non_const(MyOwned),
48+
| ^^^^^^^^^^^^^^^^^^
6149
|
62-
LL | vec![MyOwned],
63-
| ^^^^^^^^^^^^^
50+
note: function `non_const` is not const
51+
--> $DIR/check-values-constraints.rs:71:1
6452
|
65-
note: function `box_assume_init_into_vec_unsafe` is not const
66-
--> $SRC_DIR/alloc/src/boxed.rs:LL:COL
53+
LL | fn non_const<T>(value: T) -> Vec<T> {
54+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6755
= note: calls in statics are limited to constant functions, tuple structs and tuple variants
6856
= note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)`
6957

70-
error[E0015]: cannot call non-const associated function `Box::<[MyOwned; 1]>::new_uninit` in statics
71-
--> $DIR/check-values-constraints.rs:98:5
58+
error[E0015]: cannot call non-const function `non_const::<MyOwned>` in statics
59+
--> $DIR/check-values-constraints.rs:100:5
7260
|
73-
LL | vec![MyOwned],
74-
| ^^^^^^^^^^^^^
61+
LL | non_const(MyOwned),
62+
| ^^^^^^^^^^^^^^^^^^
7563
|
76-
= note: calls in statics are limited to constant functions, tuple structs and tuple variants
77-
= note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)`
78-
79-
error[E0015]: cannot call non-const function `std::boxed::box_assume_init_into_vec_unsafe::<MyOwned, 1>` in statics
80-
--> $DIR/check-values-constraints.rs:98:5
81-
|
82-
LL | vec![MyOwned],
83-
| ^^^^^^^^^^^^^
64+
note: function `non_const` is not const
65+
--> $DIR/check-values-constraints.rs:71:1
8466
|
85-
note: function `box_assume_init_into_vec_unsafe` is not const
86-
--> $SRC_DIR/alloc/src/boxed.rs:LL:COL
67+
LL | fn non_const<T>(value: T) -> Vec<T> {
68+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
8769
= note: calls in statics are limited to constant functions, tuple structs and tuple variants
8870
= note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)`
8971

90-
error[E0015]: cannot call non-const associated function `Box::<[MyOwned; 1]>::new_uninit` in statics
91-
--> $DIR/check-values-constraints.rs:103:6
72+
error[E0015]: cannot call non-const function `non_const::<MyOwned>` in statics
73+
--> $DIR/check-values-constraints.rs:104:6
9274
|
93-
LL | &vec![MyOwned],
94-
| ^^^^^^^^^^^^^
75+
LL | &non_const(MyOwned),
76+
| ^^^^^^^^^^^^^^^^^^
9577
|
96-
= note: calls in statics are limited to constant functions, tuple structs and tuple variants
97-
= note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)`
98-
99-
error[E0015]: cannot call non-const function `std::boxed::box_assume_init_into_vec_unsafe::<MyOwned, 1>` in statics
100-
--> $DIR/check-values-constraints.rs:103:6
78+
note: function `non_const` is not const
79+
--> $DIR/check-values-constraints.rs:71:1
10180
|
102-
LL | &vec![MyOwned],
103-
| ^^^^^^^^^^^^^
104-
|
105-
note: function `box_assume_init_into_vec_unsafe` is not const
106-
--> $SRC_DIR/alloc/src/boxed.rs:LL:COL
81+
LL | fn non_const<T>(value: T) -> Vec<T> {
82+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
10783
= note: calls in statics are limited to constant functions, tuple structs and tuple variants
10884
= note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)`
10985

110-
error[E0015]: cannot call non-const associated function `Box::<[MyOwned; 1]>::new_uninit` in statics
86+
error[E0015]: cannot call non-const function `non_const::<MyOwned>` in statics
11187
--> $DIR/check-values-constraints.rs:105:6
11288
|
113-
LL | &vec![MyOwned],
114-
| ^^^^^^^^^^^^^
89+
LL | &non_const(MyOwned),
90+
| ^^^^^^^^^^^^^^^^^^
11591
|
116-
= note: calls in statics are limited to constant functions, tuple structs and tuple variants
117-
= note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)`
118-
119-
error[E0015]: cannot call non-const function `std::boxed::box_assume_init_into_vec_unsafe::<MyOwned, 1>` in statics
120-
--> $DIR/check-values-constraints.rs:105:6
121-
|
122-
LL | &vec![MyOwned],
123-
| ^^^^^^^^^^^^^
92+
note: function `non_const` is not const
93+
--> $DIR/check-values-constraints.rs:71:1
12494
|
125-
note: function `box_assume_init_into_vec_unsafe` is not const
126-
--> $SRC_DIR/alloc/src/boxed.rs:LL:COL
95+
LL | fn non_const<T>(value: T) -> Vec<T> {
96+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12797
= note: calls in statics are limited to constant functions, tuple structs and tuple variants
12898
= note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)`
12999

130-
error[E0015]: cannot call non-const associated function `Box::<[isize; 1]>::new_uninit` in statics
131-
--> $DIR/check-values-constraints.rs:111:31
132-
|
133-
LL | static STATIC19: Vec<isize> = vec![3];
134-
| ^^^^^^^
100+
error[E0015]: cannot call non-const function `non_const::<isize>` in statics
101+
--> $DIR/check-values-constraints.rs:110:31
135102
|
136-
= note: calls in statics are limited to constant functions, tuple structs and tuple variants
137-
= note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)`
138-
139-
error[E0015]: cannot call non-const function `std::boxed::box_assume_init_into_vec_unsafe::<isize, 1>` in statics
140-
--> $DIR/check-values-constraints.rs:111:31
103+
LL | static STATIC19: Vec<isize> = non_const(3);
104+
| ^^^^^^^^^^^^
141105
|
142-
LL | static STATIC19: Vec<isize> = vec![3];
143-
| ^^^^^^^
106+
note: function `non_const` is not const
107+
--> $DIR/check-values-constraints.rs:71:1
144108
|
145-
note: function `box_assume_init_into_vec_unsafe` is not const
146-
--> $SRC_DIR/alloc/src/boxed.rs:LL:COL
109+
LL | fn non_const<T>(value: T) -> Vec<T> {
110+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
147111
= note: calls in statics are limited to constant functions, tuple structs and tuple variants
148112
= note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)`
149113

150-
error[E0015]: cannot call non-const associated function `Box::<[isize; 1]>::new_uninit` in statics
151-
--> $DIR/check-values-constraints.rs:117:32
152-
|
153-
LL | static x: Vec<isize> = vec![3];
154-
| ^^^^^^^
114+
error[E0015]: cannot call non-const function `non_const::<isize>` in statics
115+
--> $DIR/check-values-constraints.rs:115:32
155116
|
156-
= note: calls in statics are limited to constant functions, tuple structs and tuple variants
157-
= note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)`
158-
159-
error[E0015]: cannot call non-const function `std::boxed::box_assume_init_into_vec_unsafe::<isize, 1>` in statics
160-
--> $DIR/check-values-constraints.rs:117:32
117+
LL | static x: Vec<isize> = non_const(3);
118+
| ^^^^^^^^^^^^
161119
|
162-
LL | static x: Vec<isize> = vec![3];
163-
| ^^^^^^^
120+
note: function `non_const` is not const
121+
--> $DIR/check-values-constraints.rs:71:1
164122
|
165-
note: function `box_assume_init_into_vec_unsafe` is not const
166-
--> $SRC_DIR/alloc/src/boxed.rs:LL:COL
123+
LL | fn non_const<T>(value: T) -> Vec<T> {
124+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
167125
= note: calls in statics are limited to constant functions, tuple structs and tuple variants
168126
= note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)`
169127

170128
error[E0507]: cannot move out of static item `x`
171-
--> $DIR/check-values-constraints.rs:119:9
129+
--> $DIR/check-values-constraints.rs:116:9
172130
|
173131
LL | x
174132
| ^ move occurs because `x` has type `Vec<isize>`, which does not implement the `Copy` trait
@@ -182,7 +140,7 @@ help: consider cloning the value if the performance cost is acceptable
182140
LL | x.clone()
183141
| ++++++++
184142

185-
error: aborting due to 17 previous errors
143+
error: aborting due to 10 previous errors
186144

187145
Some errors have detailed explanations: E0015, E0493, E0507.
188146
For more information about an error, try `rustc --explain E0015`.

0 commit comments

Comments
 (0)