virtual_method_shadowed_attr.carbon 918 B

123456789101112131415161718192021222324252627282930313233343536373839
  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: %{explorer-run}
  7. // RUN: %{explorer-run-trace}
  8. // CHECK:STDOUT: c.Foo(): 1
  9. // CHECK:STDOUT: d.Foo(): 2
  10. // CHECK:STDOUT: (*cp).Foo(): 2
  11. // CHECK:STDOUT: result: 0
  12. package ExplorerTest api;
  13. base class C {
  14. var value: i32;
  15. virtual fn Foo[self: Self]() -> i32 {
  16. return self.value;
  17. }
  18. }
  19. base class D extends C {
  20. var value: i32;
  21. // TODO: This should instead use `impl` when supported
  22. virtual fn Foo[self: Self]() -> i32 {
  23. return self.value;
  24. }
  25. }
  26. fn Main() -> i32 {
  27. var c: C = {.value = 1};
  28. Print("c.Foo(): {0}", c.Foo());
  29. var d: D = {.value = 2, .base = {.value = 1}};
  30. Print("d.Foo(): {0}", d.Foo());
  31. var cp: C* = &d;
  32. Print("(*cp).Foo(): {0}", (*cp).Foo());
  33. return 0;
  34. }