a2cfb003a0e59b4e421af3880c551363b8ff78f0 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. // CHECK:STDOUT: Struct OK
  7. // CHECK:STDOUT: Choice OK
  8. // CHECK:STDOUT: Class OK
  9. // CHECK:STDOUT: Interface OK
  10. // CHECK:STDOUT: Constraint OK
  11. // CHECK:STDOUT: result: 0
  12. package Foo;
  13. choice Choice { Alternative() }
  14. class Class { fn F(n: i32) -> i32 { return n + 1; } }
  15. interface Interface { fn G[self: Self]() -> Self; }
  16. interface AnotherInterface {}
  17. impl i32 as Interface { fn G[self: i32]() -> i32 { return self + 1; } }
  18. impl i32 as AnotherInterface {}
  19. // TODO: These are intended to be called at compile time. Mark them as
  20. // constexpr once we have syntax for that.
  21. fn GetStruct() -> type { return {.n: i32}; }
  22. fn GetChoice() -> type { return Choice; }
  23. fn GetClass() -> type { return Class; }
  24. fn GetInterface() -> type { return Interface; }
  25. fn GetConstraint() -> type { return Interface & AnotherInterface; }
  26. fn TestStruct() {
  27. var s: GetStruct() = {.n = 1};
  28. if (s.(GetStruct().n) == 1) {
  29. Print("Struct OK");
  30. }
  31. }
  32. fn TestChoice() {
  33. var c: GetChoice() = GetChoice().Alternative();
  34. match (c) {
  35. case GetChoice().Alternative() => {
  36. Print("Choice OK");
  37. }
  38. }
  39. }
  40. fn TestClass() {
  41. if (GetClass().F(1) == 2) {
  42. Print("Class OK");
  43. }
  44. }
  45. fn TestInterface() {
  46. var n: i32 = 1;
  47. if (n.(GetInterface().G)() == 2) {
  48. Print("Interface OK");
  49. }
  50. }
  51. fn TestConstraint() {
  52. var n: i32 = 1;
  53. if (n.(GetConstraint().G)() == 2) {
  54. Print("Constraint OK");
  55. }
  56. }
  57. fn Main() -> i32 {
  58. TestStruct();
  59. TestChoice();
  60. TestClass();
  61. TestInterface();
  62. TestConstraint();
  63. return 0;
  64. }