match_first.carbon 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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: XYZ: 1
  9. // CHECK:STDOUT: XY: 1
  10. // CHECK:STDOUT: XZ: 1
  11. // CHECK:STDOUT: YZ: 2
  12. // CHECK:STDOUT: JustX: 1
  13. // CHECK:STDOUT: JustY: 2
  14. // CHECK:STDOUT: JustZ: 3
  15. // CHECK:STDOUT: None: 4
  16. // CHECK:STDOUT: result: 0
  17. package ExplorerTest api;
  18. interface A {
  19. fn Which() -> i32;
  20. }
  21. interface X {}
  22. interface Y {}
  23. interface Z {}
  24. __match_first {
  25. external impl forall [T:! X] T as A {
  26. fn Which() -> i32 { return 1; }
  27. }
  28. external impl forall [T:! Y] T as A {
  29. fn Which() -> i32 { return 2; }
  30. }
  31. external impl forall [T:! Z] T as A {
  32. fn Which() -> i32 { return 3; }
  33. }
  34. external impl forall [T:! type] T as A {
  35. fn Which() -> i32 { return 4; }
  36. }
  37. }
  38. class XYZ {}
  39. class XY {}
  40. class XZ {}
  41. class YZ {}
  42. class JustX {}
  43. class JustY {}
  44. class JustZ {}
  45. class None {}
  46. impl XYZ as X & Y & Z {}
  47. impl XY as X & Y {}
  48. impl XZ as X & Z {}
  49. impl YZ as Y & Z {}
  50. impl JustX as X {}
  51. impl JustY as Y {}
  52. impl JustZ as Z {}
  53. fn Main() -> i32 {
  54. Print("XYZ: {0}", XYZ.(A.Which)());
  55. Print("XY: {0}", XY.(A.Which)());
  56. Print("XZ: {0}", XZ.(A.Which)());
  57. Print("YZ: {0}", YZ.(A.Which)());
  58. Print("JustX: {0}", JustX.(A.Which)());
  59. Print("JustY: {0}", JustY.(A.Which)());
  60. Print("JustZ: {0}", JustZ.(A.Which)());
  61. Print("None: {0}", None.(A.Which)());
  62. return 0;
  63. }