Skip to content

Make some Option, Result methods unstably const - #82130

Merged
bors merged 1 commit into
rust-lang:masterfrom
jhpratt:const-option-result
Mar 7, 2021
Merged

bors merged 1 commit into
rust-lang:masterfrom
jhpratt:const-option-result

Conversation

@jhpratt

@jhpratt jhpratt commented Feb 15, 2021 •

Copy link
Copy Markdown
Member

View all comments

The following methods are now unstably const:

  • Option::transpose
  • Option::flatten
  • Result::flatten

While some methods for could likely be made const in the future, nearly all of them require something to be dropped at compile-time, which isn't currently supported. The functions listed above should have a trivial path to stabilization.

@rust-highfive

Copy link
Copy Markdown
Contributor

r? @joshtriplett

(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 Feb 15, 2021
@jhpratt

This comment has been minimized.

@rustbot rustbot added A-const-fn T-libs-api [DEPRECATED; DO NOT USE] labels Feb 15, 2021
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

Comment thread library/core/src/option.rs Resolved
@CDirkx

CDirkx commented Feb 15, 2021 •

Copy link
Copy Markdown
Contributor

The functions listed above should have a trivial path to stabilization.

These functions implicitly depend on const_precise_live_drops, without which the compiler complains that something might be dropped (even though it isn't). I believe these functions thus cannot be stabilized before const_precise_live_drops is stabilized (#76654 (comment)).

@joshtriplett

Copy link
Copy Markdown
Member

r? @ecstatic-morse

@JohnTitor

Copy link
Copy Markdown
Member

Triage: @ecstatic-morse seems busy recently, r? @RalfJung could you take a look?

Comment thread library/core/src/result.rs Resolved
Comment thread library/core/src/option.rs Resolved
@RalfJung

RalfJung commented Mar 5, 2021

Copy link
Copy Markdown
Member

This LGTM; Cc @rust-lang/wg-const-eval

@jhpratt

jhpratt commented Mar 5, 2021 •

Copy link
Copy Markdown
Member Author

With regard to the const-hack label, should we really consider this a hack? I just quickly checked on godbolt, and it looks like the new implementation actually optimizes slightly better at lower opt levels. Not a huge deal either way, but I figure we may as well take a minuscule performance gain where we can. The implementation is straightforward in either situation.

@RalfJung

RalfJung commented Mar 5, 2021 •

Copy link
Copy Markdown
Member

We typically use that label when code gets changed to be const-compatible. I don't feel competent to evaluate whether the old or the new implementation is better, but I can imagine that avoiding higher-order functions helps. @oli-obk what do you think?

@oli-obk

oli-obk commented Mar 6, 2021

Copy link
Copy Markdown
Contributor

I actually prefer the new implementation since imo it's more readable, even if the old version is more compact

Comment thread library/core/src/result.rs Resolved
@RalfJung

RalfJung commented Mar 6, 2021

Copy link
Copy Markdown
Member

I actually prefer the new implementation since imo it's more readable, even if the old version is more compact

All right, I tend to agree so I removed the label.

The following functions are now unstably const:

- Option::transpose
- Option::flatten
- Result::transpose
@RalfJung

RalfJung commented Mar 6, 2021

Copy link
Copy Markdown
Member

@bors r+ rollup

@bors

bors commented Mar 6, 2021

Copy link
Copy Markdown
Contributor

📌 Commit 79c2b75 has been approved by RalfJung

@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 Mar 6, 2021
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request Mar 6, 2021
…Jung

Make some Option, Result methods unstably const

The following methods are now unstably const:

- Option::transpose
- Option::flatten
- Result::flatten

While some methods for could likely be made `const` in the future, nearly all of them require something to be dropped at compile-time, which isn't currently supported. The functions listed above should have a trivial path to stabilization.
bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 7, 2021
Rollup of 13 pull requests

Successful merges:

 - rust-lang#77916 (Change built-in kernel targets to be os = none throughout)
 - rust-lang#82130 (Make some Option, Result methods unstably const)
 - rust-lang#82292 (Prevent specialized ZipImpl from calling `__iterator_get_unchecked` twice with the same index)
 - rust-lang#82402 (Remove RefCell around `module_trait_cache`)
 - rust-lang#82592 (Improve transmute docs with further clarifications)
 - rust-lang#82651 (Cleanup rustdoc warnings)
 - rust-lang#82720 (Fix diagnostic suggests adding type `[type error]`)
 - rust-lang#82751 (improve offset_from docs)
 - rust-lang#82793 (Move some tests to more suitable subdirs)
 - rust-lang#82803 (rustdoc: Add an unstable option to print all unversioned files)
 - rust-lang#82808 (Sync rustc_codegen_cranelift)
 - rust-lang#82822 (Fix typo)
 - rust-lang#82837 (tweak MaybeUninit docs)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors
bors merged commit 0adc196 into rust-lang:master Mar 7, 2021
@rustbot rustbot added this to the 1.52.0 milestone Mar 7, 2021
@RalfJung RalfJung added the A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) label Dec 1, 2024
@clarfonthey clarfonthey added the A-option-result Area: Option and Result combinators label Nov 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) A-option-result Area: Option and Result combinators S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs-api [DEPRECATED; DO NOT USE]

Projects

None yet

Development

Successfully merging this pull request may close these issues.