mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 21:30:12 +01:00
Explorer: fix class destructor not called with heap.Delete (#2546)
This change ensures that DestroyAction is executed for the value being deallocated when calling heap.Delete. Relates to #2521
This commit is contained in:
+36
@@ -0,0 +1,36 @@
|
||||
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
|
||||
// Exceptions. See /LICENSE for license information.
|
||||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
//
|
||||
// AUTOUPDATE
|
||||
// RUN: %{explorer-run}
|
||||
// RUN: %{explorer-run-trace}
|
||||
// CHECK:STDOUT: Allocate A
|
||||
// CHECK:STDOUT: DESTRUCTOR A
|
||||
// CHECK:STDOUT: Delete A
|
||||
// CHECK:STDOUT: DESTRUCTOR A
|
||||
// CHECK:STDOUT: Return
|
||||
// CHECK:STDOUT: result: 0
|
||||
|
||||
package ExplorerTest api;
|
||||
|
||||
|
||||
class A{
|
||||
fn Create() -> A {
|
||||
return {};
|
||||
}
|
||||
destructor[self: Self] {
|
||||
Print("DESTRUCTOR A");
|
||||
}
|
||||
}
|
||||
|
||||
fn Main() -> i32 {
|
||||
Print("Allocate A");
|
||||
var pa: A* = heap.New(A.Create());
|
||||
|
||||
Print("Delete A");
|
||||
heap.Delete(pa);
|
||||
|
||||
Print("Return");
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
|
||||
// Exceptions. See /LICENSE for license information.
|
||||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
//
|
||||
// AUTOUPDATE
|
||||
// RUN: %{explorer-run}
|
||||
// RUN: %{explorer-run-trace}
|
||||
// CHECK:STDOUT: Allocate B
|
||||
// CHECK:STDOUT: DESTRUCTOR B
|
||||
// CHECK:STDOUT: DESTRUCTOR A
|
||||
// CHECK:STDOUT: Delete B
|
||||
// CHECK:STDOUT: DESTRUCTOR B
|
||||
// CHECK:STDOUT: DESTRUCTOR A
|
||||
// CHECK:STDOUT: Return
|
||||
// CHECK:STDOUT: result: 0
|
||||
|
||||
package ExplorerTest api;
|
||||
|
||||
|
||||
base class A{
|
||||
destructor[self: Self] {
|
||||
Print("DESTRUCTOR A");
|
||||
}
|
||||
}
|
||||
|
||||
class B extends A {
|
||||
fn Create() -> Self{
|
||||
return {.base={}};
|
||||
}
|
||||
destructor[self: Self] {
|
||||
Print("DESTRUCTOR B");
|
||||
}
|
||||
}
|
||||
|
||||
fn Main() -> i32 {
|
||||
Print("Allocate B");
|
||||
var pb: B* = heap.New(B.Create());
|
||||
|
||||
Print("Delete B");
|
||||
heap.Delete(pb);
|
||||
|
||||
Print("Return");
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user