Skip to content

Commit 7418b25

Browse files
authored
Merge pull request #80480 from DougGregor/imported-union-member-unsafe
[Strict memory safety] Union member accessors are always unsafe
2 parents f3a7694 + c522138 commit 7418b25

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

lib/ClangImporter/ImportDecl.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2392,6 +2392,9 @@ namespace {
23922392
// field type.
23932393
synthesizer.makeUnionFieldAccessors(result, member);
23942394

2395+
// Union accessors are always unsafe.
2396+
member->getAttrs().add(new (Impl.SwiftContext) UnsafeAttr(/*Implicit=*/true));
2397+
23952398
// Create labeled initializers for unions that take one of the
23962399
// fields, which only initializes the data for that field.
23972400
auto valueCtor =

test/Unsafe/unsafe_c_imports.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,9 @@ struct StoreAllTheThings {
1919
let ln: ListNode // expected-note{{property 'ln' involves unsafe type 'ListNode'}}
2020

2121
let sc: SomeColor
22-
};
22+
}
23+
24+
func readFromUnion(npu: NoPointersUnion) {
25+
// expected-warning@+1{{expression uses unsafe constructs but is not marked with 'unsafe'}}
26+
_ = npu.x // expected-note{{reference to unsafe property 'x'}}
27+
}

0 commit comments

Comments
 (0)