Skip to content

Commit 238fd88

Browse files
committed
Use node choices instead of new node type
1 parent ed25015 commit 238fd88

32 files changed

+538
-398
lines changed

CodeGeneration/Sources/SyntaxSupport/ExperimentalFeatures.swift

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public enum ExperimentalFeature: String, CaseIterable {
1919
case nonescapableTypes
2020
case trailingComma
2121
case coroutineAccessors
22+
case valueGenerics
2223

2324
/// The name of the feature, which is used in the doc comment.
2425
public var featureName: String {
@@ -35,6 +36,8 @@ public enum ExperimentalFeature: String, CaseIterable {
3536
return "trailing comma"
3637
case .coroutineAccessors:
3738
return "CoroutineAccessors"
39+
case .valueGenerics:
40+
return "ValueGenerics"
3841
}
3942
}
4043

CodeGeneration/Sources/SyntaxSupport/GenericNodes.swift

+28-4
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,20 @@ public let GENERIC_NODES: [Node] = [
307307
Child(
308308
name: "leftType",
309309
deprecatedName: "leftTypeIdentifier",
310-
kind: .node(kind: .genericArgumentType),
311-
nameForDiagnostics: "left-hand type"
310+
kind: .nodeChoices(choices: [
311+
Child(
312+
name: "type",
313+
kind: .node(kind: .type)
314+
),
315+
Child(
316+
name: "expr",
317+
kind: .node(kind: .expr),
318+
experimentalFeature: .valueGenerics
319+
),
320+
]),
321+
nameForDiagnostics: "left-hand type",
322+
documentation:
323+
"The left hand side type for a same type requirement. This can either be a regular type argument or an expression for value generics."
312324
),
313325
Child(
314326
name: "equal",
@@ -318,8 +330,20 @@ public let GENERIC_NODES: [Node] = [
318330
Child(
319331
name: "rightType",
320332
deprecatedName: "rightTypeIdentifier",
321-
kind: .node(kind: .genericArgumentType),
322-
nameForDiagnostics: "right-hand type"
333+
kind: .nodeChoices(choices: [
334+
Child(
335+
name: "type",
336+
kind: .node(kind: .type)
337+
),
338+
Child(
339+
name: "expr",
340+
kind: .node(kind: .expr),
341+
experimentalFeature: .valueGenerics
342+
),
343+
]),
344+
nameForDiagnostics: "right-hand type",
345+
documentation:
346+
"The right hand side type for a same type requirement. This can either be a regular type argument or an expression for value generics."
323347
),
324348
]
325349
),

CodeGeneration/Sources/SyntaxSupport/SyntaxNodeKind.swift

-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@ public enum SyntaxNodeKind: String, CaseIterable, IdentifierConvertible, TypeCon
142142
case genericArgument
143143
case genericArgumentClause
144144
case genericArgumentList
145-
case genericArgumentType
146145
case genericParameter
147146
case genericParameterClause
148147
case genericParameterList

CodeGeneration/Sources/SyntaxSupport/TypeNodes.swift

+13-22
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,19 @@ public let TYPE_NODES: [Node] = [
238238
Child(
239239
name: "argument",
240240
deprecatedName: "argumentType",
241-
kind: .node(kind: .genericArgumentType)
241+
kind: .nodeChoices(choices: [
242+
Child(
243+
name: "type",
244+
kind: .node(kind: .type)
245+
),
246+
Child(
247+
name: "expr",
248+
kind: .node(kind: .expr),
249+
experimentalFeature: .valueGenerics
250+
),
251+
]),
252+
documentation:
253+
"The argument type for a generic argument. This can either be a regular type argument or an expression for value generics."
242254
),
243255
Child(
244256
name: "trailingComma",
@@ -604,25 +616,4 @@ public let TYPE_NODES: [Node] = [
604616
nameForDiagnostics: nil,
605617
elementChoices: [.simpleTypeSpecifier, .lifetimeTypeSpecifier]
606618
),
607-
608-
Node(
609-
kind: .genericArgumentType,
610-
base: .type,
611-
nameForDiagnostics: "generic argument type",
612-
children: [
613-
Child(
614-
name: "value",
615-
kind: .nodeChoices(choices: [
616-
Child(
617-
name: "type",
618-
kind: .node(kind: .type)
619-
),
620-
Child(
621-
name: "expr",
622-
kind: .node(kind: .expr)
623-
)
624-
])
625-
)
626-
]
627-
)
628619
]

Release Notes/601.md

+24
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@
22

33
## New APIs
44

5+
- `SameTypeRequirementSyntax` has a new `RightType` nested type.
6+
- Description: The Swift parser can now parse values as types in certain situations, so the new type reflects the possibility of the argument being either an `ExprSyntax` or a `TypeSyntax`.
7+
- Pull Request: https://github.com/swiftlang/swift-syntax/pull/2859
8+
9+
- `SameTypeRequirementSyntax` has a new `LeftType` nested type.
10+
- Description: The Swift parser can now parse values as types in certain situations, so the new type reflects the possibility of the argument being either an `ExprSyntax` or a `TypeSyntax`.
11+
- Pull Request: https://github.com/swiftlang/swift-syntax/pull/2859
12+
13+
- `GenericArgumentSynax` has a new `Argument` nested type.
14+
- Description: The Swift parser can now parse values as types in certain situations, so the new type reflects the possibility of the argument being either an `ExprSyntax` or a `TypeSyntax`.
15+
- Pull Request: https://github.com/swiftlang/swift-syntax/pull/2859
16+
517
- `GenericParameterSyntax` now has a new `specifier` property.
618
- Description: With the introduction of value generics, generic parameters can now be optionally preceded by either a `let` or an `each`. The `specifier` property captures the token representing which one was parsed.
719
- Pull Request: https://github.com/swiftlang/swift-syntax/pull/2785
@@ -46,6 +58,18 @@
4658

4759
## API-Incompatible Changes
4860

61+
- `SameTypeRequirementSyntax.rightType` has changed types from `TypeSyntax` to `SameTypeRequirementSyntax.RightType`
62+
- Description: The Swift parser can now parse values as types in certain situations, so the new type reflects the possibility of the argument being either an `ExprSyntax` or a `TypeSyntax`.
63+
- Pull Request: https://github.com/swiftlang/swift-syntax/pull/2859
64+
65+
- `SameTypeRequirementSyntax.leftType` has changed types from `TypeSyntax` to `SameTypeRequirementSyntax.LeftType`
66+
- Description: The Swift parser can now parse values as types in certain situations, so the new type reflects the possibility of the argument being either an `ExprSyntax` or a `TypeSyntax`.
67+
- Pull Request: https://github.com/swiftlang/swift-syntax/pull/2859
68+
69+
- `GenericArgumentSyntax.argument` has changed types from `TypeSyntax` to `GenericArgumentSyntax.Argument`
70+
- Description: The Swift parser can now parse values as types in certain situations, so the new type reflects the possibility of the argument being either an `ExprSyntax` or a `TypeSyntax`.
71+
- Pull Request: https://github.com/swiftlang/swift-syntax/pull/2859
72+
4973
- Moved `Radix` and `IntegerLiteralExprSyntax.radix` from `SwiftRefactor` to `SwiftSyntax`.
5074
- Description: Allows retrieving the radix value from the `literal.text`.
5175
- Issue: https://github.com/apple/swift-syntax/issues/405

Sources/SwiftParser/Declarations.swift

+25-12
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#if swift(>=6)
14-
@_spi(RawSyntax) internal import SwiftSyntax
14+
@_spi(RawSyntax) @_spi(ExperimentalLanguageFeatures) internal import SwiftSyntax
1515
#else
16-
@_spi(RawSyntax) import SwiftSyntax
16+
@_spi(RawSyntax) @_spi(ExperimentalLanguageFeatures) import SwiftSyntax
1717
#endif
1818

1919
extension DeclarationModifier {
@@ -524,6 +524,22 @@ extension Parser {
524524
return self.at(prefix: ">")
525525
}
526526

527+
mutating func parseSameTypeLeftType() -> RawSameTypeRequirementSyntax.LeftType {
528+
if let valueType = self.parseValueType() {
529+
return .expr(valueType)
530+
} else {
531+
return .type(self.parseType())
532+
}
533+
}
534+
535+
mutating func parseSameTypeRightType() -> RawSameTypeRequirementSyntax.RightType {
536+
if let valueType = self.parseValueType() {
537+
return .expr(valueType)
538+
} else {
539+
return .type(self.parseType())
540+
}
541+
}
542+
527543
mutating func parseGenericWhereClause() -> RawGenericWhereClauseSyntax {
528544
let (unexpectedBeforeWhereKeyword, whereKeyword) = self.expect(.keyword(.where))
529545

@@ -532,17 +548,17 @@ extension Parser {
532548
var keepGoing: RawTokenSyntax? = nil
533549
var loopProgress = LoopProgressCondition()
534550
repeat {
535-
let firstArgument = self.parseGenericArgumentType()
551+
let firstArgument = self.parseSameTypeLeftType()
536552

537-
guard !firstArgument.value.raw.is(RawMissingTypeSyntax.self) else {
553+
guard !firstArgument.raw.is(RawMissingTypeSyntax.self) else {
538554
keepGoing = self.consume(if: .comma)
539555
elements.append(
540556
RawGenericRequirementSyntax(
541557
requirement: .sameTypeRequirement(
542558
RawSameTypeRequirementSyntax(
543559
leftType: firstArgument,
544560
equal: missingToken(.binaryOperator, text: "=="),
545-
rightType: firstArgument,
561+
rightType: .type(RawTypeSyntax(RawMissingTypeSyntax(arena: self.arena))),
546562
arena: self.arena
547563
)
548564
),
@@ -555,7 +571,7 @@ extension Parser {
555571

556572
let requirement: RawGenericRequirementSyntax.Requirement
557573

558-
switch RawGenericArgumentTypeSyntax.Value(firstArgument.value.raw)! {
574+
switch firstArgument {
559575
// If the first argument is an expression, then we have to have a same
560576
// type requirement. We do not allow conformance requirements like
561577
// '123: Protocol' or layout constraints on expressions.
@@ -564,7 +580,7 @@ extension Parser {
564580
anyIn: SameTypeRequirementSyntax.EqualOptions.self,
565581
default: .binaryOperator
566582
)
567-
let secondArgument = self.parseGenericArgumentType()
583+
let secondArgument = self.parseSameTypeRightType()
568584
requirement = .sameTypeRequirement(
569585
RawSameTypeRequirementSyntax(
570586
leftType: firstArgument,
@@ -689,7 +705,7 @@ extension Parser {
689705
(.postfixOperator, let handle)?,
690706
(.prefixOperator, let handle)?:
691707
let equal = self.eat(handle)
692-
let secondArgument = self.parseGenericArgumentType()
708+
let secondArgument = self.parseSameTypeRightType()
693709
requirement = .sameTypeRequirement(
694710
RawSameTypeRequirementSyntax(
695711
leftType: firstArgument,
@@ -703,10 +719,7 @@ extension Parser {
703719
RawSameTypeRequirementSyntax(
704720
leftType: firstArgument,
705721
equal: RawTokenSyntax(missing: .binaryOperator, text: "==", arena: self.arena),
706-
rightType: RawGenericArgumentTypeSyntax(
707-
value: .type(RawTypeSyntax(RawMissingTypeSyntax(arena: self.arena))),
708-
arena: self.arena
709-
),
722+
rightType: .type(RawTypeSyntax(RawMissingTypeSyntax(arena: self.arena))),
710723
arena: self.arena
711724
)
712725
)

Sources/SwiftParser/Types.swift

+22-18
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ extension Parser {
418418
repeat {
419419
let argument = self.parseGenericArgumentType()
420420

421-
if arguments.isEmpty, argument.value.raw.is(RawMissingTypeSyntax.self) {
421+
if arguments.isEmpty, argument.raw.is(RawMissingTypeSyntax.self) {
422422
break
423423
}
424424

@@ -449,17 +449,11 @@ extension Parser {
449449
)
450450
}
451451

452-
mutating func parseGenericArgumentType() -> RawGenericArgumentTypeSyntax {
452+
mutating func parseGenericArgumentType() -> RawGenericArgumentSyntax.Argument {
453453
if let valueType = self.parseValueType() {
454-
return RawGenericArgumentTypeSyntax(
455-
value: .expr(valueType),
456-
arena: self.arena
457-
)
454+
return .expr(valueType)
458455
} else {
459-
return RawGenericArgumentTypeSyntax(
460-
value: .type(self.parseType()),
461-
arena: self.arena
462-
)
456+
return .type(self.parseType())
463457
}
464458
}
465459
}
@@ -710,7 +704,8 @@ extension Parser.Lookahead {
710704

711705
// '-123' for value generics.
712706
if self.currentToken.tokenText == "-",
713-
self.peek(isAt: .integerLiteral) {
707+
self.peek(isAt: .integerLiteral)
708+
{
714709
self.consumeAnyToken()
715710
self.consumeAnyToken()
716711
return true
@@ -1137,10 +1132,17 @@ extension Parser {
11371132

11381133
extension Parser {
11391134
mutating func parseValueType() -> RawExprSyntax? {
1135+
// If the 'ValueGenerics' experimental feature hasn't been added, then don't
1136+
// attempt to parse values as types.
1137+
guard self.experimentalFeatures.contains(.valueGenerics) else {
1138+
return nil
1139+
}
1140+
11401141
// Eat any '-' preceding integer literals.
11411142
var minusSign: RawTokenSyntax? = nil
1142-
if self.currentToken.tokenText == "-",
1143-
self.peek(isAt: .integerLiteral) {
1143+
if self.atContextualPunctuator("-"),
1144+
self.peek(isAt: .integerLiteral)
1145+
{
11441146
minusSign = self.consumeIfContextualPunctuator("-", remapping: .prefixOperator)
11451147
}
11461148

@@ -1156,11 +1158,13 @@ extension Parser {
11561158
return RawExprSyntax(integerExpr)
11571159
}
11581160

1159-
return RawExprSyntax(RawPrefixOperatorExprSyntax(
1160-
operator: minusSign,
1161-
expression: integerExpr,
1162-
arena: self.arena
1163-
))
1161+
return RawExprSyntax(
1162+
RawPrefixOperatorExprSyntax(
1163+
operator: minusSign,
1164+
expression: integerExpr,
1165+
arena: self.arena
1166+
)
1167+
)
11641168
}
11651169

11661170
return nil

Sources/SwiftParser/generated/ExperimentalFeatures.swift

+3
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,7 @@ extension Parser.ExperimentalFeatures {
4141

4242
/// Whether to enable the parsing of CoroutineAccessors.
4343
public static let coroutineAccessors = Self (rawValue: 1 << 5)
44+
45+
/// Whether to enable the parsing of ValueGenerics.
46+
public static let valueGenerics = Self (rawValue: 1 << 6)
4447
}

Sources/SwiftParserDiagnostics/generated/SyntaxKindNameForDiagnostics.swift

-2
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,6 @@ extension SyntaxKind {
197197
return "generic argument clause"
198198
case .genericArgument:
199199
return "generic argument"
200-
case .genericArgumentType:
201-
return "generic argument type"
202200
case .genericParameterClause:
203201
return "generic parameter clause"
204202
case .genericParameter:

Sources/SwiftSyntax/Documentation.docc/generated/SwiftSyntax.md

-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,6 @@ allows Swift tools to parse, inspect, generate, and transform Swift source code.
180180
- <doc:SwiftSyntax/CompositionTypeSyntax>
181181
- <doc:SwiftSyntax/DictionaryTypeSyntax>
182182
- <doc:SwiftSyntax/FunctionTypeSyntax>
183-
- <doc:SwiftSyntax/GenericArgumentTypeSyntax>
184183
- <doc:SwiftSyntax/IdentifierTypeSyntax>
185184
- <doc:SwiftSyntax/ImplicitlyUnwrappedOptionalTypeSyntax>
186185
- <doc:SwiftSyntax/MemberTypeSyntax>

Sources/SwiftSyntax/generated/ChildNameForKeyPath.swift

-6
Original file line numberDiff line numberDiff line change
@@ -1529,12 +1529,6 @@ public func childName(_ keyPath: AnyKeyPath) -> String? {
15291529
return "trailingComma"
15301530
case \GenericArgumentSyntax.unexpectedAfterTrailingComma:
15311531
return "unexpectedAfterTrailingComma"
1532-
case \GenericArgumentTypeSyntax.unexpectedBeforeValue:
1533-
return "unexpectedBeforeValue"
1534-
case \GenericArgumentTypeSyntax.value:
1535-
return "value"
1536-
case \GenericArgumentTypeSyntax.unexpectedAfterValue:
1537-
return "unexpectedAfterValue"
15381532
case \GenericParameterClauseSyntax.unexpectedBeforeLeftAngle:
15391533
return "unexpectedBeforeLeftAngle"
15401534
case \GenericParameterClauseSyntax.leftAngle:

0 commit comments

Comments
 (0)