virtual_destructor.carbon 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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: Allocate C
  9. // CHECK:STDOUT: DESTRUCTOR C
  10. // CHECK:STDOUT: DESTRUCTOR B
  11. // CHECK:STDOUT: DESTRUCTOR A
  12. // CHECK:STDOUT: Delete C from A*
  13. // CHECK:STDOUT: DESTRUCTOR C
  14. // CHECK:STDOUT: DESTRUCTOR B
  15. // CHECK:STDOUT: DESTRUCTOR A
  16. // CHECK:STDOUT: result: 0
  17. package ExplorerTest api;
  18. base class A {
  19. virtual destructor[self: Self] {
  20. Print("DESTRUCTOR A");
  21. }
  22. }
  23. base class B extends A {
  24. impl destructor[self: Self] {
  25. Print("DESTRUCTOR B");
  26. }
  27. }
  28. class C extends B {
  29. fn Create() -> Self{
  30. return {.base={.base={}}};
  31. }
  32. impl destructor[self: Self] {
  33. Print("DESTRUCTOR C");
  34. }
  35. }
  36. fn Main() -> i32 {
  37. Print("Allocate C");
  38. var pc: C* = heap.New(C.Create());
  39. var pa: A* = pc;
  40. Print("Delete C from A*");
  41. heap.Delete(pa);
  42. return 0;
  43. }