mirror of
https://github.com/golang/go
synced 2024-11-18 19:24:39 -07:00
go.tools/go/types: declare typeswitch local variable in correct scope
R=adonovan CC=golang-dev https://golang.org/cl/11534044
This commit is contained in:
parent
7bc647b29a
commit
09d04edfcf
@ -381,12 +381,6 @@ func (check *checker) stmt(s ast.Stmt) {
|
||||
return
|
||||
}
|
||||
|
||||
var obj Object
|
||||
if lhs != nil {
|
||||
obj = NewVar(lhs.Pos(), check.pkg, lhs.Name, x.typ)
|
||||
check.declare(check.topScope, lhs, obj)
|
||||
}
|
||||
|
||||
check.multipleDefaults(s.Body.List)
|
||||
for _, s := range s.Body.List {
|
||||
clause, _ := s.(*ast.CaseClause)
|
||||
@ -411,14 +405,19 @@ func (check *checker) stmt(s ast.Stmt) {
|
||||
}
|
||||
}
|
||||
check.openScope(clause)
|
||||
// If lhs exists, declare a corresponding object in the case-local scope if necessary.
|
||||
// If lhs exists, declare a corresponding variable in the case-local scope if necessary.
|
||||
if lhs != nil {
|
||||
// A single-type case clause implicitly declares a new variable shadowing lhs.
|
||||
if len(clause.List) == 1 && typ != nil {
|
||||
obj := NewVar(lhs.Pos(), check.pkg, lhs.Name, typ)
|
||||
check.declare(check.topScope, nil, obj)
|
||||
check.recordImplicit(clause, obj)
|
||||
// spec: "The TypeSwitchGuard may include a short variable declaration.
|
||||
// When that form is used, the variable is declared at the beginning of
|
||||
// the implicit block in each clause. In clauses with a case listing
|
||||
// exactly one type, the variable has that type; otherwise, the variable
|
||||
// has the type of the expression in the TypeSwitchGuard."
|
||||
if len(clause.List) != 1 || typ == nil {
|
||||
typ = x.typ
|
||||
}
|
||||
obj := NewVar(lhs.Pos(), check.pkg, lhs.Name, typ)
|
||||
check.declare(check.topScope, nil, obj)
|
||||
check.recordImplicit(clause, obj)
|
||||
}
|
||||
check.stmtList(clause.Body)
|
||||
check.closeScope()
|
||||
|
14
go/types/testdata/stmt0.src
vendored
14
go/types/testdata/stmt0.src
vendored
@ -229,6 +229,20 @@ func typeswitch0() {
|
||||
}
|
||||
}
|
||||
|
||||
// Test correct scope setup.
|
||||
// (no redeclaration errors expected in the type switch)
|
||||
func typeswitch1() {
|
||||
var t I
|
||||
switch t := t; t := t.(type) {
|
||||
case nil:
|
||||
var _ I = t
|
||||
case T:
|
||||
var _ T = t
|
||||
default:
|
||||
var _ I = t
|
||||
}
|
||||
}
|
||||
|
||||
func rangeloops() {
|
||||
var (
|
||||
x int
|
||||
|
Loading…
Reference in New Issue
Block a user