From 0fa48054cad1f1d57035389e767bed01bcd4a872 Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Wed, 11 Jun 2014 15:03:38 -0700 Subject: [PATCH] go.tools/go/types: comma-ok expressions return bool rather than untyped bool Per the current spec. Fixes golang/go#8188. LGTM=adonovan R=adonovan CC=golang-codereviews https://golang.org/cl/101200043 --- go/ssa/interp/testdata/coverage.go | 15 +++++++++++++-- go/types/api_test.go | 10 ++++++++-- go/types/call.go | 2 +- go/types/testdata/expr0.src | 6 ++++++ go/types/testdata/expr3.src | 18 ++++++++++++++++++ 5 files changed, 46 insertions(+), 5 deletions(-) diff --git a/go/ssa/interp/testdata/coverage.go b/go/ssa/interp/testdata/coverage.go index 4f7ea77f..ca6b1748 100644 --- a/go/ssa/interp/testdata/coverage.go +++ b/go/ssa/interp/testdata/coverage.go @@ -597,10 +597,21 @@ func init() { // select to a simple receive statement. } + // TODO(adonovan, gri) enable again once we accept issue 8189. // value,ok-form receive where TypeOf(ok) is a named boolean. - type mybool bool + // type mybool bool + // var x int + // var y mybool + // select { + // case x, y = <-ch: + // default: + // // The default case disables the simplification of + // // select to a simple receive statement. + // } + + // TODO(adonovan, gri) remove once we accept issue 8189. var x int - var y mybool + var y bool select { case x, y = <-ch: default: diff --git a/go/types/api_test.go b/go/types/api_test.go index c8847e39..1abcb973 100644 --- a/go/types/api_test.go +++ b/go/types/api_test.go @@ -167,9 +167,15 @@ func TestTypesInfo(t *testing.T) { `x.(int)`, `(int, bool)`, }, - {`package p2; type mybool bool; var m map[string]complex128; var b mybool; func _() { _, b = m["foo"] }`, + // TODO(gri): uncomment if we accept issue 8189. + // {`package p2; type mybool bool; var m map[string]complex128; var b mybool; func _() { _, b = m["foo"] }`, + // `m["foo"]`, + // `(complex128, p2.mybool)`, + // }, + // TODO(gri): remove if we accept issue 8189. + {`package p2; var m map[string]complex128; var b bool; func _() { _, b = m["foo"] }`, `m["foo"]`, - `(complex128, p2.mybool)`, + `(complex128, bool)`, }, {`package p3; var c chan string; var _, _ = <-c`, `<-c`, diff --git a/go/types/call.go b/go/types/call.go index 1bf73a3a..b044295e 100644 --- a/go/types/call.go +++ b/go/types/call.go @@ -140,7 +140,7 @@ func unpack(get getter, n int, allowCommaOk bool) (getter, int, bool) { if x0.mode == mapindex || x0.mode == commaok { // comma-ok value if allowCommaOk { - a := [2]Type{x0.typ, Typ[UntypedBool]} + a := [2]Type{x0.typ, Typ[Bool]} return func(x *operand, i int) { x.mode = value x.expr = x0.expr diff --git a/go/types/testdata/expr0.src b/go/types/testdata/expr0.src index 672665de..9a2c9933 100644 --- a/go/types/testdata/expr0.src +++ b/go/types/testdata/expr0.src @@ -6,6 +6,8 @@ package expr0 +type mybool bool + var ( // bool b0 = true @@ -133,6 +135,10 @@ var ( ch7 = <-ch ch8 = <-rc ch9 = <-sc /* ERROR "cannot receive" */ + ch10, ok = <-ch + // ok is of type bool + ch11, myok = <-ch + _ mybool = myok /* ERROR "cannot initialize" */ ) // address of composite literals diff --git a/go/types/testdata/expr3.src b/go/types/testdata/expr3.src index 8220364c..abed361b 100644 --- a/go/types/testdata/expr3.src +++ b/go/types/testdata/expr3.src @@ -91,6 +91,17 @@ func indexes() { _ = s[10:0:0] /* ERROR "invalid slice indices" */ _ = &s /* ERROR "cannot take address" */ [:10] + var m map[string]int + _ = m[0 /* ERROR "cannot convert" */ ] + _ = m /* ERROR "cannot slice" */ ["foo" : "bar"] + _ = m["foo"] + // ok is of type bool + type mybool bool + var ok mybool + _, ok = m /* ERROR "cannot assign" */ ["bar"] + _ = ok + + var t string _ = t[- /* ERROR "negative" */ 1] _ = t[- /* ERROR "negative" */ 1 :] @@ -348,6 +359,8 @@ type T2 struct{} func (T2) m(int) {} +type mybool bool + func type_asserts() { var x int _ = x /* ERROR "not an interface" */ .(int) @@ -357,6 +370,11 @@ func type_asserts() { x, ok = e.(int) _ = ok + // ok value is of type bool + var myok mybool + _, myok = e /* ERROR "cannot assign" */ .(int) + _ = myok + var t I _ = t /* ERROR "use of .* outside type switch" */ .(type) _ = t /* ERROR "missing method m" */ .(T)