Skip to content

Commit a0f0c10

Browse files
committed
[NFC] Add a new entry point for DiagnoseDependence.reportError
Allow diagnosing values that have no relevant users. Such as an @inout argument.
1 parent ce153a8 commit a0f0c10

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/LifetimeDependenceDiagnostics.swift

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ private struct DiagnoseDependence {
160160
func checkInScope(operand: Operand) -> WalkResult {
161161
if let range, !range.inclusiveRangeContains(operand.instruction) {
162162
log(" out-of-range error: \(operand.instruction)")
163-
reportError(operand: operand, diagID: .lifetime_outside_scope_use)
163+
reportError(escapingValue: operand.value, user: operand.instruction, diagID: .lifetime_outside_scope_use)
164164
return .abortWalk
165165
}
166166
log(" contains: \(operand.instruction)")
@@ -169,7 +169,12 @@ private struct DiagnoseDependence {
169169

170170
func reportEscaping(operand: Operand) {
171171
log(" escaping error: \(operand.instruction)")
172-
reportError(operand: operand, diagID: .lifetime_outside_scope_escape)
172+
reportError(escapingValue: operand.value, user: operand.instruction, diagID: .lifetime_outside_scope_escape)
173+
}
174+
175+
func reportEscaping(value: Value, user: Instruction) {
176+
log(" escaping error: \(value) at \(user)")
177+
reportError(escapingValue: value, user: user, diagID: .lifetime_outside_scope_escape)
173178
}
174179

175180
func reportUnknown(operand: Operand) {
@@ -258,15 +263,15 @@ private struct DiagnoseDependence {
258263
return .abortWalk
259264
}
260265

261-
func reportError(operand: Operand, diagID: DiagID) {
266+
func reportError(escapingValue: Value, user: Instruction, diagID: DiagID) {
262267
// If the dependent value is Escapable, then mark_dependence resolution fails, but this is not a diagnostic error.
263268
if dependence.dependentValue.isEscapable {
264269
return
265270
}
266271
onError()
267272

268273
// Identify the escaping variable.
269-
let escapingVar = LifetimeVariable(usedBy: operand, context)
274+
let escapingVar = LifetimeVariable(definedBy: escapingValue, user: user, context)
270275
if let varDecl = escapingVar.varDecl {
271276
// Use the variable location, not the access location.
272277
// Variable names like $return_value and $implicit_value don't have source locations.
@@ -288,7 +293,7 @@ private struct DiagnoseDependence {
288293
diagnoseImplicitFunction()
289294
reportScope()
290295
// Identify the use point.
291-
if let userSourceLoc = operand.instruction.location.sourceLoc {
296+
if let userSourceLoc = user.location.sourceLoc {
292297
diagnose(userSourceLoc, diagID)
293298
}
294299
}
@@ -359,13 +364,12 @@ private struct LifetimeVariable {
359364
return varDecl?.userFacingName
360365
}
361366

362-
init(usedBy operand: Operand, _ context: some Context) {
363-
self = .init(dependent: operand.value, context)
367+
init(definedBy value: Value, user: Instruction, _ context: some Context) {
368+
self = .init(dependent: value, context)
364369
// variable names like $return_value and $implicit_value don't have source locations.
365-
// For @out arguments, the operand's location is the best answer.
370+
// For @out arguments, the user's location is the best answer.
366371
// Otherwise, fall back to the function's location.
367-
self.sourceLoc = self.sourceLoc ?? operand.instruction.location.sourceLoc
368-
?? operand.instruction.parentFunction.location.sourceLoc
372+
self.sourceLoc = self.sourceLoc ?? user.location.sourceLoc ?? user.parentFunction.location.sourceLoc
369373
}
370374

371375
init(definedBy value: Value, _ context: some Context) {

0 commit comments

Comments
 (0)