1
0
mirror of https://github.com/golang/go synced 2024-11-18 13:04:46 -07:00

go/types: close scope for each "select" case after its body

Classic Go pitfall: "defer" in a loop does not do what you might expect.

+ test case

Fixes issue 9570
Fixes issue 9569

Change-Id: Iec05420872ef71190083a7192f76c92f54f4a2a1
Reviewed-on: https://go-review.googlesource.com/2655
Reviewed-by: Robert Griesemer <gri@golang.org>
This commit is contained in:
Alan Donovan 2015-01-12 12:48:42 -05:00 committed by Robert Griesemer
parent 4ed956ea27
commit 86040b7505
2 changed files with 12 additions and 1 deletions

View File

@ -569,11 +569,11 @@ func (check *Checker) stmt(ctxt stmtContext, s ast.Stmt) {
}
check.openScope(s, "case")
defer check.closeScope()
if clause.Comm != nil {
check.stmt(inner, clause.Comm)
}
check.stmtList(inner, clause.Body)
check.closeScope()
}
case *ast.ForStmt:

View File

@ -215,6 +215,17 @@ func selects() {
case x /* ERROR send or receive */ :
case a /* ERROR send or receive */ := ch:
}
// test for issue 9570: ch2 in second case falsely resolved to
// ch2 declared in body of first case
ch1 := make(chan int)
ch2 := make(chan int)
select {
case <-ch1:
var ch2 /* ERROR ch2 declared but not used */ chan bool
case i := <-ch2:
print(i + 1)
}
}
func gos() {