...
Run Format

Text file src/go/types/testdata/const0.src

Documentation: go/types/testdata

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

View as plain text