[Strict memory safety] Union member accessors are always unsafe

Union member accessors have no way to know what the "active field" is,
so consider them to always be unsafe.
This commit is contained in:
Doug Gregor
2025-04-02 16:55:52 -07:00
parent 14147bf87d
commit c522138987
2 changed files with 9 additions and 1 deletions

View File

@@ -2392,6 +2392,9 @@ namespace {
// field type.
synthesizer.makeUnionFieldAccessors(result, member);
// Union accessors are always unsafe.
member->getAttrs().add(new (Impl.SwiftContext) UnsafeAttr(/*Implicit=*/true));
// Create labeled initializers for unions that take one of the
// fields, which only initializes the data for that field.
auto valueCtor =

View File

@@ -19,4 +19,9 @@ struct StoreAllTheThings {
let ln: ListNode // expected-note{{property 'ln' involves unsafe type 'ListNode'}}
let sc: SomeColor
};
}
func readFromUnion(npu: NoPointersUnion) {
// expected-warning@+1{{expression uses unsafe constructs but is not marked with 'unsafe'}}
_ = npu.x // expected-note{{reference to unsafe property 'x'}}
}