fail_unambiguous_impl_generic.carbon 1.4 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(T:! type) {
  10. fn Add[self: Self](b: Self) -> Self;
  11. fn Scale[self: Self](v: T) -> Self;
  12. }
  13. class Point(T:! type) {
  14. var x: T;
  15. var y: T;
  16. }
  17. external impl Point(i32) as Vector(i32) {
  18. fn Add[self: Self](b: Self) -> Self {
  19. return {.x = self.x + b.x, .y = self.y + b.y};
  20. }
  21. fn Scale[self: Self](v: i32) -> Self {
  22. return {.x = self.x * v, .y = self.y * v};
  23. }
  24. }
  25. external impl forall [T:! type] Point(T) as Vector(T) {
  26. fn Add[self: Self](b: Self) -> Self {
  27. return self;
  28. }
  29. fn Scale[self: Self](v: T) -> Self {
  30. return self;
  31. }
  32. }
  33. fn AddAndScaleGeneric[T:! Vector(i32)](a: T, b: T, s: i32) -> T {
  34. return a.Add(b).Scale(s);
  35. }
  36. fn Main() -> i32 {
  37. var a: Point(i32) = {.x = 1, .y = 1};
  38. var b: Point(i32) = {.x = 2, .y = 3};
  39. // TODO: This shouldn't be considered ambiguous.
  40. // CHECK:STDERR: COMPILATION ERROR: {{.*}}/explorer/testdata/impl/fail_unambiguous_impl_generic.carbon:[[@LINE+1]]: ambiguous implementations of interface Vector(T = i32) for class Point(T = i32)
  41. var p: Point(i32) = AddAndScaleGeneric(a, b, 5);
  42. return p.x - 15;
  43. }