external_impl_use_self.carbon 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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 Vector {
  13. fn Add[me: Self](b: Self) -> Self;
  14. fn Scale[me: Self](v: i32) -> Self;
  15. }
  16. class Point {
  17. var x: i32;
  18. var y: i32;
  19. }
  20. external impl Point as Vector {
  21. // Allowed: `Self` means `Point` here.
  22. fn Add[me: Self](b: Self) -> Self {
  23. return {.x = me.x + b.x, .y = me.y + b.y};
  24. }
  25. fn Scale[me: Self](v: i32) -> Self {
  26. return {.x = me.x * v, .y = me.y * v};
  27. }
  28. }
  29. fn AddAndScaleGeneric[T:! Vector](a: T, b: T, s: i32) -> T {
  30. var m: auto = a.Add;
  31. var n: auto = m(b).Scale;
  32. return n(s);
  33. }
  34. fn Main() -> i32 {
  35. var a: Point = {.x = 1, .y = 4};
  36. var b: Point = {.x = 2, .y = 3};
  37. var p: Point = AddAndScaleGeneric(a, b, 5);
  38. return p.x - 15;
  39. }