Skip to content

Commit 8da356a

Browse files
committed
[ASTGen] Implement bridging for using declaration
(cherry picked from commit 4b5105d)
1 parent 38ce1c2 commit 8da356a

File tree

6 files changed

+78
-0
lines changed

6 files changed

+78
-0
lines changed

include/swift/AST/ASTBridging.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1714,6 +1714,19 @@ BridgedImportDecl BridgedImportDecl_createParsed(
17141714
BridgedSourceLoc cImportKeywordLoc, BridgedImportKind cImportKind,
17151715
BridgedSourceLoc cImportKindLoc, BridgedArrayRef cImportPathElements);
17161716

1717+
enum ENUM_EXTENSIBILITY_ATTR(open) BridgedUsingSpecifier {
1718+
BridgedUsingSpecifierMainActor,
1719+
BridgedUsingSpecifierNonisolated,
1720+
};
1721+
1722+
SWIFT_NAME("BridgedUsingDecl.createParsed(_:declContext:usingKeywordLoc:"
1723+
"specifierLoc:specifier:)")
1724+
BridgedUsingDecl BridgedUsingDecl_createParsed(BridgedASTContext cContext,
1725+
BridgedDeclContext cDeclContext,
1726+
BridgedSourceLoc usingKeywordLoc,
1727+
BridgedSourceLoc specifierLoc,
1728+
BridgedUsingSpecifier specifier);
1729+
17171730
SWIFT_NAME("BridgedSubscriptDecl.createParsed(_:declContext:staticLoc:"
17181731
"staticSpelling:subscriptKeywordLoc:genericParamList:parameterList:"
17191732
"arrowLoc:returnType:genericWhereClause:)")

lib/AST/Bridging/DeclBridging.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,17 @@ BridgedImportDecl BridgedImportDecl_createParsed(
619619
std::move(builder).get());
620620
}
621621

622+
BridgedUsingDecl BridgedUsingDecl_createParsed(BridgedASTContext cContext,
623+
BridgedDeclContext cDeclContext,
624+
BridgedSourceLoc usingKeywordLoc,
625+
BridgedSourceLoc specifierLoc,
626+
BridgedUsingSpecifier specifier) {
627+
ASTContext &ctx = cContext.unbridged();
628+
return UsingDecl::create(
629+
ctx, usingKeywordLoc.unbridged(), specifierLoc.unbridged(),
630+
static_cast<UsingSpecifier>(specifier), cDeclContext.unbridged());
631+
}
632+
622633
BridgedSubscriptDecl BridgedSubscriptDecl_createParsed(
623634
BridgedASTContext cContext, BridgedDeclContext cDeclContext,
624635
BridgedSourceLoc cStaticLoc, BridgedStaticSpelling cStaticSpelling,

lib/ASTGen/Sources/ASTGen/Decls.swift

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ extension ASTGenVisitor {
6969
return self.generate(typeAliasDecl: node)?.asDecl
7070
case .variableDecl(let node):
7171
return self.generate(variableDecl: node)
72+
case .usingDecl(let node):
73+
return self.generate(usingDecl: node)?.asDecl
7274
}
7375
}
7476

@@ -1083,6 +1085,37 @@ extension ASTGenVisitor {
10831085
}
10841086
}
10851087

1088+
extension ASTGenVisitor {
1089+
func generate(usingDecl node: UsingDeclSyntax) -> BridgedUsingDecl? {
1090+
var specifier: BridgedUsingSpecifier? = nil
1091+
1092+
switch node.specifier {
1093+
case .attribute(let attr):
1094+
if let identifier = attr.attributeName.as(IdentifierTypeSyntax.self),
1095+
identifier.name.tokenKind == .identifier("MainActor") {
1096+
specifier = .mainActor
1097+
}
1098+
case .modifier(let modifier):
1099+
if case .identifier("nonisolated") = modifier.tokenKind {
1100+
specifier = .nonisolated
1101+
}
1102+
}
1103+
1104+
guard let specifier else {
1105+
self.diagnose(.invalidDefaultIsolationSpecifier(node.specifier))
1106+
return nil
1107+
}
1108+
1109+
return BridgedUsingDecl.createParsed(
1110+
self.ctx,
1111+
declContext: self.declContext,
1112+
usingKeywordLoc: self.generateSourceLoc(node.usingKeyword),
1113+
specifierLoc: self.generateSourceLoc(node.specifier),
1114+
specifier: specifier
1115+
)
1116+
}
1117+
}
1118+
10861119
extension ASTGenVisitor {
10871120
func generate(memberBlockItem node: MemberBlockItemSyntax) -> BridgedDecl? {
10881121
if let node = node.decl.as(MacroExpansionDeclSyntax.self) {

lib/ASTGen/Sources/ASTGen/Diagnostics.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,13 @@ extension ASTGenDiagnostic {
146146
message: "expressions are not allowed at the top level"
147147
)
148148
}
149+
150+
static func invalidDefaultIsolationSpecifier(_ specifier: some SyntaxProtocol) -> Self {
151+
Self(
152+
node: specifier,
153+
message: "default isolation can only be set to '@MainActor' or 'nonisolated'"
154+
)
155+
}
149156
}
150157

151158
/// DeclAttributes diagnostics

test/ASTGen/decls.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ import protocol Swift.Sequence
3131
import func Swift.max
3232
import var Swift._playgroundPrintHook
3333

34+
35+
using @MainActor
36+
// FIXME: cannot add `using nonisolated` here because it's a re-declaration
37+
3438
func
3539
test1
3640
(

test/ASTGen/diagnostics.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
// REQUIRES: swift_swift_parser
66
// REQUIRES: swift_feature_ParserASTGen
7+
78
// rdar://116686158
89
// UNSUPPORTED: asan
910

@@ -66,3 +67,12 @@ func misisngPatternTest(arr: [Int]) {
6667
for {} // expected-error {{expected pattern, 'in', and expression in 'for' statement}}
6768
// expected-note@-1 {{insert pattern, 'in', and expression}} {{7-7=<#pattern#> }} {{7-7=in }} {{7-7=<#expression#> }}
6869
}
70+
71+
using @MainActor // expected-note {{default isolation was previously declared here}}
72+
using nonisolated // expected-error {{invalid redeclaration of file-level default actor isolation}}
73+
74+
using @Test
75+
// expected-error@-1 {{default isolation can only be set to '@MainActor' or 'nonisolated'}}
76+
77+
using test
78+
// expected-error@-1 {{default isolation can only be set to '@MainActor' or 'nonisolated'}}

0 commit comments

Comments
 (0)