instantiate_from_generic.carbon 842 B

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