fork/rust

git clone https://git.julia.blue/fork/rust

borsAuto merge of #103046 - JanBeh:PR_clarify_cmp_terminology, r=workingjubilee3bcad65fbf6

master
1.5 KiB32 linesraw
1   LL|       |#![feature(generators, generator_trait)]
2   LL|       |
3   LL|       |use std::ops::{Generator, GeneratorState};
4   LL|       |use std::pin::Pin;
5   LL|       |
6   LL|       |// The following implementation of a function called from a `yield` statement
7   LL|       |// (apparently requiring the Result and the `String` type or constructor)
8   LL|       |// creates conditions where the `generator::StateTransform` MIR transform will
9   LL|       |// drop all `Counter` `Coverage` statements from a MIR. `simplify.rs` has logic
10   LL|       |// to handle this condition, and still report dead block coverage.
11   LL|      1|fn get_u32(val: bool) -> Result<u32, String> {
12   LL|      1|    if val { Ok(1) } else { Err(String::from("some error")) }
13                                          ^0
14   LL|      1|}
15   LL|       |
16   LL|      1|fn main() {
17   LL|      1|    let is_true = std::env::args().len() == 1;
18   LL|      1|    let mut generator = || {
19   LL|      1|        yield get_u32(is_true);
20   LL|      1|        return "foo";
21   LL|      1|    };
22   LL|       |
23   LL|      1|    match Pin::new(&mut generator).resume(()) {
24   LL|      1|        GeneratorState::Yielded(Ok(1)) => {}
25   LL|      0|        _ => panic!("unexpected return from resume"),
26   LL|       |    }
27   LL|      1|    match Pin::new(&mut generator).resume(()) {
28   LL|      1|        GeneratorState::Complete("foo") => {}
29   LL|      0|        _ => panic!("unexpected return from resume"),
30   LL|       |    }
31   LL|      1|}
32