mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
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.
This commit is contained in:
@@ -211,7 +211,23 @@ AvailabilityContext::forLocation(SourceLoc loc, const DeclContext *declContext,
|
||||
}
|
||||
|
||||
AvailabilityContext AvailabilityContext::forDeclSignature(const Decl *decl) {
|
||||
return forLocation(decl->getLoc(), decl->getInnermostDeclContext());
|
||||
// For decls with valid source locations, query the availability scope tree.
|
||||
auto loc = decl->getLoc();
|
||||
if (loc.isValid())
|
||||
return forLocation(loc, decl->getInnermostDeclContext());
|
||||
|
||||
// Otherwise, walk the decl hierarchy to compute availability. This can't be
|
||||
// delegated to `AvailabilityContext::forLocation()` since it walks up the
|
||||
// `DeclContext` hierachy for invalid source locations and that may skip
|
||||
// some declarations with availability attributes.
|
||||
auto &ctx = decl->getASTContext();
|
||||
auto availability = forInliningTarget(ctx);
|
||||
while (decl) {
|
||||
availability.constrainWithDecl(decl);
|
||||
decl = decl->parentDeclForAvailability();
|
||||
}
|
||||
|
||||
return availability;
|
||||
}
|
||||
|
||||
AvailabilityContext
|
||||
|
||||
@@ -47,6 +47,11 @@ struct TypeWithUnavailableMethods {
|
||||
func foo() -> Int {
|
||||
.random() ? 1 : 2
|
||||
}
|
||||
|
||||
@available(*, unavailable)
|
||||
var qux: Int {
|
||||
.random() ? 1 : 2
|
||||
}
|
||||
}
|
||||
|
||||
@available(*, unavailable)
|
||||
|
||||
Reference in New Issue
Block a user