Source file src/internal/types/testdata/spec/range_int.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  // This is a subset of the tests in range.go for range over integers,
     6  // with extra tests, and without the need for -goexperiment=range.
     7  
     8  package p
     9  
    10  // test framework assumes 64-bit int/uint sizes by default
    11  const (
    12  	maxInt  = 1<<63 - 1
    13  	maxUint = 1<<64 - 1
    14  )
    15  
    16  type MyInt int32
    17  
    18  func _() {
    19  	for range -1 {
    20  	}
    21  	for range 0 {
    22  	}
    23  	for range 1 {
    24  	}
    25  	for range uint8(1) {
    26  	}
    27  	for range int64(1) {
    28  	}
    29  	for range MyInt(1) {
    30  	}
    31  	for range 'x' {
    32  	}
    33  	for range 1.0 /* ERROR "cannot range over 1.0 (untyped float constant 1)" */ {
    34  	}
    35  
    36  	var i int
    37  	var mi MyInt
    38  	for i := range 10 {
    39  		_ = i
    40  	}
    41  	for i = range 10 {
    42  		_ = i
    43  	}
    44  	for i, j /* ERROR "range over 10 (untyped int constant) permits only one iteration variable" */ := range 10 {
    45  		_, _ = i, j
    46  	}
    47  	for i = range MyInt /* ERROR "cannot use MyInt(10) (constant 10 of type MyInt) as int value in range clause" */ (10) {
    48  		_ = i
    49  	}
    50  	for mi := range MyInt(10) {
    51  		_ = mi
    52  	}
    53  	for mi = range MyInt(10) {
    54  		_ = mi
    55  	}
    56  }
    57  
    58  func _[T int | string](x T) {
    59  	for range x /* ERROR "cannot range over x (variable of type T constrained by int | string): no core type" */ {
    60  	}
    61  }
    62  
    63  func _[T int | int64](x T) {
    64  	for range x /* ERROR "cannot range over x (variable of type T constrained by int | int64): no core type" */ {
    65  	}
    66  }
    67  
    68  func _[T ~int](x T) {
    69  	for range x { // ok
    70  	}
    71  }
    72  
    73  func issue65133() {
    74  	for range maxInt {
    75  	}
    76  	for range maxInt /* ERROR "cannot use maxInt + 1 (untyped int constant 9223372036854775808) as int value in range clause (overflows)" */ + 1 {
    77  	}
    78  	for range maxUint /* ERROR "cannot use maxUint (untyped int constant 18446744073709551615) as int value in range clause (overflows)" */ {
    79  	}
    80  
    81  	for i := range maxInt {
    82  		_ = i
    83  	}
    84  	for i := range maxInt /* ERROR "cannot use maxInt + 1 (untyped int constant 9223372036854775808) as int value in range clause (overflows)" */ + 1 {
    85  		_ = i
    86  	}
    87  	for i := range maxUint /* ERROR "cannot use maxUint (untyped int constant 18446744073709551615) as int value in range clause (overflows)" */ {
    88  		_ = i
    89  	}
    90  
    91  	var i int
    92  	_ = i
    93  	for i = range maxInt {
    94  	}
    95  	for i = range maxInt /* ERROR "cannot use maxInt + 1 (untyped int constant 9223372036854775808) as int value in range clause (overflows)" */ + 1 {
    96  	}
    97  	for i = range maxUint /* ERROR "cannot use maxUint (untyped int constant 18446744073709551615) as int value in range clause (overflows)" */ {
    98  	}
    99  
   100  	var j uint
   101  	_ = j
   102  	for j = range maxInt {
   103  	}
   104  	for j = range maxInt + 1 {
   105  	}
   106  	for j = range maxUint {
   107  	}
   108  	for j = range maxUint /* ERROR "cannot use maxUint + 1 (untyped int constant 18446744073709551616) as uint value in range clause (overflows)" */ + 1 {
   109  	}
   110  
   111  	for range 256 {
   112  	}
   113  	for _ = range 256 {
   114  	}
   115  	for i = range 256 {
   116  	}
   117  	for i := range 256 {
   118  		_ = i
   119  	}
   120  
   121  	var u8 uint8
   122  	_ = u8
   123  	for u8 = range - /* ERROR "cannot use -1 (untyped int constant) as uint8 value in range clause (overflows)" */ 1 {
   124  	}
   125  	for u8 = range 0 {
   126  	}
   127  	for u8 = range 255 {
   128  	}
   129  	for u8 = range 256 /* ERROR "cannot use 256 (untyped int constant) as uint8 value in range clause (overflows)" */ {
   130  	}
   131  }
   132  
   133  func issue64471() {
   134  	for i := range 'a' {
   135  		var _ *rune = &i // ensure i has type rune
   136  	}
   137  }
   138  
   139  func issue66561() {
   140  	for range 10.0 /* ERROR "cannot range over 10.0 (untyped float constant 10)" */ {
   141  	}
   142  	for range 1e3 /* ERROR "cannot range over 1e3 (untyped float constant 1000)" */ {
   143  	}
   144  	for range 1 /* ERROR "cannot range over 1 + 0i (untyped complex constant (1 + 0i))" */ + 0i {
   145  	}
   146  
   147  	for range 1.1 /* ERROR "cannot range over 1.1 (untyped float constant)" */ {
   148  	}
   149  	for range 1i /* ERROR "cannot range over 1i (untyped complex constant (0 + 1i))" */ {
   150  	}
   151  
   152  	for i := range 10.0 /* ERROR "cannot range over 10.0 (untyped float constant 10)" */ {
   153  		_ = i
   154  	}
   155  	for i := range 1e3 /* ERROR "cannot range over 1e3 (untyped float constant 1000)" */ {
   156  		_ = i
   157  	}
   158  	for i := range 1 /* ERROR "cannot range over 1 + 0i (untyped complex constant (1 + 0i))" */ + 0i {
   159  		_ = i
   160  	}
   161  
   162  	for i := range 1.1 /* ERROR "cannot range over 1.1 (untyped float constant)" */ {
   163  		_ = i
   164  	}
   165  	for i := range 1i /* ERROR "cannot range over 1i (untyped complex constant (0 + 1i))" */ {
   166  		_ = i
   167  	}
   168  
   169  	var j float64
   170  	_ = j
   171  	for j /* ERROR "cannot use iteration variable of type float64" */ = range 1 {
   172  	}
   173  	for j = range 1.1 /* ERROR "cannot range over 1.1 (untyped float constant)" */ {
   174  	}
   175  	for j = range 10.0 /* ERROR "cannot range over 10.0 (untyped float constant 10)" */ {
   176  	}
   177  
   178  	// There shouldn't be assignment errors if there are more iteration variables than permitted.
   179  	var i int
   180  	_ = i
   181  	for i, j /* ERROR "range over 10 (untyped int constant) permits only one iteration variable" */ = range 10 {
   182  	}
   183  }
   184  
   185  func issue67027() {
   186  	var i float64
   187  	_ = i
   188  	for i /* ERROR "cannot use iteration variable of type float64" */ = range 10 {
   189  	}
   190  }
   191  

View as plain text