Skip to content

[ASTGen] Handle reasync and rethrows #79441

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 68 additions & 11 deletions lib/ASTGen/Sources/ASTGen/Decls.swift
Original file line number Diff line number Diff line change
Expand Up @@ -645,11 +645,47 @@ extension ASTGenVisitor {
// MARK: - AbstractFunctionDecl

extension ASTGenVisitor {
struct GeneratedFunctionSignature {
var parameterList: BridgedParameterList
var asyncLoc: BridgedSourceLoc
var isReasync: Bool
var throwsLoc: BridgedSourceLoc
var isRethrows: Bool
var thrownType: BridgedTypeRepr?
var returnType: BridgedTypeRepr?
}

func generate(
functionSignature node: FunctionSignatureSyntax,
for context: ParameterContext
) -> GeneratedFunctionSignature {
let parameterList = self.generate(functionParameterClause: node.parameterClause, for: context)
let asyncLoc = self.generateSourceLoc(node.effectSpecifiers?.asyncSpecifier)
let isReasync = node.effectSpecifiers?.asyncSpecifier?.rawText == "reasync"
let throwsLoc = self.generateSourceLoc(node.effectSpecifiers?.throwsClause?.throwsSpecifier)
let isRethrows = node.effectSpecifiers?.throwsClause?.throwsSpecifier.rawText == "rethrows"
let thrownType = (node.effectSpecifiers?.thrownError).map(self.generate(type:))
let returnType = (node.returnClause?.type).map(self.generate(type:))
return GeneratedFunctionSignature(
parameterList: parameterList,
asyncLoc: asyncLoc,
isReasync: isReasync,
throwsLoc: throwsLoc,
isRethrows: isRethrows,
thrownType: thrownType,
returnType: returnType
)
}

func generate(functionDecl node: FunctionDeclSyntax) -> BridgedFuncDecl? {
let attrs = self.generateDeclAttributes(node, allowStatic: true)
var attrs = self.generateDeclAttributes(node, allowStatic: true)
guard let (name, nameLoc) = self.generateIdentifierDeclNameAndLoc(node.name) else {
return nil
}
let signature = self.generate(
functionSignature: node.signature,
for: name.isOperator ? .operator : .function
)

let decl = BridgedFuncDecl.createParsed(
self.ctx,
Expand All @@ -660,13 +696,19 @@ extension ASTGenVisitor {
name: name,
nameLoc: nameLoc,
genericParamList: self.generate(genericParameterClause: node.genericParameterClause),
parameterList: self.generate(functionParameterClause: node.signature.parameterClause, for: name.isOperator ? .operator : .function),
asyncSpecifierLoc: self.generateSourceLoc(node.signature.effectSpecifiers?.asyncSpecifier),
throwsSpecifierLoc: self.generateSourceLoc(node.signature.effectSpecifiers?.throwsClause?.throwsSpecifier),
thrownType: self.generate(type: node.signature.effectSpecifiers?.thrownError),
returnType: self.generate(type: node.signature.returnClause?.type),
parameterList: signature.parameterList,
asyncSpecifierLoc: signature.asyncLoc,
throwsSpecifierLoc: signature.throwsLoc,
thrownType: signature.thrownType.asNullable,
returnType: signature.returnType.asNullable,
genericWhereClause: self.generate(genericWhereClause: node.genericWhereClause)
)
if signature.isReasync {
attrs.attributes.add(BridgedDeclAttribute.createSimple(self.ctx, kind: .reasync, atLoc: nil, nameLoc: signature.asyncLoc))
}
if signature.isRethrows {
attrs.attributes.add(BridgedDeclAttribute.createSimple(self.ctx, kind: .rethrows, atLoc: nil, nameLoc: signature.throwsLoc))
}
decl.asDecl.attachParsedAttrs(attrs.attributes)

if let body = node.body {
Expand All @@ -679,7 +721,11 @@ extension ASTGenVisitor {
}

func generate(initializerDecl node: InitializerDeclSyntax) -> BridgedConstructorDecl {
let attrs = self.generateDeclAttributes(node, allowStatic: false)
var attrs = self.generateDeclAttributes(node, allowStatic: false)
let signature = self.generate(
functionSignature: node.signature,
for: .initializer
)

let decl = BridgedConstructorDecl.createParsed(
self.ctx,
Expand All @@ -688,13 +734,24 @@ extension ASTGenVisitor {
failabilityMarkLoc: self.generateSourceLoc(node.optionalMark),
isIUO: node.optionalMark?.rawTokenKind == .exclamationMark,
genericParamList: self.generate(genericParameterClause: node.genericParameterClause),
parameterList: self.generate(functionParameterClause: node.signature.parameterClause, for: .initializer),
asyncSpecifierLoc: self.generateSourceLoc(node.signature.effectSpecifiers?.asyncSpecifier),
throwsSpecifierLoc: self.generateSourceLoc(node.signature.effectSpecifiers?.throwsClause?.throwsSpecifier),
thrownType: self.generate(type: node.signature.effectSpecifiers?.thrownError),
parameterList: signature.parameterList,
asyncSpecifierLoc: signature.asyncLoc,
throwsSpecifierLoc: signature.throwsLoc,
thrownType: signature.thrownType.asNullable,
genericWhereClause: self.generate(genericWhereClause: node.genericWhereClause)
)
if signature.isReasync {
attrs.attributes.add(BridgedDeclAttribute.createSimple(self.ctx, kind: .reasync, atLoc: nil, nameLoc: signature.asyncLoc))
}
if signature.isRethrows {
attrs.attributes.add(BridgedDeclAttribute.createSimple(self.ctx, kind: .rethrows, atLoc: nil, nameLoc: signature.throwsLoc))
}
decl.asDecl.attachParsedAttrs(attrs.attributes)

guard signature.returnType == nil else {
// TODO: Diagnose.
fatalError("unexpected return type in initializer decl")
}

if let body = node.body {
self.withDeclContext(decl.asDeclContext) {
Expand Down
15 changes: 12 additions & 3 deletions test/ASTGen/decls.swift
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@

// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend-dump-parse -disable-availability-checking -enable-experimental-move-only -enable-experimental-feature ValueGenerics -enable-experimental-feature ParserASTGen \
// RUN: %target-swift-frontend-dump-parse -disable-availability-checking -enable-experimental-move-only -enable-experimental-concurrency -enable-experimental-feature ValueGenerics -enable-experimental-feature ParserASTGen \
// RUN: | %sanitize-address > %t/astgen.ast
// RUN: %target-swift-frontend-dump-parse -disable-availability-checking -enable-experimental-move-only -enable-experimental-feature ValueGenerics \
// RUN: %target-swift-frontend-dump-parse -disable-availability-checking -enable-experimental-move-only -enable-experimental-concurrency -enable-experimental-feature ValueGenerics \
// RUN: | %sanitize-address > %t/cpp-parser.ast

// RUN: %diff -u %t/astgen.ast %t/cpp-parser.ast

// RUN: %target-run-simple-swift(-Xfrontend -disable-availability-checking -enable-experimental-feature ParserASTGen -enable-experimental-feature ValueGenerics)
// RUN: %target-run-simple-swift(-Xfrontend -disable-availability-checking -Xfrontend -enable-experimental-concurrency -enable-experimental-feature ValueGenerics -enable-experimental-feature ParserASTGen)

// REQUIRES: executable_test
// REQUIRES: swift_swift_parser
Expand Down Expand Up @@ -105,6 +105,15 @@ func testVars() {
}
}

func rethrowingFn(fn: () throws -> Void) rethrows {}
func reasyncFn(fn: () async -> Void) reasync {}
func testRethrows() {
rethrowingFn { _ = 1 }

// FIXME: Assertion failed: (isAsync()), function getAsyncContext, file GenCall.cpp, line 215.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This FIXME applies to non-ASTGen as well.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// reasyncFn { _ = 1 }
}

struct TestVars {
var a = 0
var b: Int = 0
Expand Down