impl_self_interface_parameter.carbon 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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: 0
  11. package ExplorerTest api;
  12. interface AddWith(T:! Type) {
  13. fn Op[me: Self](b: T) -> Self;
  14. }
  15. class Point {
  16. var x: i32;
  17. var y: i32;
  18. }
  19. // Allowed: `Self` means `Point` after `as`
  20. impl Point as AddWith(Self) {
  21. fn Op[me: Point](b: Point) -> Point {
  22. return {.x = me.x + b.x, .y = me.y + b.y};
  23. }
  24. }
  25. fn DoAddGeneric[T:! Type, U:! AddWith(T)](a: U, b: T) -> U {
  26. return a.Op(b);
  27. }
  28. fn Main() -> i32 {
  29. var a: Point = {.x = 2, .y = 1};
  30. var b: Point = {.x = 4, .y = 2};
  31. var p: Point = DoAddGeneric(a, b);
  32. return p.x - p.y - 3;
  33. }