Skip to content

Commit 1c9a7cd

Browse files
committed
SwiftCompilerSources: refactor DiagnosticEngine
* move it from the SIL to the AST module (where it belongs) * change the signature of `diagnose` from `diagnose(location, .some_error)` to `diagnose(.some_error, at: location)` * add an overload to allow passing a `SIL.Location` directly to `diagnose` * add a `Diagnostic : Error` utility struct which allows throwing a `Diagnostic`
1 parent 43d793b commit 1c9a7cd

15 files changed

+84
-41
lines changed

SwiftCompilerSources/Sources/AST/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ add_swift_compiler_module(AST
1212
SOURCES
1313
Declarations.swift
1414
Conformance.swift
15+
DiagnosticEngine.swift
1516
GenericSignature.swift
1617
Registration.swift
1718
SubstitutionMap.swift

SwiftCompilerSources/Sources/Optimizer/Utilities/DiagnosticEngine.swift renamed to SwiftCompilerSources/Sources/AST/DiagnosticEngine.swift

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,40 +13,34 @@
1313
import ASTBridging
1414

1515
import Basic
16-
import SIL
1716

18-
typealias DiagID = BridgedDiagID
17+
public typealias DiagID = BridgedDiagID
1918

20-
protocol DiagnosticArgument {
19+
public protocol DiagnosticArgument {
2120
func _withBridgedDiagnosticArgument(_ fn: (BridgedDiagnosticArgument) -> Void)
2221
}
2322
extension String: DiagnosticArgument {
24-
func _withBridgedDiagnosticArgument(_ fn: (BridgedDiagnosticArgument) -> Void) {
23+
public func _withBridgedDiagnosticArgument(_ fn: (BridgedDiagnosticArgument) -> Void) {
2524
_withBridgedStringRef { fn(BridgedDiagnosticArgument($0)) }
2625
}
2726
}
2827
extension StringRef: DiagnosticArgument {
29-
func _withBridgedDiagnosticArgument(_ fn: (BridgedDiagnosticArgument) -> Void) {
28+
public func _withBridgedDiagnosticArgument(_ fn: (BridgedDiagnosticArgument) -> Void) {
3029
fn(BridgedDiagnosticArgument(_bridged))
3130
}
3231
}
3332
extension Int: DiagnosticArgument {
34-
func _withBridgedDiagnosticArgument(_ fn: (BridgedDiagnosticArgument) -> Void) {
33+
public func _withBridgedDiagnosticArgument(_ fn: (BridgedDiagnosticArgument) -> Void) {
3534
fn(BridgedDiagnosticArgument(self))
3635
}
3736
}
3837
extension Type: DiagnosticArgument {
39-
func _withBridgedDiagnosticArgument(_ fn: (BridgedDiagnosticArgument) -> Void) {
40-
fn(bridged.asDiagnosticArgument())
41-
}
42-
}
43-
extension DeclRef: DiagnosticArgument {
44-
func _withBridgedDiagnosticArgument(_ fn: (BridgedDiagnosticArgument) -> Void) {
38+
public func _withBridgedDiagnosticArgument(_ fn: (BridgedDiagnosticArgument) -> Void) {
4539
fn(bridged.asDiagnosticArgument())
4640
}
4741
}
4842

49-
struct DiagnosticFixIt {
43+
public struct DiagnosticFixIt {
5044
let start: SourceLoc
5145
let byteLength: Int
5246
let text: String
@@ -67,10 +61,10 @@ struct DiagnosticFixIt {
6761
}
6862
}
6963

70-
struct DiagnosticEngine {
64+
public struct DiagnosticEngine {
7165
private let bridged: BridgedDiagnosticEngine
7266

73-
init(bridged: BridgedDiagnosticEngine) {
67+
public init(bridged: BridgedDiagnosticEngine) {
7468
self.bridged = bridged
7569
}
7670
init?(bridged: BridgedNullableDiagnosticEngine) {
@@ -80,9 +74,9 @@ struct DiagnosticEngine {
8074
self.bridged = BridgedDiagnosticEngine(raw: raw)
8175
}
8276

83-
func diagnose(_ position: SourceLoc?,
84-
_ id: DiagID,
77+
public func diagnose(_ id: DiagID,
8578
_ args: [DiagnosticArgument],
79+
at position: SourceLoc?,
8680
highlight: CharSourceRange? = nil,
8781
fixIts: [DiagnosticFixIt] = []) {
8882

@@ -136,11 +130,32 @@ struct DiagnosticEngine {
136130
closure()
137131
}
138132

139-
func diagnose(_ position: SourceLoc?,
140-
_ id: DiagID,
133+
public func diagnose(_ id: DiagID,
141134
_ args: DiagnosticArgument...,
135+
at position: SourceLoc?,
142136
highlight: CharSourceRange? = nil,
143137
fixIts: DiagnosticFixIt...) {
144-
diagnose(position, id, args, highlight: highlight, fixIts: fixIts)
138+
diagnose(id, args, at: position, highlight: highlight, fixIts: fixIts)
139+
}
140+
141+
public func diagnose(_ diagnostic: Diagnostic) {
142+
diagnose(diagnostic.id, diagnostic.arguments, at: diagnostic.position)
143+
}
144+
}
145+
146+
/// A utility struct which allows throwing a Diagnostic.
147+
public struct Diagnostic : Error {
148+
public let id: DiagID
149+
public let arguments: [DiagnosticArgument]
150+
public let position: SourceLoc?
151+
152+
public init(_ id: DiagID, _ arguments: DiagnosticArgument..., at position: SourceLoc?) {
153+
self.init(id, arguments, at: position)
154+
}
155+
156+
public init(_ id: DiagID, _ arguments: [DiagnosticArgument], at position: SourceLoc?) {
157+
self.id = id
158+
self.arguments = arguments
159+
self.position = position
145160
}
146161
}

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/DiagnoseInfiniteRecursion.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ private struct Analysis {
289289
worklist.pushIfNotVisited(function.entryBlock)
290290
while let block = worklist.pop() {
291291
if case .recursive(let apply) = block.getKind(for: invariants, context) {
292-
context.diagnosticEngine.diagnose(apply.location.sourceLoc, .warn_infinite_recursive_call)
292+
context.diagnosticEngine.diagnose(.warn_infinite_recursive_call, at: apply.location)
293293
} else {
294294
for succ in block.successors where isInInfiniteRecursionLoop(succ) {
295295
worklist.pushIfNotVisited(succ)

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/LifetimeDependenceDiagnostics.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ private struct DiagnoseDependence {
135135

136136
func diagnose(_ position: SourceLoc?, _ id: DiagID,
137137
_ args: DiagnosticArgument...) {
138-
context.diagnosticEngine.diagnose(position, id, args)
138+
context.diagnosticEngine.diagnose(id, args, at: position)
139139
}
140140

141141
/// Check that this use is inside the dependence scope.

SwiftCompilerSources/Sources/Optimizer/ModulePasses/DiagnoseUnknownConstValues.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ let diagnoseUnknownConstValues = ModulePass(name: "diagnose-unknown-const-values
3939
private func verifyGlobals(_ context: ModulePassContext) {
4040
for gv in context.globalVariables where gv.isConst {
4141
if gv.staticInitValue == nil {
42-
context.diagnosticEngine.diagnose(gv.varDecl?.location.sourceLoc,
43-
.require_const_initializer_for_const)
42+
context.diagnosticEngine.diagnose(.require_const_initializer_for_const,
43+
at: gv.varDecl?.location.sourceLoc)
4444
}
4545
}
4646
}
@@ -66,8 +66,8 @@ private func verifyLocal(debugValueInst: DebugValueInst,
6666
}
6767

6868
if !constExprState.isConstantValue(debugValueInst.operand.value) {
69-
context.diagnosticEngine.diagnose(debugValueInst.location.sourceLoc,
70-
.require_const_initializer_for_const)
69+
context.diagnosticEngine.diagnose(.require_const_initializer_for_const,
70+
at: debugValueInst.location)
7171
}
7272
}
7373

@@ -92,8 +92,8 @@ private func verifyCallArguments(apply: FullApplySite,
9292
for (paramIdx, param) in calleeFn.convention.parameters.enumerated() where param.hasOption(.const) {
9393
let matchingOperand = apply.parameterOperands[paramIdx]
9494
if !constExprState.isConstantValue(matchingOperand.value) {
95-
context.diagnosticEngine.diagnose(apply.location.sourceLoc,
96-
.require_const_arg_for_parameter)
95+
context.diagnosticEngine.diagnose(.require_const_arg_for_parameter,
96+
at: apply.location)
9797
}
9898
}
9999
}

SwiftCompilerSources/Sources/Optimizer/ModulePasses/MandatoryPerformanceOptimizations.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,11 @@ private func optimize(function: Function, _ context: FunctionPassContext, _ modu
137137
// We need to de-virtualize deinits of non-copyable types to be able to specialize the deinitializers.
138138
case let destroyValue as DestroyValueInst:
139139
if !devirtualizeDeinits(of: destroyValue, simplifyCtxt) {
140-
context.diagnosticEngine.diagnose(destroyValue.location.sourceLoc, .deinit_not_visible)
140+
context.diagnosticEngine.diagnose(.deinit_not_visible, at: destroyValue.location)
141141
}
142142
case let destroyAddr as DestroyAddrInst:
143143
if !devirtualizeDeinits(of: destroyAddr, simplifyCtxt) {
144-
context.diagnosticEngine.diagnose(destroyAddr.location.sourceLoc, .deinit_not_visible)
144+
context.diagnosticEngine.diagnose(.deinit_not_visible, at: destroyAddr.location)
145145
}
146146

147147
case let iem as InitExistentialMetatypeInst:
@@ -309,7 +309,7 @@ private func checkForGenericMethods(in witnessTable: WitnessTable,
309309
let witness,
310310
witness.isGeneric
311311
{
312-
context.diagnosticEngine.diagnose(errorLocation.sourceLoc, .cannot_specialize_witness_method, requirement)
312+
context.diagnosticEngine.diagnose(.cannot_specialize_witness_method, requirement, at: errorLocation)
313313
return
314314
}
315315
}
@@ -318,7 +318,7 @@ private func checkForGenericMethods(in witnessTable: WitnessTable,
318318
private func checkVTablesForGenericFunctions(_ context: ModulePassContext) {
319319
for vTable in context.vTables where !vTable.class.isGenericAtAnyLevel {
320320
for entry in vTable.entries where entry.implementation.isGeneric {
321-
context.diagnosticEngine.diagnose(entry.methodDecl.location.sourceLoc, .non_final_generic_class_function)
321+
context.diagnosticEngine.diagnose(.non_final_generic_class_function, at: entry.methodDecl.location)
322322
}
323323
}
324324
}

SwiftCompilerSources/Sources/Optimizer/Utilities/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ swift_compiler_sources(Optimizer
1111
AddressUtils.swift
1212
BorrowUtils.swift
1313
SpecializationCloner.swift
14-
DiagnosticEngine.swift
1514
Devirtualization.swift
1615
EscapeUtils.swift
1716
ForwardingUtils.swift

SwiftCompilerSources/Sources/Optimizer/Utilities/GenericSpecialization.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ private struct VTableSpecializer {
5050
let classDecl = classType.nominal! as! ClassDecl
5151
guard let origVTable = context.lookupVTable(for: classDecl) else {
5252
if context.enableWMORequiredDiagnostics {
53-
context.diagnosticEngine.diagnose(errorLocation.sourceLoc, .cannot_specialize_class, classType)
53+
context.diagnosticEngine.diagnose(.cannot_specialize_class, classType, at: errorLocation)
5454
}
5555
return
5656
}
@@ -91,7 +91,7 @@ private struct VTableSpecializer {
9191
context.loadFunction(function: entry.implementation, loadCalleesRecursively: true),
9292
let specializedMethod = context.specialize(function: entry.implementation, for: methodSubs) else
9393
{
94-
context.diagnosticEngine.diagnose(entry.methodDecl.location.sourceLoc, .non_final_generic_class_function)
94+
context.diagnosticEngine.diagnose(.non_final_generic_class_function, at: entry.methodDecl.location)
9595
return nil
9696
}
9797
notifyNewFunction(specializedMethod)
@@ -131,7 +131,7 @@ func specializeWitnessTable(forConformance conformance: Conformance,
131131
context.loadFunction(function: origMethod, loadCalleesRecursively: true),
132132
let specializedMethod = context.specialize(function: origMethod, for: methodSubs) else
133133
{
134-
context.diagnosticEngine.diagnose(errorLocation.sourceLoc, .cannot_specialize_witness_method, requirement)
134+
context.diagnosticEngine.diagnose(.cannot_specialize_witness_method, requirement, at: errorLocation)
135135
return origEntry
136136
}
137137
return .method(requirement: requirement, witness: specializedMethod)

SwiftCompilerSources/Sources/SIL/ASTExtensions.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,19 @@ extension Conformance {
6262
return function.bridged.conformanceMatchesActorIsolation(bridged)
6363
}
6464
}
65+
66+
extension DiagnosticEngine {
67+
public func diagnose(_ id: DiagID, _ args: DiagnosticArgument..., at location: Location) {
68+
diagnose(id, args, at: location.sourceLoc)
69+
}
70+
71+
public func diagnose(_ id: DiagID, _ args: [DiagnosticArgument], at location: Location) {
72+
diagnose(id, args, at: location.sourceLoc)
73+
}
74+
}
75+
76+
extension Diagnostic {
77+
public init(_ id: DiagID, _ arguments: DiagnosticArgument..., at location: Location) {
78+
self.init(id, arguments, at: location.sourceLoc)
79+
}
80+
}

SwiftCompilerSources/Sources/SIL/DeclRef.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,9 @@ public struct DeclRef: CustomStringConvertible, NoReflectionChildren {
3131
lhs.bridged.isEqualTo(rhs.bridged)
3232
}
3333
}
34+
35+
extension DeclRef: DiagnosticArgument {
36+
public func _withBridgedDiagnosticArgument(_ fn: (BridgedDiagnosticArgument) -> Void) {
37+
fn(bridged.asDiagnosticArgument())
38+
}
39+
}

0 commit comments

Comments
 (0)