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

alloc: make vec! unavailable under no_global_oom_handling #96089

Merged
merged 1 commit into from
Apr 19, 2022

Conversation

ojeda
Copy link
Contributor

@ojeda ojeda commented Apr 15, 2022

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):

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 Vecs 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:

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 ojeda@kernel.org

@rustbot rustbot added the T-libs Relevant to the library team, which will review and decide on the PR/issue. label Apr 15, 2022
@rust-highfive
Copy link
Collaborator

Hey! It looks like you've submitted a new PR for the library teams!

If this PR contains changes to any rust-lang/rust public library APIs then please comment with r? rust-lang/libs-api @rustbot label +T-libs-api -T-libs to request review from a libs-api team reviewer. If you're unsure where your change falls no worries, just leave it as is and the reviewer will take a look and make a decision to forward on if necessary.

Examples of T-libs-api changes:

  • Stabilizing library features
  • Introducing insta-stable changes such as new implementations of existing stable traits on existing stable types
  • Introducing new or changing existing unstable library APIs (excluding permanently unstable features / features without a tracking issue)
  • Changing public documentation in ways that create new stability guarantees
  • Changing observable runtime behavior of library APIs

@rust-highfive
Copy link
Collaborator

r? @Mark-Simulacrum

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 15, 2022
@ojeda
Copy link
Contributor Author

ojeda commented Apr 15, 2022

@Ericson2314
Copy link
Contributor

A fine lines below there is a

#[cfg(test)]
macro_rules! vec {

too. I don't think we've bothered to run tests with oom panicking disabled, but for sake of a future where we do, can we modify that cfg too?

@ojeda
Copy link
Contributor Author

ojeda commented Apr 15, 2022

If I understand correctly, you want to prevent that somebody writes vec![] (the empty one) in a test under no_global_oom_handling, right? Makes sense -- pushed!

@ojeda ojeda force-pushed the no-vec-no_global_oom_handling branch from 22701aa to 81406b1 Compare April 15, 2022 21:10
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 rust-lang#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 <ojeda@kernel.org>
@ojeda ojeda force-pushed the no-vec-no_global_oom_handling branch from 81406b1 to 8cec88b Compare April 16, 2022 06:06
@ojeda
Copy link
Contributor Author

ojeda commented Apr 16, 2022

Reworded commit message a bit better -- no changes.

@@ -34,7 +34,7 @@
/// be mindful of side effects.
///
/// [`Vec`]: crate::vec::Vec
#[cfg(not(test))]
#[cfg(all(not(no_global_oom_handling), not(test)))]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small nit: I might prefer not(any(...)) here, which I think reads a little nicer?

I generally find it faster to digest or'd lists myself, but it probably varies from person to person, so feel free to resolve if you disagree.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used this one since not(no_global_oom_handling) is the one normally found elsewhere on its own (since it is used to disable things) and also makes it a prefix of the other (so perhaps it is easier to see it is the same thing but for test/!test):

#[cfg(all(not(no_global_oom_handling), not(test)))]
#[cfg(all(not(no_global_oom_handling), test))]

From a quick look, there are currently 2 not(any(...)), and 4 all(not(...)) (for no_global_oom_handling/test).

I am fine with either, so please let me know which one you prefer given the above.

(Personally, I think multiple cfgs would be more readable and easier for diff/VCS purposes; and I hope for &&/and operators, but... :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, yeah, not sure. Let's leave it as-is for now -- I can see either direction being beneficial. Multiple cfgs would also be fine I think (they don't work when you want an or but for and I think should work fine).

@Mark-Simulacrum
Copy link
Member

r=me, modulo the nit if you agree with it.

@Mark-Simulacrum
Copy link
Member

@bors r+

@bors
Copy link
Contributor

bors commented Apr 17, 2022

📌 Commit 8cec88b has been approved by Mark-Simulacrum

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 17, 2022
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this pull request Apr 19, 2022
…, 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 rust-lang#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 <ojeda@kernel.org>
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this pull request Apr 19, 2022
…, 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 rust-lang#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 <ojeda@kernel.org>
bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 19, 2022
Rollup of 6 pull requests

Successful merges:

 - rust-lang#94493 (Improved diagnostic on failure to meet send bound on future in a foreign crate)
 - rust-lang#95809 (Fix typo in bootstrap.py)
 - rust-lang#96086 (Remove `--extern-location` and all associated code)
 - rust-lang#96089 (`alloc`: make `vec!` unavailable under `no_global_oom_handling`)
 - rust-lang#96122 (Fix an invalid error for a suggestion to add a slice in pattern-matching)
 - rust-lang#96142 (Stop using CRATE_DEF_INDEX outside of metadata encoding.)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 3518844 into rust-lang:master Apr 19, 2022
@rustbot rustbot added this to the 1.62.0 milestone Apr 19, 2022
@ojeda ojeda deleted the no-vec-no_global_oom_handling branch April 23, 2022 20:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants