1
0
mirror of https://github.com/golang/go synced 2024-11-18 14:14:46 -07:00

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
This commit is contained in:
Robert Griesemer 2014-06-11 15:03:38 -07:00
parent 30166088e4
commit 0fa48054ca
5 changed files with 46 additions and 5 deletions

View File

@ -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:

View File

@ -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`,

View File

@ -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

View File

@ -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

View File

@ -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)