From ef434a14dad153150ce126026ceab64f0a27f0b7 Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Thu, 21 Nov 2013 09:11:57 -0800 Subject: [PATCH] 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 --- go/types/api_test.go | 20 ++++++++++++++++++++ go/types/check.go | 20 ++++++++++++++------ 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/go/types/api_test.go b/go/types/api_test.go index 39f0554a..d8747188 100644 --- a/go/types/api_test.go +++ b/go/types/api_test.go @@ -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 { diff --git a/go/types/check.go b/go/types/check.go index 39c5811d..86d239c0 100644 --- a/go/types/check.go +++ b/go/types/check.go @@ -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 + } } }