github.com/go-asm/go@v1.21.1-0.20240213172139-40c5ead50c48/types/testdata/spec/range.go (about) 1 // -goexperiment=rangefunc 2 3 // Copyright 2023 The Go Authors. All rights reserved. 4 // Use of this source code is governed by a BSD-style 5 // license that can be found in the LICENSE file. 6 7 package p 8 9 type MyInt int32 10 type MyBool bool 11 type MyString string 12 type MyFunc1 func(func(int) bool) 13 type MyFunc2 func(int) bool 14 type MyFunc3 func(MyFunc2) 15 16 type T struct{} 17 18 func (*T) PM() {} 19 func (T) M() {} 20 21 func f1() {} 22 func f2(func()) {} 23 func f4(func(int) bool) {} 24 func f5(func(int, string) bool) {} 25 func f7(func(int) MyBool) {} 26 func f8(func(MyInt, MyString) MyBool) {} 27 28 func test() { 29 // TODO: Would be nice to 'for range T.M' and 'for range (*T).PM' directly, 30 // but there is no gofmt-friendly way to write the error pattern in the right place. 31 m1 := T.M 32 for range m1 /* ERROR "cannot range over m1 (variable of type func(T)): func must be func(yield func(...) bool): argument is not func" */ { 33 } 34 m2 := (*T).PM 35 for range m2 /* ERROR "cannot range over m2 (variable of type func(*T)): func must be func(yield func(...) bool): argument is not func" */ { 36 } 37 for range f1 /* ERROR "cannot range over f1 (value of type func()): func must be func(yield func(...) bool): wrong argument count" */ { 38 } 39 for range f2 /* ERROR "cannot range over f2 (value of type func(func())): func must be func(yield func(...) bool): yield func does not return bool" */ { 40 } 41 for range f4 /* ERROR "range over f4 (value of type func(func(int) bool)) must have one iteration variable" */ { 42 } 43 for _ = range f4 { 44 } 45 for _, _ = range f5 { 46 } 47 for _ = range f7 { 48 } 49 for _, _ = range f8 { 50 } 51 for range 1 { 52 } 53 for range uint8(1) { 54 } 55 for range int64(1) { 56 } 57 for range MyInt(1) { 58 } 59 for range 'x' { 60 } 61 for range 1.0 /* ERROR "cannot range over 1.0 (untyped float constant 1)" */ { 62 } 63 for _ = range MyFunc1(nil) { 64 } 65 for _ = range MyFunc3(nil) { 66 } 67 for _ = range (func(MyFunc2))(nil) { 68 } 69 70 var i int 71 var s string 72 var mi MyInt 73 var ms MyString 74 for i := range f4 { 75 _ = i 76 } 77 for i = range f4 { 78 _ = i 79 } 80 for i, s := range f5 { 81 _, _ = i, s 82 } 83 for i, s = range f5 { 84 _, _ = i, s 85 } 86 for i, _ := range f5 { 87 _ = i 88 } 89 for i, _ = range f5 { 90 _ = i 91 } 92 for i := range f7 { 93 _ = i 94 } 95 for i = range f7 { 96 _ = i 97 } 98 for mi, _ := range f8 { 99 _ = mi 100 } 101 for mi, _ = range f8 { 102 _ = mi 103 } 104 for mi, ms := range f8 { 105 _, _ = mi, ms 106 } 107 for i /* ERROR "cannot use i (value of type MyInt) as int value in assignment" */, s /* ERROR "cannot use s (value of type MyString) as string value in assignment" */ = range f8 { 108 _, _ = mi, ms 109 } 110 for mi, ms := range f8 { 111 i, s = mi /* ERROR "cannot use mi (variable of type MyInt) as int value in assignment" */, ms /* ERROR "cannot use ms (variable of type MyString) as string value in assignment" */ 112 } 113 for mi, ms = range f8 { 114 _, _ = mi, ms 115 } 116 117 for i := range 10 { 118 _ = i 119 } 120 for i = range 10 { 121 _ = i 122 } 123 for i, j /* ERROR "range over 10 (untyped int constant) permits only one iteration variable" */ := range 10 { 124 _, _ = i, j 125 } 126 for mi := range MyInt(10) { 127 _ = mi 128 } 129 for mi = range MyInt(10) { 130 _ = mi 131 } 132 } 133 134 func _[T int | string](x T) { 135 for range x /* ERROR "cannot range over x (variable of type T constrained by int | string): no core type" */ { 136 } 137 } 138 139 func _[T int | int64](x T) { 140 for range x /* ERROR "cannot range over x (variable of type T constrained by int | int64): no core type" */ { 141 } 142 } 143 144 func _[T ~int](x T) { 145 for range x { // ok 146 } 147 } 148 149 func _[T any](x func(func(T) bool)) { 150 for _ = range x { // ok 151 } 152 } 153 154 func _[T ~func(func(int) bool)](x T) { 155 for _ = range x { // ok 156 } 157 }