]> git.lizzy.rs Git - rust.git/blob - src/test/ui/consts/const-try.rs
Rollup merge of #87596 - jesyspa:issue-87318-hidden-whitespace, r=estebank
[rust.git] / src / test / ui / consts / const-try.rs
1 // check-pass
2
3 // Demonstrates what's needed to make use of `?` in const contexts.
4
5 #![crate_type = "lib"]
6 #![feature(try_trait_v2)]
7 #![feature(const_trait_impl)]
8 #![feature(const_try)]
9
10 use std::ops::{ControlFlow, FromResidual, Try};
11
12 struct TryMe;
13 struct Error;
14
15 impl const FromResidual<Error> for TryMe {
16     fn from_residual(residual: Error) -> Self {
17         TryMe
18     }
19 }
20
21 impl const Try for TryMe {
22     type Output = ();
23     type Residual = Error;
24     fn from_output(output: Self::Output) -> Self {
25         TryMe
26     }
27     fn branch(self) -> ControlFlow<Self::Residual, Self::Output> {
28         ControlFlow::Break(Error)
29     }
30 }
31
32 const fn t() -> TryMe {
33     TryMe?;
34     TryMe
35 }
36
37 const _: () = {
38     t();
39 };