Skip to content

Commit 002277d

Browse files
committed
ASTGen: Suppress warnings about handling unknown enum cases.
When built as part of the compiler toolchain SwiftSyntax is built with library evolution enabled. This means that switches over enums from SwiftSyntax must include a default case to be considered exhaustive, or a warning will be emitted. This change suppresses those warnings by adding `@unknown default` cases wherever they are expected. As a compromise, these `@unknown default` cases are wrapped with a `#if` to ensure they are only included in the CMake build of ASTGen, but continue to be omitted from the SPM build which compiler engineers use to iterate on ASTGen's implementation. This is needed to avoid generating the opposite warning during the SPM build, since the compiler thinks the `@unknown default` case is superfluous when SwiftSyntax is built non-resiliently. As an aside, this catch-22 is a bummer and I think we should change the compiler to suppress the unreachable default warning when the default is annotated `@unknown`.
1 parent f75d02a commit 002277d

13 files changed

+110
-0
lines changed

lib/ASTGen/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ endif()
103103

104104
set(compile_options
105105
${c_include_paths_args}
106+
"SHELL: -DRESILIENT_SWIFT_SYNTAX"
106107
"SHELL: ${cxx_interop_flag}"
107108
"SHELL: -Xcc -std=c++17 -Xcc -DCOMPILED_WITH_SWIFT -Xcc -DSWIFT_TARGET"
108109

lib/ASTGen/Sources/ASTGen/DeclAttrs.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ extension ASTGenVisitor {
5858
addAttribute(self.generateDeclAttribute(attribute: node))
5959
case .ifConfigDecl:
6060
fatalError("unimplemented")
61+
#if RESILIENT_SWIFT_SYNTAX
62+
@unknown default:
63+
fatalError()
64+
#endif
6165
}
6266
}
6367

lib/ASTGen/Sources/ASTGen/Decls.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ extension ASTGenVisitor {
6868
return self.generate(typeAliasDecl: node).asDecl
6969
case .variableDecl(let node):
7070
return self.generate(variableDecl: node).asDecl
71+
#if RESILIENT_SWIFT_SYNTAX
72+
@unknown default:
73+
fatalError()
74+
#endif
7175
}
7276
return self.generateWithLegacy(node)
7377
}
@@ -421,6 +425,10 @@ extension ASTGenVisitor {
421425
accessors: CollectionOfOne(accessor).bridgedArray(in: self),
422426
rBraceLoc: rightBrace
423427
)
428+
#if RESILIENT_SWIFT_SYNTAX
429+
@unknown default:
430+
fatalError()
431+
#endif
424432
}
425433
}
426434

@@ -726,6 +734,10 @@ extension ASTGenVisitor {
726734
} else {
727735
body.associativity = associativity
728736
}
737+
#if RESILIENT_SWIFT_SYNTAX
738+
@unknown default:
739+
fatalError()
740+
#endif
729741
}
730742
}
731743

lib/ASTGen/Sources/ASTGen/DiagnosticsBridge.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ fileprivate func emitDiagnosticParts(
7373
replaceStartLoc = bridgedSourceLoc(at: oldToken.endPositionBeforeTrailingTrivia)
7474
replaceEndLoc = bridgedSourceLoc(at: oldToken.endPosition)
7575
newText = newTrivia.description
76+
77+
#if RESILIENT_SWIFT_SYNTAX
78+
@unknown default:
79+
fatalError()
80+
#endif
7681
}
7782

7883
newText.withBridgedString { bridgedMessage in
@@ -208,6 +213,11 @@ extension SourceManager {
208213
at: oldToken.endPosition
209214
)
210215
newText = newTrivia.description
216+
217+
#if RESILIENT_SWIFT_SYNTAX
218+
@unknown default:
219+
fatalError()
220+
#endif
211221
}
212222

213223
newText.withBridgedString { bridgedMessage in

lib/ASTGen/Sources/ASTGen/Exprs.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,10 @@ extension ASTGenVisitor {
197197
preconditionFailure("should be handled in generate(sequenceExpr:)")
198198
case .unresolvedTernaryExpr:
199199
preconditionFailure("should be handled in generate(sequenceExpr:)")
200+
#if RESILIENT_SWIFT_SYNTAX
201+
@unknown default:
202+
fatalError()
203+
#endif
200204
}
201205
preconditionFailure("isExprMigrated() mismatch")
202206
}

lib/ASTGen/Sources/ASTGen/Generics.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ extension ASTGenVisitor {
6969
case .layoutRequirement(_):
7070
// FIXME: Implement layout requirement translation.
7171
fatalError("Translation of layout requirements not implemented!")
72+
#if RESILIENT_SWIFT_SYNTAX
73+
@unknown default:
74+
fatalError()
75+
#endif
7276
}
7377
}
7478

lib/ASTGen/Sources/ASTGen/Literals.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ extension ASTGenVisitor {
8888
colonLocs = elementNodes.lazy
8989
.map({ self.generateSourceLoc($0.colon) })
9090
.bridgedArray(in: self)
91+
#if RESILIENT_SWIFT_SYNTAX
92+
@unknown default:
93+
fatalError()
94+
#endif
9195
}
9296
return .createParsed(
9397
self.ctx,

lib/ASTGen/Sources/ASTGen/Macros.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,10 @@ func checkMacroDefinition(
359359
replacementsPtr.pointee = replacementBuffer.baseAddress
360360
numReplacementsPtr.pointee = replacements.count
361361
return Int(BridgedMacroDefinitionKind.expandedMacro.rawValue)
362+
#if RESILIENT_SWIFT_SYNTAX
363+
@unknown default:
364+
fatalError()
365+
#endif
362366
}
363367
} catch let errDiags as DiagnosticsError {
364368
let srcMgr = SourceManager(cxxDiagnosticEngine: diagEnginePtr)
@@ -501,6 +505,9 @@ func expandFreestandingMacroIPC(
501505
case .expression: pluginMacroRole = .expression
502506
case .declaration: pluginMacroRole = .declaration
503507
case .codeItem: pluginMacroRole = .codeItem
508+
#if RESILIENT_SWIFT_SYNTAX
509+
@unknown default: fatalError()
510+
#endif
504511
}
505512

506513
// Send the message.
@@ -790,6 +797,11 @@ func expandAttachedMacroIPC(
790797
.declaration,
791798
.codeItem:
792799
preconditionFailure("unhandled macro role for attached macro")
800+
801+
#if RESILIENT_SWIFT_SYNTAX
802+
@unknown default:
803+
fatalError()
804+
#endif
793805
}
794806

795807
// Prepare syntax nodes to transfer.
@@ -990,6 +1002,10 @@ fileprivate extension SyntaxProtocol {
9901002
formatted = self.formatted()
9911003
case .disabled:
9921004
formatted = Syntax(self)
1005+
#if RESILIENT_SWIFT_SYNTAX
1006+
@unknown default:
1007+
fatalError()
1008+
#endif
9931009
}
9941010
return formatted.trimmedDescription(matching: { $0.isWhitespace })
9951011
}

lib/ASTGen/Sources/ASTGen/Patterns.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ extension ASTGenVisitor {
4545
return self.generate(valueBindingPattern: node).asPattern
4646
case .wildcardPattern(let node):
4747
return self.generate(wildcardPattern: node).asPattern
48+
#if RESILIENT_SWIFT_SYNTAX
49+
@unknown default:
50+
fatalError()
51+
#endif
4852
}
4953
}
5054

lib/ASTGen/Sources/ASTGen/SourceManager+MacroExpansionContext.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,11 @@ extension SourceManager.MacroExpansionContext: MacroExpansionContext {
131131

132132
case .afterTrailingTrivia:
133133
rawPosition = node.endPosition
134+
135+
#if RESILIENT_SWIFT_SYNTAX
136+
@unknown default:
137+
fatalError()
138+
#endif
134139
}
135140

136141
let offsetWithinSyntaxNode =
@@ -164,6 +169,11 @@ extension SourceManager.MacroExpansionContext: MacroExpansionContext {
164169

165170
case .filePath:
166171
break
172+
173+
#if RESILIENT_SWIFT_SYNTAX
174+
@unknown default:
175+
fatalError()
176+
#endif
167177
}
168178

169179
// Do the location lookup.

0 commit comments

Comments
 (0)