Skip to content

Commit 030a3da

Browse files
committed
AST: Fix AvailabilityContext::forDeclSignature().
It had a bug that would cause it to return incorrect results for some decls with invalid source locations. Since not all declarations introduce a new `DeclContext`, it's not sufficient to walk the decl context hiearchy when computing availability for a decl.
1 parent 3bacfa7 commit 030a3da

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

lib/AST/AvailabilityContext.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,23 @@ AvailabilityContext::forLocation(SourceLoc loc, const DeclContext *declContext,
211211
}
212212

213213
AvailabilityContext AvailabilityContext::forDeclSignature(const Decl *decl) {
214-
return forLocation(decl->getLoc(), decl->getInnermostDeclContext());
214+
// For decls with valid source locations, query the availability scope tree.
215+
auto loc = decl->getLoc();
216+
if (loc.isValid())
217+
return forLocation(loc, decl->getInnermostDeclContext());
218+
219+
// Otherwise, walk the decl hierarchy to compute availability. This can't be
220+
// delegated to `AvailabilityContext::forLocation()` since it walks up the
221+
// `DeclContext` hierachy for invalid source locations and that may skip
222+
// some declarations with availability attributes.
223+
auto &ctx = decl->getASTContext();
224+
auto availability = forInliningTarget(ctx);
225+
while (decl) {
226+
availability.constrainWithDecl(decl);
227+
decl = decl->parentDeclForAvailability();
228+
}
229+
230+
return availability;
215231
}
216232

217233
AvailabilityContext

test/Profiler/unmapped.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ struct TypeWithUnavailableMethods {
4747
func foo() -> Int {
4848
.random() ? 1 : 2
4949
}
50+
51+
@available(*, unavailable)
52+
var qux: Int {
53+
.random() ? 1 : 2
54+
}
5055
}
5156

5257
@available(*, unavailable)

0 commit comments

Comments
 (0)