fail_impl_redefinition.carbon 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. 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. impl Point as Vector {
  16. fn Add[self: Point](b: Point) -> Point {
  17. return {.x = self.x + b.x, .y = self.y + b.y};
  18. }
  19. fn Scale[self: Point](v: i32) -> Point {
  20. return {.x = self.x * v, .y = self.y * v};
  21. }
  22. }
  23. impl Point as Vector {
  24. fn Add[self: Point](b: Point) -> Point {
  25. return {.x = self.x + b.x, .y = self.y + b.y};
  26. }
  27. fn Scale[self: Point](v: i32) -> Point {
  28. return {.x = self.x * v, .y = self.y * v};
  29. }
  30. // CHECK:STDERR: COMPILATION ERROR: fail_impl_redefinition.carbon:[[@LINE+1]]: ambiguous implementations of interface Vector for class Point
  31. }
  32. fn AddAndScaleGeneric[T:! Vector](a: T, b: T, s: i32) -> T {
  33. return a.Add(b).Scale(s);
  34. }
  35. fn Main() -> i32 {
  36. var a: Point = {.x = 1, .y = 1};
  37. var b: Point = {.x = 2, .y = 3};
  38. var p: Point = AddAndScaleGeneric(a, b, 5);
  39. return p.x - 15;
  40. }