go.tools/go/types: record correct type for parenthesized x.(T) in comma-ok expressions

Fixes golang/go#6796.

R=adonovan
CC=golang-dev
https://golang.org/cl/30070044
This commit is contained in:
Robert Griesemer 2013-11-21 09:11:57 -08:00
parent f3faf8b6e0
commit ef434a14da
2 changed files with 34 additions and 6 deletions

View File

@ -62,6 +62,26 @@ func TestCommaOkTypes(t *testing.T) {
`<-c`,
`(string, bool)`,
},
{`package issue6796_a; var x interface{}; var _, _ = (x.(int))`,
`x.(int)`,
`(int, bool)`,
},
{`package issue6796_b; var c chan string; var _, _ = (<-c)`,
`(<-c)`,
`(string, bool)`,
},
{`package issue6796_c; var c chan string; var _, _ = (<-c)`,
`<-c`,
`(string, bool)`,
},
{`package issue6796_d; var c chan string; var _, _ = ((<-c))`,
`(<-c)`,
`(string, bool)`,
},
{`package issue6796_e; func f(c chan string) { _, _ = ((<-c)) }`,
`(<-c)`,
`(string, bool)`,
},
}
for _, test := range tests {

View File

@ -124,12 +124,20 @@ func (check *checker) recordBuiltinType(f ast.Expr, sig *Signature) {
func (check *checker) recordCommaOkTypes(x ast.Expr, t1, t2 Type) {
assert(x != nil && isTyped(t1) && isTyped(t2) && isBoolean(t2))
if m := check.Types; m != nil {
assert(m[x] != nil) // should have been recorded already
pos := x.Pos()
m[x] = NewTuple(
NewVar(pos, check.pkg, "", t1),
NewVar(pos, check.pkg, "", t2),
)
for {
assert(m[x] != nil) // should have been recorded already
pos := x.Pos()
m[x] = NewTuple(
NewVar(pos, check.pkg, "", t1),
NewVar(pos, check.pkg, "", t2),
)
// if x is a parenthesized expression (p.X), update p.X
p, _ := x.(*ast.ParenExpr)
if p == nil {
break
}
x = p.X
}
}
}