choice.carbon 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. // RUN: %{explorer} %s 2>&1 | \
  6. // RUN: %{FileCheck} --match-full-lines --allow-unused-prefixes=false %s
  7. // RUN: %{explorer} --parser_debug --trace_file=- %s 2>&1 | \
  8. // RUN: %{FileCheck} --match-full-lines --allow-unused-prefixes %s
  9. // AUTOUPDATE: %{explorer} %s
  10. // CHECK: result: 0
  11. package ExplorerTest api;
  12. choice Ints {
  13. None,
  14. One(i32),
  15. Two(i32, i32)
  16. }
  17. fn Main() -> i32 {
  18. var x: Ints = Ints.None();
  19. var y: Ints = Ints.One(42);
  20. var n: i32 = 0;
  21. match (y) {
  22. case Ints.None() => { n = n + 2; }
  23. case Ints.One(x: auto) => { n = x + 1 - 42; }
  24. case Ints.Two(a: auto, b: auto) => { n = 2; }
  25. }
  26. match (x) {
  27. case Ints.One(x: auto) => { n = x + 2; }
  28. case Ints.None() => { n = n - 1; }
  29. case Ints.Two(x: auto, y: auto) => { n = 5; }
  30. }
  31. return n;
  32. }
  33. // Test some alternate syntaxes
  34. choice MoreInts {
  35. None(),
  36. One(i32),
  37. Two(i32, i32),
  38. }