2e7f4e15df56d8a05046d80c7a7ce01e6cb6779b 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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: MemberF.F
  7. // CHECK:STDOUT: ImplF.(HasF.F)
  8. // CHECK:STDOUT: BothFs.(HasF.F)
  9. // CHECK:STDOUT: BothFs.F
  10. // CHECK:STDOUT: BothFs.F
  11. // CHECK:STDOUT: result: 0
  12. package ExplorerTest api;
  13. choice ImplKind {
  14. Checked,
  15. ConstrainedTemplate,
  16. UnconstrainedTemplate
  17. }
  18. interface CallF(K:! ImplKind) {
  19. fn DoIt[self: Self]();
  20. }
  21. interface HasF {
  22. fn F[self: Self]();
  23. }
  24. impl forall [T:! HasF] T as CallF(ImplKind.Checked) {
  25. fn DoIt[self: Self]() { self.F(); }
  26. }
  27. impl forall [template T:! HasF] T as CallF(ImplKind.ConstrainedTemplate) {
  28. fn DoIt[self: Self]() { self.F(); }
  29. }
  30. impl forall [template T:! type] T as CallF(ImplKind.UnconstrainedTemplate) {
  31. fn DoIt[self: Self]() { self.F(); }
  32. }
  33. class MemberF {
  34. fn F[self: Self]() { Print("MemberF.F"); }
  35. }
  36. class ImplF {}
  37. impl ImplF as HasF {
  38. fn F[self: Self]() { Print("ImplF.(HasF.F)"); }
  39. }
  40. class BothFs {
  41. fn F[self: Self]() { Print("BothFs.F"); }
  42. }
  43. impl BothFs as HasF {
  44. fn F[self: Self]() { Print("BothFs.(HasF.F)"); }
  45. }
  46. fn Main() -> i32 {
  47. var mem: MemberF = {};
  48. var imp: ImplF = {};
  49. var both: BothFs = {};
  50. mem.(CallF(ImplKind.UnconstrainedTemplate).DoIt)();
  51. imp.(CallF(ImplKind.Checked).DoIt)();
  52. // TODO: Should be valid, but currently fails during instantiation.
  53. //imp.(CallF(ImplKind.ConstrainedTemplate).DoIt)();
  54. both.(CallF(ImplKind.Checked).DoIt)();
  55. // TODO: Should be rejected, but currently incorrectly accepted.
  56. // This line can be deleted once it starts failing; we test that this is
  57. // rejected in fail_name_lookup.carbon.
  58. both.(CallF(ImplKind.ConstrainedTemplate).DoIt)();
  59. both.(CallF(ImplKind.UnconstrainedTemplate).DoIt)();
  60. return 0;
  61. }