impl_in_generic_class.carbon 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  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. // RUN: %{explorer} %s 2>&1 | \
  6. // RUN: %{FileCheck} --match-full-lines --allow-unused-prefixes=false %s
  7. // RUN: %{explorer} --parser_debug --trace_file=- %s 2>&1 | \
  8. // RUN: %{FileCheck} --match-full-lines --allow-unused-prefixes %s
  9. // AUTOUPDATE: %{explorer} %s
  10. // CHECK: result: 1133
  11. package ExplorerTest api;
  12. interface I(T:! Type) {
  13. fn F[me: Self](t: T, o: Self) -> Self;
  14. }
  15. class X(U:! Type) {
  16. impl as I(U) {
  17. fn F[me: Self](u: U, o: Self) -> Self { return {.m = u, .n = me.n + o.n}; }
  18. }
  19. var m: U;
  20. var n: i32;
  21. }
  22. fn Run[V:! Type](x: V) -> (V, V, i32, i32) {
  23. var v: X(V) = {.m = x, .n = 1};
  24. var w: X(V) = {.m = x, .n = 2};
  25. // OK, know that `X(V)` implements `I(V)` from the `impl` in the class.
  26. var call: X(V) = v.(X(V).(I(V).F))(x, w);
  27. return (call.(X(V).m), call.m, call.(X(V).n), call.n);
  28. }
  29. fn Main() -> i32 {
  30. var (a: i32, b: i32, c: i32, d: i32) = Run(1);
  31. return a * 1000 + b * 100 + c * 10 + d;
  32. }