name_lookup.carbon 1.8 KB

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