extends.carbon 1.2 KB

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