|
| 1 | +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | +// Test control flow to follow label_break_value semantics |
| 12 | +fn label_break(a: bool, b: bool) -> u32 { |
| 13 | + let mut v = 0; |
| 14 | + 'b: { |
| 15 | + v = 1; |
| 16 | + if a { |
| 17 | + break 'b; |
| 18 | + } |
| 19 | + v = 2; |
| 20 | + if b { |
| 21 | + break 'b; |
| 22 | + } |
| 23 | + v = 3; |
| 24 | + } |
| 25 | + return v; |
| 26 | +} |
| 27 | + |
| 28 | +// Test that values can be returned |
| 29 | +fn break_value(a: bool, b: bool) -> u32 { |
| 30 | + let result = 'block: { |
| 31 | + if a { break 'block 1; } |
| 32 | + if b { break 'block 2; } |
| 33 | + 3 |
| 34 | + }; |
| 35 | + result |
| 36 | +} |
| 37 | + |
| 38 | +// Test nesting of labeled blocks |
| 39 | +// here we only check that it compiles |
| 40 | +fn label_break_nested() { |
| 41 | + 'b: { |
| 42 | + println!("hi"); |
| 43 | + if false { |
| 44 | + break 'b; |
| 45 | + } |
| 46 | + 'c: { |
| 47 | + if false { |
| 48 | + break 'b; |
| 49 | + } |
| 50 | + break 'c; |
| 51 | + } |
| 52 | + println!("hello"); |
| 53 | + if true { |
| 54 | + break 'b; |
| 55 | + } |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +// Tests for mixing labeled blocks with loop constructs |
| 60 | +// This function should be the identity function |
| 61 | +fn label_break_mixed(v: u32) -> u32 { |
| 62 | + let mut r = 0; |
| 63 | + 'b: { |
| 64 | + // Unlabeled break still works |
| 65 | + // (only crossing boundaries is an error) |
| 66 | + loop { |
| 67 | + break; |
| 68 | + } |
| 69 | + if v == 0 { |
| 70 | + break 'b; |
| 71 | + } |
| 72 | + // Labeled breaking an inner loop still works |
| 73 | + 'c: loop { |
| 74 | + if r == 1 { |
| 75 | + break 'c; |
| 76 | + } |
| 77 | + r += 1; |
| 78 | + } |
| 79 | + assert_eq!(r, 1); |
| 80 | + if v == 1 { |
| 81 | + break 'b; |
| 82 | + } |
| 83 | + // Labeled breaking an outer loop still works |
| 84 | + 'd: loop { |
| 85 | + 'e: { |
| 86 | + if v == r { |
| 87 | + break 'b; |
| 88 | + } |
| 89 | + if r == 5 { |
| 90 | + break 'd; |
| 91 | + } |
| 92 | + r += 1; |
| 93 | + } |
| 94 | + } |
| 95 | + assert_eq!(r, 5); |
| 96 | + assert!(v > r); |
| 97 | + // Here we test return from inside a labeled block |
| 98 | + return v; |
| 99 | + } |
| 100 | + r |
| 101 | +} |
| 102 | + |
| 103 | +pub fn main() { |
| 104 | + assert_eq!(label_break(true, false), 1); |
| 105 | + assert_eq!(label_break(false, true), 2); |
| 106 | + assert_eq!(label_break(false, false), 3); |
| 107 | + |
| 108 | + assert_eq!(break_value(true, false), 1); |
| 109 | + assert_eq!(break_value(false, true), 2); |
| 110 | + assert_eq!(break_value(false, false), 3); |
| 111 | + |
| 112 | + assert_eq!(label_break_mixed(0), 0); |
| 113 | + assert_eq!(label_break_mixed(1), 1); |
| 114 | + assert_eq!(label_break_mixed(2), 2); |
| 115 | + assert_eq!(label_break_mixed(3), 3); |
| 116 | + assert_eq!(label_break_mixed(4), 4); |
| 117 | + assert_eq!(label_break_mixed(5), 5); |
| 118 | + assert_eq!(label_break_mixed(6), 6); |
| 119 | + |
| 120 | + // FIXME: ensure that labeled blocks work if produced by macros and in match arms |
| 121 | +} |
0 commit comments