virtual_destructor_nested.carbon 1.4 KB

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