69bab187ee3bed045d1af0a1325d5daabbfebcb7 1.9 KB

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