Skip to content

Commit 3b33ead

Browse files
committed
Fix silly crasher and missed "in definition" checks for strict safety
1 parent bcf77c4 commit 3b33ead

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
lines changed

lib/Sema/TypeCheckAvailability.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1684,8 +1684,10 @@ class InnermostAncestorFinder : private ASTWalker {
16841684
// is contained in the range we are looking for.
16851685
FoundTarget = SM.rangeContains(TargetRange, Range);
16861686

1687-
if (FoundTarget)
1687+
if (FoundTarget) {
1688+
walkToNodePost(Node);
16881689
return Action::SkipNode(Node);
1690+
}
16891691

16901692
// Search the subtree if the target range is inside its range.
16911693
if (!SM.rangeContains(Range, TargetRange))

lib/Sema/TypeCheckUnsafe.cpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,13 @@ static void suggestUnsafeMarkerOnConformance(
8888
}
8989

9090
/// Retrieve the extra information
91-
static SourceFileExtras &getSourceFileExtrasFor(const Decl *decl) {
91+
static SourceFileExtras *getSourceFileExtrasFor(const Decl *decl) {
9292
auto dc = decl->getDeclContext();
93-
return dc->getOutermostParentSourceFile()->getExtras();
93+
auto sf = dc->getOutermostParentSourceFile();
94+
if (!sf)
95+
return nullptr;
96+
97+
return &sf->getExtras();
9498
}
9599

96100
void swift::diagnoseUnsafeUse(const UnsafeUse &use, bool asNote) {
@@ -105,8 +109,9 @@ void swift::diagnoseUnsafeUse(const UnsafeUse &use, bool asNote) {
105109
auto [enclosingDecl, _] = enclosingContextForUnsafe(
106110
use.getLocation(), use.getDeclContext());
107111
if (enclosingDecl) {
108-
getSourceFileExtrasFor(enclosingDecl).unsafeUses[enclosingDecl]
109-
.push_back(use);
112+
if (auto extras = getSourceFileExtrasFor(enclosingDecl)) {
113+
extras->unsafeUses[enclosingDecl].push_back(use);
114+
}
110115
return;
111116
}
112117
}
@@ -232,14 +237,17 @@ void swift::diagnoseUnsafeUse(const UnsafeUse &use, bool asNote) {
232237
}
233238

234239
void swift::diagnoseUnsafeUsesIn(const Decl *decl) {
235-
auto &extras = getSourceFileExtrasFor(decl);
236-
auto known = extras.unsafeUses.find(decl);
237-
if (known == extras.unsafeUses.end())
240+
auto *extras = getSourceFileExtrasFor(decl);
241+
if (!extras)
242+
return;
243+
244+
auto known = extras->unsafeUses.find(decl);
245+
if (known == extras->unsafeUses.end())
238246
return;
239247

240248
// Take the unsafe uses.
241249
auto unsafeUses = std::move(known->second);
242-
extras.unsafeUses.erase(known);
250+
extras->unsafeUses.erase(known);
243251
if (unsafeUses.empty())
244252
return;
245253

test/Unsafe/unsafe.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ struct SuperHolder {
6161
return s2 // expected-note{{reference to unowned(unsafe) property 's2' is unsafe}}
6262
}
6363

64-
// FIXME: We should be able to identify this as being inside the function
65-
// expected-warning@+1{{instance method 'getSuper2b' involves unsafe code; use '@unsafe' to indicate that its use is not memory-safe}}
64+
// expected-warning@+1{{instance method 'getSuper2b' involves unsafe code; use '@safe(unchecked)' to assert that the code is memory-safe}}
6665
func getSuper2b() -> Super {
6766
s2 // expected-note{{reference to unowned(unsafe) property 's2' is unsafe}}
6867
}

0 commit comments

Comments
 (0)