-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Make Option::unwrap
unstably const
#74956
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Conversation
`Result::unwrap` is not eligible becuase it formats the contents of the `Err` variant. `unwrap_or`, `unwrap_or_else` and friends are not eligible because they drop things or invoke closures.
The error annotations in the test are a bit weird, but it's more of a problem for |
This is useful for:
|
0666a91
to
96c84ac
Compare
@bors r+ rollup |
📌 Commit 96c84ac has been approved by |
@@ -82,6 +82,7 @@ | |||
#![feature(const_fn_union)] | |||
#![feature(const_generics)] | |||
#![feature(const_option)] | |||
#![feature(const_precise_live_drops)] |
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.
What is this? I'm in the const-eval GH group and I don't think I ever saw this or #73255 ever before.^^
const FOO: i32 = Some(42i32).unwrap(); | ||
|
||
// This causes an error, but it is attributed to the `panic` *inside* `Option::unwrap` (maybe due | ||
// to `track_caller`?). A note points to the originating `const`. |
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.
If anything it is because const-eval panic reporting ignores track_caller
.
☀️ Test successful - checks-actions, checks-azure |
This is still not allowed:
|
The |
#![feature(const_trait_impl, const_option)]
use std::convert::TryFrom;
struct A(usize);
struct B(i32);
impl const TryFrom<A> for B {
type Error = &'static str;
fn try_from(x: A) -> Result<Self, &'static str> {
let converted = x.0 as i32;
if x.0 == converted as usize {
Ok(B(converted))
} else {
Err("no")
}
}
}
const N: usize = 20;
const NI: i32 = B::try_from(A(N)).unwrap().0; EDIT: ah nevermind, this unwraps a We can't use |
This is lumped into the
const_option
feature gate (#67441), which enables a potpourri ofOption
methods.cc @rust-lang/wg-const-eval
r? @oli-obk