Skip to content

Commit e63ea8a

Browse files
authored
Merge pull request #2540 from tshortli/unknown-default
Resolve `switch covers known cases, but 'Enum' may have additional unknown values` warnings
2 parents 6ea8d50 + eee827f commit e63ea8a

File tree

15 files changed

+87
-3
lines changed

15 files changed

+87
-3
lines changed

CodeGeneration/Sources/generate-swift-syntax/templates/swiftparserdiagnostics/TokenNameForDiagnosticsFile.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,21 @@ let tokenNameForDiagnosticFile = SourceFileSyntax(leadingTrivia: copyrightHeader
2929
SwitchCaseSyntax("case .keyword(let keyword):") {
3030
StmtSyntax("return String(syntaxText: keyword.defaultText)")
3131
}
32+
IfConfigDeclSyntax(
33+
clauses: IfConfigClauseListSyntax {
34+
IfConfigClauseSyntax(
35+
poundKeyword: .poundIfToken(),
36+
condition: ExprSyntax("RESILIENT_LIBRARIES"),
37+
elements: .switchCases(
38+
SwitchCaseListSyntax {
39+
SwitchCaseSyntax("@unknown default:") {
40+
StmtSyntax("fatalError()")
41+
}
42+
}
43+
)
44+
)
45+
}
46+
)
3247
}
3348
}
3449
}

Sources/SwiftCompilerPluginMessageHandling/Diagnostics.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ extension PluginMessage.Diagnostic.Severity {
5454
case .warning: self = .warning
5555
case .note: self = .note
5656
case .remark: self = .remark
57+
#if RESILIENT_LIBRARIES
58+
@unknown default: fatalError()
59+
#endif
5760
}
5861
}
5962
}
@@ -122,6 +125,10 @@ extension PluginMessage.Diagnostic {
122125
to: .afterTrailingTrivia
123126
)
124127
text = newTrivia.description
128+
#if RESILIENT_LIBRARIES
129+
@unknown default:
130+
fatalError()
131+
#endif
125132
}
126133
guard let range = range else {
127134
return nil

Sources/SwiftCompilerPluginMessageHandling/PluginMacroExpansionContext.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,9 @@ class SourceManager {
153153
switch filePathMode {
154154
case .fileID: file = base.location.fileID
155155
case .filePath: file = base.location.fileName
156+
#if RESILIENT_LIBRARIES
157+
@unknown default: fatalError()
158+
#endif
156159
}
157160

158161
let localPosition = node.position(at: kind)
@@ -185,6 +188,10 @@ fileprivate extension Syntax {
185188
return self.endPositionBeforeTrailingTrivia
186189
case .afterTrailingTrivia:
187190
return self.endPosition
191+
#if RESILIENT_LIBRARIES
192+
@unknown default:
193+
fatalError()
194+
#endif
188195
}
189196
}
190197
}

Sources/SwiftOperators/OperatorTable+Semantics.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ extension PrecedenceGroup {
6262
default:
6363
break
6464
}
65+
#if RESILIENT_LIBRARIES
66+
@unknown default:
67+
fatalError()
68+
#endif
6569
}
6670
}
6771
}

Sources/SwiftParser/StringLiteralRepresentedLiteralValue.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ extension StringLiteralExprSyntax {
4343
case .expressionSegment:
4444
// Bail out if there are any interpolation segments.
4545
return nil
46+
#if RESILIENT_LIBRARIES
47+
@unknown default:
48+
fatalError()
49+
#endif
4650
}
4751
}
4852

Sources/SwiftParser/StringLiterals.swift

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,10 @@ extension Parser {
167167
openQuoteHasTrailingNewline: Bool,
168168
middleSegments: inout [RawStringLiteralSegmentListSyntax.Element]
169169
) -> Bool {
170-
switch middleSegments.last {
170+
guard let segment = middleSegments.last else {
171+
return openQuoteHasTrailingNewline
172+
}
173+
switch segment {
171174
case .stringSegment(let lastMiddleSegment):
172175
if !lastMiddleSegment.content.trailingTriviaPieces.isEmpty {
173176
precondition(
@@ -218,8 +221,10 @@ extension Parser {
218221
}
219222
case .expressionSegment:
220223
return false
221-
case nil:
222-
return openQuoteHasTrailingNewline
224+
#if RESILIENT_LIBRARIES
225+
@unknown default:
226+
fatalError()
227+
#endif
223228
}
224229
}
225230

@@ -282,6 +287,10 @@ extension Parser {
282287
if let rewrittenSegment = expressionIndentationChecker.checkIndentation(of: segment) {
283288
middleSegments[index] = .expressionSegment(rewrittenSegment)
284289
}
290+
#if RESILIENT_LIBRARIES
291+
@unknown default:
292+
fatalError()
293+
#endif
285294
}
286295
}
287296
}

Sources/SwiftParser/TokenPrecedence.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,10 @@ enum TokenPrecedence: Comparable {
358358
.wrt,
359359
.unsafe:
360360
self = .exprKeyword
361+
#if RESILIENT_LIBRARIES
362+
@unknown default:
363+
fatalError()
364+
#endif
361365
}
362366
}
363367
}

Sources/SwiftParserDiagnostics/LexerDiagnosticMessages.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,10 @@ public extension SwiftSyntax.TokenDiagnostic {
230230
case .unicodeCurlyQuote: return StaticTokenError.unicodeCurlyQuote
231231
case .unprintableAsciiCharacter: return StaticTokenError.unprintableAsciiCharacter
232232
case .unterminatedBlockComment: return StaticTokenError.unterminatedBlockComment
233+
#if RESILIENT_LIBRARIES
234+
@unknown default:
235+
fatalError()
236+
#endif
233237
}
234238
}
235239

Sources/SwiftParserDiagnostics/SyntaxExtensions.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,10 @@ extension TypeSpecifierListSyntax {
216216
switch specifier {
217217
case .simpleTypeSpecifier(let specifier): return specifier.specifier
218218
case .lifetimeTypeSpecifier: return nil
219+
#if RESILIENT_LIBRARIES
220+
@unknown default:
221+
fatalError()
222+
#endif
219223
}
220224
}
221225
}

Sources/SwiftParserDiagnostics/generated/TokenNameForDiagnostics.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ extension TokenKind {
117117
return "wildcard"
118118
case .keyword(let keyword):
119119
return String(syntaxText: keyword.defaultText)
120+
#if RESILIENT_LIBRARIES
121+
@unknown default:fatalError()
122+
#endif
120123
}
121124
}
122125
}

Sources/SwiftRefactor/FormatRawStringLiteral.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ public struct FormatRawStringLiteral: SyntaxRefactoringProvider {
4747
case .stringSegment(let string):
4848
// Find the longest run of # characters in the content of the literal.
4949
maximumHashes = max(maximumHashes, string.content.text.longestRun(of: "#"))
50+
#if RESILIENT_LIBRARIES
51+
@unknown default:
52+
fatalError()
53+
#endif
5054
}
5155
}
5256

Sources/SwiftSyntaxMacroExpansion/BasicMacroExpansionContext.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,11 @@ extension BasicMacroExpansionContext: MacroExpansionContext {
230230

231231
case .filePath:
232232
fileName = knownRoot.fullFilePath
233+
234+
#if RESILIENT_LIBRARIES
235+
@unknown default:
236+
fatalError()
237+
#endif
233238
}
234239

235240
// Find the node's offset relative to its root.
@@ -246,6 +251,11 @@ extension BasicMacroExpansionContext: MacroExpansionContext {
246251

247252
case .afterTrailingTrivia:
248253
rawPosition = node.endPosition
254+
255+
#if RESILIENT_LIBRARIES
256+
@unknown default:
257+
fatalError()
258+
#endif
249259
}
250260

251261
// Do the location lookup.

Sources/SwiftSyntaxMacroExpansion/MacroExpansion.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,10 @@ fileprivate extension SyntaxProtocol {
402402
formatted = self.formatted(using: BasicFormat(indentationWidth: indentationWidth))
403403
case .disabled:
404404
formatted = Syntax(self)
405+
#if RESILIENT_LIBRARIES
406+
@unknown default:
407+
fatalError()
408+
#endif
405409
}
406410
return formatted.trimmedDescription(matching: { $0.isWhitespace })
407411
}

Sources/SwiftSyntaxMacroExpansion/MacroSystem.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,6 +1296,10 @@ private extension AccessorBlockSyntax {
12961296
)
12971297
)
12981298
result.accessors = .accessors(AccessorDeclListSyntax([getterAsAccessor] + Array(newAccessors)))
1299+
#if RESILIENT_LIBRARIES
1300+
@unknown default:
1301+
fatalError()
1302+
#endif
12991303
}
13001304
return result
13011305
}

cmake/modules/AddSwiftHostLibrary.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ function(add_swift_syntax_library name)
7373
# Configure the emission of the Swift module files.
7474
target_compile_options("${name}" PRIVATE
7575
$<$<COMPILE_LANGUAGE:Swift>:
76+
-DRESILIENT_LIBRARIES;
7677
-module-name;${name};
7778
-enable-library-evolution;
7879
-emit-module-path;${module_file};

0 commit comments

Comments
 (0)