-
Notifications
You must be signed in to change notification settings - Fork 10.6k
DI: Place cleanups for partially-initialized non-Copyable values on the mark_unresolved_noncopyable marker.
#85077
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
Merged
jckarter
merged 1 commit into
swiftlang:main
from
jckarter:moveonly-after-definit-cleanup
Oct 24, 2025
+177
−13
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| // RUN: %target-run-simple-swift | ||
| // REQUIRES: executable_test | ||
|
|
||
| struct Butt: Error {} | ||
|
|
||
| struct Inside: ~Copyable { | ||
| init() throws { throw Butt() } | ||
| } | ||
|
|
||
| class C { | ||
| deinit { print("destroying") } | ||
| } | ||
|
|
||
| struct Outside: ~Copyable { | ||
| let instance: Inside | ||
| var c: C | ||
|
|
||
| init() throws { | ||
| c = C() | ||
| let instance = try Inside() | ||
| self.instance = instance | ||
| } | ||
| } | ||
|
|
||
| // CHECK: begin | ||
| // CHECK-NEXT: destroying | ||
| // CHECK-NEXT: caught | ||
| // CHECK-NEXT: end | ||
|
|
||
| func test() { | ||
| print("begin") | ||
| do { | ||
| _ = try Outside() | ||
| } catch { | ||
| print("caught") | ||
| } | ||
| print("end") | ||
| } | ||
| test() | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| // RUN: %target-sil-opt -module-name definite_init_moveonly -enable-sil-verify-all -definite-init %s | %FileCheck %s | ||
|
|
||
| import Swift | ||
|
|
||
| class C { | ||
| init() | ||
| } | ||
|
|
||
| struct S: ~Copyable { | ||
| var c1: C | ||
| var c2: C | ||
| } | ||
|
|
||
| // Ensure that, when DI introduces cleanups for partially initialized move- | ||
| // only values, the cleanups are applied under the `mark_unresolved` | ||
| // instruction, so that the move checker properly accounts for the cleanups. | ||
| // #85063 | rdar://163194098 | ||
|
|
||
| // CHECK-LABEL: sil [ossa] @$test | ||
| sil [ossa] @$test : $@convention(thin) (@owned C) -> () { | ||
| entry(%c : @owned $C): | ||
| %s = alloc_stack [lexical] [var_decl] $S, var, name "butts", type $S | ||
| %s_marked = mark_uninitialized [rootself] %s | ||
| // CHECK: [[S_MOVEONLY:%.*]] = mark_unresolved_non_copyable_value | ||
| %s_moveonly = mark_unresolved_non_copyable_value [consumable_and_assignable] %s_marked | ||
| %s_access = begin_access [modify] [static] %s_moveonly | ||
| %c1 = struct_element_addr %s_access, #S.c1 | ||
| assign %c to %c1 | ||
| end_access %s_access | ||
| // CHECK: br [[NEXT:bb[0-9]+]] | ||
| br next | ||
|
|
||
| // CHECK: [[NEXT]]: | ||
| // CHECK: [[FIELD_TO_DESTROY:%.*]] = struct_element_addr [[S_MOVEONLY]], #S.c1 | ||
| // CHECK: destroy_addr [[FIELD_TO_DESTROY]] | ||
| next: | ||
| destroy_addr %s_moveonly | ||
| dealloc_stack %s | ||
| return undef : $() | ||
| } | ||
73 changes: 73 additions & 0 deletions
73
test/SILOptimizer/moveonly_partial_consumption_inlinable.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| // RUN: %target-swift-frontend -enable-library-evolution -emit-sil -verify %s | ||
|
|
||
| public struct Consumable: ~Copyable {} | ||
|
|
||
| public func consume(_: consuming Consumable) {} | ||
|
|
||
| public struct NotFrozen: ~Copyable { | ||
| public var field: Consumable | ||
|
|
||
| public consuming func notInlinable() { | ||
| consume(field) | ||
| } | ||
|
|
||
| @inlinable | ||
| public consuming func inlinable() { | ||
| // expected-error @+1 {{consumed but not reinitialized}} | ||
| consume(field) // expected-note{{}} | ||
| } | ||
|
|
||
| @_alwaysEmitIntoClient | ||
| public consuming func aeic() { | ||
| // expected-error @+1 {{consumed but not reinitialized}} | ||
| consume(field) // expected-note{{}} | ||
| } | ||
|
|
||
| @_transparent | ||
| public consuming func transparent() { | ||
| // expected-error @+1 {{consumed but not reinitialized}} | ||
| consume(field) // expected-note{{}} | ||
| } | ||
| } | ||
|
|
||
| @frozen | ||
| public struct Frozen: ~Copyable { | ||
| public var field: Consumable | ||
|
|
||
| public consuming func notInlinable() { | ||
| consume(field) | ||
| } | ||
|
|
||
| @inlinable | ||
| public consuming func inlinable() { | ||
| consume(field) | ||
| } | ||
| } | ||
|
|
||
| @usableFromInline | ||
| internal struct NotFrozenUFI: ~Copyable { | ||
| public var field: Consumable | ||
|
|
||
| public consuming func notInlinable() { | ||
| consume(field) | ||
| } | ||
|
|
||
| @inlinable | ||
| public consuming func inlinable() { | ||
| // expected-error @+1 {{consumed but not reinitialized}} | ||
| consume(field) // expected-note{{}} | ||
| } | ||
|
|
||
| @_alwaysEmitIntoClient | ||
| public consuming func aeic() { | ||
| // expected-error @+1 {{consumed but not reinitialized}} | ||
| consume(field) // expected-note{{}} | ||
| } | ||
|
|
||
| @_transparent | ||
| public consuming func transparent() { | ||
| // expected-error @+1 {{consumed but not reinitialized}} | ||
| consume(field) // expected-note{{}} | ||
| } | ||
| } | ||
|
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
did you look into why the verifier did not flag the double destroy before this change? is that a separate problem?
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.
I filed another bug to look into that.