Skip to content

Commit 43eeb14

Browse files
committed
[ASTGen] Generate attributes on ParamDecls
1 parent 6980509 commit 43eeb14

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

lib/ASTGen/Sources/ASTGen/ParameterClause.swift

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import SwiftSyntax
1717
// MARK: - ParamDecl
1818

1919
fileprivate protocol ValueParameterSyntax: SyntaxProtocol {
20+
var optionalAttributes: AttributeListSyntax? { get }
2021
/// The `firstName` with optional type.
2122
///
2223
/// This is the lowest denominator between `FunctionParameterSyntax` and `EnumCaseParameterSyntax`.
@@ -37,6 +38,9 @@ fileprivate protocol ValueParameterSyntax: SyntaxProtocol {
3738
}
3839

3940
extension FunctionParameterSyntax: ValueParameterSyntax {
41+
fileprivate var optionalAttributes: AttributeListSyntax? {
42+
attributes
43+
}
4044
fileprivate var optionalFirstName: TokenSyntax? {
4145
firstName
4246
}
@@ -47,6 +51,10 @@ extension FunctionParameterSyntax: ValueParameterSyntax {
4751
}
4852

4953
extension EnumCaseParameterSyntax: ValueParameterSyntax {
54+
fileprivate var optionalAttributes: AttributeListSyntax? {
55+
nil
56+
}
57+
5058
fileprivate var optionalFirstName: TokenSyntax? {
5159
firstName
5260
}
@@ -61,6 +69,10 @@ extension EnumCaseParameterSyntax: ValueParameterSyntax {
6169
}
6270

6371
extension ClosureParameterSyntax: ValueParameterSyntax {
72+
fileprivate var optionalAttributes: AttributeListSyntax? {
73+
attributes
74+
}
75+
6476
fileprivate var optionalFirstName: TokenSyntax? {
6577
self.firstName
6678
}
@@ -98,6 +110,15 @@ extension ASTGenVisitor {
98110
/// Generate a ParamDecl. If `argNameByDefault` is true, then the parameter's
99111
/// argument label is inferred from the first name if no second name is present.
100112
private func makeParamDecl(_ node: some ValueParameterSyntax, argNameByDefault: Bool, at index: Int) -> BridgedParamDecl {
113+
var attrs = BridgedDeclAttributes()
114+
115+
// Attributes.
116+
if let attributes = node.optionalAttributes {
117+
self.generateDeclAttributes(attributeList: attributes) { attr in
118+
attrs.add(attr)
119+
}
120+
}
121+
101122
// FIXME: This location should be derived from the type repr.
102123
let specifierLoc: BridgedSourceLoc = nil
103124

@@ -147,7 +168,7 @@ extension ASTGenVisitor {
147168
}
148169

149170
// The decl context will be reset to the function later.
150-
return .createParsed(
171+
let param = BridgedParamDecl.createParsed(
151172
self.ctx,
152173
declContext: self.declContext,
153174
specifierLoc: specifierLoc,
@@ -159,6 +180,8 @@ extension ASTGenVisitor {
159180
defaultValue: initExpr.asNullable,
160181
defaultValueInitContext: initContext.asNullable
161182
)
183+
param.asDecl.attachParsedAttrs(attrs)
184+
return param
162185
}
163186

164187
func generate(closureShorthandParameter node : ClosureShorthandParameterSyntax) -> BridgedParamDecl {

test/ASTGen/decls.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,3 +325,14 @@ struct DefaultArgInitTestStruct {
325325
enum DefaultArgInitTestEnum {
326326
case foo(x: () -> () = {})
327327
}
328+
329+
@resultBuilder
330+
struct MyBuilder {
331+
static func buildBlock(_ items: Any...) -> [Any] { items }
332+
}
333+
func acceptBuilder(@MyBuilder fn: () -> [Any]) -> [Any] { fn() }
334+
func testBuilder() {
335+
let _: [Any] = acceptBuilder {
336+
1
337+
}
338+
}

0 commit comments

Comments
 (0)