Skip to content

Commit 96c84ac

Browse files
Test Option::unwrap in a const context
1 parent fc2c1f8 commit 96c84ac

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

src/test/ui/consts/const-unwrap.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// check-fail
2+
3+
#![feature(const_option)]
4+
5+
const FOO: i32 = Some(42i32).unwrap();
6+
7+
// This causes an error, but it is attributed to the `panic` *inside* `Option::unwrap` (maybe due
8+
// to `track_caller`?). A note points to the originating `const`.
9+
const BAR: i32 = Option::<i32>::None.unwrap(); //~ NOTE
10+
11+
fn main() {
12+
println!("{}", FOO);
13+
println!("{}", BAR);
14+
}
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error: any use of this value will cause an error
2+
--> $SRC_DIR/core/src/option.rs:LL:COL
3+
|
4+
LL | None => panic!("called `Option::unwrap()` on a `None` value"),
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
| |
7+
| the evaluated program panicked at 'called `Option::unwrap()` on a `None` value', $DIR/const-unwrap.rs:9:38
8+
| inside `std::option::Option::<i32>::unwrap` at $SRC_DIR/core/src/macros/mod.rs:LL:COL
9+
| inside `BAR` at $DIR/const-unwrap.rs:9:18
10+
|
11+
::: $DIR/const-unwrap.rs:9:1
12+
|
13+
LL | const BAR: i32 = Option::<i32>::None.unwrap();
14+
| ----------------------------------------------
15+
|
16+
= note: `#[deny(const_err)]` on by default
17+
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
18+
19+
error: aborting due to previous error
20+

0 commit comments

Comments
 (0)