fail_ambiguous_impl.carbon 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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: %{not} %{executable_semantics} %s 2>&1 | \
  6. // RUN: %{FileCheck} --match-full-lines --allow-unused-prefixes=false %s
  7. // RUN: %{not} %{executable_semantics} --parser_debug --trace_file=- %s 2>&1 | \
  8. // RUN: %{FileCheck} --match-full-lines --allow-unused-prefixes %s
  9. // AUTOUPDATE: %{executable_semantics} %s
  10. // CHECK: COMPILATION ERROR: {{.*}}/executable_semantics/testdata/impl/fail_ambiguous_impl.carbon:50: ambiguous implementations of interface Vector for class Point
  11. package ExecutableSemanticsTest 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. fn Add[me: Point](b: Point) -> Point {
  22. return {.x = me.x + b.x, .y = me.y + b.y};
  23. }
  24. fn Scale[me: Point](v: i32) -> Point {
  25. return {.x = me.x * v, .y = me.y * v};
  26. }
  27. }
  28. external impl Point as Vector {
  29. fn Add[me: Point](b: Point) -> Point {
  30. return {.x = me.x + b.x, .y = me.y + b.y};
  31. }
  32. fn Scale[me: Point](v: i32) -> Point {
  33. return {.x = me.x * v, .y = me.y * v};
  34. }
  35. }
  36. fn AddAndScaleGeneric[T:! Vector](a: T, b: T, s: i32) -> T {
  37. return a.Add(b).Scale(s);
  38. }
  39. fn Main() -> i32 {
  40. var a: Point = {.x = 1, .y = 1};
  41. var b: Point = {.x = 2, .y = 3};
  42. var p: Point = AddAndScaleGeneric(a, b, 5);
  43. return p.x - 15;
  44. }