new_and_delete_hierarchy.carbon 901 B

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