Skip to content

Parse @unsafe nonisolated conformance #3112

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
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
9 changes: 5 additions & 4 deletions CodeGeneration/Sources/SyntaxSupport/Child.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ public enum ChildKind {
kind: SyntaxNodeKind,
collectionElementName: String? = nil,
defaultsToEmpty: Bool = false,
deprecatedCollectionElementName: String? = nil
deprecatedCollectionElementName: String? = nil,
generateDeprecatedAddFunction: Bool = true
)
/// The child is a token that matches one of the given `choices`.
/// If `requiresLeadingSpace` or `requiresTrailingSpace` is not `nil`, it
Expand Down Expand Up @@ -132,7 +133,7 @@ public class Child: NodeChoiceConvertible {
return kind
case .nodeChoices:
return .syntax
case .collection(kind: let kind, _, _, _):
case .collection(kind: let kind, _, _, _, _):
return kind
case .token:
return .token
Expand Down Expand Up @@ -268,7 +269,7 @@ public class Child: NodeChoiceConvertible {
/// Whether this child has syntax kind `UnexpectedNodes`.
public var isUnexpectedNodes: Bool {
switch kind {
case .collection(kind: .unexpectedNodes, _, _, _):
case .collection(kind: .unexpectedNodes, _, _, _, _):
return true
default:
return false
Expand All @@ -283,7 +284,7 @@ public class Child: NodeChoiceConvertible {
return choices.isEmpty
case .node(let kind):
return kind.isBase
case .collection(kind: let kind, _, _, _):
case .collection(kind: let kind, _, _, _, _):
return kind.isBase
case .token:
return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ struct GrammarGenerator {
case .nodeChoices(let choices, _):
let choicesDescriptions = choices.map { grammar(for: $0) }
return "(\(choicesDescriptions.joined(separator: " | ")))\(optionality)"
case .collection(kind: let kind, _, _, _):
case .collection(kind: let kind, _, _, _, _):
return "\(kind.doccLink)\(optionality)"
case .token(let choices, _, _):
if choices.count == 1 {
Expand Down
2 changes: 1 addition & 1 deletion CodeGeneration/Sources/SyntaxSupport/Node.swift
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ fileprivate extension Child {
return [kind]
case .nodeChoices(let choices, _):
return choices.flatMap(\.kinds)
case .collection(kind: let kind, _, _, _):
case .collection(kind: let kind, _, _, _, _):
return [kind]
case .token:
return [.token]
Expand Down
11 changes: 11 additions & 0 deletions CodeGeneration/Sources/SyntaxSupport/TypeNodes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@ public let TYPE_NODES: [Node] = [
kind: .collection(kind: .attributeList, collectionElementName: "Attribute", defaultsToEmpty: true),
documentation: "A list of attributes that can be attached to the type, such as `@escaping`."
),
Child(
name: "lateSpecifiers",
kind: .collection(
kind: .typeSpecifierList,
collectionElementName: "Specifier",
defaultsToEmpty: true,
generateDeprecatedAddFunction: false
),
documentation:
"A list of specifiers that can be attached to the type after the attributes, such as 'nonisolated'."
),
Child(
name: "baseType",
kind: .node(kind: .type),
Expand Down
4 changes: 2 additions & 2 deletions CodeGeneration/Sources/Utils/SyntaxBuildableChild.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ extension Child {
buildableKind = .node(kind: kind)
case .nodeChoices:
buildableKind = .node(kind: .syntax)
case .collection(kind: let kind, _, _, _):
case .collection(kind: let kind, _, _, _, _):
buildableKind = .node(kind: kind)
case .token:
buildableKind = .token(self.tokenKind!)
Expand Down Expand Up @@ -65,7 +65,7 @@ extension Child {
return ExprSyntax("nil")
}
}
if case .collection(_, _, defaultsToEmpty: true, _) = kind {
if case .collection(_, _, defaultsToEmpty: true, _, _) = kind {
return ExprSyntax("[]")
}
guard let token = token, isToken else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ func makeCompatibilityAddMethod(for child: Child) -> DeclSyntax? {
kind: _,
collectionElementName: let collectionElementName?,
defaultsToEmpty: _,
deprecatedCollectionElementName: let deprecatedCollectionElementName?
deprecatedCollectionElementName: let deprecatedCollectionElementName?,
generateDeprecatedAddFunction: _
) = child.kind
{
let childEltType = childNode.collectionElementType.syntaxBaseName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,8 @@ func syntaxNode(nodesStartingWith: [Character]) -> SourceFileSyntax {
// If needed, this could be added in the future, but for now withUnexpected should be sufficient.
if let childNode = SYNTAX_NODE_MAP[child.syntaxNodeKind]?.collectionNode,
!child.isUnexpectedNodes,
case .collection(_, collectionElementName: let childElt?, _, _) = child.kind
case .collection(_, collectionElementName: let childElt?, _, _, generateDeprecatedAddFunction: true) = child
.kind
Comment on lines +179 to +180
Copy link
Member

Choose a reason for hiding this comment

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

Interesting formatting choice by swift-format here. swift-format seems to be happy with the following as well, which I’d prefer

Suggested change
case .collection(_, collectionElementName: let childElt?, _, _, generateDeprecatedAddFunction: true) = child
.kind
case .collection(_, collectionElementName: let childElt?, _, _, generateDeprecatedAddFunction: true) =
child.kind

Copy link
Member Author

Choose a reason for hiding this comment

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

Formatting fix in #3114 because I too eagerly pushed "merge"

{
let childEltType = childNode.collectionElementType.syntaxBaseName

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ fileprivate extension ChildKind {
return kind == otherKind
case (.nodeChoices(let choices, _), .nodeChoices(let otherChoices, _)):
return choices.count == otherChoices.count && zip(choices, otherChoices).allSatisfy { $0.hasSameType(as: $1) }
case (.collection(kind: let kind, _, _, _), .collection(kind: let otherKind, _, _, _)):
case (.collection(kind: let kind, _, _, _, _), .collection(kind: let otherKind, _, _, _, _)):
return kind == otherKind
case (.token(let choices, _, _), .token(let otherChoices, _, _)):
return choices == otherChoices
case (.node(let kind), .collection(kind: let otherKind, _, _, _)):
case (.node(let kind), .collection(kind: let otherKind, _, _, _, _)):
return kind == otherKind
case (.collection(kind: let kind, _, _, _), .node(let otherKind)):
case (.collection(kind: let kind, _, _, _, _), .node(let otherKind)):
return kind == otherKind
default:
return false
Expand Down
24 changes: 21 additions & 3 deletions Sources/SwiftParser/Types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ extension Parser {
RawAttributedTypeSyntax(
specifiers: specifiersAndAttributes.specifiers,
attributes: specifiersAndAttributes.attributes,
lateSpecifiers: specifiersAndAttributes.lateSpecifiers,
baseType: base,
arena: self.arena
)
Expand Down Expand Up @@ -1221,7 +1222,8 @@ extension Parser {
misplacedSpecifiers: [RawTokenSyntax] = []
) -> (
specifiers: RawTypeSpecifierListSyntax,
attributes: RawAttributeListSyntax
attributes: RawAttributeListSyntax,
lateSpecifiers: RawTypeSpecifierListSyntax
)? {
var specifiers: [RawTypeSpecifierListSyntax.Element] = []
SPECIFIER_PARSING: while canHaveParameterSpecifier {
Expand Down Expand Up @@ -1260,7 +1262,15 @@ extension Parser {
attributes = nil
}

guard !specifiers.isEmpty || attributes != nil else {
// Only handle `nonisolated` as a late specifier.
var lateSpecifiers: [RawTypeSpecifierListSyntax.Element] = []
if self.at(.keyword(.nonisolated)) && !(self.peek(isAt: .leftParen) && self.peek().isAtStartOfLine)
&& canHaveParameterSpecifier
{
lateSpecifiers.append(parseNonisolatedTypeSpecifier())
}

guard !specifiers.isEmpty || attributes != nil || !lateSpecifiers.isEmpty else {
// No specifiers or attributes on this type
return nil
}
Expand All @@ -1271,9 +1281,17 @@ extension Parser {
specifierList = RawTypeSpecifierListSyntax(elements: specifiers, arena: arena)
}

let lateSpecifierList: RawTypeSpecifierListSyntax
if lateSpecifiers.isEmpty {
lateSpecifierList = self.emptyCollection(RawTypeSpecifierListSyntax.self)
} else {
lateSpecifierList = RawTypeSpecifierListSyntax(elements: lateSpecifiers, arena: arena)
}

return (
specifierList,
attributes ?? self.emptyCollection(RawAttributeListSyntax.self)
attributes ?? self.emptyCollection(RawAttributeListSyntax.self),
lateSpecifierList
)
}

Expand Down
8 changes: 6 additions & 2 deletions Sources/SwiftSyntax/generated/ChildNameForKeyPath.swift

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Sources/SwiftSyntax/generated/SyntaxCollections.swift

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 20 additions & 8 deletions Sources/SwiftSyntax/generated/raw/RawSyntaxNodesAB.swift

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions Sources/SwiftSyntax/generated/raw/RawSyntaxValidation.swift

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 38 additions & 9 deletions Sources/SwiftSyntax/generated/syntaxNodes/SyntaxNodesAB.swift

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading