Skip to content

Commit 3518844

Browse files
authored
Rollup merge of #96089 - ojeda:no-vec-no_global_oom_handling, r=Mark-Simulacrum
`alloc`: make `vec!` unavailable under `no_global_oom_handling` `alloc`: make `vec!` unavailable under `no_global_oom_handling` The `vec!` macro has 3 rules, but two are not usable under `no_global_oom_handling` builds of the standard library (even with a zero size): ```rust let _ = vec![42]; // Error: requires `exchange_malloc` lang_item. let _ = vec![42; 0]; // Error: cannot find function `from_elem`. ``` Thus those two rules should not be available to begin with. The remaining one, with an empty matcher, is just a shorthand for `new()` and may not make as much sense to have alone, since the idea behind `vec!` is to enable `Vec`s to be defined with the same syntax as array expressions. Furthermore, the documentation can be confusing since it shows the other rules. Thus perhaps it is better and simpler to disable `vec!` entirely under `no_global_oom_handling` environments, and let users call `new()` instead: ```rust let _: Vec<i32> = vec![]; let _: Vec<i32> = Vec::new(); ``` Notwithstanding this, a `try_vec!` macro would be useful, such as the one introduced in #95051. If the shorthand for `new()` is deemed worth keeping on its own, then it may be interesting to have a separate `vec!` macro with a single rule and different, simpler documentation. Signed-off-by: Miguel Ojeda <[email protected]>
2 parents 5f10d13 + 8cec88b commit 3518844

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

library/alloc/src/macros.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
/// be mindful of side effects.
3535
///
3636
/// [`Vec`]: crate::vec::Vec
37-
#[cfg(not(test))]
37+
#[cfg(all(not(no_global_oom_handling), not(test)))]
3838
#[macro_export]
3939
#[stable(feature = "rust1", since = "1.0.0")]
4040
#[rustc_diagnostic_item = "vec_macro"]
@@ -55,7 +55,7 @@ macro_rules! vec {
5555
// required for this macro definition, is not available. Instead use the
5656
// `slice::into_vec` function which is only available with cfg(test)
5757
// NB see the slice::hack module in slice.rs for more information
58-
#[cfg(test)]
58+
#[cfg(all(not(no_global_oom_handling), test))]
5959
macro_rules! vec {
6060
() => (
6161
$crate::vec::Vec::new()

0 commit comments

Comments
 (0)