github.com/zebozhuang/go@v0.0.0-20200207033046-f8a98f6f5c5d/src/go/types/testdata/const1.src (about)

     1  // Copyright 2012 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  // constant conversions
     6  
     7  package const1
     8  
     9  const(
    10  	mi = ^int(0)
    11  	mu = ^uint(0)
    12  	mp = ^uintptr(0)
    13  
    14  	logSizeofInt     = uint(mi>>8&1 + mi>>16&1 + mi>>32&1)
    15  	logSizeofUint    = uint(mu>>8&1 + mu>>16&1 + mu>>32&1)
    16  	logSizeofUintptr = uint(mp>>8&1 + mp>>16&1 + mp>>32&1)
    17  )
    18  
    19  const (
    20  	minInt8 = -1<<(8<<iota - 1)
    21  	minInt16
    22  	minInt32
    23  	minInt64
    24  	minInt = -1<<(8<<logSizeofInt - 1)
    25  )
    26  
    27  const (
    28  	maxInt8 = 1<<(8<<iota - 1) - 1
    29  	maxInt16
    30  	maxInt32
    31  	maxInt64
    32  	maxInt = 1<<(8<<logSizeofInt - 1) - 1
    33  )
    34  
    35  const (
    36  	maxUint8 = 1<<(8<<iota) - 1
    37  	maxUint16
    38  	maxUint32
    39  	maxUint64
    40  	maxUint    = 1<<(8<<logSizeofUint) - 1
    41  	maxUintptr = 1<<(8<<logSizeofUintptr) - 1
    42  )
    43  
    44  const (
    45  	smallestFloat32 = 1.0 / (1<<(127 - 1 + 23))
    46  	smallestFloat64 = 1.0 / (1<<(1023 - 1 + 52))
    47  )
    48  
    49  const (
    50  	_ = assert(smallestFloat32 > 0)
    51  	_ = assert(smallestFloat64 > 0)
    52  )
    53  
    54  const (
    55  	maxFloat32 = 1<<127 * (1<<24 - 1) / (1.0<<23)
    56  	maxFloat64 = 1<<1023 * (1<<53 - 1) / (1.0<<52)
    57  )
    58  
    59  const (
    60  	_ int8 = minInt8 /* ERROR "overflows" */ - 1
    61  	_ int8 = minInt8
    62  	_ int8 = maxInt8
    63  	_ int8 = maxInt8 /* ERROR "overflows" */ + 1
    64  	_ int8 = smallestFloat64 /* ERROR "truncated" */
    65  
    66  	_ = int8(minInt8 /* ERROR "cannot convert" */ - 1)
    67  	_ = int8(minInt8)
    68  	_ = int8(maxInt8)
    69  	_ = int8(maxInt8 /* ERROR "cannot convert" */ + 1)
    70  	_ = int8(smallestFloat64 /* ERROR "cannot convert" */)
    71  )
    72  
    73  const (
    74  	_ int16 = minInt16 /* ERROR "overflows" */ - 1
    75  	_ int16 = minInt16
    76  	_ int16 = maxInt16
    77  	_ int16 = maxInt16 /* ERROR "overflows" */ + 1
    78  	_ int16 = smallestFloat64 /* ERROR "truncated" */
    79  
    80  	_ = int16(minInt16 /* ERROR "cannot convert" */ - 1)
    81  	_ = int16(minInt16)
    82  	_ = int16(maxInt16)
    83  	_ = int16(maxInt16 /* ERROR "cannot convert" */ + 1)
    84  	_ = int16(smallestFloat64 /* ERROR "cannot convert" */)
    85  )
    86  
    87  const (
    88  	_ int32 = minInt32 /* ERROR "overflows" */ - 1
    89  	_ int32 = minInt32
    90  	_ int32 = maxInt32
    91  	_ int32 = maxInt32 /* ERROR "overflows" */ + 1
    92  	_ int32 = smallestFloat64 /* ERROR "truncated" */
    93  
    94  	_ = int32(minInt32 /* ERROR "cannot convert" */ - 1)
    95  	_ = int32(minInt32)
    96  	_ = int32(maxInt32)
    97  	_ = int32(maxInt32 /* ERROR "cannot convert" */ + 1)
    98  	_ = int32(smallestFloat64 /* ERROR "cannot convert" */)
    99  )
   100  
   101  const (
   102  	_ int64 = minInt64 /* ERROR "overflows" */ - 1
   103  	_ int64 = minInt64
   104  	_ int64 = maxInt64
   105  	_ int64 = maxInt64 /* ERROR "overflows" */ + 1
   106  	_ int64 = smallestFloat64 /* ERROR "truncated" */
   107  
   108  	_ = int64(minInt64 /* ERROR "cannot convert" */ - 1)
   109  	_ = int64(minInt64)
   110  	_ = int64(maxInt64)
   111  	_ = int64(maxInt64 /* ERROR "cannot convert" */ + 1)
   112  	_ = int64(smallestFloat64 /* ERROR "cannot convert" */)
   113  )
   114  
   115  const (
   116  	_ int = minInt /* ERROR "overflows" */ - 1
   117  	_ int = minInt
   118  	_ int = maxInt
   119  	_ int = maxInt /* ERROR "overflows" */ + 1
   120  	_ int = smallestFloat64 /* ERROR "truncated" */
   121  
   122  	_ = int(minInt /* ERROR "cannot convert" */ - 1)
   123  	_ = int(minInt)
   124  	_ = int(maxInt)
   125  	_ = int(maxInt /* ERROR "cannot convert" */ + 1)
   126  	_ = int(smallestFloat64 /* ERROR "cannot convert" */)
   127  )
   128  
   129  const (
   130  	_ uint8 = 0 /* ERROR "overflows" */ - 1
   131  	_ uint8 = 0
   132  	_ uint8 = maxUint8
   133  	_ uint8 = maxUint8 /* ERROR "overflows" */ + 1
   134  	_ uint8 = smallestFloat64 /* ERROR "truncated" */
   135  
   136  	_ = uint8(0 /* ERROR "cannot convert" */ - 1)
   137  	_ = uint8(0)
   138  	_ = uint8(maxUint8)
   139  	_ = uint8(maxUint8 /* ERROR "cannot convert" */ + 1)
   140  	_ = uint8(smallestFloat64 /* ERROR "cannot convert" */)
   141  )
   142  
   143  const (
   144  	_ uint16 = 0 /* ERROR "overflows" */ - 1
   145  	_ uint16 = 0
   146  	_ uint16 = maxUint16
   147  	_ uint16 = maxUint16 /* ERROR "overflows" */ + 1
   148  	_ uint16 = smallestFloat64 /* ERROR "truncated" */
   149  
   150  	_ = uint16(0 /* ERROR "cannot convert" */ - 1)
   151  	_ = uint16(0)
   152  	_ = uint16(maxUint16)
   153  	_ = uint16(maxUint16 /* ERROR "cannot convert" */ + 1)
   154  	_ = uint16(smallestFloat64 /* ERROR "cannot convert" */)
   155  )
   156  
   157  const (
   158  	_ uint32 = 0 /* ERROR "overflows" */ - 1
   159  	_ uint32 = 0
   160  	_ uint32 = maxUint32
   161  	_ uint32 = maxUint32 /* ERROR "overflows" */ + 1
   162  	_ uint32 = smallestFloat64 /* ERROR "truncated" */
   163  
   164  	_ = uint32(0 /* ERROR "cannot convert" */ - 1)
   165  	_ = uint32(0)
   166  	_ = uint32(maxUint32)
   167  	_ = uint32(maxUint32 /* ERROR "cannot convert" */ + 1)
   168  	_ = uint32(smallestFloat64 /* ERROR "cannot convert" */)
   169  )
   170  
   171  const (
   172  	_ uint64 = 0 /* ERROR "overflows" */ - 1
   173  	_ uint64 = 0
   174  	_ uint64 = maxUint64
   175  	_ uint64 = maxUint64 /* ERROR "overflows" */ + 1
   176  	_ uint64 = smallestFloat64 /* ERROR "truncated" */
   177  
   178  	_ = uint64(0 /* ERROR "cannot convert" */ - 1)
   179  	_ = uint64(0)
   180  	_ = uint64(maxUint64)
   181  	_ = uint64(maxUint64 /* ERROR "cannot convert" */ + 1)
   182  	_ = uint64(smallestFloat64 /* ERROR "cannot convert" */)
   183  )
   184  
   185  const (
   186  	_ uint = 0 /* ERROR "overflows" */ - 1
   187  	_ uint = 0
   188  	_ uint = maxUint
   189  	_ uint = maxUint /* ERROR "overflows" */ + 1
   190  	_ uint = smallestFloat64 /* ERROR "truncated" */
   191  
   192  	_ = uint(0 /* ERROR "cannot convert" */ - 1)
   193  	_ = uint(0)
   194  	_ = uint(maxUint)
   195  	_ = uint(maxUint /* ERROR "cannot convert" */ + 1)
   196  	_ = uint(smallestFloat64 /* ERROR "cannot convert" */)
   197  )
   198  
   199  const (
   200  	_ uintptr = 0 /* ERROR "overflows" */ - 1
   201  	_ uintptr = 0
   202  	_ uintptr = maxUintptr
   203  	_ uintptr = maxUintptr /* ERROR "overflows" */ + 1
   204  	_ uintptr = smallestFloat64 /* ERROR "truncated" */
   205  
   206  	_ = uintptr(0 /* ERROR "cannot convert" */ - 1)
   207  	_ = uintptr(0)
   208  	_ = uintptr(maxUintptr)
   209  	_ = uintptr(maxUintptr /* ERROR "cannot convert" */ + 1)
   210  	_ = uintptr(smallestFloat64 /* ERROR "cannot convert" */)
   211  )
   212  
   213  const (
   214  	_ float32 = minInt64
   215  	_ float64 = minInt64
   216  	_ complex64 = minInt64
   217  	_ complex128 = minInt64
   218  
   219  	_ = float32(minInt64)
   220  	_ = float64(minInt64)
   221  	_ = complex64(minInt64)
   222  	_ = complex128(minInt64)
   223  )
   224  
   225  const (
   226  	_ float32 = maxUint64
   227  	_ float64 = maxUint64
   228  	_ complex64 = maxUint64
   229  	_ complex128 = maxUint64
   230  
   231  	_ = float32(maxUint64)
   232  	_ = float64(maxUint64)
   233  	_ = complex64(maxUint64)
   234  	_ = complex128(maxUint64)
   235  )
   236  
   237  // TODO(gri) find smaller deltas below
   238  
   239  const delta32 = maxFloat32/(1 << 23)
   240  
   241  const (
   242  	_ float32 = - /* ERROR "overflow" */ (maxFloat32 + delta32)
   243  	_ float32 = -maxFloat32
   244  	_ float32 = maxFloat32
   245  	_ float32 = maxFloat32 /* ERROR "overflow" */ + delta32
   246  
   247  	_ = float32(- /* ERROR "cannot convert" */ (maxFloat32 + delta32))
   248  	_ = float32(-maxFloat32)
   249  	_ = float32(maxFloat32)
   250  	_ = float32(maxFloat32 /* ERROR "cannot convert" */ + delta32)
   251  
   252  	_ = assert(float32(smallestFloat32) == smallestFloat32)
   253  	_ = assert(float32(smallestFloat32/2) == 0)
   254  	_ = assert(float32(smallestFloat64) == 0)
   255  	_ = assert(float32(smallestFloat64/2) == 0)
   256  )
   257  
   258  const delta64 = maxFloat64/(1 << 52)
   259  
   260  const (
   261  	_ float64 = - /* ERROR "overflow" */ (maxFloat64 + delta64)
   262  	_ float64 = -maxFloat64
   263  	_ float64 = maxFloat64
   264  	_ float64 = maxFloat64 /* ERROR "overflow" */ + delta64
   265  
   266  	_ = float64(- /* ERROR "cannot convert" */ (maxFloat64 + delta64))
   267  	_ = float64(-maxFloat64)
   268  	_ = float64(maxFloat64)
   269  	_ = float64(maxFloat64 /* ERROR "cannot convert" */ + delta64)
   270  
   271  	_ = assert(float64(smallestFloat32) == smallestFloat32)
   272  	_ = assert(float64(smallestFloat32/2) == smallestFloat32/2)
   273  	_ = assert(float64(smallestFloat64) == smallestFloat64)
   274  	_ = assert(float64(smallestFloat64/2) == 0)
   275  )
   276  
   277  const (
   278  	_ complex64 = - /* ERROR "overflow" */ (maxFloat32 + delta32)
   279  	_ complex64 = -maxFloat32
   280  	_ complex64 = maxFloat32
   281  	_ complex64 = maxFloat32 /* ERROR "overflow" */ + delta32
   282  
   283  	_ = complex64(- /* ERROR "cannot convert" */ (maxFloat32 + delta32))
   284  	_ = complex64(-maxFloat32)
   285  	_ = complex64(maxFloat32)
   286  	_ = complex64(maxFloat32 /* ERROR "cannot convert" */ + delta32)
   287  )
   288  
   289  const (
   290  	_ complex128 = - /* ERROR "overflow" */ (maxFloat64 + delta64)
   291  	_ complex128 = -maxFloat64
   292  	_ complex128 = maxFloat64
   293  	_ complex128 = maxFloat64 /* ERROR "overflow" */ + delta64
   294  
   295  	_ = complex128(- /* ERROR "cannot convert" */ (maxFloat64 + delta64))
   296  	_ = complex128(-maxFloat64)
   297  	_ = complex128(maxFloat64)
   298  	_ = complex128(maxFloat64 /* ERROR "cannot convert" */ + delta64)
   299  )
   300  
   301  // Initialization of typed constant and conversion are the same:
   302  const (
   303  	f32 = 1 + smallestFloat32
   304  	x32 float32 = f32
   305  	y32 = float32(f32)
   306  	_ = assert(x32 - y32 == 0)
   307  )
   308  
   309  const (
   310  	f64 = 1 + smallestFloat64
   311  	x64 float64 = f64
   312  	y64 = float64(f64)
   313  	_ = assert(x64 - y64 == 0)
   314  )
   315  
   316  const (
   317  	_ = int8(-1) << 7
   318  	_ = int8 /* ERROR "overflows" */ (-1) << 8
   319  
   320  	_ = uint32(1) << 31
   321  	_ = uint32 /* ERROR "overflows" */ (1) << 32
   322  )