github.com/go-asm/go@v1.21.1-0.20240213172139-40c5ead50c48/types/testdata/spec/range_int.go (about) 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 }