Skip to content
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

Parse integer generics #2859

Merged
merged 10 commits into from
Nov 7, 2024
Original file line number Diff line number Diff line change
@@ -19,6 +19,7 @@ public enum ExperimentalFeature: String, CaseIterable {
case nonescapableTypes
case trailingComma
case coroutineAccessors
case valueGenerics

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

32 changes: 28 additions & 4 deletions CodeGeneration/Sources/SyntaxSupport/GenericNodes.swift
Original file line number Diff line number Diff line change
@@ -334,17 +334,41 @@ public let GENERIC_NODES: [Node] = [
children: [
Child(
name: "leftType",
kind: .node(kind: .type),
nameForDiagnostics: "left-hand type"
kind: .nodeChoices(choices: [
Child(
name: "type",
kind: .node(kind: .type)
),
Child(
name: "expr",
kind: .node(kind: .expr),
experimentalFeature: .valueGenerics
),
]),
nameForDiagnostics: "left-hand type",
documentation:
"The left hand side type for a same type requirement. This can either be a regular type argument or an expression for value generics."
),
Child(
name: "equal",
kind: .token(choices: [.token(.binaryOperator), .token(.prefixOperator), .token(.postfixOperator)])
),
Child(
name: "rightType",
kind: .node(kind: .type),
nameForDiagnostics: "right-hand type"
kind: .nodeChoices(choices: [
Child(
name: "type",
kind: .node(kind: .type)
),
Child(
name: "expr",
kind: .node(kind: .expr),
experimentalFeature: .valueGenerics
),
]),
nameForDiagnostics: "right-hand type",
documentation:
"The right hand side type for a same type requirement. This can either be a regular type argument or an expression for value generics."
),
],
childHistory: [
14 changes: 13 additions & 1 deletion CodeGeneration/Sources/SyntaxSupport/TypeNodes.swift
Original file line number Diff line number Diff line change
@@ -257,7 +257,19 @@ public let TYPE_NODES: [Node] = [
children: [
Child(
name: "argument",
kind: .node(kind: .type)
kind: .nodeChoices(choices: [
Child(
name: "type",
kind: .node(kind: .type)
),
Child(
name: "expr",
kind: .node(kind: .expr),
experimentalFeature: .valueGenerics
),
]),
documentation:
"The argument type for a generic argument. This can either be a regular type argument or an expression for value generics."
),
Child(
name: "trailingComma",
Original file line number Diff line number Diff line change
@@ -81,7 +81,7 @@ public struct OptionSetMacro {
attachedTo decl: some DeclGroupSyntax,
in context: some MacroExpansionContext,
emitDiagnostics: Bool
) -> (StructDeclSyntax, EnumDeclSyntax, TypeSyntax)? {
) -> (StructDeclSyntax, EnumDeclSyntax, GenericArgumentSyntax.Argument)? {
// Determine the name of the options enum.
let optionsEnumName: String
if case let .argumentList(arguments) = attribute.arguments,
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@
//
//===----------------------------------------------------------------------===//

import SwiftSyntax
@_spi(ExperimentalLanguageFeatures) import SwiftSyntax
import SwiftSyntaxMacros

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

let isResultReturn = returnType?.children(viewMode: .all).first?.description == "Result"
let successReturnType =
if isResultReturn {
returnType!.as(IdentifierTypeSyntax.self)!.genericArgumentClause?.arguments.first!.argument
} else {
returnType
let successReturnType: TypeSyntax?

if isResultReturn {
let argument = returnType!.as(IdentifierTypeSyntax.self)!.genericArgumentClause?.arguments.first!.argument

switch argument {
case .some(.type(let type)):
successReturnType = type

case .some(.expr(_)):
throw CustomError.message("Found unexpected value generic in Result type")

case .none:
successReturnType = nil
}
} else {
successReturnType = returnType
}

// Remove completionHandler and comma from the previous parameter
var newParameterList = funcDecl.signature.parameterClause.parameters
24 changes: 24 additions & 0 deletions Release Notes/601.md
Original file line number Diff line number Diff line change
@@ -2,6 +2,18 @@

## New APIs

- `SameTypeRequirementSyntax` has a new `RightType` nested type.
- 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`.
- Pull Request: https://github.com/swiftlang/swift-syntax/pull/2859

- `SameTypeRequirementSyntax` has a new `LeftType` nested type.
- 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`.
- Pull Request: https://github.com/swiftlang/swift-syntax/pull/2859

- `GenericArgumentSynax` has a new `Argument` nested type.
- 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`.
- Pull Request: https://github.com/swiftlang/swift-syntax/pull/2859

- `GenericParameterSyntax` now has a new `specifier` property.
- 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.
- Pull Request: https://github.com/swiftlang/swift-syntax/pull/2785
@@ -54,6 +66,18 @@

## API-Incompatible Changes

- `SameTypeRequirementSyntax.rightType` has changed types from `TypeSyntax` to `SameTypeRequirementSyntax.RightType`
- 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`.
- Pull Request: https://github.com/swiftlang/swift-syntax/pull/2859

- `SameTypeRequirementSyntax.leftType` has changed types from `TypeSyntax` to `SameTypeRequirementSyntax.LeftType`
- 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`.
- Pull Request: https://github.com/swiftlang/swift-syntax/pull/2859

- `GenericArgumentSyntax.argument` has changed types from `TypeSyntax` to `GenericArgumentSyntax.Argument`
- 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`.
- Pull Request: https://github.com/swiftlang/swift-syntax/pull/2859

- Moved `Radix` and `IntegerLiteralExprSyntax.radix` from `SwiftRefactor` to `SwiftSyntax`.
- Description: Allows retrieving the radix value from the `literal.text`.
- Issue: https://github.com/apple/swift-syntax/issues/405
Loading