Skip to content

Commit bdddaeb

Browse files
Uzair Nawazgvisor-bot
authored andcommitted
[checklocks] Use function line info for error messages when return instruction has no line numbers attached
Resolves line information issues described by #11203 For functions that don't include explicit return statements in the Go source code, the ssa package inserts return instructions without line information attached, leading to poor error messages. Ex: -: return with unexpected locks held (locks: &({param:f}.mu) exclusively) Quick fix is to instead use the line number information of the parent function instead. FUTURE_COPYBARA_INTEGRATE_REVIEW=#12347 from uzairnawaz:return-parent-line-numbers 9a04865 PiperOrigin-RevId: 842286019
1 parent 561ff6d commit bdddaeb

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

tools/checklocks/analysis.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -731,17 +731,25 @@ func (pc *passContext) checkBasicBlock(fn *ssa.Function, block *ssa.BasicBlock,
731731
rv, rls = pc.checkInstruction(inst, lff, ls)
732732
if rls != nil {
733733
failed := false
734+
735+
// if there is no line information attached to the return
736+
// instruction, use the line info of the function itself
737+
pos := rv.Pos()
738+
if pos == token.NoPos {
739+
pos = rv.Parent().Pos()
740+
}
741+
734742
// Validate held locks.
735743
for fieldName, fg := range lff.HeldOnExit {
736744
r := fg.Resolver.resolveStatic(pc, ls, fn, rv)
737745
if !r.valid() {
738746
// This cannot be forced, since we have no reference.
739-
pc.maybeFail(rv.Pos(), "lock %s cannot be resolved", fieldName)
747+
pc.maybeFail(pos, "lock %s cannot be resolved", fieldName)
740748
continue
741749
}
742750
if s, ok := rls.isHeld(r, fg.Exclusive); !ok {
743751
if _, ok := pc.forced[pc.positionKey(rv.Pos())]; !ok && !lff.Ignore {
744-
pc.maybeFail(rv.Pos(), "lock %s (%s) not held %s (locks: %s)", fieldName, s, exclusiveStr(fg.Exclusive), rls.String())
752+
pc.maybeFail(pos, "lock %s (%s) not held %s (locks: %s)", fieldName, s, exclusiveStr(fg.Exclusive), rls.String())
745753
failed = true
746754
} else {
747755
// Force the lock to be acquired.
@@ -751,7 +759,7 @@ func (pc *passContext) checkBasicBlock(fn *ssa.Function, block *ssa.BasicBlock,
751759
}
752760
// Check for other locks, but only if the above didn't trip.
753761
if !failed && rls.count() != len(lff.HeldOnExit) && !lff.Ignore {
754-
pc.maybeFail(rv.Pos(), "return with unexpected locks held (locks: %s)", rls.String())
762+
pc.maybeFail(pos, "return with unexpected locks held (locks: %s)", rls.String())
755763
}
756764
}
757765
}

0 commit comments

Comments
 (0)