microsoft/qdk
Publicmirrored from https://github.com/microsoft/qdkAvailable
compiler/qsc_passes/src/loop_unification/tests.rs
589lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | #![allow(clippy::too_many_lines)] |
| 5 | |
| 6 | use expect_test::{expect, Expect}; |
| 7 | use indoc::indoc; |
| 8 | use qsc_frontend::compile::{self, compile, PackageStore, RuntimeCapabilityFlags, SourceMap}; |
| 9 | use qsc_hir::{mut_visit::MutVisitor, validate::Validator, visit::Visitor}; |
| 10 | |
| 11 | use crate::loop_unification::LoopUni; |
| 12 | |
| 13 | fn check(file: &str, expect: &Expect) { |
| 14 | let store = PackageStore::new(compile::core()); |
| 15 | let sources = SourceMap::new([("test".into(), file.into())], None); |
| 16 | let mut unit = compile(&store, &[], sources, RuntimeCapabilityFlags::all()); |
| 17 | assert!(unit.errors.is_empty(), "{:?}", unit.errors); |
| 18 | LoopUni { |
| 19 | core: store.core(), |
| 20 | assigner: &mut unit.assigner, |
| 21 | } |
| 22 | .visit_package(&mut unit.package); |
| 23 | Validator::default().visit_package(&unit.package); |
| 24 | expect.assert_eq(&unit.package.to_string()); |
| 25 | } |
| 26 | |
| 27 | #[test] |
| 28 | fn convert_for_array() { |
| 29 | check( |
| 30 | indoc! {r#" |
| 31 | namespace test { |
| 32 | operation Main(arr : Int[]) : Unit { |
| 33 | for i in arr { |
| 34 | let x = "Hello World"; |
| 35 | } |
| 36 | } |
| 37 | } |
| 38 | "#}, |
| 39 | &expect![[r#" |
| 40 | Package: |
| 41 | Item 0 [0-133] (Public): |
| 42 | Namespace (Ident 16 [10-14] "test"): Item 1 |
| 43 | Item 1 [21-131] (Public): |
| 44 | Parent: 0 |
| 45 | Callable 0 [21-131] (operation): |
| 46 | name: Ident 1 [31-35] "Main" |
| 47 | input: Pat 2 [36-47] [Type Int[]]: Bind: Ident 3 [36-39] "arr" |
| 48 | output: Unit |
| 49 | functors: empty set |
| 50 | body: SpecDecl 4 [21-131]: Impl: |
| 51 | Block 5 [56-131] [Type Unit]: |
| 52 | Stmt 6 [66-125]: Expr: Expr 43 [66-125] [Type Unit]: Expr Block: Block 44 [66-125] [Type Unit]: |
| 53 | Stmt 18 [0-0]: Local (Immutable): |
| 54 | Pat 19 [75-78] [Type Int[]]: Bind: Ident 17 [75-78] "@array_id_17" |
| 55 | Expr 10 [75-78] [Type Int[]]: Var: Local 3 |
| 56 | Stmt 24 [0-0]: Local (Immutable): |
| 57 | Pat 25 [75-78] [Type Int]: Bind: Ident 21 [75-78] "@len_id_21" |
| 58 | Expr 22 [75-78] [Type Int[]]: Call: |
| 59 | Expr 20 [75-78] [Type (Int[] -> Int)]: Var: |
| 60 | res: Item 1 (Package 0) |
| 61 | generics: |
| 62 | Int |
| 63 | Expr 23 [75-78] [Type Int[]]: Var: Local 17 |
| 64 | Stmt 28 [75-78]: Local (Mutable): |
| 65 | Pat 29 [75-78] [Type Int]: Bind: Ident 26 [75-78] "@index_id_26" |
| 66 | Expr 27 [75-78] [Type Int]: Lit: Int(0) |
| 67 | Stmt 41 [0-0]: Expr: Expr 42 [66-125] [Type Unit]: While: |
| 68 | Expr 38 [75-78] [Type Bool]: BinOp (Lt): |
| 69 | Expr 39 [75-78] [Type Int]: Var: Local 26 |
| 70 | Expr 40 [75-78] [Type Int]: Var: Local 21 |
| 71 | Block 11 [79-125] [Type Unit]: |
| 72 | Stmt 30 [70-71]: Local (Immutable): |
| 73 | Pat 8 [70-71] [Type Int]: Bind: Ident 9 [70-71] "i" |
| 74 | Expr 31 [75-78] [Type Int]: Index: |
| 75 | Expr 32 [75-78] [Type Int[]]: Var: Local 17 |
| 76 | Expr 33 [75-78] [Type Int]: Var: Local 26 |
| 77 | Stmt 12 [93-115]: Local (Immutable): |
| 78 | Pat 13 [97-98] [Type String]: Bind: Ident 14 [97-98] "x" |
| 79 | Expr 15 [101-114] [Type String]: String: |
| 80 | Lit: "Hello World" |
| 81 | Stmt 35 [75-78]: Semi: Expr 36 [75-78] [Type Unit]: AssignOp (Add): |
| 82 | Expr 37 [75-78] [Type Int]: Var: Local 26 |
| 83 | Expr 34 [75-78] [Type Int]: Lit: Int(1) |
| 84 | adj: <none> |
| 85 | ctl: <none> |
| 86 | ctl-adj: <none>"#]], |
| 87 | ); |
| 88 | } |
| 89 | |
| 90 | #[test] |
| 91 | fn convert_for_array_deconstruct() { |
| 92 | check( |
| 93 | indoc! {r#" |
| 94 | namespace test { |
| 95 | operation Main(arr : (Int, Double)[]) : Unit { |
| 96 | for (i, d) in arr { |
| 97 | let x = "Hello World"; |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | "#}, |
| 102 | &expect![[r#" |
| 103 | Package: |
| 104 | Item 0 [0-148] (Public): |
| 105 | Namespace (Ident 19 [10-14] "test"): Item 1 |
| 106 | Item 1 [21-146] (Public): |
| 107 | Parent: 0 |
| 108 | Callable 0 [21-146] (operation): |
| 109 | name: Ident 1 [31-35] "Main" |
| 110 | input: Pat 2 [36-57] [Type (Int, Double)[]]: Bind: Ident 3 [36-39] "arr" |
| 111 | output: Unit |
| 112 | functors: empty set |
| 113 | body: SpecDecl 4 [21-146]: Impl: |
| 114 | Block 5 [66-146] [Type Unit]: |
| 115 | Stmt 6 [76-140]: Expr: Expr 46 [76-140] [Type Unit]: Expr Block: Block 47 [76-140] [Type Unit]: |
| 116 | Stmt 21 [0-0]: Local (Immutable): |
| 117 | Pat 22 [90-93] [Type (Int, Double)[]]: Bind: Ident 20 [90-93] "@array_id_20" |
| 118 | Expr 13 [90-93] [Type (Int, Double)[]]: Var: Local 3 |
| 119 | Stmt 27 [0-0]: Local (Immutable): |
| 120 | Pat 28 [90-93] [Type Int]: Bind: Ident 24 [90-93] "@len_id_24" |
| 121 | Expr 25 [90-93] [Type (Int, Double)[]]: Call: |
| 122 | Expr 23 [90-93] [Type ((Int, Double)[] -> Int)]: Var: |
| 123 | res: Item 1 (Package 0) |
| 124 | generics: |
| 125 | (Int, Double) |
| 126 | Expr 26 [90-93] [Type (Int, Double)[]]: Var: Local 20 |
| 127 | Stmt 31 [90-93]: Local (Mutable): |
| 128 | Pat 32 [90-93] [Type Int]: Bind: Ident 29 [90-93] "@index_id_29" |
| 129 | Expr 30 [90-93] [Type Int]: Lit: Int(0) |
| 130 | Stmt 44 [0-0]: Expr: Expr 45 [76-140] [Type Unit]: While: |
| 131 | Expr 41 [90-93] [Type Bool]: BinOp (Lt): |
| 132 | Expr 42 [90-93] [Type Int]: Var: Local 29 |
| 133 | Expr 43 [90-93] [Type Int]: Var: Local 24 |
| 134 | Block 14 [94-140] [Type Unit]: |
| 135 | Stmt 33 [80-86]: Local (Immutable): |
| 136 | Pat 8 [80-86] [Type (Int, Double)]: Tuple: |
| 137 | Pat 9 [81-82] [Type Int]: Bind: Ident 10 [81-82] "i" |
| 138 | Pat 11 [84-85] [Type Double]: Bind: Ident 12 [84-85] "d" |
| 139 | Expr 34 [90-93] [Type (Int, Double)]: Index: |
| 140 | Expr 35 [90-93] [Type (Int, Double)[]]: Var: Local 20 |
| 141 | Expr 36 [90-93] [Type Int]: Var: Local 29 |
| 142 | Stmt 15 [108-130]: Local (Immutable): |
| 143 | Pat 16 [112-113] [Type String]: Bind: Ident 17 [112-113] "x" |
| 144 | Expr 18 [116-129] [Type String]: String: |
| 145 | Lit: "Hello World" |
| 146 | Stmt 38 [90-93]: Semi: Expr 39 [90-93] [Type Unit]: AssignOp (Add): |
| 147 | Expr 40 [90-93] [Type Int]: Var: Local 29 |
| 148 | Expr 37 [90-93] [Type Int]: Lit: Int(1) |
| 149 | adj: <none> |
| 150 | ctl: <none> |
| 151 | ctl-adj: <none>"#]], |
| 152 | ); |
| 153 | } |
| 154 | |
| 155 | #[test] |
| 156 | fn convert_for_slice() { |
| 157 | check( |
| 158 | indoc! {r#" |
| 159 | namespace test { |
| 160 | operation Main(arr : Int[]) : Unit { |
| 161 | for i in arr[6..-2..2] { |
| 162 | let x = "Hello World"; |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | "#}, |
| 167 | &expect![[r#" |
| 168 | Package: |
| 169 | Item 0 [0-143] (Public): |
| 170 | Namespace (Ident 22 [10-14] "test"): Item 1 |
| 171 | Item 1 [21-141] (Public): |
| 172 | Parent: 0 |
| 173 | Callable 0 [21-141] (operation): |
| 174 | name: Ident 1 [31-35] "Main" |
| 175 | input: Pat 2 [36-47] [Type Int[]]: Bind: Ident 3 [36-39] "arr" |
| 176 | output: Unit |
| 177 | functors: empty set |
| 178 | body: SpecDecl 4 [21-141]: Impl: |
| 179 | Block 5 [56-141] [Type Unit]: |
| 180 | Stmt 6 [66-135]: Expr: Expr 49 [66-135] [Type Unit]: Expr Block: Block 50 [66-135] [Type Unit]: |
| 181 | Stmt 24 [0-0]: Local (Immutable): |
| 182 | Pat 25 [75-88] [Type Int[]]: Bind: Ident 23 [75-88] "@array_id_23" |
| 183 | Expr 10 [75-88] [Type Int[]]: Index: |
| 184 | Expr 11 [75-78] [Type Int[]]: Var: Local 3 |
| 185 | Expr 12 [79-87] [Type Range]: Range: |
| 186 | Expr 13 [79-80] [Type Int]: Lit: Int(6) |
| 187 | Expr 14 [82-84] [Type Int]: UnOp (Neg): |
| 188 | Expr 15 [83-84] [Type Int]: Lit: Int(2) |
| 189 | Expr 16 [86-87] [Type Int]: Lit: Int(2) |
| 190 | Stmt 30 [0-0]: Local (Immutable): |
| 191 | Pat 31 [75-88] [Type Int]: Bind: Ident 27 [75-88] "@len_id_27" |
| 192 | Expr 28 [75-88] [Type Int[]]: Call: |
| 193 | Expr 26 [75-88] [Type (Int[] -> Int)]: Var: |
| 194 | res: Item 1 (Package 0) |
| 195 | generics: |
| 196 | Int |
| 197 | Expr 29 [75-88] [Type Int[]]: Var: Local 23 |
| 198 | Stmt 34 [75-88]: Local (Mutable): |
| 199 | Pat 35 [75-88] [Type Int]: Bind: Ident 32 [75-88] "@index_id_32" |
| 200 | Expr 33 [75-88] [Type Int]: Lit: Int(0) |
| 201 | Stmt 47 [0-0]: Expr: Expr 48 [66-135] [Type Unit]: While: |
| 202 | Expr 44 [75-88] [Type Bool]: BinOp (Lt): |
| 203 | Expr 45 [75-88] [Type Int]: Var: Local 32 |
| 204 | Expr 46 [75-88] [Type Int]: Var: Local 27 |
| 205 | Block 17 [89-135] [Type Unit]: |
| 206 | Stmt 36 [70-71]: Local (Immutable): |
| 207 | Pat 8 [70-71] [Type Int]: Bind: Ident 9 [70-71] "i" |
| 208 | Expr 37 [75-88] [Type Int]: Index: |
| 209 | Expr 38 [75-88] [Type Int[]]: Var: Local 23 |
| 210 | Expr 39 [75-88] [Type Int]: Var: Local 32 |
| 211 | Stmt 18 [103-125]: Local (Immutable): |
| 212 | Pat 19 [107-108] [Type String]: Bind: Ident 20 [107-108] "x" |
| 213 | Expr 21 [111-124] [Type String]: String: |
| 214 | Lit: "Hello World" |
| 215 | Stmt 41 [75-88]: Semi: Expr 42 [75-88] [Type Unit]: AssignOp (Add): |
| 216 | Expr 43 [75-88] [Type Int]: Var: Local 32 |
| 217 | Expr 40 [75-88] [Type Int]: Lit: Int(1) |
| 218 | adj: <none> |
| 219 | ctl: <none> |
| 220 | ctl-adj: <none>"#]], |
| 221 | ); |
| 222 | } |
| 223 | |
| 224 | #[test] |
| 225 | fn convert_for_range() { |
| 226 | check( |
| 227 | indoc! {r#" |
| 228 | namespace test { |
| 229 | operation Main() : Unit { |
| 230 | for i in 0..4 { |
| 231 | let x = "Hello World"; |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | "#}, |
| 236 | &expect![[r#" |
| 237 | Package: |
| 238 | Item 0 [0-123] (Public): |
| 239 | Namespace (Ident 17 [10-14] "test"): Item 1 |
| 240 | Item 1 [21-121] (Public): |
| 241 | Parent: 0 |
| 242 | Callable 0 [21-121] (operation): |
| 243 | name: Ident 1 [31-35] "Main" |
| 244 | input: Pat 2 [35-37] [Type Unit]: Unit |
| 245 | output: Unit |
| 246 | functors: empty set |
| 247 | body: SpecDecl 3 [21-121]: Impl: |
| 248 | Block 4 [45-121] [Type Unit]: |
| 249 | Stmt 5 [55-115]: Expr: Expr 59 [55-115] [Type Unit]: Expr Block: Block 60 [55-115] [Type Unit]: |
| 250 | Stmt 19 [0-0]: Local (Immutable): |
| 251 | Pat 20 [64-68] [Type Range]: Bind: Ident 18 [64-68] "@range_id_18" |
| 252 | Expr 9 [64-68] [Type Range]: Range: |
| 253 | Expr 10 [64-65] [Type Int]: Lit: Int(0) |
| 254 | <no step> |
| 255 | Expr 11 [67-68] [Type Int]: Lit: Int(4) |
| 256 | Stmt 24 [64-68]: Local (Mutable): |
| 257 | Pat 25 [64-68] [Type Int]: Bind: Ident 21 [64-68] "@index_id_21" |
| 258 | Expr 22 [64-68] [Type Int]: Field: |
| 259 | Expr 23 [64-68] [Type Range]: Var: Local 18 |
| 260 | Prim(Start) |
| 261 | Stmt 29 [0-0]: Local (Immutable): |
| 262 | Pat 30 [64-68] [Type Int]: Bind: Ident 26 [64-68] "@step_id_26" |
| 263 | Expr 27 [64-68] [Type Int]: Field: |
| 264 | Expr 28 [64-68] [Type Range]: Var: Local 18 |
| 265 | Prim(Step) |
| 266 | Stmt 34 [0-0]: Local (Immutable): |
| 267 | Pat 35 [64-68] [Type Int]: Bind: Ident 31 [64-68] "@end_id_31" |
| 268 | Expr 32 [64-68] [Type Int]: Field: |
| 269 | Expr 33 [64-68] [Type Range]: Var: Local 18 |
| 270 | Prim(End) |
| 271 | Stmt 57 [0-0]: Expr: Expr 58 [55-115] [Type Unit]: While: |
| 272 | Expr 42 [64-68] [Type Bool]: BinOp (OrL): |
| 273 | Expr 43 [64-68] [Type Bool]: BinOp (AndL): |
| 274 | Expr 44 [64-68] [Type Bool]: BinOp (Gt): |
| 275 | Expr 45 [64-68] [Type Int]: Var: Local 26 |
| 276 | Expr 46 [64-68] [Type Int]: Lit: Int(0) |
| 277 | Expr 47 [64-68] [Type Bool]: BinOp (Lte): |
| 278 | Expr 48 [64-68] [Type Int]: Var: Local 21 |
| 279 | Expr 49 [64-68] [Type Int]: Var: Local 31 |
| 280 | Expr 50 [64-68] [Type Bool]: BinOp (AndL): |
| 281 | Expr 51 [64-68] [Type Bool]: BinOp (Lt): |
| 282 | Expr 52 [64-68] [Type Int]: Var: Local 26 |
| 283 | Expr 53 [64-68] [Type Int]: Lit: Int(0) |
| 284 | Expr 54 [64-68] [Type Bool]: BinOp (Gte): |
| 285 | Expr 55 [64-68] [Type Int]: Var: Local 21 |
| 286 | Expr 56 [64-68] [Type Int]: Var: Local 31 |
| 287 | Block 12 [69-115] [Type Unit]: |
| 288 | Stmt 36 [59-60]: Local (Immutable): |
| 289 | Pat 7 [59-60] [Type Int]: Bind: Ident 8 [59-60] "i" |
| 290 | Expr 37 [64-68] [Type Int]: Var: Local 21 |
| 291 | Stmt 13 [83-105]: Local (Immutable): |
| 292 | Pat 14 [87-88] [Type String]: Bind: Ident 15 [87-88] "x" |
| 293 | Expr 16 [91-104] [Type String]: String: |
| 294 | Lit: "Hello World" |
| 295 | Stmt 39 [64-68]: Semi: Expr 40 [64-68] [Type Unit]: AssignOp (Add): |
| 296 | Expr 41 [64-68] [Type Int]: Var: Local 21 |
| 297 | Expr 38 [64-68] [Type Int]: Var: Local 26 |
| 298 | adj: <none> |
| 299 | ctl: <none> |
| 300 | ctl-adj: <none>"#]], |
| 301 | ); |
| 302 | } |
| 303 | |
| 304 | #[test] |
| 305 | fn convert_for_reverse_range() { |
| 306 | check( |
| 307 | indoc! {r#" |
| 308 | namespace test { |
| 309 | operation Main() : Unit { |
| 310 | for i in 4..-1..0 { |
| 311 | let x = "Hello World"; |
| 312 | } |
| 313 | } |
| 314 | } |
| 315 | "#}, |
| 316 | &expect![[r#" |
| 317 | Package: |
| 318 | Item 0 [0-127] (Public): |
| 319 | Namespace (Ident 19 [10-14] "test"): Item 1 |
| 320 | Item 1 [21-125] (Public): |
| 321 | Parent: 0 |
| 322 | Callable 0 [21-125] (operation): |
| 323 | name: Ident 1 [31-35] "Main" |
| 324 | input: Pat 2 [35-37] [Type Unit]: Unit |
| 325 | output: Unit |
| 326 | functors: empty set |
| 327 | body: SpecDecl 3 [21-125]: Impl: |
| 328 | Block 4 [45-125] [Type Unit]: |
| 329 | Stmt 5 [55-119]: Expr: Expr 61 [55-119] [Type Unit]: Expr Block: Block 62 [55-119] [Type Unit]: |
| 330 | Stmt 21 [0-0]: Local (Immutable): |
| 331 | Pat 22 [64-72] [Type Range]: Bind: Ident 20 [64-72] "@range_id_20" |
| 332 | Expr 9 [64-72] [Type Range]: Range: |
| 333 | Expr 10 [64-65] [Type Int]: Lit: Int(4) |
| 334 | Expr 11 [67-69] [Type Int]: UnOp (Neg): |
| 335 | Expr 12 [68-69] [Type Int]: Lit: Int(1) |
| 336 | Expr 13 [71-72] [Type Int]: Lit: Int(0) |
| 337 | Stmt 26 [64-72]: Local (Mutable): |
| 338 | Pat 27 [64-72] [Type Int]: Bind: Ident 23 [64-72] "@index_id_23" |
| 339 | Expr 24 [64-72] [Type Int]: Field: |
| 340 | Expr 25 [64-72] [Type Range]: Var: Local 20 |
| 341 | Prim(Start) |
| 342 | Stmt 31 [0-0]: Local (Immutable): |
| 343 | Pat 32 [64-72] [Type Int]: Bind: Ident 28 [64-72] "@step_id_28" |
| 344 | Expr 29 [64-72] [Type Int]: Field: |
| 345 | Expr 30 [64-72] [Type Range]: Var: Local 20 |
| 346 | Prim(Step) |
| 347 | Stmt 36 [0-0]: Local (Immutable): |
| 348 | Pat 37 [64-72] [Type Int]: Bind: Ident 33 [64-72] "@end_id_33" |
| 349 | Expr 34 [64-72] [Type Int]: Field: |
| 350 | Expr 35 [64-72] [Type Range]: Var: Local 20 |
| 351 | Prim(End) |
| 352 | Stmt 59 [0-0]: Expr: Expr 60 [55-119] [Type Unit]: While: |
| 353 | Expr 44 [64-72] [Type Bool]: BinOp (OrL): |
| 354 | Expr 45 [64-72] [Type Bool]: BinOp (AndL): |
| 355 | Expr 46 [64-72] [Type Bool]: BinOp (Gt): |
| 356 | Expr 47 [64-72] [Type Int]: Var: Local 28 |
| 357 | Expr 48 [64-72] [Type Int]: Lit: Int(0) |
| 358 | Expr 49 [64-72] [Type Bool]: BinOp (Lte): |
| 359 | Expr 50 [64-72] [Type Int]: Var: Local 23 |
| 360 | Expr 51 [64-72] [Type Int]: Var: Local 33 |
| 361 | Expr 52 [64-72] [Type Bool]: BinOp (AndL): |
| 362 | Expr 53 [64-72] [Type Bool]: BinOp (Lt): |
| 363 | Expr 54 [64-72] [Type Int]: Var: Local 28 |
| 364 | Expr 55 [64-72] [Type Int]: Lit: Int(0) |
| 365 | Expr 56 [64-72] [Type Bool]: BinOp (Gte): |
| 366 | Expr 57 [64-72] [Type Int]: Var: Local 23 |
| 367 | Expr 58 [64-72] [Type Int]: Var: Local 33 |
| 368 | Block 14 [73-119] [Type Unit]: |
| 369 | Stmt 38 [59-60]: Local (Immutable): |
| 370 | Pat 7 [59-60] [Type Int]: Bind: Ident 8 [59-60] "i" |
| 371 | Expr 39 [64-72] [Type Int]: Var: Local 23 |
| 372 | Stmt 15 [87-109]: Local (Immutable): |
| 373 | Pat 16 [91-92] [Type String]: Bind: Ident 17 [91-92] "x" |
| 374 | Expr 18 [95-108] [Type String]: String: |
| 375 | Lit: "Hello World" |
| 376 | Stmt 41 [64-72]: Semi: Expr 42 [64-72] [Type Unit]: AssignOp (Add): |
| 377 | Expr 43 [64-72] [Type Int]: Var: Local 23 |
| 378 | Expr 40 [64-72] [Type Int]: Var: Local 28 |
| 379 | adj: <none> |
| 380 | ctl: <none> |
| 381 | ctl-adj: <none>"#]], |
| 382 | ); |
| 383 | } |
| 384 | |
| 385 | #[test] |
| 386 | fn convert_repeat() { |
| 387 | check( |
| 388 | indoc! {r#" |
| 389 | namespace test { |
| 390 | operation Main() : Unit { |
| 391 | repeat { |
| 392 | let x = "Hello World"; |
| 393 | } until true; |
| 394 | } |
| 395 | } |
| 396 | "#}, |
| 397 | &expect![[r#" |
| 398 | Package: |
| 399 | Item 0 [0-128] (Public): |
| 400 | Namespace (Ident 13 [10-14] "test"): Item 1 |
| 401 | Item 1 [21-126] (Public): |
| 402 | Parent: 0 |
| 403 | Callable 0 [21-126] (operation): |
| 404 | name: Ident 1 [31-35] "Main" |
| 405 | input: Pat 2 [35-37] [Type Unit]: Unit |
| 406 | output: Unit |
| 407 | functors: empty set |
| 408 | body: SpecDecl 3 [21-126]: Impl: |
| 409 | Block 4 [45-126] [Type Unit]: |
| 410 | Stmt 5 [55-120]: Semi: Expr 26 [55-119] [Type Unit]: Expr Block: Block 22 [55-119] [Type Unit]: |
| 411 | Stmt 16 [0-0]: Local (Mutable): |
| 412 | Pat 17 [115-119] [Type Bool]: Bind: Ident 14 [115-119] "@continue_cond_14" |
| 413 | Expr 15 [115-119] [Type Bool]: Lit: Bool(true) |
| 414 | Stmt 23 [0-0]: Expr: Expr 24 [55-119] [Type Unit]: While: |
| 415 | Expr 25 [115-119] [Type Bool]: Var: Local 14 |
| 416 | Block 7 [62-108] [Type Unit]: |
| 417 | Stmt 8 [76-98]: Local (Immutable): |
| 418 | Pat 9 [80-81] [Type String]: Bind: Ident 10 [80-81] "x" |
| 419 | Expr 11 [84-97] [Type String]: String: |
| 420 | Lit: "Hello World" |
| 421 | Stmt 18 [115-119]: Semi: Expr 19 [115-119] [Type Unit]: Assign: |
| 422 | Expr 20 [115-119] [Type Bool]: Var: Local 14 |
| 423 | Expr 21 [115-119] [Type Bool]: UnOp (NotL): |
| 424 | Expr 12 [115-119] [Type Bool]: Lit: Bool(true) |
| 425 | adj: <none> |
| 426 | ctl: <none> |
| 427 | ctl-adj: <none>"#]], |
| 428 | ); |
| 429 | } |
| 430 | |
| 431 | #[test] |
| 432 | fn convert_repeat_fixup() { |
| 433 | check( |
| 434 | indoc! {r#" |
| 435 | namespace test { |
| 436 | operation Main() : Unit { |
| 437 | repeat { |
| 438 | let x = "Hello World"; |
| 439 | } until true |
| 440 | fixup { |
| 441 | let y = "Fixup"; |
| 442 | } |
| 443 | } |
| 444 | } |
| 445 | "#}, |
| 446 | &expect![[r#" |
| 447 | Package: |
| 448 | Item 0 [0-182] (Public): |
| 449 | Namespace (Ident 18 [10-14] "test"): Item 1 |
| 450 | Item 1 [21-180] (Public): |
| 451 | Parent: 0 |
| 452 | Callable 0 [21-180] (operation): |
| 453 | name: Ident 1 [31-35] "Main" |
| 454 | input: Pat 2 [35-37] [Type Unit]: Unit |
| 455 | output: Unit |
| 456 | functors: empty set |
| 457 | body: SpecDecl 3 [21-180]: Impl: |
| 458 | Block 4 [45-180] [Type Unit]: |
| 459 | Stmt 5 [55-174]: Expr: Expr 35 [55-174] [Type Unit]: Expr Block: Block 31 [55-174] [Type Unit]: |
| 460 | Stmt 21 [0-0]: Local (Mutable): |
| 461 | Pat 22 [115-119] [Type Bool]: Bind: Ident 19 [115-119] "@continue_cond_19" |
| 462 | Expr 20 [115-119] [Type Bool]: Lit: Bool(true) |
| 463 | Stmt 32 [0-0]: Expr: Expr 33 [55-174] [Type Unit]: While: |
| 464 | Expr 34 [115-119] [Type Bool]: Var: Local 19 |
| 465 | Block 7 [62-108] [Type Unit]: |
| 466 | Stmt 8 [76-98]: Local (Immutable): |
| 467 | Pat 9 [80-81] [Type String]: Bind: Ident 10 [80-81] "x" |
| 468 | Expr 11 [84-97] [Type String]: String: |
| 469 | Lit: "Hello World" |
| 470 | Stmt 23 [115-119]: Semi: Expr 24 [115-119] [Type Unit]: Assign: |
| 471 | Expr 25 [115-119] [Type Bool]: Var: Local 19 |
| 472 | Expr 26 [115-119] [Type Bool]: UnOp (NotL): |
| 473 | Expr 12 [115-119] [Type Bool]: Lit: Bool(true) |
| 474 | Stmt 27 [134-174]: Expr: Expr 28 [134-174] [Type Unit]: If: |
| 475 | Expr 29 [115-119] [Type Bool]: Var: Local 19 |
| 476 | Expr 30 [134-174] [Type Unit]: Expr Block: Block 13 [134-174] [Type Unit]: |
| 477 | Stmt 14 [148-164]: Local (Immutable): |
| 478 | Pat 15 [152-153] [Type String]: Bind: Ident 16 [152-153] "y" |
| 479 | Expr 17 [156-163] [Type String]: String: |
| 480 | Lit: "Fixup" |
| 481 | adj: <none> |
| 482 | ctl: <none> |
| 483 | ctl-adj: <none>"#]], |
| 484 | ); |
| 485 | } |
| 486 | |
| 487 | #[test] |
| 488 | fn convert_repeat_nested() { |
| 489 | check( |
| 490 | indoc! {r#" |
| 491 | namespace test { |
| 492 | operation Main() : Unit { |
| 493 | let a = true; |
| 494 | let b = false; |
| 495 | let c = true; |
| 496 | repeat { |
| 497 | repeat { |
| 498 | let x = "First"; |
| 499 | } until a |
| 500 | fixup { |
| 501 | let y = "Second"; |
| 502 | } |
| 503 | } until b |
| 504 | fixup { |
| 505 | repeat { |
| 506 | let z = "Third"; |
| 507 | } until c; |
| 508 | } |
| 509 | } |
| 510 | } |
| 511 | "#}, |
| 512 | &expect![[r#" |
| 513 | Package: |
| 514 | Item 0 [0-403] (Public): |
| 515 | Namespace (Ident 43 [10-14] "test"): Item 1 |
| 516 | Item 1 [21-401] (Public): |
| 517 | Parent: 0 |
| 518 | Callable 0 [21-401] (operation): |
| 519 | name: Ident 1 [31-35] "Main" |
| 520 | input: Pat 2 [35-37] [Type Unit]: Unit |
| 521 | output: Unit |
| 522 | functors: empty set |
| 523 | body: SpecDecl 3 [21-401]: Impl: |
| 524 | Block 4 [45-401] [Type Unit]: |
| 525 | Stmt 5 [55-68]: Local (Immutable): |
| 526 | Pat 6 [59-60] [Type Bool]: Bind: Ident 7 [59-60] "a" |
| 527 | Expr 8 [63-67] [Type Bool]: Lit: Bool(true) |
| 528 | Stmt 9 [77-91]: Local (Immutable): |
| 529 | Pat 10 [81-82] [Type Bool]: Bind: Ident 11 [81-82] "b" |
| 530 | Expr 12 [85-90] [Type Bool]: Lit: Bool(false) |
| 531 | Stmt 13 [100-113]: Local (Immutable): |
| 532 | Pat 14 [104-105] [Type Bool]: Bind: Ident 15 [104-105] "c" |
| 533 | Expr 16 [108-112] [Type Bool]: Lit: Bool(true) |
| 534 | Stmt 17 [122-395]: Expr: Expr 90 [122-395] [Type Unit]: Expr Block: Block 86 [122-395] [Type Unit]: |
| 535 | Stmt 76 [0-0]: Local (Mutable): |
| 536 | Pat 77 [291-292] [Type Bool]: Bind: Ident 74 [291-292] "@continue_cond_74" |
| 537 | Expr 75 [291-292] [Type Bool]: Lit: Bool(true) |
| 538 | Stmt 87 [0-0]: Expr: Expr 88 [122-395] [Type Unit]: While: |
| 539 | Expr 89 [291-292] [Type Bool]: Var: Local 74 |
| 540 | Block 19 [129-284] [Type Unit]: |
| 541 | Stmt 20 [143-274]: Expr: Expr 60 [143-274] [Type Unit]: Expr Block: Block 56 [143-274] [Type Unit]: |
| 542 | Stmt 46 [0-0]: Local (Mutable): |
| 543 | Pat 47 [205-206] [Type Bool]: Bind: Ident 44 [205-206] "@continue_cond_44" |
| 544 | Expr 45 [205-206] [Type Bool]: Lit: Bool(true) |
| 545 | Stmt 57 [0-0]: Expr: Expr 58 [143-274] [Type Unit]: While: |
| 546 | Expr 59 [205-206] [Type Bool]: Var: Local 44 |
| 547 | Block 22 [150-198] [Type Unit]: |
| 548 | Stmt 23 [168-184]: Local (Immutable): |
| 549 | Pat 24 [172-173] [Type String]: Bind: Ident 25 [172-173] "x" |
| 550 | Expr 26 [176-183] [Type String]: String: |
| 551 | Lit: "First" |
| 552 | Stmt 48 [205-206]: Semi: Expr 49 [205-206] [Type Unit]: Assign: |
| 553 | Expr 50 [205-206] [Type Bool]: Var: Local 44 |
| 554 | Expr 51 [205-206] [Type Bool]: UnOp (NotL): |
| 555 | Expr 27 [205-206] [Type Bool]: Var: Local 7 |
| 556 | Stmt 52 [225-274]: Expr: Expr 53 [225-274] [Type Unit]: If: |
| 557 | Expr 54 [205-206] [Type Bool]: Var: Local 44 |
| 558 | Expr 55 [225-274] [Type Unit]: Expr Block: Block 28 [225-274] [Type Unit]: |
| 559 | Stmt 29 [243-260]: Local (Immutable): |
| 560 | Pat 30 [247-248] [Type String]: Bind: Ident 31 [247-248] "y" |
| 561 | Expr 32 [251-259] [Type String]: String: |
| 562 | Lit: "Second" |
| 563 | Stmt 78 [291-292]: Semi: Expr 79 [291-292] [Type Unit]: Assign: |
| 564 | Expr 80 [291-292] [Type Bool]: Var: Local 74 |
| 565 | Expr 81 [291-292] [Type Bool]: UnOp (NotL): |
| 566 | Expr 33 [291-292] [Type Bool]: Var: Local 11 |
| 567 | Stmt 82 [307-395]: Expr: Expr 83 [307-395] [Type Unit]: If: |
| 568 | Expr 84 [291-292] [Type Bool]: Var: Local 74 |
| 569 | Expr 85 [307-395] [Type Unit]: Expr Block: Block 34 [307-395] [Type Unit]: |
| 570 | Stmt 35 [321-385]: Semi: Expr 73 [321-384] [Type Unit]: Expr Block: Block 69 [321-384] [Type Unit]: |
| 571 | Stmt 63 [0-0]: Local (Mutable): |
| 572 | Pat 64 [383-384] [Type Bool]: Bind: Ident 61 [383-384] "@continue_cond_61" |
| 573 | Expr 62 [383-384] [Type Bool]: Lit: Bool(true) |
| 574 | Stmt 70 [0-0]: Expr: Expr 71 [321-384] [Type Unit]: While: |
| 575 | Expr 72 [383-384] [Type Bool]: Var: Local 61 |
| 576 | Block 37 [328-376] [Type Unit]: |
| 577 | Stmt 38 [346-362]: Local (Immutable): |
| 578 | Pat 39 [350-351] [Type String]: Bind: Ident 40 [350-351] "z" |
| 579 | Expr 41 [354-361] [Type String]: String: |
| 580 | Lit: "Third" |
| 581 | Stmt 65 [383-384]: Semi: Expr 66 [383-384] [Type Unit]: Assign: |
| 582 | Expr 67 [383-384] [Type Bool]: Var: Local 61 |
| 583 | Expr 68 [383-384] [Type Bool]: UnOp (NotL): |
| 584 | Expr 42 [383-384] [Type Bool]: Var: Local 15 |
| 585 | adj: <none> |
| 586 | ctl: <none> |
| 587 | ctl-adj: <none>"#]], |
| 588 | ); |
| 589 | } |
| 590 | |