Skip to content

Commit b169789

Browse files
committed
go/ssa: add position information for switch case conditions
This allows better reporting in passes, such as nilness. Updates golang/go#31008 Change-Id: Ie188844b550bf2b924747f3ac476dd6feeda6d6c Reviewed-on: https://go-review.googlesource.com/c/tools/+/394694 Run-TryBot: Zvonimir Pavlinovic <[email protected]> gopls-CI: kokoro <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Robert Findley <[email protected]> Trust: Zvonimir Pavlinovic <[email protected]>
1 parent 9814b1b commit b169789

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

go/analysis/passes/nilness/testdata/src/a/a.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,26 @@ func f9(x interface {
158158
func unknown() bool {
159159
return false
160160
}
161+
162+
func f10(a interface{}) {
163+
switch a.(type) {
164+
case nil:
165+
return
166+
}
167+
switch a.(type) {
168+
case nil: // want "impossible condition: non-nil == nil"
169+
return
170+
}
171+
}
172+
173+
func f11(a interface{}) {
174+
switch a {
175+
case nil:
176+
return
177+
}
178+
switch a {
179+
case 5,
180+
nil: // want "impossible condition: non-nil == nil"
181+
return
182+
}
183+
}

go/ssa/builder.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1297,7 +1297,7 @@ func (b *builder) switchStmt(fn *Function, s *ast.SwitchStmt, label *lblock) {
12971297
// instead of BinOp(EQL, tag, b.expr(cond))
12981298
// followed by If. Don't forget conversions
12991299
// though.
1300-
cond := emitCompare(fn, token.EQL, tag, b.expr(fn, cond), token.NoPos)
1300+
cond := emitCompare(fn, token.EQL, tag, b.expr(fn, cond), cond.Pos())
13011301
emitIf(fn, cond, body, nextCond)
13021302
fn.currentBlock = nextCond
13031303
}
@@ -1375,7 +1375,6 @@ func (b *builder) typeSwitchStmt(fn *Function, s *ast.TypeSwitchStmt, label *lbl
13751375
// ...SD...
13761376
// goto done
13771377
// .done:
1378-
13791378
if s.Init != nil {
13801379
b.stmt(fn, s.Init)
13811380
}
@@ -1408,7 +1407,7 @@ func (b *builder) typeSwitchStmt(fn *Function, s *ast.TypeSwitchStmt, label *lbl
14081407
casetype = fn.typeOf(cond)
14091408
var condv Value
14101409
if casetype == tUntypedNil {
1411-
condv = emitCompare(fn, token.EQL, x, nilConst(x.Type()), token.NoPos)
1410+
condv = emitCompare(fn, token.EQL, x, nilConst(x.Type()), cond.Pos())
14121411
ti = x
14131412
} else {
14141413
yok := emitTypeTest(fn, x, casetype, cc.Case)

0 commit comments

Comments
 (0)