Skip to content

Commit a98a653

Browse files
committed
Ensure that @safe(unchecked) works within variable initializers
1 parent f49c594 commit a98a653

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

lib/Sema/TypeCheckAvailability.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -950,6 +950,12 @@ class AvailabilityScopeBuilder : private ASTWalker {
950950
if (auto accessor = dyn_cast<AccessorDecl>(decl))
951951
return declHasSafeAttr(accessor->getStorage());
952952

953+
// Attributes for pattern binding declarations are on the first variable.
954+
if (auto pbd = dyn_cast<PatternBindingDecl>(decl)) {
955+
if (auto var = pbd->getAnchoringVarDecl(0))
956+
return declHasSafeAttr(var);
957+
}
958+
953959
return false;
954960
}
955961

test/Unsafe/safe.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ func rethrowing(body: (UnsafeType) throws -> Void) rethrows { } // expected-warn
3131
class HasStatics {
3232
// expected-note@+1{{make static method 'f' @unsafe to indicate that its use is not memory-safe}}{{3-3=@unsafe }}
3333
static internal func f(_: UnsafeType) { } // expected-warning{{reference to unsafe struct 'UnsafeType' [Unsafe]}}
34+
35+
3436
}
3537

3638
@unsafe
@@ -44,6 +46,14 @@ struct HasProperties {
4446
@unsafe var computedUnsafe: Int {
4547
unsafeInt()
4648
}
49+
50+
@safe(unchecked) static var blah: Int = {
51+
unsafeInt()
52+
}()
53+
54+
@unsafe static var blahUnsafe: Int = {
55+
unsafeInt()
56+
}()
4757
}
4858

4959
// Parsing issues

0 commit comments

Comments
 (0)