Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion library/alloc/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ macro_rules! vec {
$crate::vec::Vec::new()
);
($elem:expr; $n:expr) => (
$crate::vec::from_elem($elem, $n)
$crate::vec::from_elem_global($elem, $n)
);
($($x:expr),+ $(,)?) => (
// Using `write_box_via_move` produces a dramatic improvement in stack usage for unoptimized
Expand Down
15 changes: 14 additions & 1 deletion library/alloc/src/vec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3941,11 +3941,24 @@ fn find_first_duplicate<T: PartialEq>(v: &[T], chunk_size: usize) -> Option<usiz
// Internal methods and functions
////////////////////////////////////////////////////////////////////////////////

// This function is no longer used internally (`from_elem_global` is used instead).
// It is only kept so as not to break a few crates who mistakenly used it at a
// time when we didn't have the ability to gate internal methods used by public macros.
#[doc(hidden)]
#[cfg(not(no_global_oom_handling))]
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_diagnostic_item = "vec_from_elem"]
// NOTE: the real deprecation version is the same as the stabilization version.
// I.e. it should never have been used even in version 1.0.
#[deprecated(since = "CURRENT_RUSTC_VERSION", note = "use the `vec!` macro instead")]
pub fn from_elem<T: Clone>(elem: T, n: usize) -> Vec<T> {
vec![elem; n]
}

#[doc(hidden)]
#[cfg(not(no_global_oom_handling))]
#[unstable(feature = "liballoc_internals", issue = "none")]
#[rustc_diagnostic_item = "vec_from_elem"]
pub fn from_elem_global<T: Clone>(elem: T, n: usize) -> Vec<T> {

@ChrisDenton ChrisDenton Oct 20, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This might be overkill but I duplicated from_elem so the the standard library can fully act as if it's unstable. And it does avoid needing to allow deprecated.

View changes since the review

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yeah, I dunno if there's an allow_internal_deprecated like allow_internal_unstable has.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We can hopefully use Vec::from_elem (perhaps named differently) for that soon (see rust-lang/libs-team#912)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I know it's internal but the name still doesn't seem that great (what is "global" about?). Maybe from_repeated_elem or so?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This has one use: in the vec! macro. If someone does not know (or can't find out) what global means in the context of allocating a Vec then they really should not be touching the macro at all. And I would really prefer its name not to diverge too far from its diagnostic item name. Especially not in this PR.

Btw, the best place to bikeshed this would be rust-lang/libs-team#912 since it proposes a stable API (though personally I'm not in favour of having a guaranteed specialisation wrapper before specialisation is stable).

<T as SpecFromElem>::from_elem(elem, n, Global)
}

Expand Down
13 changes: 6 additions & 7 deletions tests/ui/array-slice-vec/vec-spec-extend-with-correct-drop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

use std::cell::Cell;
use std::clone::TrivialClone;
use std::vec::from_elem;

#[derive(Clone)]
struct DropCounter<'a>(&'a Cell<i32>);
Expand Down Expand Up @@ -44,16 +43,16 @@ fn main() {
// TrivialClone
let c = Cell::new(0);

let mut vec = from_elem(DropCounter(&c), 3);
let mut vec = vec![DropCounter(&c); 3];
vec.clear();

assert_eq!(c.get(), 3);
vec = from_elem(DropCounter(&c), 6);
vec = vec![DropCounter(&c); 6];
vec.clear();

assert_eq!(c.get(), 9);

vec = from_elem(DropCounter(&c), 0);
vec = vec![DropCounter(&c); 0];
assert_eq!(c.get(), 10);
vec.clear();
assert_eq!(c.get(), 10);
Expand All @@ -62,21 +61,21 @@ fn main() {
let clones = Cell::new(0);
let drops = Cell::new(0);

let mut vec = from_elem(CloneCounter::new(&clones, &drops), 3);
let mut vec = vec![CloneCounter::new(&clones, &drops); 3];
assert_eq!(clones.get(), 2);
assert_eq!(drops.get(), 0);
vec.clear();
assert_eq!(clones.get(), 2);
assert_eq!(drops.get(), 3);

vec = from_elem(CloneCounter::new(&clones, &drops), 6);
vec = vec![CloneCounter::new(&clones, &drops); 6];
assert_eq!(clones.get(), 7);
assert_eq!(drops.get(), 3);
vec.clear();
assert_eq!(clones.get(), 7);
assert_eq!(drops.get(), 9);

vec = from_elem(CloneCounter::new(&clones, &drops), 0);
vec = vec![CloneCounter::new(&clones, &drops); 0];
assert_eq!(clones.get(), 7);
assert_eq!(drops.get(), 10);
vec.clear();
Expand Down
Loading