-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Make Vec::new
a const fn
#50233
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Make Vec::new
a const fn
#50233
Conversation
r? @kennytm (rust_highfive has picked a reviewer for you, use r? to override) |
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
src/liballoc/vec.rs
Outdated
@@ -324,7 +324,7 @@ impl<T> Vec<T> { | |||
#[stable(feature = "rust1", since = "1.0.0")] | |||
pub fn new() -> Vec<T> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You forgot the actual change of this PR 😆
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh. 🤦♂️ Thanks!
src/liballoc/vec.rs
Outdated
@@ -322,9 +322,9 @@ impl<T> Vec<T> { | |||
/// ``` | |||
#[inline] | |||
#[stable(feature = "rust1", since = "1.0.0")] | |||
pub fn new() -> Vec<T> { | |||
pub const fn new() -> Vec<T> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please mark this with #[rustc_const_unstable(feature = "const_vec_new")]
so we don't need to insta-stable this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kennytm Done.
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
The test needs |
This changes the behavior for zero-sized types. Before this change, If we want to preserve the current optimization for zero-sized types, then instead of getting rid of the |
|
@alexreg would it be possible to get an ETA on that? What does "very soon" mean exactly? If it will be in the next few weeks or so, then I think it would be worth it to wait for that. Otherwise, we could use the |
@mbrubeck That approach doesn't quite work:
|
If you don't use an intermediate const, then the trick should work. Const if is still ways off. So use the array trick |
Still. the trick works now. No need to block this PR |
Yeah, I don't dispute that. ;-) |
Hmm... tidy is segfaulting on my machine, so I can't run tests at the moment... |
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
Oh 🤦♂️ |
@kennytm I think this is ready for another review |
@bors r+ |
📌 Commit f9f9923 has been approved by |
Make `Vec::new` a `const fn` `RawVec::empty/_in` are a hack. They're there because `if size_of::<T> == 0 { !0 } else { 0 }` is not allowed in `const` yet. However, because `RawVec` is unstable, the `empty/empty_in` constructors can be removed when rust-lang#49146 is done...
Rollup of 7 pull requests Successful merges: - #50233 (Make `Vec::new` a `const fn`) - #50312 (Add more links in panic docs) - #50316 (Fix some broken links in docs.) - #50325 (Add a few more tests for proc macro feature gating) - #50327 (Display correct unused field suggestion for nested struct patterns) - #50330 (check that #[used] is used only on statics) - #50344 (Update Cargo to 2018-04-28 122fd5be5201913d42e219e132d6569493583bca) Failed merges:
impl String {
pub fn new() -> String {
String { vec: Vec::new() }
}
} Given that |
Good idea! |
Make `String::new()` const Following the steps of rust-lang#50233 , make `String::new()` a `const fn`.
Initially, rust-lang#50233 accidentally changed the capacity of empty ZST. This was pointed out during code review. This commit adds a test to prevent capacity of ZST vectors from accidentally changing to prevent that from happening again.
Test capacity of ZST vector Initially, rust-lang#50233 accidentally changed the capacity of empty ZST. This was pointed out during code review. This commit adds a test to prevent capacity of ZST vectors from accidentally changing to prevent that from happening again.
Test capacity of ZST vector Initially, rust-lang#50233 accidentally changed the capacity of empty ZST. This was pointed out during code review. This commit adds a test to prevent capacity of ZST vectors from accidentally changing to prevent that from happening again.
Test capacity of ZST vector Initially, rust-lang#50233 accidentally changed the capacity of empty ZST. This was pointed out during code review. This commit adds a test to prevent capacity of ZST vectors from accidentally changing to prevent that from happening again.
Test capacity of ZST vector Initially, rust-lang#50233 accidentally changed the capacity of empty ZST. This was pointed out during code review. This commit adds a test to prevent capacity of ZST vectors from accidentally changing to prevent that from happening again.
Test capacity of ZST vector Initially, rust-lang#50233 accidentally changed the capacity of empty ZST. This was pointed out during code review. This commit adds a test to prevent capacity of ZST vectors from accidentally changing to prevent that from happening again.
Test capacity of ZST vector Initially, rust-lang#50233 accidentally changed the capacity of empty ZST. This was pointed out during code review. This commit adds a test to prevent capacity of ZST vectors from accidentally changing to prevent that from happening again.
Test capacity of ZST vector Initially, rust-lang#50233 accidentally changed the capacity of empty ZST. This was pointed out during code review. This commit adds a test to prevent capacity of ZST vectors from accidentally changing to prevent that from happening again.
RawVec::empty/_in
are a hack. They're there becauseif size_of::<T> == 0 { !0 } else { 0 }
is not allowed inconst
yet. However, becauseRawVec
is unstable, theempty/empty_in
constructors can be removed when #49146 is done...