Skip to content

Commit 38459d4

Browse files
authored
Merge pull request #2859 from Azoy/integer-types
Parse integer generics
2 parents 8ed7645 + 864b1a0 commit 38459d4

24 files changed

+987
-190
lines changed

CodeGeneration/Sources/SyntaxSupport/ExperimentalFeatures.swift

Lines changed: 3 additions & 0 deletions
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

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -334,17 +334,41 @@ public let GENERIC_NODES: [Node] = [
334334
children: [
335335
Child(
336336
name: "leftType",
337-
kind: .node(kind: .type),
338-
nameForDiagnostics: "left-hand type"
337+
kind: .nodeChoices(choices: [
338+
Child(
339+
name: "type",
340+
kind: .node(kind: .type)
341+
),
342+
Child(
343+
name: "expr",
344+
kind: .node(kind: .expr),
345+
experimentalFeature: .valueGenerics
346+
),
347+
]),
348+
nameForDiagnostics: "left-hand type",
349+
documentation:
350+
"The left hand side type for a same type requirement. This can either be a regular type argument or an expression for value generics."
339351
),
340352
Child(
341353
name: "equal",
342354
kind: .token(choices: [.token(.binaryOperator), .token(.prefixOperator), .token(.postfixOperator)])
343355
),
344356
Child(
345357
name: "rightType",
346-
kind: .node(kind: .type),
347-
nameForDiagnostics: "right-hand type"
358+
kind: .nodeChoices(choices: [
359+
Child(
360+
name: "type",
361+
kind: .node(kind: .type)
362+
),
363+
Child(
364+
name: "expr",
365+
kind: .node(kind: .expr),
366+
experimentalFeature: .valueGenerics
367+
),
368+
]),
369+
nameForDiagnostics: "right-hand type",
370+
documentation:
371+
"The right hand side type for a same type requirement. This can either be a regular type argument or an expression for value generics."
348372
),
349373
],
350374
childHistory: [

CodeGeneration/Sources/SyntaxSupport/TypeNodes.swift

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,19 @@ public let TYPE_NODES: [Node] = [
257257
children: [
258258
Child(
259259
name: "argument",
260-
kind: .node(kind: .type)
260+
kind: .nodeChoices(choices: [
261+
Child(
262+
name: "type",
263+
kind: .node(kind: .type)
264+
),
265+
Child(
266+
name: "expr",
267+
kind: .node(kind: .expr),
268+
experimentalFeature: .valueGenerics
269+
),
270+
]),
271+
documentation:
272+
"The argument type for a generic argument. This can either be a regular type argument or an expression for value generics."
261273
),
262274
Child(
263275
name: "trailingComma",

Examples/Sources/MacroExamples/Implementation/ComplexMacros/OptionSetMacro.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public struct OptionSetMacro {
8181
attachedTo decl: some DeclGroupSyntax,
8282
in context: some MacroExpansionContext,
8383
emitDiagnostics: Bool
84-
) -> (StructDeclSyntax, EnumDeclSyntax, TypeSyntax)? {
84+
) -> (StructDeclSyntax, EnumDeclSyntax, GenericArgumentSyntax.Argument)? {
8585
// Determine the name of the options enum.
8686
let optionsEnumName: String
8787
if case let .argumentList(arguments) = attribute.arguments,

Examples/Sources/MacroExamples/Implementation/Peer/AddAsyncMacro.swift

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
import SwiftSyntax
13+
@_spi(ExperimentalLanguageFeatures) import SwiftSyntax
1414
import SwiftSyntaxMacros
1515

1616
extension SyntaxCollection {
@@ -71,12 +71,24 @@ public struct AddAsyncMacro: PeerMacro {
7171
let returnType = completionHandlerParameter.parameters.first?.type
7272

7373
let isResultReturn = returnType?.children(viewMode: .all).first?.description == "Result"
74-
let successReturnType =
75-
if isResultReturn {
76-
returnType!.as(IdentifierTypeSyntax.self)!.genericArgumentClause?.arguments.first!.argument
77-
} else {
78-
returnType
74+
let successReturnType: TypeSyntax?
75+
76+
if isResultReturn {
77+
let argument = returnType!.as(IdentifierTypeSyntax.self)!.genericArgumentClause?.arguments.first!.argument
78+
79+
switch argument {
80+
case .some(.type(let type)):
81+
successReturnType = type
82+
83+
case .some(.expr(_)):
84+
throw CustomError.message("Found unexpected value generic in Result type")
85+
86+
case .none:
87+
successReturnType = nil
7988
}
89+
} else {
90+
successReturnType = returnType
91+
}
8092

8193
// Remove completionHandler and comma from the previous parameter
8294
var newParameterList = funcDecl.signature.parameterClause.parameters

Release Notes/601.md

Lines changed: 24 additions & 0 deletions
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
@@ -54,6 +66,18 @@
5466

5567
## API-Incompatible Changes
5668

69+
- `SameTypeRequirementSyntax.rightType` has changed types from `TypeSyntax` to `SameTypeRequirementSyntax.RightType`
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+
73+
- `SameTypeRequirementSyntax.leftType` has changed types from `TypeSyntax` to `SameTypeRequirementSyntax.LeftType`
74+
- 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`.
75+
- Pull Request: https://github.com/swiftlang/swift-syntax/pull/2859
76+
77+
- `GenericArgumentSyntax.argument` has changed types from `TypeSyntax` to `GenericArgumentSyntax.Argument`
78+
- 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`.
79+
- Pull Request: https://github.com/swiftlang/swift-syntax/pull/2859
80+
5781
- Moved `Radix` and `IntegerLiteralExprSyntax.radix` from `SwiftRefactor` to `SwiftSyntax`.
5882
- Description: Allows retrieving the radix value from the `literal.text`.
5983
- Issue: https://github.com/apple/swift-syntax/issues/405

0 commit comments

Comments
 (0)