Source file src/internal/types/testdata/spec/range.go

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

View as plain text