-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[embedded] Handle retain/retain ops inside deinit in Embedded Swift's swift_release #76231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// RUN: %empty-directory(%t) | ||
// RUN: %target-swift-frontend %s -enable-experimental-feature Embedded -O -c -o %t/main.o | ||
// RUN: %target-clang %t/main.o -o %t/a.out -dead_strip | ||
// RUN: %target-run %t/a.out | %FileCheck %s | ||
|
||
// REQUIRES: swift_in_compiler | ||
// REQUIRES: executable_test | ||
// REQUIRES: OS=macosx || OS=linux-gnu | ||
|
||
class C {} | ||
|
||
struct Foo { | ||
let foo: C = C() | ||
let bar: C = C() | ||
} | ||
|
||
class Bar {} | ||
class SubBar: Bar { | ||
var qwe = Foo() | ||
} | ||
|
||
var bar: SubBar? = SubBar() | ||
bar = nil | ||
print("OK!") | ||
|
||
// CHECK: OK! | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm a bit lost as to how this test actually tests this scenario in question, and I worry it might silently stop testing this scenario with some compiler change. It might be better to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added a more targeted test. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks great, thanks! |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// RUN: %empty-directory(%t) | ||
// RUN: %target-swift-frontend %s -enable-experimental-feature Embedded -O -c -o %t/main.o | ||
// RUN: %target-clang %t/main.o -o %t/a.out -dead_strip | ||
// RUN: %target-run %t/a.out | %FileCheck %s | ||
|
||
// REQUIRES: swift_in_compiler | ||
// REQUIRES: executable_test | ||
// REQUIRES: OS=macosx || OS=linux-gnu | ||
|
||
public var global_c: C? = nil | ||
|
||
@inline(never) func opaque() { print(global_c == nil ? "global is nil" : "global is not nil") } | ||
|
||
public class C { | ||
init() { print("init") } | ||
deinit { | ||
print("deinit") | ||
global_c = self | ||
opaque() | ||
global_c = nil | ||
opaque() | ||
print("end of deinit") | ||
} | ||
} | ||
|
||
var bar: C? = C() | ||
bar = nil | ||
print("OK!") | ||
|
||
// CHECK: init | ||
// CHECK: deinit | ||
// CHECK: global is not nil | ||
// CHECK: global is nil | ||
// CHECK: end of deinit | ||
// CHECK: OK! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe worth commenting explicitly that there can only be one thread holding a reference to this object at this point (unless there's a bug somewhere), which is why a relaxed store is fine here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seconded. On another note, the non-embedded runtime continues tracking refcounts, and this allows it to raise a fatal error when
self
escapes fromdeinit
by checking the refcount when the memory is freed. See here:swift/stdlib/public/runtime/HeapObject.cpp
Line 850 in 03abb1f
Obviously this is an optional feature and not something you need to do now (or ever), but might be worth keeping in mind as a possible enhancement.