-
Notifications
You must be signed in to change notification settings - Fork 13.5k
constify Option
methods
#143967
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
constify Option
methods
#143967
Conversation
F: ~const Destruct + ~const FnOnce() -> Option<T>, | ||
//FIXME(const_hack): this `T: ~const Destruct` is unnecessary, but even precise live drops can't tell | ||
// no value of type `T` gets dropped here | ||
T: ~const Destruct, | ||
{ | ||
match self { | ||
x @ Some(_) => x, |
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.
This pattern was introduced in #116481 because it optimizes better.
//FIXME(const_hack): this `T: ~const Destruct` is unnecessary, but even precise live drops can't tell | ||
// no value of type `T` gets dropped here |
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.
I have a feeling this will nerdsnipe me hard.
Here's how code is currently generated
#![feature(const_destruct)]
#![feature(const_trait_impl)]
use std::marker::Destruct;
pub const fn opt_or_else<T, F>(this: Option<T>, f: F) -> Option<T>
where
F: ~const Destruct + ~const FnOnce() -> Option<T>,
T: ~const Destruct
{
match this {
x @ Some(_) => x,
None => f()
}
}
// MIR FOR CTFE
fn opt_or_else(_1: Option<T>, _2: F) -> Option<T> {
debug this => _1;
debug f => _2;
let mut _0: std::option::Option<T>;
let mut _3: isize;
let _4: std::option::Option<T>;
let mut _5: F;
let mut _6: ();
let mut _7: bool;
let mut _8: bool;
scope 1 {
debug x => _4;
}
bb0: {
_8 = const false;
_7 = const false;
_8 = const true;
_7 = const true;
_3 = discriminant(_1);
switchInt(move _3) -> [0: bb2, 1: bb3, otherwise: bb1];
}
bb1: {
unreachable;
}
bb2: {
StorageLive(_5);
_7 = const false;
_5 = move _2;
StorageLive(_6);
_6 = ();
ConstEvalCounter;
_0 = <F as FnOnce<()>>::call_once(move _5, move _6) -> [return: bb4, unwind: bb12];
}
bb3: {
StorageLive(_4);
_8 = const false;
_4 = move _1;
_0 = move _4;
StorageDead(_4);
goto -> bb9;
}
bb4: {
StorageDead(_6);
StorageDead(_5);
goto -> bb9;
}
bb5: {
switchInt(copy _8) -> [0: bb6, otherwise: bb10];
}
bb6: {
return;
}
bb7 (cleanup): {
resume;
}
bb8: {
drop(_2) -> [return: bb5, unwind: bb12];
}
bb9: {
switchInt(copy _7) -> [0: bb5, otherwise: bb8];
}
bb10: {
drop(_1) -> [return: bb6, unwind: bb7];
}
bb11 (cleanup): {
drop(_1) -> [return: bb7, unwind terminate(cleanup)];
}
bb12 (cleanup): {
switchInt(copy _8) -> [0: bb7, otherwise: bb11];
}
}
looks like it tries to drop self
at the None
arm, which is just weird.
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.
r=me after nit
library/core/src/option.rs
Outdated
@@ -649,7 +650,8 @@ impl<T> Option<T> { | |||
#[must_use] | |||
#[inline] | |||
#[stable(feature = "is_some_and", since = "1.70.0")] | |||
pub fn is_some_and(self, f: impl FnOnce(T) -> bool) -> bool { | |||
#[rustc_const_unstable(feature = "const_option_ops", issue = "143956")] | |||
pub const fn is_some_and(self, f: impl ~const Destruct + ~const FnOnce(T) -> bool) -> bool { |
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.
I think we should put FnOnce
first, here and elsewhere in the PR. the destruct bound is noisy and likely to make it harder for someone to read the code.
5d2acad
to
853333d
Compare
@bors r=fee1-dead |
…e1-dead constify `Option` methods r? `@fee1-dead` tracking issue: rust-lang#143956 these unblock making `PartialOrd` and `Ord` const without resorting to inlining some of these at call sites.
…e1-dead constify `Option` methods r? ``@fee1-dead`` tracking issue: rust-lang#143956 these unblock making `PartialOrd` and `Ord` const without resorting to inlining some of these at call sites.
Rollup of 15 pull requests Successful merges: - #142300 (Disable `tests/run-make/mte-ffi` because no CI runners have MTE extensions enabled) - #143271 (Store the type of each GVN value) - #143293 (fix `-Zsanitizer=kcfi` on `#[naked]` functions) - #143719 (Emit warning when there is no space between `-o` and arg) - #143833 (Ban projecting into SIMD types [MCP838]) - #143846 (pass --gc-sections if -Zexport-executable-symbols is enabled and improve tests) - #143879 (parse `const trait Trait`) - #143891 (Port `#[coverage]` to the new attribute system) - #143967 (constify `Option` methods) - #143985 (rustc_public: de-StableMIR-ize) - #144008 (Fix false positive double negations with macro invocation) - #144010 (Boostrap: add warning on `optimize = false`) - #144034 (tests: Test line number in debuginfo for diverging function calls) - #144049 (rustc-dev-guide subtree update) - #144056 (Copy GCC sources into the build directory even outside CI) r? `@ghost` `@rustbot` modify labels: rollup
…e1-dead constify `Option` methods r? ```@fee1-dead``` tracking issue: rust-lang#143956 these unblock making `PartialOrd` and `Ord` const without resorting to inlining some of these at call sites.
Rollup of 12 pull requests Successful merges: - #142300 (Disable `tests/run-make/mte-ffi` because no CI runners have MTE extensions enabled) - #143271 (Store the type of each GVN value) - #143293 (fix `-Zsanitizer=kcfi` on `#[naked]` functions) - #143719 (Emit warning when there is no space between `-o` and arg) - #143846 (pass --gc-sections if -Zexport-executable-symbols is enabled and improve tests) - #143891 (Port `#[coverage]` to the new attribute system) - #143967 (constify `Option` methods) - #144008 (Fix false positive double negations with macro invocation) - #144010 (Boostrap: add warning on `optimize = false`) - #144034 (tests: Test line number in debuginfo for diverging function calls) - #144049 (rustc-dev-guide subtree update) - #144056 (Copy GCC sources into the build directory even outside CI) r? `@ghost` `@rustbot` modify labels: rollup
Rollup of 12 pull requests Successful merges: - #142300 (Disable `tests/run-make/mte-ffi` because no CI runners have MTE extensions enabled) - #143271 (Store the type of each GVN value) - #143293 (fix `-Zsanitizer=kcfi` on `#[naked]` functions) - #143719 (Emit warning when there is no space between `-o` and arg) - #143846 (pass --gc-sections if -Zexport-executable-symbols is enabled and improve tests) - #143891 (Port `#[coverage]` to the new attribute system) - #143967 (constify `Option` methods) - #144008 (Fix false positive double negations with macro invocation) - #144010 (Boostrap: add warning on `optimize = false`) - #144034 (tests: Test line number in debuginfo for diverging function calls) - #144049 (rustc-dev-guide subtree update) - #144056 (Copy GCC sources into the build directory even outside CI) r? `@ghost` `@rustbot` modify labels: rollup
…e1-dead constify `Option` methods r? ````@fee1-dead```` tracking issue: rust-lang#143956 these unblock making `PartialOrd` and `Ord` const without resorting to inlining some of these at call sites.
…e1-dead constify `Option` methods r? `````@fee1-dead````` tracking issue: rust-lang#143956 these unblock making `PartialOrd` and `Ord` const without resorting to inlining some of these at call sites.
…e1-dead constify `Option` methods r? ``````@fee1-dead`````` tracking issue: rust-lang#143956 these unblock making `PartialOrd` and `Ord` const without resorting to inlining some of these at call sites.
Rollup of 11 pull requests Successful merges: - #142300 (Disable `tests/run-make/mte-ffi` because no CI runners have MTE extensions enabled) - #143271 (Store the type of each GVN value) - #143293 (fix `-Zsanitizer=kcfi` on `#[naked]` functions) - #143719 (Emit warning when there is no space between `-o` and arg) - #143846 (pass --gc-sections if -Zexport-executable-symbols is enabled and improve tests) - #143891 (Port `#[coverage]` to the new attribute system) - #143967 (constify `Option` methods) - #144008 (Fix false positive double negations with macro invocation) - #144010 (Boostrap: add warning on `optimize = false`) - #144049 (rustc-dev-guide subtree update) - #144056 (Copy GCC sources into the build directory even outside CI) r? `@ghost` `@rustbot` modify labels: rollup
r? @fee1-dead
tracking issue: #143956
these unblock making
PartialOrd
andOrd
const without resorting to inlining some of these at call sites.