Skip to content

Commit d7a0f08

Browse files
authored
Merge pull request #72828 from tshortli/isolation-macro-availability-6.0
[6.0] AST: Fix availability checking for macros as default arguments
2 parents 2bc6cdc + ac5909a commit d7a0f08

File tree

6 files changed

+64
-12
lines changed

6 files changed

+64
-12
lines changed

include/swift/AST/SourceFile.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,11 @@ class SourceFile final : public FileUnit {
624624
/// enclosed in.
625625
SourceFile *getEnclosingSourceFile() const;
626626

627+
/// If this file has an enclosing source file (because it is the result of
628+
/// expanding a macro or default argument), returns the node in the enclosing
629+
/// file that this file's contents were expanded from.
630+
ASTNode getNodeInEnclosingSourceFile() const;
631+
627632
/// If this buffer corresponds to a file on disk, returns the path.
628633
/// Otherwise, return an empty string.
629634
StringRef getFilename() const;

include/swift/AST/TypeRefinementContext.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,8 @@ class TypeRefinementContext : public ASTAllocated<TypeRefinementContext> {
193193
public:
194194

195195
/// Create the root refinement context for the given SourceFile.
196-
static TypeRefinementContext *createRoot(SourceFile *SF,
197-
const AvailabilityContext &Info);
196+
static TypeRefinementContext *
197+
createForSourceFile(SourceFile *SF, const AvailabilityContext &Info);
198198

199199
/// Create a refinement context for the given declaration.
200200
static TypeRefinementContext *createForDecl(ASTContext &Ctx, Decl *D,

lib/AST/Module.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,9 +1179,7 @@ ASTNode SourceFile::getMacroExpansion() const {
11791179
if (Kind != SourceFileKind::MacroExpansion)
11801180
return nullptr;
11811181

1182-
auto genInfo =
1183-
*getASTContext().SourceMgr.getGeneratedSourceInfo(*getBufferID());
1184-
return ASTNode::getFromOpaqueValue(genInfo.astNode);
1182+
return getNodeInEnclosingSourceFile();
11851183
}
11861184

11871185
SourceRange SourceFile::getMacroInsertionRange() const {
@@ -1233,6 +1231,16 @@ SourceFile *SourceFile::getEnclosingSourceFile() const {
12331231
return getParentModule()->getSourceFileContainingLocation(sourceLoc);
12341232
}
12351233

1234+
ASTNode SourceFile::getNodeInEnclosingSourceFile() const {
1235+
if (Kind != SourceFileKind::MacroExpansion &&
1236+
Kind != SourceFileKind::DefaultArgument)
1237+
return nullptr;
1238+
1239+
auto genInfo =
1240+
*getASTContext().SourceMgr.getGeneratedSourceInfo(*getBufferID());
1241+
return ASTNode::getFromOpaqueValue(genInfo.astNode);
1242+
}
1243+
12361244
void ModuleDecl::lookupClassMember(ImportPath::Access accessPath,
12371245
DeclName name,
12381246
SmallVectorImpl<ValueDecl*> &results) const {

lib/AST/TypeRefinementContext.cpp

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,24 +42,38 @@ TypeRefinementContext::TypeRefinementContext(ASTContext &Ctx, IntroNode Node,
4242
}
4343

4444
TypeRefinementContext *
45-
TypeRefinementContext::createRoot(SourceFile *SF,
46-
const AvailabilityContext &Info) {
45+
TypeRefinementContext::createForSourceFile(SourceFile *SF,
46+
const AvailabilityContext &Info) {
4747
assert(SF);
4848

4949
ASTContext &Ctx = SF->getASTContext();
5050

5151
SourceRange range;
5252
TypeRefinementContext *parentContext = nullptr;
5353
AvailabilityContext availabilityContext = Info;
54-
if (auto parentExpansion = SF->getMacroExpansion()) {
54+
switch (SF->Kind) {
55+
case SourceFileKind::MacroExpansion:
56+
case SourceFileKind::DefaultArgument: {
57+
// Look up the parent context in the enclosing file that this file's
58+
// root context should be nested under.
5559
if (auto parentTRC =
5660
SF->getEnclosingSourceFile()->getTypeRefinementContext()) {
5761
auto charRange = Ctx.SourceMgr.getRangeForBuffer(*SF->getBufferID());
5862
range = SourceRange(charRange.getStart(), charRange.getEnd());
59-
parentContext = parentTRC->findMostRefinedSubContext(
60-
parentExpansion.getStartLoc(), Ctx);
61-
availabilityContext = parentContext->getAvailabilityInfo();
63+
auto originalNode = SF->getNodeInEnclosingSourceFile();
64+
parentContext =
65+
parentTRC->findMostRefinedSubContext(originalNode.getStartLoc(), Ctx);
66+
if (parentContext)
67+
availabilityContext = parentContext->getAvailabilityInfo();
6268
}
69+
break;
70+
}
71+
case SourceFileKind::Library:
72+
case SourceFileKind::Main:
73+
case SourceFileKind::Interface:
74+
break;
75+
case SourceFileKind::SIL:
76+
llvm_unreachable("unexpected SourceFileKind");
6377
}
6478

6579
return new (Ctx)

lib/Sema/TypeCheckAvailability.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1279,7 +1279,7 @@ void TypeChecker::buildTypeRefinementContextHierarchy(SourceFile &SF) {
12791279
// the source file are guaranteed to be executing on at least the minimum
12801280
// platform version for inlining.
12811281
auto MinPlatformReq = AvailabilityContext::forInliningTarget(Context);
1282-
RootTRC = TypeRefinementContext::createRoot(&SF, MinPlatformReq);
1282+
RootTRC = TypeRefinementContext::createForSourceFile(&SF, MinPlatformReq);
12831283
SF.setTypeRefinementContext(RootTRC);
12841284
}
12851285

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// RUN: %target-typecheck-verify-swift
2+
3+
// REQUIRES: concurrency
4+
// REQUIRES: swift_swift_parser
5+
// REQUIRES: VENDOR=apple
6+
7+
@available(SwiftStdlib 5.1, *)
8+
func isolatedFunc(isolation: isolated (any Actor)? = #isolation) {}
9+
10+
func test() { // expected-note 3 {{add @available attribute to enclosing global function}}
11+
_ = #isolation // expected-error {{'isolation()' is only available in}} expected-note {{add 'if #available' version check}}
12+
isolatedFunc() // expected-error {{'isolatedFunc(isolation:)' is only available in}} expected-note {{add 'if #available' version check}}
13+
// expected-error@-1 {{'isolation()' is only available in}}
14+
15+
if #available(SwiftStdlib 5.1, *) {
16+
_ = #isolation
17+
isolatedFunc()
18+
}
19+
}
20+
21+
@available(SwiftStdlib 5.1, *)
22+
func testAvailable5_1() {
23+
_ = #isolation
24+
isolatedFunc()
25+
}

0 commit comments

Comments
 (0)