Skip to content

Commit

Permalink
go/ssa: add position information for switch case conditions
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
zpavlinovic committed Mar 23, 2022
1 parent 9814b1b commit b169789
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
23 changes: 23 additions & 0 deletions go/analysis/passes/nilness/testdata/src/a/a.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,26 @@ func f9(x interface {
func unknown() bool {
return false
}

func f10(a interface{}) {
switch a.(type) {
case nil:
return
}
switch a.(type) {
case nil: // want "impossible condition: non-nil == nil"
return
}
}

func f11(a interface{}) {
switch a {
case nil:
return
}
switch a {
case 5,
nil: // want "impossible condition: non-nil == nil"
return
}
}
5 changes: 2 additions & 3 deletions go/ssa/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -1297,7 +1297,7 @@ func (b *builder) switchStmt(fn *Function, s *ast.SwitchStmt, label *lblock) {
// instead of BinOp(EQL, tag, b.expr(cond))
// followed by If. Don't forget conversions
// though.
cond := emitCompare(fn, token.EQL, tag, b.expr(fn, cond), token.NoPos)
cond := emitCompare(fn, token.EQL, tag, b.expr(fn, cond), cond.Pos())
emitIf(fn, cond, body, nextCond)
fn.currentBlock = nextCond
}
Expand Down Expand Up @@ -1375,7 +1375,6 @@ func (b *builder) typeSwitchStmt(fn *Function, s *ast.TypeSwitchStmt, label *lbl
// ...SD...
// goto done
// .done:

if s.Init != nil {
b.stmt(fn, s.Init)
}
Expand Down Expand Up @@ -1408,7 +1407,7 @@ func (b *builder) typeSwitchStmt(fn *Function, s *ast.TypeSwitchStmt, label *lbl
casetype = fn.typeOf(cond)
var condv Value
if casetype == tUntypedNil {
condv = emitCompare(fn, token.EQL, x, nilConst(x.Type()), token.NoPos)
condv = emitCompare(fn, token.EQL, x, nilConst(x.Type()), cond.Pos())
ti = x
} else {
yok := emitTypeTest(fn, x, casetype, cc.Case)
Expand Down

0 comments on commit b169789

Please sign in to comment.