github.com/q45/go@v0.0.0-20151101211701-a4fb8c13db3f/src/go/types/testdata/const0.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 declarations
     6  
     7  package const0
     8  
     9  // constants declarations must be initialized by constants
    10  var x = 0
    11  const c0 = x /* ERROR "not constant" */
    12  
    13  // typed constants must have constant types
    14  const _ interface /* ERROR invalid constant type */ {} = 0
    15  
    16  func _ () {
    17  	const _ interface /* ERROR invalid constant type */ {} = 0
    18  	for i := 0; i < 10; i++ {} // don't crash with non-nil iota here
    19  }
    20  
    21  // untyped constants
    22  const (
    23  	// boolean values
    24  	ub0 = false
    25  	ub1 = true
    26  	ub2 = 2 < 1
    27  	ub3 = ui1 == uf1
    28  	ub4 = true /* ERROR "cannot convert" */ == 0
    29  
    30  	// integer values
    31  	ui0 = 0
    32  	ui1 = 1
    33  	ui2 = 42
    34  	ui3 = 3141592653589793238462643383279502884197169399375105820974944592307816406286
    35  	ui4 = -10
    36  
    37  	ui5 = ui0 + ui1
    38  	ui6 = ui1 - ui1
    39  	ui7 = ui2 * ui1
    40  	ui8 = ui3 / ui3
    41  	ui9 = ui3 % ui3
    42  
    43  	ui10 = 1 / 0 /* ERROR "division by zero" */
    44  	ui11 = ui1 / 0 /* ERROR "division by zero" */
    45  	ui12 = ui3 / ui0 /* ERROR "division by zero" */
    46  	ui13 = 1 % 0 /* ERROR "division by zero" */
    47  	ui14 = ui1 % 0 /* ERROR "division by zero" */
    48  	ui15 = ui3 % ui0 /* ERROR "division by zero" */
    49  
    50  	ui16 = ui2 & ui3
    51  	ui17 = ui2 | ui3
    52  	ui18 = ui2 ^ ui3
    53  	ui19 = 1 /* ERROR "invalid operation" */ % 1.0
    54  
    55  	// floating point values
    56  	uf0 = 0.
    57  	uf1 = 1.
    58  	uf2 = 4.2e1
    59  	uf3 = 3.141592653589793238462643383279502884197169399375105820974944592307816406286
    60  	uf4 = 1e-1
    61  
    62  	uf5 = uf0 + uf1
    63  	uf6 = uf1 - uf1
    64  	uf7 = uf2 * uf1
    65  	uf8 = uf3 / uf3
    66  	uf9 = uf3 /* ERROR "not defined" */ % uf3
    67  
    68  	uf10 = 1 / 0 /* ERROR "division by zero" */
    69  	uf11 = uf1 / 0 /* ERROR "division by zero" */
    70  	uf12 = uf3 / uf0 /* ERROR "division by zero" */
    71  
    72  	uf16 = uf2 /* ERROR "not defined" */ & uf3
    73  	uf17 = uf2 /* ERROR "not defined" */ | uf3
    74  	uf18 = uf2 /* ERROR "not defined" */ ^ uf3
    75  
    76  	// complex values
    77  	uc0 = 0.i
    78  	uc1 = 1.i
    79  	uc2 = 4.2e1i
    80  	uc3 = 3.141592653589793238462643383279502884197169399375105820974944592307816406286i
    81  	uc4 = 1e-1i
    82  
    83  	uc5 = uc0 + uc1
    84  	uc6 = uc1 - uc1
    85  	uc7 = uc2 * uc1
    86  	uc8 = uc3 / uc3
    87  	uc9 = uc3 /* ERROR "not defined" */ % uc3
    88  
    89  	uc10 = 1 / 0 /* ERROR "division by zero" */
    90  	uc11 = uc1 / 0 /* ERROR "division by zero" */
    91  	uc12 = uc3 / uc0 /* ERROR "division by zero" */
    92  
    93  	uc16 = uc2 /* ERROR "not defined" */ & uc3
    94  	uc17 = uc2 /* ERROR "not defined" */ | uc3
    95  	uc18 = uc2 /* ERROR "not defined" */ ^ uc3
    96  )
    97  
    98  type (
    99  	mybool bool
   100  	myint int
   101  	myfloat float64
   102  	mycomplex complex128
   103  )
   104  
   105  // typed constants
   106  const (
   107  	// boolean values
   108  	tb0 bool = false
   109  	tb1 bool = true
   110  	tb2 mybool = 2 < 1
   111  	tb3 mybool = ti1 /* ERROR "mismatched types" */ == tf1
   112  
   113  	// integer values
   114  	ti0 int8 = ui0
   115  	ti1 int32 = ui1
   116  	ti2 int64 = ui2
   117  	ti3 myint = ui3 /* ERROR "overflows" */
   118  	ti4 myint = ui4
   119  
   120  	ti5 = ti0 /* ERROR "mismatched types" */ + ti1
   121  	ti6 = ti1 - ti1
   122  	ti7 = ti2 /* ERROR "mismatched types" */ * ti1
   123  	ti8 = ti3 / ti3
   124  	ti9 = ti3 % ti3
   125  
   126  	ti10 = 1 / 0 /* ERROR "division by zero" */
   127  	ti11 = ti1 / 0 /* ERROR "division by zero" */
   128  	ti12 = ti3 /* ERROR "mismatched types" */ / ti0
   129  	ti13 = 1 % 0 /* ERROR "division by zero" */
   130  	ti14 = ti1 % 0 /* ERROR "division by zero" */
   131  	ti15 = ti3 /* ERROR "mismatched types" */ % ti0
   132  
   133  	ti16 = ti2 /* ERROR "mismatched types" */ & ti3
   134  	ti17 = ti2 /* ERROR "mismatched types" */ | ti4
   135  	ti18 = ti2 ^ ti5 // no mismatched types error because the type of ti5 is unknown
   136  
   137  	// floating point values
   138  	tf0 float32 = 0.
   139  	tf1 float32 = 1.
   140  	tf2 float64 = 4.2e1
   141  	tf3 myfloat = 3.141592653589793238462643383279502884197169399375105820974944592307816406286
   142  	tf4 myfloat = 1e-1
   143  
   144  	tf5 = tf0 + tf1
   145  	tf6 = tf1 - tf1
   146  	tf7 = tf2 /* ERROR "mismatched types" */ * tf1
   147  	tf8 = tf3 / tf3
   148  	tf9 = tf3 /* ERROR "not defined" */ % tf3
   149  
   150  	tf10 = 1 / 0 /* ERROR "division by zero" */
   151  	tf11 = tf1 / 0 /* ERROR "division by zero" */
   152  	tf12 = tf3 /* ERROR "mismatched types" */ / tf0
   153  
   154  	tf16 = tf2 /* ERROR "mismatched types" */ & tf3
   155  	tf17 = tf2 /* ERROR "mismatched types" */ | tf3
   156  	tf18 = tf2 /* ERROR "mismatched types" */ ^ tf3
   157  
   158  	// complex values
   159  	tc0 = 0.i
   160  	tc1 = 1.i
   161  	tc2 = 4.2e1i
   162  	tc3 = 3.141592653589793238462643383279502884197169399375105820974944592307816406286i
   163  	tc4 = 1e-1i
   164  
   165  	tc5 = tc0 + tc1
   166  	tc6 = tc1 - tc1
   167  	tc7 = tc2 * tc1
   168  	tc8 = tc3 / tc3
   169  	tc9 = tc3 /* ERROR "not defined" */ % tc3
   170  
   171  	tc10 = 1 / 0 /* ERROR "division by zero" */
   172  	tc11 = tc1 / 0 /* ERROR "division by zero" */
   173  	tc12 = tc3 / tc0 /* ERROR "division by zero" */
   174  
   175  	tc16 = tc2 /* ERROR "not defined" */ & tc3
   176  	tc17 = tc2 /* ERROR "not defined" */ | tc3
   177  	tc18 = tc2 /* ERROR "not defined" */ ^ tc3
   178  )
   179  
   180  // initialization cycles
   181  const (
   182  	a /* ERROR "initialization cycle" */ = a
   183  	b /* ERROR "initialization cycle" */ , c /* ERROR "initialization cycle" */, d, e = e, d, c, b // TODO(gri) should only have one cycle error
   184  	f float64 = d
   185  )
   186  
   187  // multiple initialization
   188  const (
   189  	a1, a2, a3 = 7, 3.1415926, "foo"
   190  	b1, b2, b3 = b3, b1, 42
   191  	c1, c2, c3  /* ERROR "missing init expr for c3" */ = 1, 2
   192  	d1, d2, d3 = 1, 2, 3, 4 /* ERROR "extra init expr 4" */
   193  	_p0 = assert(a1 == 7)
   194  	_p1 = assert(a2 == 3.1415926)
   195  	_p2 = assert(a3 == "foo")
   196  	_p3 = assert(b1 == 42)
   197  	_p4 = assert(b2 == 42)
   198  	_p5 = assert(b3 == 42)
   199  )
   200  
   201  func _() {
   202  	const (
   203  		a1, a2, a3 = 7, 3.1415926, "foo"
   204  		b1, b2, b3 = b3, b1, 42
   205  		c1, c2, c3  /* ERROR "missing init expr for c3" */ = 1, 2
   206  		d1, d2, d3 = 1, 2, 3, 4 /* ERROR "extra init expr 4" */
   207  		_p0 = assert(a1 == 7)
   208  		_p1 = assert(a2 == 3.1415926)
   209  		_p2 = assert(a3 == "foo")
   210  		_p3 = assert(b1 == 42)
   211  		_p4 = assert(b2 == 42)
   212  		_p5 = assert(b3 == 42)
   213  	)
   214  }
   215  
   216  // iota
   217  const (
   218  	iota0 = iota
   219  	iota1 = iota
   220  	iota2 = iota*2
   221  	_a0 = assert(iota0 == 0)
   222  	_a1 = assert(iota1 == 1)
   223  	_a2 = assert(iota2 == 4)
   224  	iota6 = iota*3
   225  
   226  	iota7
   227  	iota8
   228  	_a3 = assert(iota7 == 21)
   229  	_a4 = assert(iota8 == 24)
   230  )
   231  
   232  const (
   233  	_b0 = iota
   234  	_b1 = assert(iota + iota2 == 5)
   235  	_b2 = len([iota]int{}) // iota may appear in a type!
   236  	_b3 = assert(_b2 == 2)
   237  	_b4 = len(A{})
   238  )
   239  
   240  type A [iota /* ERROR "cannot use iota" */ ]int
   241  
   242  // constant expressions with operands accross different
   243  // constant declarations must use the right iota values
   244  const (
   245  	_c0 = iota
   246  	_c1
   247  	_c2
   248  	_x = _c2 + _d1 + _e0 // 3
   249  )
   250  
   251  const (
   252  	_d0 = iota
   253  	_d1
   254  )
   255  
   256  const (
   257  	_e0 = iota
   258  )
   259  
   260  var _ = assert(_x == 3)
   261  
   262  // special cases
   263  const (
   264  	_n0 = nil /* ERROR "not constant" */
   265  	_n1 = [ /* ERROR "not constant" */ ]int{}
   266  )
   267  
   268  // iotas must not be usable in expressions outside constant declarations
   269  type _ [iota /* ERROR "iota outside constant decl" */ ]byte
   270  var _ = iota /* ERROR "iota outside constant decl" */
   271  func _() {
   272  	_ = iota /* ERROR "iota outside constant decl" */
   273  	const _ = iota
   274  	_ = iota /* ERROR "iota outside constant decl" */
   275  }
   276  
   277  func _() {
   278  	iota := 123
   279  	const x = iota /* ERROR "is not constant" */
   280  	var y = iota
   281  	_ = y
   282  }