1
0
mirror of https://github.com/golang/go synced 2024-11-22 03:44:39 -07:00

go/parser: fix type switch scoping

Introduce extra scope for the variable declared by a
TypeSwitchGuard so that it doesn't conflict with vars
declared by the initial SimpleStmt of a type switch.

This is a replacement for CL 4896053 which caused
a build breakage.

Also:
- explicitly detect type switches (as opposed to detecting
  expression switches and then do extra testing for type switches)
- fix all outstanding TODOs in parser.go
- ran all tests

R=rsc
CC=golang-dev
https://golang.org/cl/4914044
This commit is contained in:
Robert Griesemer 2011-08-23 09:22:41 -07:00
parent f0ef4f4746
commit f6d536bea4
3 changed files with 61 additions and 28 deletions

View File

@ -4,3 +4,20 @@ func _() {
// the scope of a local type declaration starts immediately after the type name // the scope of a local type declaration starts immediately after the type name
type T struct{ _ *T } type T struct{ _ *T }
} }
func _(x interface{}) {
// the variable defined by a TypeSwitchGuard is declared in each TypeCaseClause
switch t := x.(type) {
case int:
_ = t
case float32:
_ = t
default:
_ = t
}
// the variable defined by a TypeSwitchGuard must not conflict with other
// variables declared in the initial simple statement
switch t := 0; t := x.(type) {
}
}

View File

@ -587,7 +587,6 @@ func (p *parser) parseStructType() *ast.StructType {
} }
rbrace := p.expect(token.RBRACE) rbrace := p.expect(token.RBRACE)
// TODO(gri): store struct scope in AST
return &ast.StructType{pos, &ast.FieldList{lbrace, list, rbrace}, false} return &ast.StructType{pos, &ast.FieldList{lbrace, list, rbrace}, false}
} }
@ -800,7 +799,6 @@ func (p *parser) parseInterfaceType() *ast.InterfaceType {
} }
rbrace := p.expect(token.RBRACE) rbrace := p.expect(token.RBRACE)
// TODO(gri): store interface scope in AST
return &ast.InterfaceType{pos, &ast.FieldList{lbrace, list, rbrace}, false} return &ast.InterfaceType{pos, &ast.FieldList{lbrace, list, rbrace}, false}
} }
@ -1436,14 +1434,14 @@ func (p *parser) parseSimpleStmt(mode int) (ast.Stmt, bool) {
case token.ARROW: case token.ARROW:
// send statement // send statement
arrow := p.pos arrow := p.pos
p.next() // consume "<-" p.next()
y := p.parseRhs() y := p.parseRhs()
return &ast.SendStmt{x[0], arrow, y}, false return &ast.SendStmt{x[0], arrow, y}, false
case token.INC, token.DEC: case token.INC, token.DEC:
// increment or decrement // increment or decrement
s := &ast.IncDecStmt{x[0], p.pos, p.tok} s := &ast.IncDecStmt{x[0], p.pos, p.tok}
p.next() // consume "++" or "--" p.next()
return s, false return s, false
} }
@ -1591,7 +1589,7 @@ func (p *parser) parseTypeList() (list []ast.Expr) {
return return
} }
func (p *parser) parseCaseClause(exprSwitch bool) *ast.CaseClause { func (p *parser) parseCaseClause(typeSwitch bool) *ast.CaseClause {
if p.trace { if p.trace {
defer un(trace(p, "CaseClause")) defer un(trace(p, "CaseClause"))
} }
@ -1600,10 +1598,10 @@ func (p *parser) parseCaseClause(exprSwitch bool) *ast.CaseClause {
var list []ast.Expr var list []ast.Expr
if p.tok == token.CASE { if p.tok == token.CASE {
p.next() p.next()
if exprSwitch { if typeSwitch {
list = p.parseRhsList()
} else {
list = p.parseTypeList() list = p.parseTypeList()
} else {
list = p.parseRhsList()
} }
} else { } else {
p.expect(token.DEFAULT) p.expect(token.DEFAULT)
@ -1617,15 +1615,19 @@ func (p *parser) parseCaseClause(exprSwitch bool) *ast.CaseClause {
return &ast.CaseClause{pos, list, colon, body} return &ast.CaseClause{pos, list, colon, body}
} }
func isExprSwitch(s ast.Stmt) bool { func isTypeSwitchAssert(x ast.Expr) bool {
if s == nil { a, ok := x.(*ast.TypeAssertExpr)
return true return ok && a.Type == nil
} }
if e, ok := s.(*ast.ExprStmt); ok {
if a, ok := e.X.(*ast.TypeAssertExpr); ok { func isTypeSwitchGuard(s ast.Stmt) bool {
return a.Type != nil // regular type assertion switch t := s.(type) {
} case *ast.ExprStmt:
return true // x.(nil)
return isTypeSwitchAssert(t.X)
case *ast.AssignStmt:
// v := x.(nil)
return len(t.Lhs) == 1 && t.Tok == token.DEFINE && len(t.Rhs) == 1 && isTypeSwitchAssert(t.Rhs[0])
} }
return false return false
} }
@ -1651,28 +1653,41 @@ func (p *parser) parseSwitchStmt() ast.Stmt {
s1 = s2 s1 = s2
s2 = nil s2 = nil
if p.tok != token.LBRACE { if p.tok != token.LBRACE {
// A TypeSwitchGuard may declare a variable in addition
// to the variable declared in the initial SimpleStmt.
// Introduce extra scope to avoid redeclaration errors:
//
// switch t := 0; t := x.(T) { ... }
//
// (this code is not valid Go because the first t will
// cannot be accessed and thus is never used, the extra
// scope is needed for the correct error message).
//
// If we don't have a type switch, s2 must be an expression.
// Having the extra nested but empty scope won't affect it.
p.openScope()
defer p.closeScope()
s2, _ = p.parseSimpleStmt(basic) s2, _ = p.parseSimpleStmt(basic)
} }
} }
p.exprLev = prevLev p.exprLev = prevLev
} }
exprSwitch := isExprSwitch(s2) typeSwitch := isTypeSwitchGuard(s2)
lbrace := p.expect(token.LBRACE) lbrace := p.expect(token.LBRACE)
var list []ast.Stmt var list []ast.Stmt
for p.tok == token.CASE || p.tok == token.DEFAULT { for p.tok == token.CASE || p.tok == token.DEFAULT {
list = append(list, p.parseCaseClause(exprSwitch)) list = append(list, p.parseCaseClause(typeSwitch))
} }
rbrace := p.expect(token.RBRACE) rbrace := p.expect(token.RBRACE)
p.expectSemi() p.expectSemi()
body := &ast.BlockStmt{lbrace, list, rbrace} body := &ast.BlockStmt{lbrace, list, rbrace}
if exprSwitch { if typeSwitch {
return &ast.SwitchStmt{pos, s1, p.makeExpr(s2), body} return &ast.TypeSwitchStmt{pos, s1, s2, body}
} }
// type switch
// TODO(gri): do all the checks! return &ast.SwitchStmt{pos, s1, p.makeExpr(s2), body}
return &ast.TypeSwitchStmt{pos, s1, s2, body}
} }
func (p *parser) parseCommClause() *ast.CommClause { func (p *parser) parseCommClause() *ast.CommClause {
@ -2001,14 +2016,12 @@ func (p *parser) parseReceiver(scope *ast.Scope) *ast.FieldList {
defer un(trace(p, "Receiver")) defer un(trace(p, "Receiver"))
} }
pos := p.pos
par := p.parseParameters(scope, false) par := p.parseParameters(scope, false)
// must have exactly one receiver // must have exactly one receiver
if par.NumFields() != 1 { if par.NumFields() != 1 {
p.errorExpected(pos, "exactly one receiver") p.errorExpected(par.Opening, "exactly one receiver")
// TODO determine a better range for BadExpr below par.List = []*ast.Field{&ast.Field{Type: &ast.BadExpr{par.Opening, par.Closing + 1}}}
par.List = []*ast.Field{&ast.Field{Type: &ast.BadExpr{pos, pos}}}
return par return par
} }

View File

@ -26,6 +26,9 @@ var illegalInputs = []interface{}{
`package p; func f() { for _ = range x ; ; {} };`, `package p; func f() { for _ = range x ; ; {} };`,
`package p; func f() { for ; ; _ = range x {} };`, `package p; func f() { for ; ; _ = range x {} };`,
`package p; func f() { for ; _ = range x ; {} };`, `package p; func f() { for ; _ = range x ; {} };`,
`package p; func f() { switch t = t.(type) {} };`,
`package p; func f() { switch t, t = t.(type) {} };`,
`package p; func f() { switch t = t.(type), t {} };`,
`package p; var a = [1]int; /* illegal expression */`, `package p; var a = [1]int; /* illegal expression */`,
`package p; var a = [...]int; /* illegal expression */`, `package p; var a = [...]int; /* illegal expression */`,
`package p; var a = struct{} /* illegal expression */`, `package p; var a = struct{} /* illegal expression */`,