fail_ambiguous_impl.carbon 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. // AUTOUPDATE
  6. // RUN: %{not} %{explorer-run}
  7. // RUN: %{not} %{explorer-run-trace}
  8. package ExplorerTest api;
  9. interface Vector {
  10. fn Add[self: Self](b: Self) -> Self;
  11. fn Scale[self: Self](v: i32) -> Self;
  12. }
  13. class Point {
  14. var x: i32;
  15. var y: i32;
  16. }
  17. external impl Point 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. external impl Point as Vector {
  26. fn Add[self: Point](b: Point) -> Point {
  27. return {.x = self.x + b.x, .y = self.y + b.y};
  28. }
  29. fn Scale[self: Point](v: i32) -> Point {
  30. return {.x = self.x * v, .y = self.y * v};
  31. }
  32. // CHECK:STDERR: COMPILATION ERROR: {{.*}}/explorer/testdata/impl/fail_ambiguous_impl.carbon:[[@LINE+1]]: ambiguous implementations of interface Vector for class Point
  33. }
  34. fn AddAndScaleGeneric[T:! Vector](a: T, b: T, s: i32) -> T {
  35. return a.Add(b).Scale(s);
  36. }
  37. fn Main() -> i32 {
  38. var a: Point = {.x = 1, .y = 1};
  39. var b: Point = {.x = 2, .y = 3};
  40. var p: Point = AddAndScaleGeneric(a, b, 5);
  41. return p.x - 15;
  42. }