Skip to content

Commit 36ebbef

Browse files
authored
Merge pull request #1888 from ahoppen/ahoppen/naming-inconsistencies
Fix child name validation failures found by `testConsistentNamingOfChildren`
2 parents 6864c93 + 687bad2 commit 36ebbef

40 files changed

+1111
-613
lines changed

CodeGeneration/Sources/SyntaxSupport/AttributeNodes.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,8 @@ public let ATTRIBUTE_NODES: [Node] = [
302302
nameForDiagnostics: "declaration name",
303303
children: [
304304
Child(
305-
name: "DeclBaseName",
305+
name: "BaseName",
306+
deprecatedName: "DeclBaseName",
306307
kind: .token(choices: [
307308
.token(tokenKind: "IdentifierToken"),
308309
.token(tokenKind: "BinaryOperatorToken"),
@@ -314,7 +315,8 @@ public let ATTRIBUTE_NODES: [Node] = [
314315
documentation: "The base name of the protocol's requirement."
315316
),
316317
Child(
317-
name: "DeclNameArguments",
318+
name: "Arguments",
319+
deprecatedName: "DeclNameArguments",
318320
kind: .node(kind: .declNameArguments),
319321
nameForDiagnostics: "arguments",
320322
documentation: "The argument labels of the protocol's requirement if it is a function requirement.",
@@ -505,7 +507,8 @@ public let ATTRIBUTE_NODES: [Node] = [
505507
isOptional: true
506508
),
507509
Child(
508-
name: "WhereClause",
510+
name: "GenericWhereClause",
511+
deprecatedName: "WhereClause",
509512
kind: .node(kind: .genericWhereClause),
510513
isOptional: true
511514
),

CodeGeneration/Sources/SyntaxSupport/DeclNodes.swift

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,7 +1087,8 @@ public let DECL_NODES: [Node] = [
10871087
nameForDiagnostics: "function signature",
10881088
children: [
10891089
Child(
1090-
name: "Input",
1090+
name: "ParameterClause",
1091+
deprecatedName: "Input",
10911092
kind: .node(kind: .parameterClause)
10921093
),
10931094
Child(
@@ -1096,7 +1097,8 @@ public let DECL_NODES: [Node] = [
10961097
isOptional: true
10971098
),
10981099
Child(
1099-
name: "Output",
1100+
name: "ReturnClause",
1101+
deprecatedName: "Output",
11001102
kind: .node(kind: .returnClause),
11011103
isOptional: true
11021104
),
@@ -1449,7 +1451,8 @@ public let DECL_NODES: [Node] = [
14491451
kind: .token(choices: [.token(tokenKind: "IdentifierToken")])
14501452
),
14511453
Child(
1452-
name: "GenericArguments",
1454+
name: "GenericArgumentClause",
1455+
deprecatedName: "GenericArguments",
14531456
kind: .node(kind: .genericArgumentClause),
14541457
isOptional: true
14551458
),
@@ -2214,11 +2217,13 @@ public let DECL_NODES: [Node] = [
22142217
isOptional: true
22152218
),
22162219
Child(
2217-
name: "Indices",
2220+
name: "ParameterClause",
2221+
deprecatedName: "Indices",
22182222
kind: .node(kind: .parameterClause)
22192223
),
22202224
Child(
2221-
name: "Result",
2225+
name: "ReturnClause",
2226+
deprecatedName: "Result",
22222227
kind: .node(kind: .returnClause)
22232228
),
22242229
Child(

CodeGeneration/Sources/SyntaxSupport/ExprNodes.swift

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,8 @@ public let EXPR_NODES: [Node] = [
240240
kind: .token(choices: [.token(tokenKind: "ColonToken")])
241241
),
242242
Child(
243-
name: "VersionTuple",
243+
name: "Version",
244+
deprecatedName: "VersionTuple",
244245
kind: .node(kind: .versionTuple)
245246
),
246247
]
@@ -536,14 +537,16 @@ public let EXPR_NODES: [Node] = [
536537
isOptional: true
537538
),
538539
Child(
539-
name: "Input",
540+
name: "ParameterClause",
541+
deprecatedName: "Input",
540542
kind: .nodeChoices(choices: [
541543
Child(
542544
name: "SimpleInput",
543545
kind: .node(kind: .closureParamList)
544546
),
545547
Child(
546-
name: "Input",
548+
name: "ParameterClause",
549+
deprecatedName: "Input",
547550
kind: .node(kind: .closureParameterClause)
548551
),
549552
]),
@@ -555,7 +558,8 @@ public let EXPR_NODES: [Node] = [
555558
isOptional: true
556559
),
557560
Child(
558-
name: "Output",
561+
name: "ReturnClause",
562+
deprecatedName: "Output",
559563
kind: .node(kind: .returnClause),
560564
isOptional: true
561565
),
@@ -1154,7 +1158,8 @@ public let EXPR_NODES: [Node] = [
11541158
kind: .token(choices: [.token(tokenKind: "IdentifierToken")])
11551159
),
11561160
Child(
1157-
name: "GenericArguments",
1161+
name: "GenericArgumentClause",
1162+
deprecatedName: "GenericArguments",
11581163
kind: .node(kind: .genericArgumentClause),
11591164
isOptional: true
11601165
),

CodeGeneration/Sources/SyntaxSupport/Traits.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public let TRAITS: [Trait] = [
5959
children: [
6060
Child(name: "PoundToken", kind: .token(choices: [.token(tokenKind: "PoundToken")])),
6161
Child(name: "Macro", kind: .token(choices: [.token(tokenKind: "IdentifierToken")])),
62-
Child(name: "GenericArguments", kind: .node(kind: .genericArgumentClause), isOptional: true),
62+
Child(name: "GenericArgumentClause", kind: .node(kind: .genericArgumentClause), isOptional: true),
6363
Child(name: "LeftParen", kind: .token(choices: [.token(tokenKind: "LeftParenToken")]), isOptional: true),
6464
Child(name: "ArgumentList", kind: .node(kind: .tupleExprElementList)),
6565
Child(name: "RightParen", kind: .token(choices: [.token(tokenKind: "RightParenToken")]), isOptional: true),

CodeGeneration/Sources/SyntaxSupport/TypeNodes.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,8 @@ public let TYPE_NODES: [Node] = [
202202
isOptional: true
203203
),
204204
Child(
205-
name: "Output",
205+
name: "ReturnClause",
206+
deprecatedName: "Output",
206207
kind: .node(kind: .returnClause)
207208
),
208209
]

CodeGeneration/Sources/generate-swiftsyntax/templates/swiftsyntax/SyntaxNodesFile.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func syntaxNode(emitKind: SyntaxNodeKind) -> SourceFileSyntax {
9191
}
9292

9393
let closureSignature = ClosureSignatureSyntax(
94-
input: .input(
94+
parameterClause: .parameterClause(
9595
ClosureParameterClauseSyntax(
9696
parameterList: ClosureParameterListSyntax {
9797
ClosureParameterSyntax(firstName: .identifier("arena"))

CodeGeneration/Sources/generate-swiftsyntax/templates/swiftsyntaxbuilder/RenamedChildrenBuilderCompatibilityFile.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ let renamedChildrenBuilderCompatibilityFile = try! SourceFileSyntax(leadingTrivi
2121
for layoutNode in SYNTAX_NODES.compactMap(\.layoutNode).filter({ $0.children.hasDeprecatedChild }) {
2222
if let convenienceInit = try layoutNode.createConvenienceBuilderInitializer(useDeprecatedChildName: true) {
2323
let deprecatedNames = layoutNode.children
24-
.filter { !$0.isUnexpectedNodes }
25-
.compactMap { $0.deprecatedName?.withFirstCharacterLowercased }
24+
.filter { !$0.isUnexpectedNodes && $0.deprecatedName != nil }
25+
.compactMap { $0.varName }
2626
.joined(separator: ", ")
2727

2828
DeclSyntax(

CodeGeneration/Tests/ValidateSyntaxNodes/ValidateSyntaxNodes.swift

Lines changed: 39 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,22 @@ fileprivate extension Child {
9090
}
9191
}
9292

93+
fileprivate extension Array where Element: Hashable, Element: Comparable {
94+
/// Returns the element that occurs most frequently in the array.
95+
///
96+
/// If there is a tie, returns the lexicographically first element.
97+
///
98+
/// Returns `nil` if the array is empty.
99+
var mostCommon: Element? {
100+
var elementCounts: [Element: Int] = [:]
101+
for element in self {
102+
elementCounts[element, default: 0] += 1
103+
}
104+
let maxCount = elementCounts.values.max()
105+
return elementCounts.filter({ $0.value == maxCount }).map(\.key).sorted().first
106+
}
107+
}
108+
93109
class ValidateSyntaxNodes: XCTestCase {
94110
/// All nodes with base kind e.g. `ExprSyntax` should end with `ExprSyntax`.
95111
func testBaseKindSuffix() {
@@ -395,17 +411,18 @@ class ValidateSyntaxNodes: XCTestCase {
395411
}
396412
}
397413

398-
for (kind, children) in childrenByNodeKind where !kind.isBase && kind != .token {
414+
for (kind, children) in childrenByNodeKind where !kind.isBase && kind != .token && kind != .stringLiteralExpr {
399415
let childNames = children.map(\.child.name)
400-
let firstChildName = childNames.first!
416+
let mostCommonChildName = childNames.mostCommon!
417+
let mostCommonChild = children.first(where: { $0.child.name == mostCommonChildName })!
401418

402-
for (node, child) in children.dropFirst() {
403-
if child.name != firstChildName {
419+
for (node, child) in children {
420+
if child.name != mostCommonChildName {
404421
failures.append(
405422
ValidationFailure(
406423
node: node.kind,
407424
message:
408-
"child '\(child.name)' is named inconsistently with '\(children.first!.node.kind.syntaxType).\(children.first!.child.name)', which has the same type ('\(kind.syntaxType)')"
425+
"child '\(child.name)' is named inconsistently with '\(mostCommonChild.node.kind.syntaxType).\(mostCommonChildName)', which has the same type ('\(kind.syntaxType)')"
409426
)
410427
)
411428
}
@@ -415,99 +432,45 @@ class ValidateSyntaxNodes: XCTestCase {
415432
assertFailuresMatchXFails(
416433
failures,
417434
expectedFailures: [
418-
ValidationFailure(
419-
node: .differentiableAttributeArguments,
420-
message: "child 'WhereClause' is named inconsistently with 'ActorDeclSyntax.GenericWhereClause', which has the same type ('GenericWhereClauseSyntax')"
421-
),
422-
ValidationFailure(
423-
node: .subscriptDecl,
424-
message: "child 'Indices' is named inconsistently with 'FunctionSignatureSyntax.Input', which has the same type ('ParameterClauseSyntax')"
425-
),
435+
// MARK: DeclNameArguments
436+
// FIXME: IdentifierExprSyntax etc. should probably use DeclName as child instead of Name and Arguments
426437
ValidationFailure(
427438
node: .qualifiedDeclName,
428-
message: "child 'Arguments' is named inconsistently with 'DeclNameSyntax.DeclNameArguments', which has the same type ('DeclNameArgumentsSyntax')"
439+
message:
440+
"child 'Arguments' is named inconsistently with 'IdentifierExprSyntax.DeclNameArguments', which has the same type ('DeclNameArgumentsSyntax')"
429441
),
430442
ValidationFailure(
431-
node: .macroExpansionDecl,
443+
node: .declName,
432444
message:
433-
"child 'GenericArguments' is named inconsistently with 'KeyPathPropertyComponentSyntax.GenericArgumentClause', which has the same type ('GenericArgumentClauseSyntax')"
445+
"child 'Arguments' is named inconsistently with 'IdentifierExprSyntax.DeclNameArguments', which has the same type ('DeclNameArgumentsSyntax')"
434446
),
447+
// MARK: Alternate names for InitializerClauseSyntax
448+
// The cases below don’t have intializers but just a syntactic element that happens to be spelled the same
435449
ValidationFailure(
436-
node: .macroExpansionExpr,
450+
node: .enumCaseElement,
437451
message:
438-
"child 'GenericArguments' is named inconsistently with 'KeyPathPropertyComponentSyntax.GenericArgumentClause', which has the same type ('GenericArgumentClauseSyntax')"
452+
"child 'RawValue' is named inconsistently with 'MatchingPatternConditionSyntax.Initializer', which has the same type ('InitializerClauseSyntax')"
439453
),
440454
ValidationFailure(
441455
node: .enumCaseParameter,
442-
message: "child 'DefaultArgument' is named inconsistently with 'EnumCaseElementSyntax.RawValue', which has the same type ('InitializerClauseSyntax')"
456+
message:
457+
"child 'DefaultArgument' is named inconsistently with 'MatchingPatternConditionSyntax.Initializer', which has the same type ('InitializerClauseSyntax')"
443458
),
444459
ValidationFailure(
445460
node: .functionParameter,
446-
message: "child 'DefaultArgument' is named inconsistently with 'EnumCaseElementSyntax.RawValue', which has the same type ('InitializerClauseSyntax')"
461+
message:
462+
"child 'DefaultArgument' is named inconsistently with 'MatchingPatternConditionSyntax.Initializer', which has the same type ('InitializerClauseSyntax')"
447463
),
448464
ValidationFailure(
449465
node: .macroDecl,
450-
message: "child 'Definition' is named inconsistently with 'EnumCaseElementSyntax.RawValue', which has the same type ('InitializerClauseSyntax')"
451-
),
452-
ValidationFailure(
453-
node: .matchingPatternCondition,
454-
message: "child 'Initializer' is named inconsistently with 'EnumCaseElementSyntax.RawValue', which has the same type ('InitializerClauseSyntax')"
455-
),
456-
ValidationFailure(
457-
node: .optionalBindingCondition,
458-
message: "child 'Initializer' is named inconsistently with 'EnumCaseElementSyntax.RawValue', which has the same type ('InitializerClauseSyntax')"
459-
),
460-
ValidationFailure(
461-
node: .patternBinding,
462-
message: "child 'Initializer' is named inconsistently with 'EnumCaseElementSyntax.RawValue', which has the same type ('InitializerClauseSyntax')"
463-
),
464-
ValidationFailure(
465-
node: .tupleTypeElement,
466-
message: "child 'Initializer' is named inconsistently with 'EnumCaseElementSyntax.RawValue', which has the same type ('InitializerClauseSyntax')"
466+
message:
467+
"child 'Definition' is named inconsistently with 'MatchingPatternConditionSyntax.Initializer', which has the same type ('InitializerClauseSyntax')"
467468
),
469+
// MARK: Miscellaneous
468470
ValidationFailure(
469471
node: .multipleTrailingClosureElement,
470472
message: "child 'Closure' is named inconsistently with 'FunctionCallExprSyntax.TrailingClosure', which has the same type ('ClosureExprSyntax')"
471473
),
472-
ValidationFailure(
473-
node: .subscriptDecl,
474-
message: "child 'Result' is named inconsistently with 'ClosureSignatureSyntax.Output', which has the same type ('ReturnClauseSyntax')"
475-
),
476-
ValidationFailure(
477-
node: .canImportVersionInfo,
478-
message:
479-
"child 'VersionTuple' is named inconsistently with 'AvailabilityVersionRestrictionSyntax.Version', which has the same type ('VersionTupleSyntax')"
480-
),
481-
ValidationFailure(
482-
node: .exposeAttributeArguments,
483-
message:
484-
"child 'CxxName' is named inconsistently with 'ConventionAttributeArgumentsSyntax.CTypeString', which has the same type ('StringLiteralExprSyntax')"
485-
),
486-
ValidationFailure(
487-
node: .opaqueReturnTypeOfAttributeArguments,
488-
message:
489-
"child 'MangledName' is named inconsistently with 'ConventionAttributeArgumentsSyntax.CTypeString', which has the same type ('StringLiteralExprSyntax')"
490-
),
491-
ValidationFailure(
492-
node: .originallyDefinedInArguments,
493-
message:
494-
"child 'ModuleName' is named inconsistently with 'ConventionAttributeArgumentsSyntax.CTypeString', which has the same type ('StringLiteralExprSyntax')"
495-
),
496-
ValidationFailure(
497-
node: .poundSourceLocationArgs,
498-
message:
499-
"child 'FileName' is named inconsistently with 'ConventionAttributeArgumentsSyntax.CTypeString', which has the same type ('StringLiteralExprSyntax')"
500-
),
501-
ValidationFailure(
502-
node: .unavailableFromAsyncArguments,
503-
message:
504-
"child 'Message' is named inconsistently with 'ConventionAttributeArgumentsSyntax.CTypeString', which has the same type ('StringLiteralExprSyntax')"
505-
),
506-
ValidationFailure(
507-
node: .underscorePrivateAttributeArguments,
508-
message:
509-
"child 'Filename' is named inconsistently with 'ConventionAttributeArgumentsSyntax.CTypeString', which has the same type ('StringLiteralExprSyntax')"
510-
),
511474
]
512475
)
513476
}

Sources/SwiftParser/Attributes.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ extension Parser {
423423
kindSpecifierComma: kindSpecifierComma,
424424
parameters: parameters,
425425
parametersComma: parametersComma,
426-
whereClause: whereClause,
426+
genericWhereClause: whereClause,
427427
arena: self.arena
428428
)
429429
}
@@ -683,8 +683,8 @@ extension Parser {
683683
let (unexpectedBeforeColon, colon) = self.expect(.colon)
684684
let (targetFunction, args) = self.parseDeclNameRef([.zeroArgCompoundNames, .keywordsUsingSpecialNames, .operators])
685685
let declName = RawDeclNameSyntax(
686-
declBaseName: targetFunction,
687-
declNameArguments: args,
686+
baseName: targetFunction,
687+
arguments: args,
688688
arena: self.arena
689689
)
690690
let comma = self.consume(if: .comma)
@@ -1031,7 +1031,7 @@ extension Parser {
10311031
.zeroArgCompoundNames, .keywordsUsingSpecialNames, .operators,
10321032
])
10331033
}
1034-
let method = RawDeclNameSyntax(declBaseName: base, declNameArguments: args, arena: self.arena)
1034+
let method = RawDeclNameSyntax(baseName: base, arguments: args, arena: self.arena)
10351035
return RawDynamicReplacementArgumentsSyntax(
10361036
unexpectedBeforeLabel,
10371037
forLabel: label,

0 commit comments

Comments
 (0)