choice.carbon 968 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. //
  5. // AUTOUPDATE
  6. // RUN: %{explorer-run}
  7. // RUN: %{explorer-run-trace}
  8. // CHECK:STDOUT: result: 1
  9. package ExplorerTest api;
  10. choice Ints {
  11. None,
  12. One(i32),
  13. Two(i32, i32)
  14. }
  15. // Test some alternate syntaxes
  16. choice MoreInts {
  17. None(),
  18. One(i32),
  19. Two(i32, i32),
  20. }
  21. fn Main() -> i32 {
  22. var x: Ints = Ints.None;
  23. var y: Ints = Ints.One(42);
  24. var z: MoreInts = MoreInts.None();
  25. var n: i32 = 0;
  26. match (y) {
  27. case Ints.None => { n = n + 2; }
  28. case Ints.One(x: auto) => { n = x + 1 - 42; }
  29. case Ints.Two(a: auto, b: auto) => { n = 2; }
  30. }
  31. match (x) {
  32. case Ints.One(x: auto) => { n = x + 2; }
  33. case Ints.None => { n = n - 1; }
  34. case Ints.Two(x: auto, y: auto) => { n = 5; }
  35. }
  36. match (z) {
  37. case MoreInts.None() => { ++n; }
  38. }
  39. return n;
  40. }