extends.carbon 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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: 5
  7. // CHECK:STDOUT: 5
  8. // CHECK:STDOUT: 5
  9. // CHECK:STDOUT: 5
  10. // CHECK:STDOUT: 5
  11. // CHECK:STDOUT: result: 0
  12. package ExplorerTest api;
  13. interface Maker(T:! type) {
  14. fn Make[self: Self]() -> T;
  15. }
  16. constraint IndirectMaker(T:! type) {
  17. extends Maker(T*);
  18. }
  19. constraint MoreIndirectMaker {
  20. extends IndirectMaker(i32);
  21. }
  22. class PointerFactory {
  23. var p: i32*;
  24. external impl as Maker(i32*) {
  25. fn Make[self: Self]() -> i32* { return self.p; }
  26. }
  27. }
  28. fn CallIndirect[T:! IndirectMaker(i32)](x: T) -> i32 {
  29. return *x.Make();
  30. }
  31. fn CallMoreIndirect[T:! MoreIndirectMaker](x: T) -> i32 {
  32. return *x.Make();
  33. }
  34. fn Main() -> i32 {
  35. var n: i32 = 5;
  36. var f: PointerFactory = {.p = &n};
  37. Print("{0}", *f.(Maker(i32*).Make)());
  38. Print("{0}", *f.(IndirectMaker(i32).Make)());
  39. Print("{0}", *f.(MoreIndirectMaker.Make)());
  40. Print("{0}", CallIndirect(f));
  41. Print("{0}", CallMoreIndirect(f));
  42. return 0;
  43. }