Text file src/go/types/testdata/check/builtins.src

     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  // builtin calls
     6  
     7  package builtins
     8  
     9  import "unsafe"
    10  
    11  func f0() {}
    12  
    13  func append1() {
    14  	var b byte
    15  	var x int
    16  	var s []byte
    17  	_ = append() // ERROR not enough arguments
    18  	_ = append("foo" /* ERROR must be a slice */ )
    19  	_ = append(nil /* ERROR must be a slice */ , s)
    20  	_ = append(x /* ERROR must be a slice */ , s)
    21  	_ = append(s)
    22  	_ = append(s, nil...)
    23  	append /* ERROR not used */ (s)
    24  
    25  	_ = append(s, b)
    26  	_ = append(s, x /* ERROR cannot use x */ )
    27  	_ = append(s, s /* ERROR cannot use s */ )
    28  	_ = append(s...) /* ERROR not enough arguments */
    29  	_ = append(s, b, s /* ERROR too many arguments */ ...)
    30  	_ = append(s, 1, 2, 3)
    31  	_ = append(s, 1, 2, 3, x /* ERROR cannot use x */ , 5, 6, 6)
    32  	_ = append(s, 1, 2 /* ERROR too many arguments */, s...)
    33  	_ = append([]interface{}(nil), 1, 2, "foo", x, 3.1425, false)
    34  
    35  	type S []byte
    36  	type T string
    37  	var t T
    38  	_ = append(s, "foo" /* ERROR cannot use .* in argument to append */ )
    39  	_ = append(s, "foo"...)
    40  	_ = append(S(s), "foo" /* ERROR cannot use .* in argument to append */ )
    41  	_ = append(S(s), "foo"...)
    42  	_ = append(s, t /* ERROR cannot use t */ )
    43  	_ = append(s, t...)
    44  	_ = append(s, T("foo")...)
    45  	_ = append(S(s), t /* ERROR cannot use t */ )
    46  	_ = append(S(s), t...)
    47  	_ = append(S(s), T("foo")...)
    48  	_ = append([]string{}, t /* ERROR cannot use t */ , "foo")
    49  	_ = append([]T{}, t, "foo")
    50  }
    51  
    52  // from the spec
    53  func append2() {
    54  	s0 := []int{0, 0}
    55  	s1 := append(s0, 2)                // append a single element     s1 == []int{0, 0, 2}
    56  	s2 := append(s1, 3, 5, 7)          // append multiple elements    s2 == []int{0, 0, 2, 3, 5, 7}
    57  	s3 := append(s2, s0...)            // append a slice              s3 == []int{0, 0, 2, 3, 5, 7, 0, 0}
    58  	s4 := append(s3[3:6], s3[2:]...)   // append overlapping slice    s4 == []int{3, 5, 7, 2, 3, 5, 7, 0, 0}
    59  
    60  	var t []interface{}
    61  	t = append(t, 42, 3.1415, "foo")   //                             t == []interface{}{42, 3.1415, "foo"}
    62  
    63  	var b []byte
    64  	b = append(b, "bar"...)            // append string contents      b == []byte{'b', 'a', 'r' }
    65  
    66  	_ = s4
    67  }
    68  
    69  func append3() {
    70  	f1 := func() (s []int) { return }
    71  	f2 := func() (s []int, x int) { return }
    72  	f3 := func() (s []int, x, y int) { return }
    73  	f5 := func() (s []interface{}, x int, y float32, z string, b bool) { return }
    74  	ff := func() (int, float32) { return 0, 0 }
    75  	_ = append(f0 /* ERROR used as value */ ())
    76  	_ = append(f1())
    77  	_ = append(f2())
    78  	_ = append(f3())
    79  	_ = append(f5())
    80  	_ = append(ff /* ERROR must be a slice */ ()) // TODO(gri) better error message
    81  }
    82  
    83  func cap1() {
    84  	var a [10]bool
    85  	var p *[20]int
    86  	var c chan string
    87  	_ = cap() // ERROR not enough arguments
    88  	_ = cap(1, 2) // ERROR too many arguments
    89  	_ = cap(42 /* ERROR invalid */)
    90  	const _3 = cap(a)
    91  	assert(_3 == 10)
    92  	const _4 = cap(p)
    93  	assert(_4 == 20)
    94  	_ = cap(c)
    95  	cap /* ERROR not used */ (c)
    96  
    97  	// issue 4744
    98  	type T struct{ a [10]int }
    99  	const _ = cap(((*T)(nil)).a)
   100  
   101  	var s [][]byte
   102  	_ = cap(s)
   103  	_ = cap(s... /* ERROR invalid use of \.\.\. */ )
   104  }
   105  
   106  func cap2() {
   107  	f1a := func() (a [10]int) { return }
   108  	f1s := func() (s []int) { return }
   109  	f2 := func() (s []int, x int) { return }
   110  	_ = cap(f0 /* ERROR used as value */ ())
   111  	_ = cap(f1a())
   112  	_ = cap(f1s())
   113  	_ = cap(f2()) // ERROR too many arguments
   114  }
   115  
   116  // test cases for issue 7387
   117  func cap3() {
   118  	var f = func() int { return 0 }
   119  	var x = f()
   120  	const (
   121  		_ = cap([4]int{})
   122  		_ = cap([4]int{x})
   123  		_ = cap /* ERROR not constant */ ([4]int{f()})
   124  		_ = cap /* ERROR not constant */ ([4]int{cap([]int{})})
   125  		_ = cap([4]int{cap([4]int{})})
   126  	)
   127  	var y float64
   128  	var z complex128
   129  	const (
   130  		_ = cap([4]float64{})
   131  		_ = cap([4]float64{y})
   132  		_ = cap([4]float64{real(2i)})
   133  		_ = cap /* ERROR not constant */ ([4]float64{real(z)})
   134  	)
   135  	var ch chan [10]int
   136  	const (
   137  		_ = cap /* ERROR not constant */ (<-ch)
   138  		_ = cap /* ERROR not constant */ ([4]int{(<-ch)[0]})
   139  	)
   140  }
   141  
   142  func close1() {
   143  	var c chan int
   144  	var r <-chan int
   145  	close() // ERROR not enough arguments
   146  	close(1, 2) // ERROR too many arguments
   147  	close(42 /* ERROR cannot close non-channel */)
   148  	close(r /* ERROR receive-only channel */)
   149  	close(c)
   150  	_ = close /* ERROR used as value */ (c)
   151  
   152  	var s []chan int
   153  	close(s... /* ERROR invalid use of \.\.\. */ )
   154  }
   155  
   156  func close2() {
   157  	f1 := func() (ch chan int) { return }
   158  	f2 := func() (ch chan int, x int) { return }
   159  	close(f0 /* ERROR used as value */ ())
   160  	close(f1())
   161  	close(f2()) // ERROR too many arguments
   162  }
   163  
   164  func complex1() {
   165  	var i32 int32
   166  	var f32 float32
   167  	var f64 float64
   168  	var c64 complex64
   169  	var c128 complex128
   170  	_ = complex() // ERROR not enough arguments
   171  	_ = complex(1) // ERROR not enough arguments
   172  	_ = complex(true /* ERROR mismatched types */ , 0)
   173  	_ = complex(i32 /* ERROR expected floating-point */ , 0)
   174  	_ = complex("foo" /* ERROR mismatched types */ , 0)
   175  	_ = complex(c64 /* ERROR expected floating-point */ , 0)
   176  	_ = complex(0 /* ERROR mismatched types */ , true)
   177  	_ = complex(0 /* ERROR expected floating-point */ , i32)
   178  	_ = complex(0 /* ERROR mismatched types */ , "foo")
   179  	_ = complex(0 /* ERROR expected floating-point */ , c64)
   180  	_ = complex(f32, f32)
   181  	_ = complex(f32, 1)
   182  	_ = complex(f32, 1.0)
   183  	_ = complex(f32, 'a')
   184  	_ = complex(f64, f64)
   185  	_ = complex(f64, 1)
   186  	_ = complex(f64, 1.0)
   187  	_ = complex(f64, 'a')
   188  	_ = complex(f32 /* ERROR mismatched types */ , f64)
   189  	_ = complex(f64 /* ERROR mismatched types */ , f32)
   190  	_ = complex(1, 1)
   191  	_ = complex(1, 1.1)
   192  	_ = complex(1, 'a')
   193  	complex /* ERROR not used */ (1, 2)
   194  
   195  	var _ complex64 = complex(f32, f32)
   196  	var _ complex64 = complex /* ERROR cannot use .* in variable declaration */ (f64, f64)
   197  
   198  	var _ complex128 = complex /* ERROR cannot use .* in variable declaration */ (f32, f32)
   199  	var _ complex128 = complex(f64, f64)
   200  
   201  	// untyped constants
   202  	const _ int = complex(1, 0)
   203  	const _ float32 = complex(1, 0)
   204  	const _ complex64 = complex(1, 0)
   205  	const _ complex128 = complex(1, 0)
   206  	const _ = complex(0i, 0i)
   207  	const _ = complex(0i, 0)
   208  	const _ int = 1.0 + complex(1, 0i)
   209  
   210  	const _ int = complex /* ERROR int */ (1.1, 0)
   211  	const _ float32 = complex /* ERROR float32 */ (1, 2)
   212  
   213  	// untyped values
   214  	var s uint
   215  	_ = complex(1 /* ERROR integer */ <<s, 0)
   216  	const _ = complex /* ERROR not constant */ (1 /* ERROR integer */ <<s, 0)
   217  	var _ int = complex /* ERROR cannot use .* in variable declaration */ (1 /* ERROR integer */ <<s, 0)
   218  
   219  	// floating-point argument types must be identical
   220  	type F32 float32
   221  	type F64 float64
   222  	var x32 F32
   223  	var x64 F64
   224  	c64 = complex(x32, x32)
   225  	_ = complex(x32 /* ERROR mismatched types */ , f32)
   226  	_ = complex(f32 /* ERROR mismatched types */ , x32)
   227  	c128 = complex(x64, x64)
   228  	_ = c128
   229  	_ = complex(x64 /* ERROR mismatched types */ , f64)
   230  	_ = complex(f64 /* ERROR mismatched types */ , x64)
   231  
   232  	var t []float32
   233  	_ = complex(t... /* ERROR invalid use of \.\.\. */ )
   234  }
   235  
   236  func complex2() {
   237  	f1 := func() (x float32) { return }
   238  	f2 := func() (x, y float32) { return }
   239  	f3 := func() (x, y, z float32) { return }
   240  	_ = complex(f0 /* ERROR used as value */ ())
   241  	_ = complex(f1()) // ERROR not enough arguments
   242  	_ = complex(f2())
   243  	_ = complex(f3()) // ERROR too many arguments
   244  }
   245  
   246  func copy1() {
   247  	copy() // ERROR not enough arguments
   248  	copy("foo") // ERROR not enough arguments
   249  	copy([ /* ERROR copy expects slice arguments */ ...]int{}, []int{})
   250  	copy([ /* ERROR copy expects slice arguments */ ]int{}, [...]int{})
   251  	copy([ /* ERROR different element types */ ]int8{}, "foo")
   252  
   253  	// spec examples
   254  	var a = [...]int{0, 1, 2, 3, 4, 5, 6, 7}
   255  	var s = make([]int, 6)
   256  	var b = make([]byte, 5)
   257  	n1 := copy(s, a[0:])            // n1 == 6, s == []int{0, 1, 2, 3, 4, 5}
   258  	n2 := copy(s, s[2:])            // n2 == 4, s == []int{2, 3, 4, 5, 4, 5}
   259  	n3 := copy(b, "Hello, World!")  // n3 == 5, b == []byte("Hello")
   260  	_, _, _ = n1, n2, n3
   261  
   262  	var t [][]int
   263  	copy(t, t)
   264  	copy(t /* ERROR copy expects slice arguments */ , nil)
   265  	copy(nil /* ERROR copy expects slice arguments */ , t)
   266  	copy(nil /* ERROR copy expects slice arguments */ , nil)
   267  	copy(t... /* ERROR invalid use of \.\.\. */ )
   268  }
   269  
   270  func copy2() {
   271  	f1 := func() (a []int) { return }
   272  	f2 := func() (a, b []int) { return }
   273  	f3 := func() (a, b, c []int) { return }
   274  	copy(f0 /* ERROR used as value */ ())
   275  	copy(f1()) // ERROR not enough arguments
   276  	copy(f2())
   277  	copy(f3()) // ERROR too many arguments
   278  }
   279  
   280  func delete1() {
   281  	var m map[string]int
   282  	var s string
   283  	delete() // ERROR not enough arguments
   284  	delete(1) // ERROR not enough arguments
   285  	delete(1, 2, 3) // ERROR too many arguments
   286  	delete(m, 0 /* ERROR cannot use */)
   287  	delete(m, s)
   288  	_ = delete /* ERROR used as value */ (m, s)
   289  
   290  	var t []map[string]string
   291  	delete(t... /* ERROR invalid use of \.\.\. */ )
   292  }
   293  
   294  func delete2() {
   295  	f1 := func() (m map[string]int) { return }
   296  	f2 := func() (m map[string]int, k string) { return }
   297  	f3 := func() (m map[string]int, k string, x float32) { return }
   298  	delete(f0 /* ERROR used as value */ ())
   299  	delete(f1()) // ERROR not enough arguments
   300  	delete(f2())
   301  	delete(f3()) // ERROR too many arguments
   302  }
   303  
   304  func imag1() {
   305  	var f32 float32
   306  	var f64 float64
   307  	var c64 complex64
   308  	var c128 complex128
   309  	_ = imag() // ERROR not enough arguments
   310  	_ = imag(1, 2) // ERROR too many arguments
   311  	_ = imag(10)
   312  	_ = imag(2.7182818)
   313  	_ = imag("foo" /* ERROR expected complex */)
   314  	_ = imag('a')
   315  	const _5 = imag(1 + 2i)
   316  	assert(_5 == 2)
   317  	f32 = _5
   318  	f64 = _5
   319  	const _6 = imag(0i)
   320  	assert(_6 == 0)
   321  	f32 = imag(c64)
   322  	f64 = imag(c128)
   323  	f32 = imag /* ERROR cannot use .* in assignment */ (c128)
   324  	f64 = imag /* ERROR cannot use .* in assignment */ (c64)
   325  	imag /* ERROR not used */ (c64)
   326  	_, _ = f32, f64
   327  
   328  	// complex type may not be predeclared
   329  	type C64 complex64
   330  	type C128 complex128
   331  	var x64 C64
   332  	var x128 C128
   333  	f32 = imag(x64)
   334  	f64 = imag(x128)
   335  
   336  	var a []complex64
   337  	_ = imag(a... /* ERROR invalid use of \.\.\. */ )
   338  
   339  	// if argument is untyped, result is untyped
   340  	const _ byte = imag(1.2 + 3i)
   341  	const _ complex128 = imag(1.2 + 3i)
   342  
   343  	// lhs constant shift operands are typed as complex128
   344  	var s uint
   345  	_ = imag(1 /* ERROR must be integer */ << s)
   346  }
   347  
   348  func imag2() {
   349  	f1 := func() (x complex128) { return }
   350  	f2 := func() (x, y complex128) { return }
   351  	_ = imag(f0 /* ERROR used as value */ ())
   352  	_ = imag(f1())
   353  	_ = imag(f2()) // ERROR too many arguments
   354  }
   355  
   356  func len1() {
   357  	const c = "foobar"
   358  	var a [10]bool
   359  	var p *[20]int
   360  	var m map[string]complex128
   361  	_ = len() // ERROR not enough arguments
   362  	_ = len(1, 2) // ERROR too many arguments
   363  	_ = len(42 /* ERROR invalid */)
   364  	const _3 = len(c)
   365  	assert(_3 == 6)
   366  	const _4 = len(a)
   367  	assert(_4 == 10)
   368  	const _5 = len(p)
   369  	assert(_5 == 20)
   370  	_ = len(m)
   371  	len /* ERROR not used */ (c)
   372  
   373  	// esoteric case
   374  	var t string
   375  	var hash map[interface{}][]*[10]int
   376  	const n = len /* ERROR not constant */ (hash[recover()][len(t)])
   377  	assert(n == 10) // ok because n has unknown value and no error is reported
   378  	var ch <-chan int
   379  	const nn = len /* ERROR not constant */ (hash[<-ch][len(t)])
   380  
   381  	// issue 4744
   382  	type T struct{ a [10]int }
   383  	const _ = len(((*T)(nil)).a)
   384  
   385  	var s [][]byte
   386  	_ = len(s)
   387  	_ = len(s... /* ERROR invalid use of \.\.\. */ )
   388  }
   389  
   390  func len2() {
   391  	f1 := func() (x []int) { return }
   392  	f2 := func() (x, y []int) { return }
   393  	_ = len(f0 /* ERROR used as value */ ())
   394  	_ = len(f1())
   395  	_ = len(f2()) // ERROR too many arguments
   396  }
   397  
   398  // test cases for issue 7387
   399  func len3() {
   400  	var f = func() int { return 0 }
   401  	var x = f()
   402  	const (
   403  		_ = len([4]int{})
   404  		_ = len([4]int{x})
   405  		_ = len /* ERROR not constant */ ([4]int{f()})
   406  		_ = len /* ERROR not constant */ ([4]int{len([]int{})})
   407  		_ = len([4]int{len([4]int{})})
   408  	)
   409  	var y float64
   410  	var z complex128
   411  	const (
   412  		_ = len([4]float64{})
   413  		_ = len([4]float64{y})
   414  		_ = len([4]float64{real(2i)})
   415  		_ = len /* ERROR not constant */ ([4]float64{real(z)})
   416  	)
   417  	var ch chan [10]int
   418  	const (
   419  		_ = len /* ERROR not constant */ (<-ch)
   420  		_ = len /* ERROR not constant */ ([4]int{(<-ch)[0]})
   421  	)
   422  }
   423  
   424  func make1() {
   425  	var n int
   426  	var m float32
   427  	var s uint
   428  
   429  	_ = make() // ERROR not enough arguments
   430  	_ = make(1 /* ERROR not a type */)
   431  	_ = make(int /* ERROR cannot make */)
   432  
   433  	// slices
   434  	_ = make/* ERROR arguments */ ([]int)
   435  	_ = make/* ERROR arguments */ ([]int, 2, 3, 4)
   436  	_ = make([]int, int /* ERROR not an expression */)
   437  	_ = make([]int, 10, float32 /* ERROR not an expression */)
   438  	_ = make([]int, "foo" /* ERROR cannot convert */)
   439  	_ = make([]int, 10, 2.3 /* ERROR truncated */)
   440  	_ = make([]int, 5, 10.0)
   441  	_ = make([]int, 0i)
   442  	_ = make([]int, 1.0)
   443  	_ = make([]int, 1.0<<s)
   444  	_ = make([]int, 1.1 /* ERROR int */ <<s)
   445  	_ = make([]int, - /* ERROR must not be negative */ 1, 10)
   446  	_ = make([]int, 0, - /* ERROR must not be negative */ 1)
   447  	_ = make([]int, - /* ERROR must not be negative */ 1, - /* ERROR must not be negative */ 1)
   448  	_ = make([]int, 1 /* ERROR overflows */ <<100, 1 /* ERROR overflows */ <<100)
   449  	_ = make([]int, 10 /* ERROR length and capacity swapped */ , 9)
   450  	_ = make([]int, 1 /* ERROR overflows */ <<100, 12345)
   451  	_ = make([]int, m /* ERROR must be integer */ )
   452          _ = &make /* ERROR cannot take address */ ([]int, 0)
   453  
   454  	// maps
   455  	_ = make /* ERROR arguments */ (map[int]string, 10, 20)
   456  	_ = make(map[int]float32, int /* ERROR not an expression */)
   457  	_ = make(map[int]float32, "foo" /* ERROR cannot convert */)
   458  	_ = make(map[int]float32, 10)
   459  	_ = make(map[int]float32, n)
   460  	_ = make(map[int]float32, int64(n))
   461  	_ = make(map[string]bool, 10.0)
   462  	_ = make(map[string]bool, 10.0<<s)
   463          _ = &make /* ERROR cannot take address */ (map[string]bool)
   464  
   465  	// channels
   466  	_ = make /* ERROR arguments */ (chan int, 10, 20)
   467  	_ = make(chan int, int /* ERROR not an expression */)
   468  	_ = make(chan<- int, "foo" /* ERROR cannot convert */)
   469  	_ = make(chan int, - /* ERROR must not be negative */ 10)
   470  	_ = make(<-chan float64, 10)
   471  	_ = make(chan chan int, n)
   472  	_ = make(chan string, int64(n))
   473  	_ = make(chan bool, 10.0)
   474  	_ = make(chan bool, 10.0<<s)
   475          _ = &make /* ERROR cannot take address */ (chan bool)
   476  
   477  	make /* ERROR not used */ ([]int, 10)
   478  
   479  	var t []int
   480  	_ = make([]int, t[0], t[1])
   481  	_ = make([]int, t... /* ERROR invalid use of \.\.\. */ )
   482  }
   483  
   484  func make2() {
   485  	f1 /* ERROR not used */ := func() (x []int) { return }
   486  	_ = make(f0 /* ERROR not a type */ ())
   487  	_ = make(f1 /* ERROR not a type */ ())
   488  }
   489  
   490  func new1() {
   491  	_ = new() // ERROR not enough arguments
   492  	_ = new(1, 2) // ERROR too many arguments
   493  	_ = new("foo" /* ERROR not a type */)
   494  	p := new(float64)
   495  	_ = new(struct{ x, y int })
   496  	q := new(*float64)
   497  	_ = *p == **q
   498  	new /* ERROR not used */ (int)
   499          _ = &new /* ERROR cannot take address */ (int)
   500  
   501  	_ = new(int... /* ERROR invalid use of \.\.\. */ )
   502  }
   503  
   504  func new2() {
   505  	f1 /* ERROR not used */ := func() (x []int) { return }
   506  	_ = new(f0 /* ERROR not a type */ ())
   507  	_ = new(f1 /* ERROR not a type */ ())
   508  }
   509  
   510  func panic1() {
   511  	panic() // ERROR not enough arguments
   512  	panic(1, 2) // ERROR too many arguments
   513  	panic(0)
   514  	panic("foo")
   515  	panic(false)
   516  	panic(1<<10)
   517  	panic(1 << /* ERROR constant shift overflow */ 1000)
   518  	_ = panic /* ERROR used as value */ (0)
   519  
   520  	var s []byte
   521  	panic(s)
   522  	panic(s... /* ERROR invalid use of \.\.\. */ )
   523  }
   524  
   525  func panic2() {
   526  	f1 := func() (x int) { return }
   527  	f2 := func() (x, y int) { return }
   528  	panic(f0 /* ERROR used as value */ ())
   529  	panic(f1())
   530  	panic(f2()) // ERROR too many arguments
   531  }
   532  
   533  func print1() {
   534  	print()
   535  	print(1)
   536  	print(1, 2)
   537  	print("foo")
   538  	print(2.718281828)
   539  	print(false)
   540  	print(1<<10)
   541  	print(1 << /* ERROR constant shift overflow */ 1000)
   542  	println(nil /* ERROR untyped nil */ )
   543  
   544  	var s []int
   545  	print(s... /* ERROR invalid use of \.\.\. */ )
   546  	_ = print /* ERROR used as value */ ()
   547  }
   548  
   549  func print2() {
   550  	f1 := func() (x int) { return }
   551  	f2 := func() (x, y int) { return }
   552  	f3 := func() (x int, y float32, z string) { return }
   553  	print(f0 /* ERROR used as value */ ())
   554  	print(f1())
   555  	print(f2())
   556  	print(f3())
   557  }
   558  
   559  func println1() {
   560  	println()
   561  	println(1)
   562  	println(1, 2)
   563  	println("foo")
   564  	println(2.718281828)
   565  	println(false)
   566  	println(1<<10)
   567  	println(1 << /* ERROR constant shift overflow */ 1000)
   568  	println(nil /* ERROR untyped nil */ )
   569  
   570  	var s []int
   571  	println(s... /* ERROR invalid use of \.\.\. */ )
   572  	_ = println /* ERROR used as value */ ()
   573  }
   574  
   575  func println2() {
   576  	f1 := func() (x int) { return }
   577  	f2 := func() (x, y int) { return }
   578  	f3 := func() (x int, y float32, z string) { return }
   579  	println(f0 /* ERROR used as value */ ())
   580  	println(f1())
   581  	println(f2())
   582  	println(f3())
   583  }
   584  
   585  func real1() {
   586  	var f32 float32
   587  	var f64 float64
   588  	var c64 complex64
   589  	var c128 complex128
   590  	_ = real() // ERROR not enough arguments
   591  	_ = real(1, 2) // ERROR too many arguments
   592  	_ = real(10)
   593  	_ = real(2.7182818)
   594  	_ = real("foo" /* ERROR expected complex */)
   595  	const _5 = real(1 + 2i)
   596  	assert(_5 == 1)
   597  	f32 = _5
   598  	f64 = _5
   599  	const _6 = real(0i)
   600  	assert(_6 == 0)
   601  	f32 = real(c64)
   602  	f64 = real(c128)
   603  	f32 = real /* ERROR cannot use .* in assignment */ (c128)
   604  	f64 = real /* ERROR cannot use .* in assignment */ (c64)
   605  	real /* ERROR not used */ (c64)
   606  
   607  	// complex type may not be predeclared
   608  	type C64 complex64
   609  	type C128 complex128
   610  	var x64 C64
   611  	var x128 C128
   612  	f32 = imag(x64)
   613  	f64 = imag(x128)
   614  	_, _ = f32, f64
   615  
   616  	var a []complex64
   617  	_ = real(a... /* ERROR invalid use of \.\.\. */ )
   618  
   619  	// if argument is untyped, result is untyped
   620  	const _ byte = real(1 + 2.3i)
   621  	const _ complex128 = real(1 + 2.3i)
   622  
   623  	// lhs constant shift operands are typed as complex128
   624  	var s uint
   625  	_ = real(1 /* ERROR must be integer */ << s)
   626  }
   627  
   628  func real2() {
   629  	f1 := func() (x complex128) { return }
   630  	f2 := func() (x, y complex128) { return }
   631  	_ = real(f0 /* ERROR used as value */ ())
   632  	_ = real(f1())
   633  	_ = real(f2()) // ERROR too many arguments
   634  }
   635  
   636  func recover1() {
   637  	_ = recover()
   638  	_ = recover(10) // ERROR too many arguments
   639  	recover()
   640  
   641  	var s []int
   642  	recover(s... /* ERROR invalid use of \.\.\. */ )
   643  }
   644  
   645  func recover2() {
   646  	f1 := func() (x int) { return }
   647  	f2 := func() (x, y int) { return }
   648  	_ = recover(f0 /* ERROR used as value */ ())
   649  	_ = recover(f1()) // ERROR too many arguments
   650  	_ = recover(f2()) // ERROR too many arguments
   651  }
   652  
   653  // assuming types.DefaultPtrSize == 8
   654  type S0 struct{      // offset
   655  	a bool       //  0
   656  	b rune       //  4
   657  	c *int       //  8
   658  	d bool       // 16
   659  	e complex128 // 24
   660  }                    // 40
   661  
   662  type S1 struct{   // offset
   663  	x float32 //  0
   664  	y string  //  8
   665  	z *S1     // 24
   666  	S0        // 32
   667  }                 // 72
   668  
   669  type S2 struct{ // offset
   670  	*S1     //  0
   671  }               //  8
   672  
   673  type S3 struct { // offset
   674  	a int64  //  0
   675  	b int32  //  8
   676  }                // 12
   677  
   678  type S4 struct { // offset
   679  	S3       //  0
   680  	int32    // 12
   681  }                // 16
   682  
   683  type S5 struct {   // offset
   684  	a [3]int32 //  0
   685  	b int32    // 12
   686  }                  // 16
   687  
   688  func (S2) m() {}
   689  
   690  func Alignof1() {
   691  	var x int
   692  	_ = unsafe.Alignof() // ERROR not enough arguments
   693  	_ = unsafe.Alignof(1, 2) // ERROR too many arguments
   694  	_ = unsafe.Alignof(int /* ERROR not an expression */)
   695  	_ = unsafe.Alignof(42)
   696  	_ = unsafe.Alignof(new(struct{}))
   697  	_ = unsafe.Alignof(1<<10)
   698  	_ = unsafe.Alignof(1 << /* ERROR constant shift overflow */ 1000)
   699  	_ = unsafe.Alignof(nil /* ERROR "untyped nil */ )
   700  	unsafe /* ERROR not used */ .Alignof(x)
   701  
   702  	var y S0
   703  	assert(unsafe.Alignof(y.a) == 1)
   704  	assert(unsafe.Alignof(y.b) == 4)
   705  	assert(unsafe.Alignof(y.c) == 8)
   706  	assert(unsafe.Alignof(y.d) == 1)
   707  	assert(unsafe.Alignof(y.e) == 8)
   708  
   709  	var s []byte
   710  	_ = unsafe.Alignof(s)
   711  	_ = unsafe.Alignof(s... /* ERROR invalid use of \.\.\. */ )
   712  }
   713  
   714  func Alignof2() {
   715  	f1 := func() (x int32) { return }
   716  	f2 := func() (x, y int32) { return }
   717  	_ = unsafe.Alignof(f0 /* ERROR used as value */ ())
   718  	assert(unsafe.Alignof(f1()) == 4)
   719  	_ = unsafe.Alignof(f2()) // ERROR too many arguments
   720  }
   721  
   722  func Offsetof1() {
   723  	var x struct{ f int }
   724  	_ = unsafe.Offsetof() // ERROR not enough arguments
   725  	_ = unsafe.Offsetof(1, 2) // ERROR too many arguments
   726  	_ = unsafe.Offsetof(int /* ERROR not a selector expression */ )
   727  	_ = unsafe.Offsetof(x /* ERROR not a selector expression */ )
   728  	_ = unsafe.Offsetof(nil /* ERROR not a selector expression */ )
   729  	_ = unsafe.Offsetof(x.f)
   730  	_ = unsafe.Offsetof((x.f))
   731  	_ = unsafe.Offsetof((((((((x))).f)))))
   732  	unsafe /* ERROR not used */ .Offsetof(x.f)
   733  
   734  	var y0 S0
   735  	assert(unsafe.Offsetof(y0.a) == 0)
   736  	assert(unsafe.Offsetof(y0.b) == 4)
   737  	assert(unsafe.Offsetof(y0.c) == 8)
   738  	assert(unsafe.Offsetof(y0.d) == 16)
   739  	assert(unsafe.Offsetof(y0.e) == 24)
   740  
   741  	var y1 S1
   742  	assert(unsafe.Offsetof(y1.x) == 0)
   743  	assert(unsafe.Offsetof(y1.y) == 8)
   744  	assert(unsafe.Offsetof(y1.z) == 24)
   745  	assert(unsafe.Offsetof(y1.S0) == 32)
   746  
   747  	assert(unsafe.Offsetof(y1.S0.a) == 0) // relative to S0
   748  	assert(unsafe.Offsetof(y1.a) == 32)   // relative to S1
   749  	assert(unsafe.Offsetof(y1.b) == 36)   // relative to S1
   750  	assert(unsafe.Offsetof(y1.c) == 40)   // relative to S1
   751  	assert(unsafe.Offsetof(y1.d) == 48)   // relative to S1
   752  	assert(unsafe.Offsetof(y1.e) == 56)   // relative to S1
   753  
   754  	var y1p *S1
   755  	assert(unsafe.Offsetof(y1p.S0) == 32)
   756  
   757  	type P *S1
   758  	var p P = y1p
   759  	assert(unsafe.Offsetof(p.S0) == 32)
   760  
   761  	var y2 S2
   762  	assert(unsafe.Offsetof(y2.S1) == 0)
   763  	_ = unsafe.Offsetof(y2 /* ERROR embedded via a pointer */ .x)
   764  	_ = unsafe.Offsetof(y2 /* ERROR method value */ .m)
   765  
   766  	var s []byte
   767  	_ = unsafe.Offsetof(s... /* ERROR invalid use of \.\.\. */ )
   768  }
   769  
   770  func Offsetof2() {
   771  	f1 := func() (x int32) { return }
   772  	f2 := func() (x, y int32) { return }
   773  	_ = unsafe.Offsetof(f0 /* ERROR not a selector expression */ ())
   774  	_ = unsafe.Offsetof(f1 /* ERROR not a selector expression */ ())
   775  	_ = unsafe.Offsetof(f2 /* ERROR not a selector expression */ ())
   776  }
   777  
   778  func Sizeof1() {
   779  	var x int
   780  	_ = unsafe.Sizeof() // ERROR not enough arguments
   781  	_ = unsafe.Sizeof(1, 2) // ERROR too many arguments
   782  	_ = unsafe.Sizeof(int /* ERROR not an expression */)
   783  	_ = unsafe.Sizeof(42)
   784  	_ = unsafe.Sizeof(new(complex128))
   785  	_ = unsafe.Sizeof(1<<10)
   786  	_ = unsafe.Sizeof(1 << /* ERROR constant shift overflow */ 1000)
   787  	_ = unsafe.Sizeof(nil /* ERROR untyped nil */ )
   788  	unsafe /* ERROR not used */ .Sizeof(x)
   789  
   790  	// basic types have size guarantees
   791  	assert(unsafe.Sizeof(byte(0)) == 1)
   792  	assert(unsafe.Sizeof(uint8(0)) == 1)
   793  	assert(unsafe.Sizeof(int8(0)) == 1)
   794  	assert(unsafe.Sizeof(uint16(0)) == 2)
   795  	assert(unsafe.Sizeof(int16(0)) == 2)
   796  	assert(unsafe.Sizeof(uint32(0)) == 4)
   797  	assert(unsafe.Sizeof(int32(0)) == 4)
   798  	assert(unsafe.Sizeof(float32(0)) == 4)
   799  	assert(unsafe.Sizeof(uint64(0)) == 8)
   800  	assert(unsafe.Sizeof(int64(0)) == 8)
   801  	assert(unsafe.Sizeof(float64(0)) == 8)
   802  	assert(unsafe.Sizeof(complex64(0)) == 8)
   803  	assert(unsafe.Sizeof(complex128(0)) == 16)
   804  
   805  	var y0 S0
   806  	assert(unsafe.Sizeof(y0.a) == 1)
   807  	assert(unsafe.Sizeof(y0.b) == 4)
   808  	assert(unsafe.Sizeof(y0.c) == 8)
   809  	assert(unsafe.Sizeof(y0.d) == 1)
   810  	assert(unsafe.Sizeof(y0.e) == 16)
   811  	assert(unsafe.Sizeof(y0) == 40)
   812  
   813  	var y1 S1
   814  	assert(unsafe.Sizeof(y1) == 72)
   815  
   816  	var y2 S2
   817  	assert(unsafe.Sizeof(y2) == 8)
   818  
   819  	var y3 S3
   820  	assert(unsafe.Sizeof(y3) == 12)
   821  
   822  	var y4 S4
   823  	assert(unsafe.Sizeof(y4) == 16)
   824  
   825  	var y5 S5
   826  	assert(unsafe.Sizeof(y5) == 16)
   827  
   828  	var a3 [10]S3
   829  	assert(unsafe.Sizeof(a3) == 156)
   830  
   831  	// test case for issue 5670
   832  	type T struct {
   833  		a int32
   834  		_ int32
   835  		c int32
   836  	}
   837  	assert(unsafe.Sizeof(T{}) == 12)
   838  
   839  	var s []byte
   840  	_ = unsafe.Sizeof(s)
   841  	_ = unsafe.Sizeof(s... /* ERROR invalid use of \.\.\. */ )
   842  }
   843  
   844  func Sizeof2() {
   845  	f1 := func() (x int64) { return }
   846  	f2 := func() (x, y int64) { return }
   847  	_ = unsafe.Sizeof(f0 /* ERROR used as value */ ())
   848  	assert(unsafe.Sizeof(f1()) == 8)
   849  	_ = unsafe.Sizeof(f2()) // ERROR too many arguments
   850  }
   851  
   852  // self-testing only
   853  func assert1() {
   854  	var x int
   855  	assert() /* ERROR not enough arguments */
   856  	assert(1, 2) /* ERROR too many arguments */
   857  	assert("foo" /* ERROR boolean constant */ )
   858  	assert(x /* ERROR boolean constant */)
   859  	assert(true)
   860  	assert /* ERROR failed */ (false)
   861  	_ = assert(true)
   862  
   863  	var s []byte
   864  	assert(s... /* ERROR invalid use of \.\.\. */ )
   865  }
   866  
   867  func assert2() {
   868  	f1 := func() (x bool) { return }
   869  	f2 := func() (x bool) { return }
   870  	assert(f0 /* ERROR used as value */ ())
   871  	assert(f1 /* ERROR boolean constant */ ())
   872  	assert(f2 /* ERROR boolean constant */ ())
   873  }
   874  
   875  // self-testing only
   876  func trace1() {
   877  	// Uncomment the code below to test trace - will produce console output
   878  	// _ = trace /* ERROR no value */ ()
   879  	// _ = trace(1)
   880  	// _ = trace(true, 1.2, '\'', "foo", 42i, "foo" <= "bar")
   881  
   882  	var s []byte
   883  	trace(s... /* ERROR invalid use of \.\.\. */ )
   884  }
   885  
   886  func trace2() {
   887  	f1 := func() (x int) { return }
   888  	f2 := func() (x int, y string) { return }
   889  	f3 := func() (x int, y string, z []int) { return }
   890  	_ = f1
   891  	_ = f2
   892  	_ = f3
   893  	// Uncomment the code below to test trace - will produce console output
   894  	// trace(f0())
   895  	// trace(f1())
   896  	// trace(f2())
   897  	// trace(f3())
   898  	// trace(f0(), 1)
   899  	// trace(f1(), 1, 2)
   900  	// trace(f2(), 1, 2, 3)
   901  	// trace(f3(), 1, 2, 3, 4)
   902  }
   903  

View as plain text