instantiate_from_generic.carbon 839 B

12345678910111213141516171819202122232425262728293031323334
  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: result: 0
  8. package ExplorerTest api;
  9. interface I { fn F[self: Self](); }
  10. fn CheckTimplsI[T:! I](x: T) { x.F(); }
  11. interface J { fn F[self: Self](); }
  12. impl forall [template T:! I] T as J {
  13. fn F[self: Self]() { CheckTimplsI(self); }
  14. }
  15. // Ensure that the instantiated `impl T as J` is type-checked in an impl scope
  16. // where `T impls I` is available, so that its call to `CheckTimplsI` is valid.
  17. fn UseTemplatedImplFromGeneric[T:! I](x: T) { x.(J.F)(); }
  18. impl i32 as I {
  19. fn F[self: i32]() {
  20. Print("{0}", self);
  21. }
  22. }
  23. fn Main() -> i32 {
  24. UseTemplatedImplFromGeneric(1);
  25. return 0;
  26. }