fail_external_impl_omit_self.carbon 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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 Vector {
  8. fn Add[self: Self](b: Self) -> Self;
  9. fn Scale[self: Self](v: i32) -> Self;
  10. }
  11. class Point {
  12. var x: i32;
  13. var y: i32;
  14. }
  15. // Error: need to specify which type implementing `Vector` for.
  16. // CHECK:STDERR: COMPILATION ERROR: fail_external_impl_omit_self.carbon:[[@LINE+1]]: could not resolve 'Self'
  17. impl as Vector {
  18. fn Add[self: Point](b: Point) -> Point {
  19. return {.x = self.x + b.x, .y = self.y + b.y};
  20. }
  21. fn Scale[self: Point](v: i32) -> Point {
  22. return {.x = self.x * v, .y = self.y * v};
  23. }
  24. }
  25. fn AddAndScaleGeneric[T:! Vector](a: T, b: T, s: i32) -> T {
  26. var m: auto = a.Add;
  27. var n: auto = m(b).Scale;
  28. return n(s);
  29. }
  30. fn Main() -> i32 {
  31. var a: Point = {.x = 1, .y = 4};
  32. var b: Point = {.x = 2, .y = 3};
  33. var p: Point = AddAndScaleGeneric(a, b, 5);
  34. return p.x - 15;
  35. }