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:
parent
30166088e4
commit
0fa48054ca
|
|
@ -597,10 +597,21 @@ func init() {
|
||||||
// select to a simple receive statement.
|
// 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.
|
// 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 x int
|
||||||
var y mybool
|
var y bool
|
||||||
select {
|
select {
|
||||||
case x, y = <-ch:
|
case x, y = <-ch:
|
||||||
default:
|
default:
|
||||||
|
|
|
||||||
|
|
@ -167,9 +167,15 @@ func TestTypesInfo(t *testing.T) {
|
||||||
`x.(int)`,
|
`x.(int)`,
|
||||||
`(int, bool)`,
|
`(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"]`,
|
`m["foo"]`,
|
||||||
`(complex128, p2.mybool)`,
|
`(complex128, bool)`,
|
||||||
},
|
},
|
||||||
{`package p3; var c chan string; var _, _ = <-c`,
|
{`package p3; var c chan string; var _, _ = <-c`,
|
||||||
`<-c`,
|
`<-c`,
|
||||||
|
|
|
||||||
|
|
@ -140,7 +140,7 @@ func unpack(get getter, n int, allowCommaOk bool) (getter, int, bool) {
|
||||||
if x0.mode == mapindex || x0.mode == commaok {
|
if x0.mode == mapindex || x0.mode == commaok {
|
||||||
// comma-ok value
|
// comma-ok value
|
||||||
if allowCommaOk {
|
if allowCommaOk {
|
||||||
a := [2]Type{x0.typ, Typ[UntypedBool]}
|
a := [2]Type{x0.typ, Typ[Bool]}
|
||||||
return func(x *operand, i int) {
|
return func(x *operand, i int) {
|
||||||
x.mode = value
|
x.mode = value
|
||||||
x.expr = x0.expr
|
x.expr = x0.expr
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,8 @@
|
||||||
|
|
||||||
package expr0
|
package expr0
|
||||||
|
|
||||||
|
type mybool bool
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// bool
|
// bool
|
||||||
b0 = true
|
b0 = true
|
||||||
|
|
@ -133,6 +135,10 @@ var (
|
||||||
ch7 = <-ch
|
ch7 = <-ch
|
||||||
ch8 = <-rc
|
ch8 = <-rc
|
||||||
ch9 = <-sc /* ERROR "cannot receive" */
|
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
|
// address of composite literals
|
||||||
|
|
|
||||||
|
|
@ -91,6 +91,17 @@ func indexes() {
|
||||||
_ = s[10:0:0] /* ERROR "invalid slice indices" */
|
_ = s[10:0:0] /* ERROR "invalid slice indices" */
|
||||||
_ = &s /* ERROR "cannot take address" */ [:10]
|
_ = &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
|
var t string
|
||||||
_ = t[- /* ERROR "negative" */ 1]
|
_ = t[- /* ERROR "negative" */ 1]
|
||||||
_ = t[- /* ERROR "negative" */ 1 :]
|
_ = t[- /* ERROR "negative" */ 1 :]
|
||||||
|
|
@ -348,6 +359,8 @@ type T2 struct{}
|
||||||
|
|
||||||
func (T2) m(int) {}
|
func (T2) m(int) {}
|
||||||
|
|
||||||
|
type mybool bool
|
||||||
|
|
||||||
func type_asserts() {
|
func type_asserts() {
|
||||||
var x int
|
var x int
|
||||||
_ = x /* ERROR "not an interface" */ .(int)
|
_ = x /* ERROR "not an interface" */ .(int)
|
||||||
|
|
@ -357,6 +370,11 @@ func type_asserts() {
|
||||||
x, ok = e.(int)
|
x, ok = e.(int)
|
||||||
_ = ok
|
_ = ok
|
||||||
|
|
||||||
|
// ok value is of type bool
|
||||||
|
var myok mybool
|
||||||
|
_, myok = e /* ERROR "cannot assign" */ .(int)
|
||||||
|
_ = myok
|
||||||
|
|
||||||
var t I
|
var t I
|
||||||
_ = t /* ERROR "use of .* outside type switch" */ .(type)
|
_ = t /* ERROR "use of .* outside type switch" */ .(type)
|
||||||
_ = t /* ERROR "missing method m" */ .(T)
|
_ = t /* ERROR "missing method m" */ .(T)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue