lookup_in_rewrite.carbon 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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: 1
  9. // CHECK:STDOUT: i32.Hash
  10. // CHECK:STDOUT: 0
  11. // CHECK:STDOUT: Potato.(Hashable.Hash)
  12. // CHECK:STDOUT: 1
  13. // CHECK:STDOUT: Potato.Hash
  14. // CHECK:STDOUT: 2
  15. // CHECK:STDOUT: Potato.Hash
  16. // CHECK:STDOUT: 2
  17. // CHECK:STDOUT: result: 0
  18. package ExplorerTest api;
  19. interface Hashable {
  20. fn Hash[self: Self]() -> i32;
  21. }
  22. class Potato {
  23. external impl as Hashable {
  24. fn Hash[self: Self]() -> i32 {
  25. Print("Potato.(Hashable.Hash)");
  26. return 1;
  27. }
  28. }
  29. fn Hash[self: Self]() -> i32 {
  30. Print("Potato.Hash");
  31. return 2;
  32. }
  33. }
  34. interface Maker {
  35. let Result:! Hashable;
  36. fn Make() -> Result;
  37. }
  38. external impl i32 as Hashable {
  39. fn Hash[self: Self]() -> i32 {
  40. Print("i32.Hash");
  41. return self;
  42. }
  43. }
  44. fn F[T:! Maker where .Result = i32](x: T) -> i32 {
  45. // OK, can treat T.Make() as an i32.
  46. return T.Make() + 1;
  47. }
  48. fn G[T:! Maker](x: T) -> i32 {
  49. // OK, Potato.(Hashable.Hash), not Potato.Hash.
  50. return T.Make().Hash();
  51. }
  52. fn H[T:! Maker where .Result = Potato](x: T) -> i32 {
  53. // OK, Potato.Hash, not Potato.(Hashable.Hash).
  54. return T.Make().Hash();
  55. }
  56. fn I[T:! Maker where .Result = Potato](x: T) -> i32 {
  57. var p: Potato = {};
  58. // OK, Potato.Hash, not Potato.(Hashable.Hash), even though we know Potato is
  59. // Hashable here.
  60. return p.Hash();
  61. }
  62. class IntFactory {
  63. impl as Maker where .Result = i32 {
  64. fn Make() -> i32 { return 0; }
  65. }
  66. }
  67. class PotatoFactory {
  68. impl as Maker where .Result = Potato {
  69. fn Make() -> Potato { return {}; }
  70. }
  71. }
  72. fn Main() -> i32 {
  73. var f: IntFactory = {};
  74. var g: PotatoFactory = {};
  75. Print("{0}", F(f));
  76. Print("{0}", G(f));
  77. Print("{0}", G(g));
  78. Print("{0}", H(g));
  79. Print("{0}", I(g));
  80. return 0;
  81. }