name_lookup.carbon 1.9 KB

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