Skip to content

Commit

Permalink
Remove #[rustc_box] usage in the vec![] macro
Browse files Browse the repository at this point in the history
  • Loading branch information
Nadrieril committed Jan 3, 2025
1 parent a94fce9 commit 16a3688
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 49 deletions.
3 changes: 0 additions & 3 deletions library/alloc/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@ macro_rules! vec {
);
($($x:expr),+ $(,)?) => (
<[_]>::into_vec(
// This rustc_box is not required, but it produces a dramatic improvement in compile
// time when constructing arrays with many elements.
#[rustc_box]
$crate::boxed::Box::new([$($x),+])
)
);
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/consts/min_const_fn/bad_const_fn_body_ice.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const fn foo(a: i32) -> Vec<i32> {
vec![1, 2, 3]
//~^ ERROR allocations are not allowed
//~^ ERROR cannot call non-const fn
//~| ERROR cannot call non-const fn
}

Expand Down
8 changes: 4 additions & 4 deletions tests/ui/consts/min_const_fn/bad_const_fn_body_ice.stderr
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
error[E0010]: allocations are not allowed in constant functions
error[E0015]: cannot call non-const fn `Box::<[i32; 3]>::new` in constant functions
--> $DIR/bad_const_fn_body_ice.rs:2:5
|
LL | vec![1, 2, 3]
| ^^^^^^^^^^^^^ allocation not allowed in constant functions
| ^^^^^^^^^^^^^
|
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
= note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0015]: cannot call non-const fn `slice::<impl [i32]>::into_vec::<std::alloc::Global>` in constant functions
Expand All @@ -17,5 +18,4 @@ LL | vec![1, 2, 3]

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0010, E0015.
For more information about an error, try `rustc --explain E0010`.
For more information about this error, try `rustc --explain E0015`.
2 changes: 1 addition & 1 deletion tests/ui/error-codes/E0010-teach.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

#![allow(warnings)]

const CON: Vec<i32> = vec![1, 2, 3]; //~ ERROR E0010
const CON: Vec<i32> = vec![1, 2, 3]; //~ ERROR cannot call non-const fn
//~| ERROR cannot call non-const fn
fn main() {}
9 changes: 4 additions & 5 deletions tests/ui/error-codes/E0010-teach.stderr
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
error[E0010]: allocations are not allowed in constants
error[E0015]: cannot call non-const fn `Box::<[i32; 3]>::new` in constants
--> $DIR/E0010-teach.rs:5:23
|
LL | const CON: Vec<i32> = vec![1, 2, 3];
| ^^^^^^^^^^^^^ allocation not allowed in constants
| ^^^^^^^^^^^^^
|
= note: The runtime heap is not yet available at compile-time, so no runtime heap allocations can be created.
= note: calls in constants are limited to constant functions, tuple structs and tuple variants
= note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0015]: cannot call non-const fn `slice::<impl [i32]>::into_vec::<std::alloc::Global>` in constants
Expand All @@ -18,5 +18,4 @@ LL | const CON: Vec<i32> = vec![1, 2, 3];

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0010, E0015.
For more information about an error, try `rustc --explain E0010`.
For more information about this error, try `rustc --explain E0015`.
2 changes: 1 addition & 1 deletion tests/ui/error-codes/E0010.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#![allow(warnings)]

const CON: Vec<i32> = vec![1, 2, 3]; //~ ERROR E0010
const CON: Vec<i32> = vec![1, 2, 3]; //~ ERROR cannot call non-const fn
//~| ERROR cannot call non-const fn
fn main() {}
8 changes: 4 additions & 4 deletions tests/ui/error-codes/E0010.stderr
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
error[E0010]: allocations are not allowed in constants
error[E0015]: cannot call non-const fn `Box::<[i32; 3]>::new` in constants
--> $DIR/E0010.rs:3:23
|
LL | const CON: Vec<i32> = vec![1, 2, 3];
| ^^^^^^^^^^^^^ allocation not allowed in constants
| ^^^^^^^^^^^^^
|
= note: calls in constants are limited to constant functions, tuple structs and tuple variants
= note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0015]: cannot call non-const fn `slice::<impl [i32]>::into_vec::<std::alloc::Global>` in constants
Expand All @@ -17,5 +18,4 @@ LL | const CON: Vec<i32> = vec![1, 2, 3];

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0010, E0015.
For more information about an error, try `rustc --explain E0010`.
For more information about this error, try `rustc --explain E0015`.
4 changes: 3 additions & 1 deletion tests/ui/macros/vec-macro-in-pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

fn main() {
match Some(vec![42]) {
Some(vec![43]) => {} //~ ERROR expected pattern, found `#`
Some(vec![43]) => {} //~ ERROR expected tuple struct or tuple variant, found associated function
//~| ERROR found associated function
//~| ERROR usage of qualified paths in this context is experimental
_ => {}
}
}
31 changes: 25 additions & 6 deletions tests/ui/macros/vec-macro-in-pattern.stderr
Original file line number Diff line number Diff line change
@@ -1,14 +1,33 @@
error: expected pattern, found `#`
error[E0658]: usage of qualified paths in this context is experimental
--> $DIR/vec-macro-in-pattern.rs:7:14
|
LL | Some(vec![43]) => {}
| ^^^^^^^^
| |
| expected pattern
| in this macro invocation
| this macro call doesn't expand to a pattern
|
= note: see issue #86935 <https://github.com/rust-lang/rust/issues/86935> for more information
= help: add `#![feature(more_qualified_paths)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
= note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 1 previous error
error[E0164]: expected tuple struct or tuple variant, found associated function `<[_]>::into_vec`
--> $DIR/vec-macro-in-pattern.rs:7:14
|
LL | Some(vec![43]) => {}
| ^^^^^^^^ `fn` calls are not allowed in patterns
|
= help: for more information, visit https://doc.rust-lang.org/book/ch19-00-patterns.html
= note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0164]: expected tuple struct or tuple variant, found associated function `::alloc::boxed::Box::new`
--> $DIR/vec-macro-in-pattern.rs:7:14
|
LL | Some(vec![43]) => {}
| ^^^^^^^^ `fn` calls are not allowed in patterns
|
= help: for more information, visit https://doc.rust-lang.org/book/ch19-00-patterns.html
= note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 3 previous errors

Some errors have detailed explanations: E0164, E0658.
For more information about an error, try `rustc --explain E0164`.
14 changes: 7 additions & 7 deletions tests/ui/statics/check-values-constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ static STATIC10: UnsafeStruct = UnsafeStruct;
struct MyOwned;

static STATIC11: Vec<MyOwned> = vec![MyOwned];
//~^ ERROR allocations are not allowed in statics
//~^ ERROR cannot call non-const
//~^^ ERROR cannot call non-const

static mut STATIC12: UnsafeStruct = UnsafeStruct;
Expand All @@ -93,28 +93,28 @@ static mut STATIC14: SafeStruct = SafeStruct {
};

static STATIC15: &'static [Vec<MyOwned>] = &[
vec![MyOwned], //~ ERROR allocations are not allowed in statics
vec![MyOwned], //~ ERROR cannot call non-const
//~^ ERROR cannot call non-const
vec![MyOwned], //~ ERROR allocations are not allowed in statics
vec![MyOwned], //~ ERROR cannot call non-const
//~^ ERROR cannot call non-const
];

static STATIC16: (&'static Vec<MyOwned>, &'static Vec<MyOwned>) = (
&vec![MyOwned], //~ ERROR allocations are not allowed in statics
&vec![MyOwned], //~ ERROR cannot call non-const
//~^ ERROR cannot call non-const
&vec![MyOwned], //~ ERROR allocations are not allowed in statics
&vec![MyOwned], //~ ERROR cannot call non-const
//~^ ERROR cannot call non-const
);

static mut STATIC17: SafeEnum = SafeEnum::Variant1;

static STATIC19: Vec<isize> = vec![3];
//~^ ERROR allocations are not allowed in statics
//~^ ERROR cannot call non-const
//~^^ ERROR cannot call non-const

pub fn main() {
let y = {
static x: Vec<isize> = vec![3]; //~ ERROR allocations are not allowed in statics
static x: Vec<isize> = vec![3]; //~ ERROR cannot call non-const
//~^ ERROR cannot call non-const
x
//~^ ERROR cannot move out of static
Expand Down
46 changes: 30 additions & 16 deletions tests/ui/statics/check-values-constraints.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ LL | | }
LL | };
| - value is dropped here

error[E0010]: allocations are not allowed in statics
error[E0015]: cannot call non-const fn `Box::<[MyOwned; 1]>::new` in statics
--> $DIR/check-values-constraints.rs:81:33
|
LL | static STATIC11: Vec<MyOwned> = vec![MyOwned];
| ^^^^^^^^^^^^^ allocation not allowed in statics
| ^^^^^^^^^^^^^
|
= note: calls in statics are limited to constant functions, tuple structs and tuple variants
= note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)`
= note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0015]: cannot call non-const fn `slice::<impl [MyOwned]>::into_vec::<std::alloc::Global>` in statics
Expand All @@ -38,12 +40,14 @@ LL | field2: SafeEnum::Variant4("str".to_string()),
= note: calls in statics are limited to constant functions, tuple structs and tuple variants
= note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)`

error[E0010]: allocations are not allowed in statics
error[E0015]: cannot call non-const fn `Box::<[MyOwned; 1]>::new` in statics
--> $DIR/check-values-constraints.rs:96:5
|
LL | vec![MyOwned],
| ^^^^^^^^^^^^^ allocation not allowed in statics
| ^^^^^^^^^^^^^
|
= note: calls in statics are limited to constant functions, tuple structs and tuple variants
= note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)`
= note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0015]: cannot call non-const fn `slice::<impl [MyOwned]>::into_vec::<std::alloc::Global>` in statics
Expand All @@ -56,12 +60,14 @@ LL | vec![MyOwned],
= note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)`
= note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0010]: allocations are not allowed in statics
error[E0015]: cannot call non-const fn `Box::<[MyOwned; 1]>::new` in statics
--> $DIR/check-values-constraints.rs:98:5
|
LL | vec![MyOwned],
| ^^^^^^^^^^^^^ allocation not allowed in statics
| ^^^^^^^^^^^^^
|
= note: calls in statics are limited to constant functions, tuple structs and tuple variants
= note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)`
= note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0015]: cannot call non-const fn `slice::<impl [MyOwned]>::into_vec::<std::alloc::Global>` in statics
Expand All @@ -74,12 +80,14 @@ LL | vec![MyOwned],
= note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)`
= note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0010]: allocations are not allowed in statics
error[E0015]: cannot call non-const fn `Box::<[MyOwned; 1]>::new` in statics
--> $DIR/check-values-constraints.rs:103:6
|
LL | &vec![MyOwned],
| ^^^^^^^^^^^^^ allocation not allowed in statics
| ^^^^^^^^^^^^^
|
= note: calls in statics are limited to constant functions, tuple structs and tuple variants
= note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)`
= note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0015]: cannot call non-const fn `slice::<impl [MyOwned]>::into_vec::<std::alloc::Global>` in statics
Expand All @@ -92,12 +100,14 @@ LL | &vec![MyOwned],
= note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)`
= note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0010]: allocations are not allowed in statics
error[E0015]: cannot call non-const fn `Box::<[MyOwned; 1]>::new` in statics
--> $DIR/check-values-constraints.rs:105:6
|
LL | &vec![MyOwned],
| ^^^^^^^^^^^^^ allocation not allowed in statics
| ^^^^^^^^^^^^^
|
= note: calls in statics are limited to constant functions, tuple structs and tuple variants
= note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)`
= note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0015]: cannot call non-const fn `slice::<impl [MyOwned]>::into_vec::<std::alloc::Global>` in statics
Expand All @@ -110,12 +120,14 @@ LL | &vec![MyOwned],
= note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)`
= note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0010]: allocations are not allowed in statics
error[E0015]: cannot call non-const fn `Box::<[isize; 1]>::new` in statics
--> $DIR/check-values-constraints.rs:111:31
|
LL | static STATIC19: Vec<isize> = vec![3];
| ^^^^^^^ allocation not allowed in statics
| ^^^^^^^
|
= note: calls in statics are limited to constant functions, tuple structs and tuple variants
= note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)`
= note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0015]: cannot call non-const fn `slice::<impl [isize]>::into_vec::<std::alloc::Global>` in statics
Expand All @@ -128,12 +140,14 @@ LL | static STATIC19: Vec<isize> = vec![3];
= note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)`
= note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0010]: allocations are not allowed in statics
error[E0015]: cannot call non-const fn `Box::<[isize; 1]>::new` in statics
--> $DIR/check-values-constraints.rs:117:32
|
LL | static x: Vec<isize> = vec![3];
| ^^^^^^^ allocation not allowed in statics
| ^^^^^^^
|
= note: calls in statics are limited to constant functions, tuple structs and tuple variants
= note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)`
= note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0015]: cannot call non-const fn `slice::<impl [isize]>::into_vec::<std::alloc::Global>` in statics
Expand Down Expand Up @@ -163,5 +177,5 @@ LL | x.clone()

error: aborting due to 17 previous errors

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

0 comments on commit 16a3688

Please sign in to comment.