Skip to content

In Option::get_or_insert_with(), forget the None instead of dropping it.#148562

Merged
rust-bors[bot] merged 1 commit intorust-lang:mainfrom
kpreid:get-init-drop
Mar 10, 2026
Merged

In Option::get_or_insert_with(), forget the None instead of dropping it.#148562
rust-bors[bot] merged 1 commit intorust-lang:mainfrom
kpreid:get-init-drop

Conversation

@kpreid
Copy link
Contributor

@kpreid kpreid commented Nov 6, 2025

View all comments

Per #148486 (comment)

In Option::get_or_insert_with(), after replacing the None with Some, forget the None instead of dropping it.

This allows eliminating the T: [const] Destruct bounds, making the functions more flexible in (unstable) const contexts, and avoids generating an implicit drop_in_place::<Option<T>>() that will never do anything (and which might even persist after optimization).

@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 Nov 6, 2025
@rustbot
Copy link
Collaborator

rustbot commented Nov 6, 2025

r? @ibraheemdev

rustbot has assigned @ibraheemdev.
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

@kpreid
Copy link
Contributor Author

kpreid commented Nov 6, 2025

cc @cyb0124 (this change is based on their idea)

@kpreid kpreid force-pushed the get-init-drop branch 2 times, most recently from ecc05f4 to 2e71187 Compare November 6, 2025 23:31
Copy link
Member

@hkBst hkBst left a comment

Choose a reason for hiding this comment

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

Thanks! Looks good to me.

View changes since this review

…ing it.

This allows eliminating the `T: [const] Destruct` bounds and avoids
generating an implicit `drop_in_place::<Option<T>>()` that will never do
anything.

Ideally, the compiler would prove that that drop is not necessary
itself, but it currently doesn't, even with `const_precise_live_drops`
enabled.
@ibraheemdev
Copy link
Member

The implementation looks good to me. r? libs-api for the unstable API change.

@rustbot rustbot added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label Feb 9, 2026
@rustbot rustbot assigned the8472 and unassigned ibraheemdev Feb 9, 2026
@kpreid
Copy link
Contributor Author

kpreid commented Mar 9, 2026

Per advice:

@rustbot label -S-waiting-on-review +S-waiting-on-t-libs-api

@rustbot
Copy link
Collaborator

rustbot commented Mar 9, 2026

Error: Unknown labels: S-waiting-on-libs-api

Please file an issue on GitHub at triagebot if there's a problem with this bot, or reach out on #triagebot on Zulip.

@rustbot rustbot added S-waiting-on-t-libs-api Status: Awaiting decision from T-libs-api and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 9, 2026
@oli-obk
Copy link
Contributor

oli-obk commented Mar 9, 2026

Can you check the final optimized runtime MIR before and after your change? I'm not sure we're optimizing the new code properly.

Also wondering about this pattern in general. Seems like sth const precise live drops could also handle, haven't looked at that in forever so I don't remember the details of it.

Anyway, if the final runtime MIR looks good, let's merge this

@kpreid
Copy link
Contributor Author

kpreid commented Mar 9, 2026

Can you check the final optimized runtime MIR before and after your change? I'm not sure we're optimizing the new code properly.

I’m not familiar with how to do that, but I will look into it.

Seems like sth const precise live drops could also handle,

See prior discussion #148562 (comment) — in brief, having const_precise_live_drops would mean we are having the compiler doing static checking on how control flow affects values (whether the option is None), which is something that we generally only do for lint (e.g. if false {} is not counted as unreachable code).

@kpreid
Copy link
Contributor Author

kpreid commented Mar 9, 2026

oli-obk and I have looked at the MIR through dropping both implementations into Compiler Explorer and it seems generally similar, but also not very optimized either before or after.

@rustbot label +const-hack

@rustbot
Copy link
Collaborator

rustbot commented Mar 9, 2026

Error: Unknown labels: const-hack

Please file an issue on GitHub at triagebot if there's a problem with this bot, or reach out on #triagebot on Zulip.

@oli-obk
Copy link
Contributor

oli-obk commented Mar 9, 2026

@bors r+ rollup

r? @oli-obk

@rust-bors
Copy link
Contributor

rust-bors bot commented Mar 9, 2026

📋 This PR cannot be approved because it currently has the following label: S-waiting-on-t-libs-api.

@rustbot rustbot assigned oli-obk and unassigned the8472 Mar 9, 2026
@ChrisDenton ChrisDenton removed the S-waiting-on-t-libs-api Status: Awaiting decision from T-libs-api label Mar 9, 2026
@rust-bors rust-bors bot added the S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. label Mar 9, 2026
@rust-bors

This comment has been minimized.

rust-bors bot pushed a commit that referenced this pull request Mar 9, 2026
In `Option::get_or_insert_with()`, forget the `None` instead of dropping it.

Per #148486 (comment)

In `Option::get_or_insert_with()`, after replacing the `None` with `Some`, forget the `None` instead of dropping it.

This allows eliminating the `T: [const] Destruct` bounds, making the functions more flexible in (unstable) const contexts, and avoids generating an implicit `drop_in_place::<Option<T>>()` that will never do anything (and which might even persist after optimization).
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Mar 9, 2026
In `Option::get_or_insert_with()`, forget the `None` instead of dropping it.

Per rust-lang#148486 (comment)

In `Option::get_or_insert_with()`, after replacing the `None` with `Some`, forget the `None` instead of dropping it.

This allows eliminating the `T: [const] Destruct` bounds, making the functions more flexible in (unstable) const contexts, and avoids generating an implicit `drop_in_place::<Option<T>>()` that will never do anything (and which might even persist after optimization).
@matthiaskrgr
Copy link
Member

@bors yield

@rust-bors
Copy link
Contributor

rust-bors bot commented Mar 9, 2026

Auto build was cancelled. Cancelled workflows:

The next pull request likely to be tested is #153628.

rust-bors bot pushed a commit that referenced this pull request Mar 9, 2026
Rollup of 8 pull requests

Successful merges:

 - #148562 (In `Option::get_or_insert_with()`, forget the `None` instead of dropping it.)
 - #149931 (rustdoc: don't give depreciation notes special handling)
 - #153608 (ast_passes: unsupported arch w/ scalable vectors)
 - #153609 (Add missing `Diag::with_span_suggestion_with_style` method)
 - #153610 (Only lint unused features if they are unstable)
 - #153616 (Update `sysinfo` version to `0.38.4`)
 - #153619 (Update books)
 - #153624 (Ping fmease on parser modifications)
@rust-bors

This comment has been minimized.

rust-bors bot pushed a commit that referenced this pull request Mar 9, 2026
In `Option::get_or_insert_with()`, forget the `None` instead of dropping it.

Per #148486 (comment)

In `Option::get_or_insert_with()`, after replacing the `None` with `Some`, forget the `None` instead of dropping it.

This allows eliminating the `T: [const] Destruct` bounds, making the functions more flexible in (unstable) const contexts, and avoids generating an implicit `drop_in_place::<Option<T>>()` that will never do anything (and which might even persist after optimization).
@Zalathar
Copy link
Member

The dist-x86_64-linux job is stuck.

@bors yield

@rust-bors
Copy link
Contributor

rust-bors bot commented Mar 10, 2026

Auto build was cancelled. Cancelled workflows:

The next pull request likely to be tested is #153631.

rust-bors bot pushed a commit that referenced this pull request Mar 10, 2026
Rollup of 4 pull requests

Successful merges:

 - #148562 (In `Option::get_or_insert_with()`, forget the `None` instead of dropping it.)
 - #153325 (tests/ui/cfg: add annotations for reference rules)
 - #153621 (Remove `TyCtxt::node_span_lint` method)
 - #153627 (rustdoc-json: Improve docs for `ItemEnum::item_kind`)
@rust-bors

This comment has been minimized.

rust-bors bot pushed a commit that referenced this pull request Mar 10, 2026
In `Option::get_or_insert_with()`, forget the `None` instead of dropping it.

Per #148486 (comment)

In `Option::get_or_insert_with()`, after replacing the `None` with `Some`, forget the `None` instead of dropping it.

This allows eliminating the `T: [const] Destruct` bounds, making the functions more flexible in (unstable) const contexts, and avoids generating an implicit `drop_in_place::<Option<T>>()` that will never do anything (and which might even persist after optimization).
@rust-bors rust-bors bot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Mar 10, 2026
@rust-bors
Copy link
Contributor

rust-bors bot commented Mar 10, 2026

💔 Test for 8744ff0 failed: CI. Failed job:

@Zalathar
Copy link
Member

@bors retry (network failure)

@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 Mar 10, 2026
rust-bors bot pushed a commit that referenced this pull request Mar 10, 2026
Rollup of 4 pull requests

Successful merges:

 - #148562 (In `Option::get_or_insert_with()`, forget the `None` instead of dropping it.)
 - #153325 (tests/ui/cfg: add annotations for reference rules)
 - #153621 (Remove `TyCtxt::node_span_lint` method)
 - #153627 (rustdoc-json: Improve docs for `ItemEnum::item_kind`)
@rust-log-analyzer
Copy link
Collaborator

A job failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
/dev/sda15      105M  6.2M   99M   6% /boot/efi
tmpfs           1.6G   12K  1.6G   1% /run/user/1001
================================================================================

Sufficient disk space available (95001912KB >= 52428800KB). Skipping cleanup.
##[group]Run src/ci/scripts/setup-environment.sh
src/ci/scripts/setup-environment.sh
shell: /usr/bin/bash --noprofile --norc -e -o pipefail {0}

rust-bors bot pushed a commit that referenced this pull request Mar 10, 2026
Rollup of 4 pull requests

Successful merges:

 - #148562 (In `Option::get_or_insert_with()`, forget the `None` instead of dropping it.)
 - #153325 (tests/ui/cfg: add annotations for reference rules)
 - #153621 (Remove `TyCtxt::node_span_lint` method)
 - #153627 (rustdoc-json: Improve docs for `ItemEnum::item_kind`)
@rust-bors rust-bors bot merged commit ce97229 into rust-lang:main Mar 10, 2026
11 of 12 checks passed
@rustbot rustbot added this to the 1.96.0 milestone Mar 10, 2026
rust-timer added a commit that referenced this pull request Mar 10, 2026
Rollup merge of #148562 - kpreid:get-init-drop, r=oli-obk

In `Option::get_or_insert_with()`, forget the `None` instead of dropping it.

Per #148486 (comment)

In `Option::get_or_insert_with()`, after replacing the `None` with `Some`, forget the `None` instead of dropping it.

This allows eliminating the `T: [const] Destruct` bounds, making the functions more flexible in (unstable) const contexts, and avoids generating an implicit `drop_in_place::<Option<T>>()` that will never do anything (and which might even persist after optimization).
@kpreid kpreid deleted the get-init-drop branch March 10, 2026 20:40
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. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.