3bddb2be6780b792248656a086347171ecca2f1d 1.3 KB

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