Skip to content

Deprecate internal function vec::from_elem - #164653

Merged
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
ChrisDenton:deprecate-vec-from_elem
Nov 5, 2026
Merged

rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
ChrisDenton:deprecate-vec-from_elem

Conversation

@ChrisDenton

@ChrisDenton ChrisDenton commented Oct 20, 2026 •

Copy link
Copy Markdown
Member

View all comments

This is only used internally and is only public because we didn't used to have the ability to gate internal functions used in public macros (other than using doc(hidden) to hide it).

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Oct 20, 2026
@rustbot

rustbot commented Oct 20, 2026

Copy link
Copy Markdown
Collaborator

r? @joboet

rustbot has assigned @joboet.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: libs
  • libs expanded to 37 candidates
  • Random selection from JohnTitor, Mark-Simulacrum, clarfonthey, jhpratt, joboet

#[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).

@clarfonthey

Copy link
Copy Markdown
Contributor

@rfcbot merge libs

Since this is technically an API change, even though this API should have been perma-unstable initially.

@rust-rfcbot

rust-rfcbot commented Oct 20, 2026 •

Copy link
Copy Markdown
Collaborator

@clarfonthey has proposed to merge this. The next step is review by the rest of the tagged team members:

No concerns currently listed.

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

cc @rust-lang/libs-ping: FCP proposed for libs, please feel free to register concerns.
See this document for info about what commands tagged team members can give me.

@rust-rfcbot rust-rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. labels Oct 20, 2026
@ChrisDenton
ChrisDenton force-pushed the deprecate-vec-from_elem branch from 4f09f80 to 07daf37 Compare October 20, 2026 18:48
@joboet

joboet commented Oct 22, 2026

Copy link
Copy Markdown
Member

r=me when FCP completes.

@joboet joboet added S-waiting-on-fcp Status: PR is in FCP and is awaiting for FCP to complete. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 22, 2026
@rust-rfcbot rust-rfcbot added final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. and removed proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. labels Oct 25, 2026
@rust-rfcbot

Copy link
Copy Markdown
Collaborator

🔔 This is now entering its final comment period, as per the review above. 🔔

Comment thread library/alloc/src/vec/mod.rs Outdated
#[cfg(not(no_global_oom_handling))]
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_diagnostic_item = "vec_from_elem"]
#[deprecated(since = "1.0.0", note = "use the `vec!` macro instead")]

@RalfJung RalfJung Oct 30, 2026 •

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.

It does not seem correct to say that this was deprecated since Rust 1.0? This should be CURRENT_RUST_VERSION I think.

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.

So, it was an internal detail since 1.0, it's only that we never prevented its use.

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.

I think that it should be CURRENT_RUST_VERSION, so people don't get confused about "why didn't I get a warning earlier?", and so they can figure out how "bad" the deprecation is based on recency.

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.

Aside from adding the literal attribute, this isn't a recent deprecation. It's been deprecated for as long as it has existed. It was always wrong to use it (and we're only talking about a few crates that ever did).

People thinking it's less bad to use it since it's "only a recent deprecation" is exactly what I'd like to avoid.

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 think it's reasonable to tie the version number to when the attribute was added. It's quite confusing otherwise if code previously was warning free, and now suddenly gets a warning effectively claiming that we warned against this since 1.0.

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.

I will change because I've already spent way too much time arguing about a warning approximately nobody will ever see. However, I do think it's a bad mistake to treat this at all like a publicly documented function. The #[deprecated] lint is being used out of convenience and I strongly disagree that it should list the version the warning was added. That is not at all actionable information.

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.

Its previous state is more like what we usually call "soft deprecation".

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.

Strictly speaking, do we even need to put anything in since? It's technically optional...

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.

I think that putting in a note why we marked this as deprecated since 1.0 is enough to clear any concerns, but I'm not going to complain too hard.

We could also add a dedicated FCW that gets reported for dependencies too, which would be stronger than a deprecation but get the point across better.

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 will change because I've already spent way too much time arguing about a warning approximately nobody will ever see. However, I do think it's a bad mistake to treat this at all like a publicly documented function. The #[deprecated] lint is being used out of convenience and I strongly disagree that it should list the version the warning was added. That is not at all actionable information.

For what it's worth, I think 1.0 was probably the right choice: it reflects that this wasn't ever supposed to be usable, which is what users should be caring about. Why would a user ever care about having the error message show the deprecated attribute was added? Out lints don't say "lint emitted since 1.xx". If they're checking with two Rust versions then they've figured it out, if they care more than that then look at the blame 🤷‍♂️.

But agreed, it's not worth a battle.

@ChrisDenton
ChrisDenton force-pushed the deprecate-vec-from_elem branch from 07daf37 to 458cde8 Compare October 30, 2026 16:24
@rust-rfcbot rust-rfcbot added finished-final-comment-period The final comment period is finished for this PR / Issue. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. to-announce Announce this issue on triage meeting and removed final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. S-waiting-on-fcp Status: PR is in FCP and is awaiting for FCP to complete. labels Nov 4, 2026
@rust-rfcbot

Copy link
Copy Markdown
Collaborator

The final comment period, with a disposition to merge, as per the review above, is now complete.

As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed.

@ChrisDenton

Copy link
Copy Markdown
Member Author

@bors r=joboet rollup

@rust-bors

rust-bors Bot commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 458cde8 has been approved by joboet

It is now in the queue for this repository.

@rust-bors rust-bors Bot 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 Nov 4, 2026
jhpratt added a commit to jhpratt/rust that referenced this pull request Nov 5, 2026
…m, r=joboet

Deprecate internal function `vec::from_elem`

This is only used internally and is only public because we didn't used to have the ability to gate internal functions used in public macros (other than using `doc(hidden)` to hide it).
@jhpratt

jhpratt commented Nov 5, 2026

Copy link
Copy Markdown
Member

@bors r- #165480 (comment)


warning: use of deprecated function `std::vec::from_elem`: use the `vec!` macro instead
##[warning]  --> $DIR/vec-spec-extend-with-correct-drop.rs:47:19
   |
LL |     let mut vec = from_elem(DropCounter(&c), 3);
   |                   ^^^^^^^^^

warning: use of deprecated function `std::vec::from_elem`: use the `vec!` macro instead
##[warning]  --> $DIR/vec-spec-extend-with-correct-drop.rs:51:11
   |
LL |     vec = from_elem(DropCounter(&c), 6);
   |           ^^^^^^^^^

warning: use of deprecated function `std::vec::from_elem`: use the `vec!` macro instead
##[warning]  --> $DIR/vec-spec-extend-with-correct-drop.rs:56:11
   |
LL |     vec = from_elem(DropCounter(&c), 0);
   |           ^^^^^^^^^

warning: use of deprecated function `std::vec::from_elem`: use the `vec!` macro instead
##[warning]  --> $DIR/vec-spec-extend-with-correct-drop.rs:65:19
   |
LL |     let mut vec = from_elem(CloneCounter::new(&clones, &drops), 3);
   |                   ^^^^^^^^^

warning: use of deprecated function `std::vec::from_elem`: use the `vec!` macro instead
##[warning]  --> $DIR/vec-spec-extend-with-correct-drop.rs:72:11
   |
LL |     vec = from_elem(CloneCounter::new(&clones, &drops), 6);
   |           ^^^^^^^^^

warning: use of deprecated function `std::vec::from_elem`: use the `vec!` macro instead
##[warning]  --> $DIR/vec-spec-extend-with-correct-drop.rs:79:11
   |
LL |     vec = from_elem(CloneCounter::new(&clones, &drops), 0);
   |           ^^^^^^^^^

warning: 7 warnings emitted


---
To only update this specific test, also pass `--test-args array-slice-vec/vec-spec-extend-with-correct-drop.rs`

error: 1 errors occurred comparing output.
status: exit status: 0
command: env -u RUSTC_LOG_COLOR RUSTC_ICE="0" RUST_BACKTRACE="short" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/ui/array-slice-vec/vec-spec-extend-with-correct-drop.rs" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=/cargo" "-Z" "ignore-directory-in-diagnostics-source-blocks=/checkout/vendor" "--sysroot" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2" "--target=aarch64-unknown-linux-gnu" "--check-cfg" "cfg(test,FALSE)" "-O" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Zwrite-long-types-to-disk=no" "-Cstrip=debuginfo" "-C" "prefer-dynamic" "-o" "/checkout/obj/build/aarch64-unknown-linux-gnu/test/ui/array-slice-vec/vec-spec-extend-with-correct-drop/a" "-Znext-solver=coherence" "-A" "internal_features" "-A" "incomplete_features" "-A" "unused_parens" "-A" "unused_braces" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/aarch64-unknown-linux-gnu/native/rust-test-helpers" "--edition=2015"
stdout: none
--- stderr -------------------------------
warning: use of deprecated function `std::vec::from_elem`: use the `vec!` macro instead
##[warning]  --> /checkout/tests/ui/array-slice-vec/vec-spec-extend-with-correct-drop.rs:6:15
   |
---

warning: use of deprecated function `std::vec::from_elem`: use the `vec!` macro instead
##[warning]  --> /checkout/tests/ui/array-slice-vec/vec-spec-extend-with-correct-drop.rs:47:19
   |
LL |     let mut vec = from_elem(DropCounter(&c), 3);
   |                   ^^^^^^^^^

warning: use of deprecated function `std::vec::from_elem`: use the `vec!` macro instead
##[warning]  --> /checkout/tests/ui/array-slice-vec/vec-spec-extend-with-correct-drop.rs:51:11
   |
LL |     vec = from_elem(DropCounter(&c), 6);
   |           ^^^^^^^^^

warning: use of deprecated function `std::vec::from_elem`: use the `vec!` macro instead
##[warning]  --> /checkout/tests/ui/array-slice-vec/vec-spec-extend-with-correct-drop.rs:56:11
   |
LL |     vec = from_elem(DropCounter(&c), 0);
   |           ^^^^^^^^^

warning: use of deprecated function `std::vec::from_elem`: use the `vec!` macro instead
##[warning]  --> /checkout/tests/ui/array-slice-vec/vec-spec-extend-with-correct-drop.rs:65:19
   |
LL |     let mut vec = from_elem(CloneCounter::new(&clones, &drops), 3);
   |                   ^^^^^^^^^

warning: use of deprecated function `std::vec::from_elem`: use the `vec!` macro instead
##[warning]  --> /checkout/tests/ui/array-slice-vec/vec-spec-extend-with-correct-drop.rs:72:11
   |
LL |     vec = from_elem(CloneCounter::new(&clones, &drops), 6);
   |           ^^^^^^^^^

warning: use of deprecated function `std::vec::from_elem`: use the `vec!` macro instead
##[warning]  --> /checkout/tests/ui/array-slice-vec/vec-spec-extend-with-correct-drop.rs:79:11
   |
LL |     vec = from_elem(CloneCounter::new(&clones, &drops), 0);
   |           ^^^^^^^^^

warning: 7 warnings emitted
------------------------------------------

@rust-bors rust-bors Bot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Nov 5, 2026
@rust-bors

rust-bors Bot commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

This pull request was unapproved.

View changes since this unapproval

@ChrisDenton

ChrisDenton commented Nov 5, 2026 •

Copy link
Copy Markdown
Member Author

Why would we add UI test that uses from_elem?! Easy enough to replace I suppose.

@ChrisDenton
ChrisDenton force-pushed the deprecate-vec-from_elem branch from 458cde8 to ddfc252 Compare November 5, 2026 05:06
@rustbot rustbot added the A-slice-vec Area: `[T]` and `Vec<T>` label Nov 5, 2026
@rustbot

rustbot commented Nov 5, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@ChrisDenton

Copy link
Copy Markdown
Member Author

Let's try again

@bors r=joboet rollup

@rust-bors

rust-bors Bot commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

📌 Commit ddfc252 has been tentatively approved by joboet

It will be put into the queue for this repository once PR CI succeeds.

@rust-bors rust-bors Bot 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-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 5, 2026
@tgross35

tgross35 commented Nov 5, 2026

Copy link
Copy Markdown
Member

That is unbelievable timing, but it seems like they may actually want from_elem? Since #163757 mentions it in the description, cc @Paladynee.

@ChrisDenton

ChrisDenton commented Nov 5, 2026 •

Copy link
Copy Markdown
Member Author

Hm, if the intent is to test the UI of both extend_with and resize as well then those should be explicitly used since internals can change in many different ways. I don't think it should be using a proxy for that. On the other hand if only one thing needs to be tested then vec![elem; n] is fine.

rust-bors Bot pushed a commit that referenced this pull request Nov 5, 2026
Rollup of 15 pull requests

Successful merges:

 - #163477 (Fancy paramenv accessors that communicate the access pattern better.)
 - #160593 (turn ABI-controlling target features into a target modifier)
 - #164896 (offload: pass the linked device image to the host pass)
 - #165468 (Remove return type from `BuilderMethods::store`)
 - #164564 (Re-enable outline-atomics by default on more AArch64 platforms)
 - #164623 (add next-solver regression tests for two fixed ices)
 - #164653 (Deprecate internal function `vec::from_elem`)
 - #164678 (add next-solver regression test for recursive opaque ICE under validate-mir)
 - #165387 (windows OpenOptionsExt examples: replace winapi by windows crate)
 - #165429 (Add tests for `-Zcrate-attr` edition and position)
 - #165430 ([tiny] Show context for found assoc item in E0107)
 - #165456 (std: Fix build for the L4Re targets)
 - #165463 (Add BTreeMap::allocator and BTreeSet::allocator)
 - #165466 (Don't "exit" using `panic` on teeos)
 - #165491 (Remove unused compiler types and associated helpers)
@rust-bors
rust-bors Bot merged commit d4aa552 into rust-lang:main Nov 5, 2026
14 checks passed
@rustbot rustbot added this to the 1.101.0 milestone Nov 5, 2026
rust-bors Bot pushed a commit that referenced this pull request Nov 5, 2026
Rollup merge of #164653 - ChrisDenton:deprecate-vec-from_elem, r=joboet

Deprecate internal function `vec::from_elem`

This is only used internally and is only public because we didn't used to have the ability to gate internal functions used in public macros (other than using `doc(hidden)` to hide it).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-slice-vec Area: `[T]` and `Vec<T>` disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. finished-final-comment-period The final comment period is finished for this PR / Issue. 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. to-announce Announce this issue on triage meeting

Projects

None yet

Development

Successfully merging this pull request may close these issues.