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:
parent
f3faf8b6e0
commit
ef434a14da
|
|
@ -62,6 +62,26 @@ func TestCommaOkTypes(t *testing.T) {
|
||||||
`<-c`,
|
`<-c`,
|
||||||
`(string, bool)`,
|
`(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 {
|
for _, test := range tests {
|
||||||
|
|
|
||||||
|
|
@ -124,12 +124,20 @@ func (check *checker) recordBuiltinType(f ast.Expr, sig *Signature) {
|
||||||
func (check *checker) recordCommaOkTypes(x ast.Expr, t1, t2 Type) {
|
func (check *checker) recordCommaOkTypes(x ast.Expr, t1, t2 Type) {
|
||||||
assert(x != nil && isTyped(t1) && isTyped(t2) && isBoolean(t2))
|
assert(x != nil && isTyped(t1) && isTyped(t2) && isBoolean(t2))
|
||||||
if m := check.Types; m != nil {
|
if m := check.Types; m != nil {
|
||||||
assert(m[x] != nil) // should have been recorded already
|
for {
|
||||||
pos := x.Pos()
|
assert(m[x] != nil) // should have been recorded already
|
||||||
m[x] = NewTuple(
|
pos := x.Pos()
|
||||||
NewVar(pos, check.pkg, "", t1),
|
m[x] = NewTuple(
|
||||||
NewVar(pos, check.pkg, "", t2),
|
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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue