Skip to content

Commit 5ab33b4

Browse files
authored
Merge pull request #67836 from ahoppen/ahoppen/no-optional-collections
[ASTGen/Macros] Update for the fact that syntax collections are always non-optional in SwiftSyntax now
2 parents 72bcb75 + 8231b27 commit 5ab33b4

File tree

6 files changed

+33
-53
lines changed

6 files changed

+33
-53
lines changed

lib/ASTGen/Sources/ASTGen/Types.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,9 @@ extension ASTGenVisitor {
190190
}
191191

192192
// Handle type attributes.
193-
if let attributes = node.attributes {
193+
if !node.attributes.isEmpty {
194194
let typeAttributes = TypeAttributes_create()
195-
for attributeElt in attributes {
195+
for attributeElt in node.attributes {
196196
// FIXME: Ignoring #ifs entirely. We want to provide a filtered view,
197197
// but we don't have that ability right now.
198198
guard case let .attribute(attribute) = attributeElt else {

lib/Macros/Sources/ObservationMacros/Availability.swift

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,3 @@ extension AttributeListSyntax {
108108
return AttributeListSyntax(elements)
109109
}
110110
}
111-
112-
extension DeclGroupSyntax {
113-
var availability: AttributeListSyntax? {
114-
if let attributes {
115-
return attributes.availability
116-
} else {
117-
return nil
118-
}
119-
}
120-
}

lib/Macros/Sources/ObservationMacros/Extensions.swift

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,10 @@ extension VariableDeclSyntax {
2121
}
2222

2323
var isInstance: Bool {
24-
if let modifiers {
25-
for modifier in modifiers {
26-
for token in modifier.tokens(viewMode: .all) {
27-
if token.tokenKind == .keyword(.static) || token.tokenKind == .keyword(.class) {
28-
return false
29-
}
24+
for modifier in modifiers {
25+
for token in modifier.tokens(viewMode: .all) {
26+
if token.tokenKind == .keyword(.static) || token.tokenKind == .keyword(.class) {
27+
return false
3028
}
3129
}
3230
}
@@ -103,7 +101,6 @@ extension VariableDeclSyntax {
103101
}
104102

105103
func hasMacroApplication(_ name: String) -> Bool {
106-
guard let attributes else { return false }
107104
for attribute in attributes {
108105
switch attribute {
109106
case .attribute(let attr):
@@ -179,12 +176,10 @@ extension TypeSyntax {
179176

180177
extension FunctionDeclSyntax {
181178
var isInstance: Bool {
182-
if let modifiers {
183-
for modifier in modifiers {
184-
for token in modifier.tokens(viewMode: .all) {
185-
if token.tokenKind == .keyword(.static) || token.tokenKind == .keyword(.class) {
186-
return false
187-
}
179+
for modifier in modifiers {
180+
for token in modifier.tokens(viewMode: .all) {
181+
if token.tokenKind == .keyword(.static) || token.tokenKind == .keyword(.class) {
182+
return false
188183
}
189184
}
190185
}

lib/Macros/Sources/ObservationMacros/ObservableMacro.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,12 +173,11 @@ extension PatternBindingListSyntax {
173173

174174
extension VariableDeclSyntax {
175175
func privatePrefixed(_ prefix: String, addingAttribute attribute: AttributeSyntax) -> VariableDeclSyntax {
176-
let newAttributes = AttributeListSyntax(
177-
(attributes.map(Array.init) ?? []) + [.attribute(attribute)])
176+
let newAttributes = attributes + [.attribute(attribute)]
178177
return VariableDeclSyntax(
179178
leadingTrivia: leadingTrivia,
180179
attributes: newAttributes,
181-
modifiers: modifiers?.privatePrefixed(prefix) ?? DeclModifierListSyntax(keyword: .private),
180+
modifiers: modifiers.privatePrefixed(prefix),
182181
bindingSpecifier: TokenSyntax(bindingSpecifier.tokenKind, leadingTrivia: .space, trailingTrivia: .space, presence: .present),
183182
bindings: bindings.privatePrefixed(prefix),
184183
trailingTrivia: trailingTrivia
@@ -276,7 +275,7 @@ extension ObservableMacro: ExtensionMacro {
276275
"""
277276
let ext = decl.cast(ExtensionDeclSyntax.self)
278277

279-
if let availability = declaration.availability {
278+
if let availability = declaration.attributes.availability {
280279
return [ext.with(\.attributes, availability)]
281280
} else {
282281
return [ext]

lib/Macros/Sources/SwiftMacros/OptionSetMacro.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ extension OptionSetMacro: MemberMacro {
168168
}
169169

170170
// Dig out the access control keyword we need.
171-
let access = decl.modifiers?.first(where: \.isNeededAccessLevelModifier)
171+
let access = decl.modifiers.first(where: \.isNeededAccessLevelModifier)
172172

173173
let staticVars = caseElements.map { (element) -> DeclSyntax in
174174
"""

test/Macros/Inputs/syntax_macro_definitions.swift

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -900,18 +900,16 @@ public struct AddCompletionHandler: PeerMacro {
900900
"""
901901

902902
// Drop the @addCompletionHandler attribute from the new declaration.
903-
let newAttributeList = AttributeListSyntax(
904-
funcDecl.attributes?.filter {
905-
guard case let .attribute(attribute) = $0,
906-
let attributeType = attribute.attributeName.as(IdentifierTypeSyntax.self),
907-
let nodeType = node.attributeName.as(IdentifierTypeSyntax.self)
908-
else {
909-
return true
910-
}
903+
let newAttributeList = funcDecl.attributes.filter {
904+
guard case let .attribute(attribute) = $0,
905+
let attributeType = attribute.attributeName.as(IdentifierTypeSyntax.self),
906+
let nodeType = node.attributeName.as(IdentifierTypeSyntax.self)
907+
else {
908+
return true
909+
}
911910

912-
return attributeType.name.text != nodeType.name.text
913-
} ?? []
914-
)
911+
return attributeType.name.text != nodeType.name.text
912+
}
915913

916914
var newFunc = funcDecl
917915
newFunc.signature.effectSpecifiers?.asyncSpecifier = nil // drop async
@@ -1010,18 +1008,16 @@ public struct WrapInType: PeerMacro {
10101008
"""
10111009

10121010
// Drop the peer macro attribute from the new declaration.
1013-
let newAttributeList = AttributeListSyntax(
1014-
funcDecl.attributes?.filter {
1015-
guard case let .attribute(attribute) = $0,
1016-
let attributeType = attribute.attributeName.as(IdentifierTypeSyntax.self),
1017-
let nodeType = node.attributeName.as(IdentifierTypeSyntax.self)
1018-
else {
1019-
return true
1020-
}
1011+
let newAttributeList = funcDecl.attributes.filter {
1012+
guard case let .attribute(attribute) = $0,
1013+
let attributeType = attribute.attributeName.as(IdentifierTypeSyntax.self),
1014+
let nodeType = node.attributeName.as(IdentifierTypeSyntax.self)
1015+
else {
1016+
return true
1017+
}
10211018

1022-
return attributeType.name.text != nodeType.name.text
1023-
} ?? []
1024-
)
1019+
return attributeType.name.text != nodeType.name.text
1020+
}
10251021

10261022
var method = funcDecl
10271023
method.name = "\(context.makeUniqueName(funcDecl.name.text))"
@@ -1251,7 +1247,7 @@ public struct NewTypeMacro: MemberMacro {
12511247
throw CustomError.message("@NewType can only be applied to a struct declarations.")
12521248
}
12531249

1254-
let access = declaration.modifiers?.first(where: \.isNeededAccessLevelModifier)
1250+
let access = declaration.modifiers.first(where: \.isNeededAccessLevelModifier)
12551251

12561252
return [
12571253
"\(access)typealias RawValue = \(rawType)",

0 commit comments

Comments
 (0)