Source file src/internal/types/testdata/check/builtins0.go

     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" /* ERRORx `cannot use .* in argument to append` */ )
    39  	_ = append(s, "foo"...)
    40  	_ = append(S(s), "foo" /* ERRORx `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 ... with built-in cap" */ )
   104  
   105  	var x int
   106  	_ = cap(x /* ERROR "invalid argument: x (variable of type int) for built-in cap" */ )
   107  }
   108  
   109  func cap2() {
   110  	f1a := func() (a [10]int) { return }
   111  	f1s := func() (s []int) { return }
   112  	f2 := func() (s []int, x int) { return }
   113  	_ = cap(f0 /* ERROR "used as value" */ ())
   114  	_ = cap(f1a())
   115  	_ = cap(f1s())
   116  	_ = cap(f2()) // ERROR "too many arguments"
   117  }
   118  
   119  // test cases for issue 7387
   120  func cap3() {
   121  	var f = func() int { return 0 }
   122  	var x = f()
   123  	const (
   124  		_ = cap([4]int{})
   125  		_ = cap([4]int{x})
   126  		_ = cap /* ERROR "not constant" */ ([4]int{f()})
   127  		_ = cap /* ERROR "not constant" */ ([4]int{cap([]int{})})
   128  		_ = cap([4]int{cap([4]int{})})
   129  	)
   130  	var y float64
   131  	var z complex128
   132  	const (
   133  		_ = cap([4]float64{})
   134  		_ = cap([4]float64{y})
   135  		_ = cap([4]float64{real(2i)})
   136  		_ = cap /* ERROR "not constant" */ ([4]float64{real(z)})
   137  	)
   138  	var ch chan [10]int
   139  	const (
   140  		_ = cap /* ERROR "not constant" */ (<-ch)
   141  		_ = cap /* ERROR "not constant" */ ([4]int{(<-ch)[0]})
   142  	)
   143  }
   144  
   145  func clear1() {
   146  	var a [10]int
   147  	var m map[float64]string
   148  	var s []byte
   149  	clear(a /* ERROR "cannot clear a" */)
   150  	clear(&/* ERROR "cannot clear &a" */a)
   151  	clear(m)
   152  	clear(s)
   153  	clear([]int{})
   154  }
   155  
   156  func close1() {
   157  	var c chan int
   158  	var r <-chan int
   159  	close() // ERROR "not enough arguments"
   160  	close(1, 2) // ERROR "too many arguments"
   161  	close(42 /* ERROR "cannot close non-channel" */)
   162  	close(r /* ERROR "receive-only channel" */)
   163  	close(c)
   164  	_ = close /* ERROR "used as value" */ (c)
   165  
   166  	var s []chan int
   167  	close(s... /* ERROR "invalid use of ..." */ )
   168  }
   169  
   170  func close2() {
   171  	f1 := func() (ch chan int) { return }
   172  	f2 := func() (ch chan int, x int) { return }
   173  	close(f0 /* ERROR "used as value" */ ())
   174  	close(f1())
   175  	close(f2()) // ERROR "too many arguments"
   176  }
   177  
   178  func complex1() {
   179  	var i32 int32
   180  	var f32 float32
   181  	var f64 float64
   182  	var c64 complex64
   183  	var c128 complex128
   184  	_ = complex() // ERROR "not enough arguments"
   185  	_ = complex(1) // ERROR "not enough arguments"
   186  	_ = complex(true /* ERROR "mismatched types" */ , 0)
   187  	_ = complex(i32 /* ERROR "expected floating-point" */ , 0)
   188  	_ = complex("foo" /* ERROR "mismatched types" */ , 0)
   189  	_ = complex(c64 /* ERROR "expected floating-point" */ , 0)
   190  	_ = complex(0 /* ERROR "mismatched types" */ , true)
   191  	_ = complex(0 /* ERROR "expected floating-point" */ , i32)
   192  	_ = complex(0 /* ERROR "mismatched types" */ , "foo")
   193  	_ = complex(0 /* ERROR "expected floating-point" */ , c64)
   194  	_ = complex(f32, f32)
   195  	_ = complex(f32, 1)
   196  	_ = complex(f32, 1.0)
   197  	_ = complex(f32, 'a')
   198  	_ = complex(f64, f64)
   199  	_ = complex(f64, 1)
   200  	_ = complex(f64, 1.0)
   201  	_ = complex(f64, 'a')
   202  	_ = complex(f32 /* ERROR "mismatched types" */ , f64)
   203  	_ = complex(f64 /* ERROR "mismatched types" */ , f32)
   204  	_ = complex(1, 1)
   205  	_ = complex(1, 1.1)
   206  	_ = complex(1, 'a')
   207  	complex /* ERROR "not used" */ (1, 2)
   208  
   209  	var _ complex64 = complex(f32, f32)
   210  	var _ complex64 = complex /* ERRORx `cannot use .* in variable declaration` */ (f64, f64)
   211  
   212  	var _ complex128 = complex /* ERRORx `cannot use .* in variable declaration` */ (f32, f32)
   213  	var _ complex128 = complex(f64, f64)
   214  
   215  	// untyped constants
   216  	const _ int = complex(1, 0)
   217  	const _ float32 = complex(1, 0)
   218  	const _ complex64 = complex(1, 0)
   219  	const _ complex128 = complex(1, 0)
   220  	const _ = complex(0i, 0i)
   221  	const _ = complex(0i, 0)
   222  	const _ int = 1.0 + complex(1, 0i)
   223  
   224  	const _ int = complex /* ERROR "int" */ (1.1, 0)
   225  	const _ float32 = complex /* ERROR "float32" */ (1, 2)
   226  
   227  	// untyped values
   228  	var s uint
   229  	_ = complex(1 /* ERROR "integer" */ <<s, 0)
   230  	const _ = complex /* ERROR "not constant" */ (1 /* ERROR "integer" */ <<s, 0)
   231  	var _ int = complex /* ERRORx `cannot use .* in variable declaration` */ (1 /* ERROR "integer" */ <<s, 0)
   232  
   233  	// floating-point argument types must be identical
   234  	type F32 float32
   235  	type F64 float64
   236  	var x32 F32
   237  	var x64 F64
   238  	c64 = complex(x32, x32)
   239  	_ = complex(x32 /* ERROR "mismatched types" */ , f32)
   240  	_ = complex(f32 /* ERROR "mismatched types" */ , x32)
   241  	c128 = complex(x64, x64)
   242  	_ = c128
   243  	_ = complex(x64 /* ERROR "mismatched types" */ , f64)
   244  	_ = complex(f64 /* ERROR "mismatched types" */ , x64)
   245  
   246  	var t []float32
   247  	_ = complex(t... /* ERROR "invalid use of ..." */ )
   248  }
   249  
   250  func complex2() {
   251  	f1 := func() (x float32) { return }
   252  	f2 := func() (x, y float32) { return }
   253  	f3 := func() (x, y, z float32) { return }
   254  	_ = complex(f0 /* ERROR "used as value" */ ())
   255  	_ = complex(f1()) // ERROR "not enough arguments"
   256  	_ = complex(f2())
   257  	_ = complex(f3()) // ERROR "too many arguments"
   258  }
   259  
   260  func copy1() {
   261  	copy() // ERROR "not enough arguments"
   262  	copy("foo") // ERROR "not enough arguments"
   263  	copy([ /* ERROR "copy expects slice arguments" */ ...]int{}, []int{})
   264  	copy([ /* ERROR "copy expects slice arguments" */ ]int{}, [...]int{})
   265  	copy([ /* ERROR "different element types" */ ]int8{}, "foo")
   266  
   267  	// spec examples
   268  	var a = [...]int{0, 1, 2, 3, 4, 5, 6, 7}
   269  	var s = make([]int, 6)
   270  	var b = make([]byte, 5)
   271  	n1 := copy(s, a[0:])            // n1 == 6, s == []int{0, 1, 2, 3, 4, 5}
   272  	n2 := copy(s, s[2:])            // n2 == 4, s == []int{2, 3, 4, 5, 4, 5}
   273  	n3 := copy(b, "Hello, World!")  // n3 == 5, b == []byte("Hello")
   274  	_, _, _ = n1, n2, n3
   275  
   276  	var t [][]int
   277  	copy(t, t)
   278  	copy(t /* ERROR "copy expects slice arguments" */ , nil)
   279  	copy(nil /* ERROR "copy expects slice arguments" */ , t)
   280  	copy(nil /* ERROR "copy expects slice arguments" */ , nil)
   281  	copy(t... /* ERROR "invalid use of ..." */ )
   282  }
   283  
   284  func copy2() {
   285  	f1 := func() (a []int) { return }
   286  	f2 := func() (a, b []int) { return }
   287  	f3 := func() (a, b, c []int) { return }
   288  	copy(f0 /* ERROR "used as value" */ ())
   289  	copy(f1()) // ERROR "not enough arguments"
   290  	copy(f2())
   291  	copy(f3()) // ERROR "too many arguments"
   292  }
   293  
   294  func delete1() {
   295  	var m map[string]int
   296  	var s string
   297  	delete() // ERROR "not enough arguments"
   298  	delete(1) // ERROR "not enough arguments"
   299  	delete(1, 2, 3) // ERROR "too many arguments"
   300  	delete(m, 0 /* ERROR "cannot use" */)
   301  	delete(m, s)
   302  	_ = delete /* ERROR "used as value" */ (m, s)
   303  
   304  	var t []map[string]string
   305  	delete(t... /* ERROR "invalid use of ..." */ )
   306  }
   307  
   308  func delete2() {
   309  	f1 := func() (m map[string]int) { return }
   310  	f2 := func() (m map[string]int, k string) { return }
   311  	f3 := func() (m map[string]int, k string, x float32) { return }
   312  	delete(f0 /* ERROR "used as value" */ ())
   313  	delete(f1()) // ERROR "not enough arguments"
   314  	delete(f2())
   315  	delete(f3()) // ERROR "too many arguments"
   316  }
   317  
   318  func imag1() {
   319  	var f32 float32
   320  	var f64 float64
   321  	var c64 complex64
   322  	var c128 complex128
   323  	_ = imag() // ERROR "not enough arguments"
   324  	_ = imag(1, 2) // ERROR "too many arguments"
   325  	_ = imag(10)
   326  	_ = imag(2.7182818)
   327  	_ = imag("foo" /* ERROR "expected complex" */)
   328  	_ = imag('a')
   329  	const _5 = imag(1 + 2i)
   330  	assert(_5 == 2)
   331  	f32 = _5
   332  	f64 = _5
   333  	const _6 = imag(0i)
   334  	assert(_6 == 0)
   335  	f32 = imag(c64)
   336  	f64 = imag(c128)
   337  	f32 = imag /* ERRORx `cannot use .* in assignment` */ (c128)
   338  	f64 = imag /* ERRORx `cannot use .* in assignment` */ (c64)
   339  	imag /* ERROR "not used" */ (c64)
   340  	_, _ = f32, f64
   341  
   342  	// complex type may not be predeclared
   343  	type C64 complex64
   344  	type C128 complex128
   345  	var x64 C64
   346  	var x128 C128
   347  	f32 = imag(x64)
   348  	f64 = imag(x128)
   349  
   350  	var a []complex64
   351  	_ = imag(a... /* ERROR "invalid use of ..." */ )
   352  
   353  	// if argument is untyped, result is untyped
   354  	const _ byte = imag(1.2 + 3i)
   355  	const _ complex128 = imag(1.2 + 3i)
   356  
   357  	// lhs constant shift operands are typed as complex128
   358  	var s uint
   359  	_ = imag(1 /* ERROR "must be integer" */ << s)
   360  }
   361  
   362  func imag2() {
   363  	f1 := func() (x complex128) { return }
   364  	f2 := func() (x, y complex128) { return }
   365  	_ = imag(f0 /* ERROR "used as value" */ ())
   366  	_ = imag(f1())
   367  	_ = imag(f2()) // ERROR "too many arguments"
   368  }
   369  
   370  func len1() {
   371  	const c = "foobar"
   372  	var a [10]bool
   373  	var p *[20]int
   374  	var m map[string]complex128
   375  	_ = len() // ERROR "not enough arguments"
   376  	_ = len(1, 2) // ERROR "too many arguments"
   377  	_ = len(42 /* ERROR "invalid" */)
   378  	const _3 = len(c)
   379  	assert(_3 == 6)
   380  	const _4 = len(a)
   381  	assert(_4 == 10)
   382  	const _5 = len(p)
   383  	assert(_5 == 20)
   384  	_ = len(m)
   385  	len /* ERROR "not used" */ (c)
   386  
   387  	// esoteric case
   388  	var t string
   389  	var hash map[interface{}][]*[10]int
   390  	const n = len /* ERROR "not constant" */ (hash[recover()][len(t)])
   391  	assert(n == 10) // ok because n has unknown value and no error is reported
   392  	var ch <-chan int
   393  	const nn = len /* ERROR "not constant" */ (hash[<-ch][len(t)])
   394  
   395  	// issue 4744
   396  	type T struct{ a [10]int }
   397  	const _ = len(((*T)(nil)).a)
   398  
   399  	var s [][]byte
   400  	_ = len(s)
   401  	_ = len(s... /* ERROR "invalid use of ..." */ )
   402  }
   403  
   404  func len2() {
   405  	f1 := func() (x []int) { return }
   406  	f2 := func() (x, y []int) { return }
   407  	_ = len(f0 /* ERROR "used as value" */ ())
   408  	_ = len(f1())
   409  	_ = len(f2()) // ERROR "too many arguments"
   410  }
   411  
   412  // test cases for issue 7387
   413  func len3() {
   414  	var f = func() int { return 0 }
   415  	var x = f()
   416  	const (
   417  		_ = len([4]int{})
   418  		_ = len([4]int{x})
   419  		_ = len /* ERROR "not constant" */ ([4]int{f()})
   420  		_ = len /* ERROR "not constant" */ ([4]int{len([]int{})})
   421  		_ = len([4]int{len([4]int{})})
   422  	)
   423  	var y float64
   424  	var z complex128
   425  	const (
   426  		_ = len([4]float64{})
   427  		_ = len([4]float64{y})
   428  		_ = len([4]float64{real(2i)})
   429  		_ = len /* ERROR "not constant" */ ([4]float64{real(z)})
   430  	)
   431  	var ch chan [10]int
   432  	const (
   433  		_ = len /* ERROR "not constant" */ (<-ch)
   434  		_ = len /* ERROR "not constant" */ ([4]int{(<-ch)[0]})
   435  	)
   436  }
   437  
   438  func make1() {
   439  	var n int
   440  	var m float32
   441  	var s uint
   442  
   443  	_ = make() // ERROR "not enough arguments"
   444  	_ = make(1 /* ERROR "not a type" */)
   445  	_ = make(int /* ERROR "cannot make" */)
   446  
   447  	// slices
   448  	_ = make/* ERROR "arguments" */ ([]int)
   449  	_ = make/* ERROR "arguments" */ ([]int, 2, 3, 4)
   450  	_ = make([]int, int /* ERROR "not an expression" */)
   451  	_ = make([]int, 10, float32 /* ERROR "not an expression" */)
   452  	_ = make([]int, "foo" /* ERROR "cannot convert" */)
   453  	_ = make([]int, 10, 2.3 /* ERROR "truncated" */)
   454  	_ = make([]int, 5, 10.0)
   455  	_ = make([]int, 0i)
   456  	_ = make([]int, 1.0)
   457  	_ = make([]int, 1.0<<s)
   458  	_ = make([]int, 1.1 /* ERROR "int" */ <<s)
   459  	_ = make([]int, - /* ERROR "must not be negative" */ 1, 10)
   460  	_ = make([]int, 0, - /* ERROR "must not be negative" */ 1)
   461  	_ = make([]int, - /* ERROR "must not be negative" */ 1, - /* ERROR "must not be negative" */ 1)
   462  	_ = make([]int, 1 /* ERROR "overflows" */ <<100, 1 /* ERROR "overflows" */ <<100)
   463  	_ = make([]int, 10 /* ERROR "length and capacity swapped" */ , 9)
   464  	_ = make([]int, 1 /* ERROR "overflows" */ <<100, 12345)
   465  	_ = make([]int, m /* ERROR "must be integer" */ )
   466          _ = &make /* ERROR "cannot take address" */ ([]int, 0)
   467  
   468  	// maps
   469  	_ = make /* ERROR "arguments" */ (map[int]string, 10, 20)
   470  	_ = make(map[int]float32, int /* ERROR "not an expression" */)
   471  	_ = make(map[int]float32, "foo" /* ERROR "cannot convert" */)
   472  	_ = make(map[int]float32, 10)
   473  	_ = make(map[int]float32, n)
   474  	_ = make(map[int]float32, int64(n))
   475  	_ = make(map[string]bool, 10.0)
   476  	_ = make(map[string]bool, 10.0<<s)
   477          _ = &make /* ERROR "cannot take address" */ (map[string]bool)
   478  
   479  	// channels
   480  	_ = make /* ERROR "arguments" */ (chan int, 10, 20)
   481  	_ = make(chan int, int /* ERROR "not an expression" */)
   482  	_ = make(chan<- int, "foo" /* ERROR "cannot convert" */)
   483  	_ = make(chan int, - /* ERROR "must not be negative" */ 10)
   484  	_ = make(<-chan float64, 10)
   485  	_ = make(chan chan int, n)
   486  	_ = make(chan string, int64(n))
   487  	_ = make(chan bool, 10.0)
   488  	_ = make(chan bool, 10.0<<s)
   489          _ = &make /* ERROR "cannot take address" */ (chan bool)
   490  
   491  	make /* ERROR "not used" */ ([]int, 10)
   492  
   493  	var t []int
   494  	_ = make([]int, t[0], t[1])
   495  	_ = make([]int, t... /* ERROR "invalid use of ..." */ )
   496  }
   497  
   498  func make2() {
   499  	f1 := func() (x []int) { return }
   500  	_ = make(f0 /* ERROR "not a type" */ ())
   501  	_ = make(f1 /* ERROR "not a type" */ ())
   502  }
   503  
   504  func max1() {
   505  	var b bool
   506  	var c complex128
   507  	var x int
   508  	var s string
   509  	type myint int
   510  	var m myint
   511  	_ = max() /* ERROR "not enough arguments" */
   512  	_ = max(b /* ERROR "cannot be ordered" */ )
   513  	_ = max(c /* ERROR "cannot be ordered" */ )
   514  	_ = max(x)
   515  	_ = max(s)
   516  	_ = max(x, x)
   517  	_ = max(x, x, x, x, x)
   518  	var _ int = max /* ERROR "cannot use max(m) (value of type myint) as int value" */ (m)
   519  	_ = max(x, m /* ERROR "invalid argument: mismatched types int (previous argument) and myint (type of m)" */ , x)
   520  
   521  	_ = max(1, x)
   522  	_ = max(1.0, x)
   523  	_ = max(1.2 /* ERROR "1.2 (untyped float constant) truncated to int" */ , x)
   524  	_ = max(-10, 1.0, c /* ERROR "cannot be ordered" */ )
   525  
   526  	const (
   527  		_ = max /* ERROR "max(x) (value of type int) is not constant" */ (x)
   528  		_ = max(true /* ERROR "invalid argument: true (untyped bool constant) cannot be ordered" */ )
   529  		_ = max(1)
   530  		_ = max(1, 2.3, 'a')
   531  		_ = max(1, "foo" /* ERROR "mismatched types" */ )
   532  		_ = max(1, 0i /* ERROR "cannot be ordered" */ )
   533  		_ = max(1, 2 /* ERROR "cannot be ordered" */ + 3i )
   534  	)
   535  }
   536  
   537  func max2() {
   538  	_ = assert(max(0) == 0)
   539  	_ = assert(max(0, 1) == 1)
   540  	_ = assert(max(0, -10, 123456789) == 123456789)
   541  	_ = assert(max(-12345678901234567890, 0) == 0)
   542  
   543  	_ = assert(max(1, 2.3) == 2.3)
   544  	_ = assert(max(1, 2.3, 'a') == 'a')
   545  
   546  	_ = assert(max("", "a") == "a")
   547  	_ = assert(max("abcde", "xyz", "foo", "bar") == "xyz")
   548  
   549  	const (
   550  		_ int = max(1.0)
   551  		_ float32 = max(1, 2)
   552  		_ int = max /* ERROR "cannot use max(1, 2.3) (untyped float constant 2.3) as int value" */ (1, 2.3)
   553  		_ int = max(1.2, 3) // ok!
   554  		_ byte = max(1, 'a')
   555  	)
   556  }
   557  
   558  func min1() {
   559  	var b bool
   560  	var c complex128
   561  	var x int
   562  	var s string
   563  	type myint int
   564  	var m myint
   565  	_ = min() /* ERROR "not enough arguments" */
   566  	_ = min(b /* ERROR "cannot be ordered" */ )
   567  	_ = min(c /* ERROR "cannot be ordered" */ )
   568  	_ = min(x)
   569  	_ = min(s)
   570  	_ = min(x, x)
   571  	_ = min(x, x, x, x, x)
   572  	var _ int = min /* ERROR "cannot use min(m) (value of type myint) as int value" */ (m)
   573  	_ = min(x, m /* ERROR "invalid argument: mismatched types int (previous argument) and myint (type of m)" */ , x)
   574  
   575  	_ = min(1, x)
   576  	_ = min(1.0, x)
   577  	_ = min(1.2 /* ERROR "1.2 (untyped float constant) truncated to int" */ , x)
   578  	_ = min(-10, 1.0, c /* ERROR "cannot be ordered" */ )
   579  
   580  	const (
   581  		_ = min /* ERROR "min(x) (value of type int) is not constant" */ (x)
   582  		_ = min(true /* ERROR "invalid argument: true (untyped bool constant) cannot be ordered" */ )
   583  		_ = min(1)
   584  		_ = min(1, 2.3, 'a')
   585  		_ = min(1, "foo" /* ERROR "mismatched types" */ )
   586  		_ = min(1, 0i /* ERROR "cannot be ordered" */ )
   587  		_ = min(1, 2 /* ERROR "cannot be ordered" */ + 3i )
   588  	)
   589  }
   590  
   591  func min2() {
   592  	_ = assert(min(0) == 0)
   593  	_ = assert(min(0, 1) == 0)
   594  	_ = assert(min(0, -10, 123456789) == -10)
   595  	_ = assert(min(-12345678901234567890, 0) == -12345678901234567890)
   596  
   597  	_ = assert(min(1, 2.3) == 1)
   598  	_ = assert(min(1, 2.3, 'a') == 1)
   599  
   600  	_ = assert(min("", "a") == "")
   601  	_ = assert(min("abcde", "xyz", "foo", "bar") == "abcde")
   602  
   603  	const (
   604  		_ int = min(1.0)
   605  		_ float32 = min(1, 2)
   606  		_ int = min(1, 2.3) // ok!
   607  		_ int = min /* ERROR "cannot use min(1.2, 3) (untyped float constant 1.2) as int value" */ (1.2, 3)
   608  		_ byte = min(1, 'a')
   609  	)
   610  }
   611  
   612  func new1() {
   613  	_ = new() // ERROR "not enough arguments"
   614  	_ = new(1, 2) // ERROR "too many arguments"
   615  	_ = new("foo" /* ERROR "not a type" */)
   616  	p := new(float64)
   617  	_ = new(struct{ x, y int })
   618  	q := new(*float64)
   619  	_ = *p == **q
   620  	new /* ERROR "not used" */ (int)
   621          _ = &new /* ERROR "cannot take address" */ (int)
   622  
   623  	_ = new(int... /* ERROR "invalid use of ..." */ )
   624  }
   625  
   626  func new2() {
   627  	f1 := func() (x []int) { return }
   628  	_ = new(f0 /* ERROR "not a type" */ ())
   629  	_ = new(f1 /* ERROR "not a type" */ ())
   630  }
   631  
   632  func panic1() {
   633  	panic() // ERROR "not enough arguments"
   634  	panic(1, 2) // ERROR "too many arguments"
   635  	panic(0)
   636  	panic("foo")
   637  	panic(false)
   638  	panic(1<<10)
   639  	panic(1 << /* ERROR "constant shift overflow" */ 1000)
   640  	_ = panic /* ERROR "used as value" */ (0)
   641  
   642  	var s []byte
   643  	panic(s)
   644  	panic(s... /* ERROR "invalid use of ..." */ )
   645  }
   646  
   647  func panic2() {
   648  	f1 := func() (x int) { return }
   649  	f2 := func() (x, y int) { return }
   650  	panic(f0 /* ERROR "used as value" */ ())
   651  	panic(f1())
   652  	panic(f2()) // ERROR "too many arguments"
   653  }
   654  
   655  func print1() {
   656  	print()
   657  	print(1)
   658  	print(1, 2)
   659  	print("foo")
   660  	print(2.718281828)
   661  	print(false)
   662  	print(1<<10)
   663  	print(1 << /* ERROR "constant shift overflow" */ 1000)
   664  	println(nil /* ERROR "untyped nil" */ )
   665  
   666  	var s []int
   667  	print(s... /* ERROR "invalid use of ..." */ )
   668  	_ = print /* ERROR "used as value" */ ()
   669  }
   670  
   671  func print2() {
   672  	f1 := func() (x int) { return }
   673  	f2 := func() (x, y int) { return }
   674  	f3 := func() (x int, y float32, z string) { return }
   675  	print(f0 /* ERROR "used as value" */ ())
   676  	print(f1())
   677  	print(f2())
   678  	print(f3())
   679  }
   680  
   681  func println1() {
   682  	println()
   683  	println(1)
   684  	println(1, 2)
   685  	println("foo")
   686  	println(2.718281828)
   687  	println(false)
   688  	println(1<<10)
   689  	println(1 << /* ERROR "constant shift overflow" */ 1000)
   690  	println(nil /* ERROR "untyped nil" */ )
   691  
   692  	var s []int
   693  	println(s... /* ERROR "invalid use of ..." */ )
   694  	_ = println /* ERROR "used as value" */ ()
   695  }
   696  
   697  func println2() {
   698  	f1 := func() (x int) { return }
   699  	f2 := func() (x, y int) { return }
   700  	f3 := func() (x int, y float32, z string) { return }
   701  	println(f0 /* ERROR "used as value" */ ())
   702  	println(f1())
   703  	println(f2())
   704  	println(f3())
   705  }
   706  
   707  func real1() {
   708  	var f32 float32
   709  	var f64 float64
   710  	var c64 complex64
   711  	var c128 complex128
   712  	_ = real() // ERROR "not enough arguments"
   713  	_ = real(1, 2) // ERROR "too many arguments"
   714  	_ = real(10)
   715  	_ = real(2.7182818)
   716  	_ = real("foo" /* ERROR "expected complex" */)
   717  	const _5 = real(1 + 2i)
   718  	assert(_5 == 1)
   719  	f32 = _5
   720  	f64 = _5
   721  	const _6 = real(0i)
   722  	assert(_6 == 0)
   723  	f32 = real(c64)
   724  	f64 = real(c128)
   725  	f32 = real /* ERRORx `cannot use .* in assignment` */ (c128)
   726  	f64 = real /* ERRORx `cannot use .* in assignment` */ (c64)
   727  	real /* ERROR "not used" */ (c64)
   728  
   729  	// complex type may not be predeclared
   730  	type C64 complex64
   731  	type C128 complex128
   732  	var x64 C64
   733  	var x128 C128
   734  	f32 = imag(x64)
   735  	f64 = imag(x128)
   736  	_, _ = f32, f64
   737  
   738  	var a []complex64
   739  	_ = real(a... /* ERROR "invalid use of ..." */ )
   740  
   741  	// if argument is untyped, result is untyped
   742  	const _ byte = real(1 + 2.3i)
   743  	const _ complex128 = real(1 + 2.3i)
   744  
   745  	// lhs constant shift operands are typed as complex128
   746  	var s uint
   747  	_ = real(1 /* ERROR "must be integer" */ << s)
   748  }
   749  
   750  func real2() {
   751  	f1 := func() (x complex128) { return }
   752  	f2 := func() (x, y complex128) { return }
   753  	_ = real(f0 /* ERROR "used as value" */ ())
   754  	_ = real(f1())
   755  	_ = real(f2()) // ERROR "too many arguments"
   756  }
   757  
   758  func recover1() {
   759  	_ = recover()
   760  	_ = recover(10) // ERROR "too many arguments"
   761  	recover()
   762  
   763  	var s []int
   764  	recover(s... /* ERROR "invalid use of ..." */ )
   765  }
   766  
   767  func recover2() {
   768  	f1 := func() (x int) { return }
   769  	f2 := func() (x, y int) { return }
   770  	_ = recover(f0 /* ERROR "used as value" */ ())
   771  	_ = recover(f1()) // ERROR "too many arguments"
   772  	_ = recover(f2()) // ERROR "too many arguments"
   773  }
   774  
   775  // assuming types.DefaultPtrSize == 8
   776  type S0 struct{      // offset
   777  	a bool       //  0
   778  	b rune       //  4
   779  	c *int       //  8
   780  	d bool       // 16
   781  	e complex128 // 24
   782  }                    // 40
   783  
   784  type S1 struct{   // offset
   785  	x float32 //  0
   786  	y string  //  8
   787  	z *S1     // 24
   788  	S0        // 32
   789  }                 // 72
   790  
   791  type S2 struct{ // offset
   792  	*S1     //  0
   793  }               //  8
   794  
   795  type S3 struct { // offset
   796  	a int64  //  0
   797  	b int32  //  8
   798  }                // 16
   799  
   800  type S4 struct { // offset
   801  	S3       //  0
   802  	int32    // 12
   803  }                // 24
   804  
   805  type S5 struct {   // offset
   806  	a [3]int32 //  0
   807  	b int32    // 16
   808  }                  // 16
   809  
   810  func (S2) m() {}
   811  
   812  func Alignof1() {
   813  	var x int
   814  	_ = unsafe.Alignof() // ERROR "not enough arguments"
   815  	_ = unsafe.Alignof(1, 2) // ERROR "too many arguments"
   816  	_ = unsafe.Alignof(int /* ERROR "not an expression" */)
   817  	_ = unsafe.Alignof(42)
   818  	_ = unsafe.Alignof(new(struct{}))
   819  	_ = unsafe.Alignof(1<<10)
   820  	_ = unsafe.Alignof(1 << /* ERROR "constant shift overflow" */ 1000)
   821  	_ = unsafe.Alignof(nil /* ERROR "untyped nil" */ )
   822  	unsafe /* ERROR "not used" */ .Alignof(x)
   823  
   824  	var y S0
   825  	assert(unsafe.Alignof(y.a) == 1)
   826  	assert(unsafe.Alignof(y.b) == 4)
   827  	assert(unsafe.Alignof(y.c) == 8)
   828  	assert(unsafe.Alignof(y.d) == 1)
   829  	assert(unsafe.Alignof(y.e) == 8)
   830  
   831  	var s []byte
   832  	_ = unsafe.Alignof(s)
   833  	_ = unsafe.Alignof(s... /* ERROR "invalid use of ..." */ )
   834  }
   835  
   836  func Alignof2() {
   837  	f1 := func() (x int32) { return }
   838  	f2 := func() (x, y int32) { return }
   839  	_ = unsafe.Alignof(f0 /* ERROR "used as value" */ ())
   840  	assert(unsafe.Alignof(f1()) == 4)
   841  	_ = unsafe.Alignof(f2()) // ERROR "too many arguments"
   842  }
   843  
   844  func Offsetof1() {
   845  	var x struct{ f int }
   846  	_ = unsafe.Offsetof() // ERROR "not enough arguments"
   847  	_ = unsafe.Offsetof(1, 2) // ERROR "too many arguments"
   848  	_ = unsafe.Offsetof(int /* ERROR "not a selector expression" */ )
   849  	_ = unsafe.Offsetof(x /* ERROR "not a selector expression" */ )
   850  	_ = unsafe.Offsetof(nil /* ERROR "not a selector expression" */ )
   851  	_ = unsafe.Offsetof(x.f)
   852  	_ = unsafe.Offsetof((x.f))
   853  	_ = unsafe.Offsetof((((((((x))).f)))))
   854  	unsafe /* ERROR "not used" */ .Offsetof(x.f)
   855  
   856  	var y0 S0
   857  	assert(unsafe.Offsetof(y0.a) == 0)
   858  	assert(unsafe.Offsetof(y0.b) == 4)
   859  	assert(unsafe.Offsetof(y0.c) == 8)
   860  	assert(unsafe.Offsetof(y0.d) == 16)
   861  	assert(unsafe.Offsetof(y0.e) == 24)
   862  
   863  	var y1 S1
   864  	assert(unsafe.Offsetof(y1.x) == 0)
   865  	assert(unsafe.Offsetof(y1.y) == 8)
   866  	assert(unsafe.Offsetof(y1.z) == 24)
   867  	assert(unsafe.Offsetof(y1.S0) == 32)
   868  
   869  	assert(unsafe.Offsetof(y1.S0.a) == 0) // relative to S0
   870  	assert(unsafe.Offsetof(y1.a) == 32)   // relative to S1
   871  	assert(unsafe.Offsetof(y1.b) == 36)   // relative to S1
   872  	assert(unsafe.Offsetof(y1.c) == 40)   // relative to S1
   873  	assert(unsafe.Offsetof(y1.d) == 48)   // relative to S1
   874  	assert(unsafe.Offsetof(y1.e) == 56)   // relative to S1
   875  
   876  	var y1p *S1
   877  	assert(unsafe.Offsetof(y1p.S0) == 32)
   878  
   879  	type P *S1
   880  	var p P = y1p
   881  	assert(unsafe.Offsetof(p.S0) == 32)
   882  
   883  	var y2 S2
   884  	assert(unsafe.Offsetof(y2.S1) == 0)
   885  	_ = unsafe.Offsetof(y2 /* ERROR "embedded via a pointer" */ .x)
   886  	_ = unsafe.Offsetof(y2 /* ERROR "method value" */ .m)
   887  
   888  	var s []byte
   889  	_ = unsafe.Offsetof(s... /* ERROR "invalid use of ..." */ )
   890  }
   891  
   892  func Offsetof2() {
   893  	f1 := func() (x int32) { return }
   894  	f2 := func() (x, y int32) { return }
   895  	_ = unsafe.Offsetof(f0 /* ERROR "not a selector expression" */ ())
   896  	_ = unsafe.Offsetof(f1 /* ERROR "not a selector expression" */ ())
   897  	_ = unsafe.Offsetof(f2 /* ERROR "not a selector expression" */ ())
   898  }
   899  
   900  func Sizeof1() {
   901  	var x int
   902  	_ = unsafe.Sizeof() // ERROR "not enough arguments"
   903  	_ = unsafe.Sizeof(1, 2) // ERROR "too many arguments"
   904  	_ = unsafe.Sizeof(int /* ERROR "not an expression" */)
   905  	_ = unsafe.Sizeof(42)
   906  	_ = unsafe.Sizeof(new(complex128))
   907  	_ = unsafe.Sizeof(1<<10)
   908  	_ = unsafe.Sizeof(1 << /* ERROR "constant shift overflow" */ 1000)
   909  	_ = unsafe.Sizeof(nil /* ERROR "untyped nil" */ )
   910  	unsafe /* ERROR "not used" */ .Sizeof(x)
   911  
   912  	// basic types have size guarantees
   913  	assert(unsafe.Sizeof(byte(0)) == 1)
   914  	assert(unsafe.Sizeof(uint8(0)) == 1)
   915  	assert(unsafe.Sizeof(int8(0)) == 1)
   916  	assert(unsafe.Sizeof(uint16(0)) == 2)
   917  	assert(unsafe.Sizeof(int16(0)) == 2)
   918  	assert(unsafe.Sizeof(uint32(0)) == 4)
   919  	assert(unsafe.Sizeof(int32(0)) == 4)
   920  	assert(unsafe.Sizeof(float32(0)) == 4)
   921  	assert(unsafe.Sizeof(uint64(0)) == 8)
   922  	assert(unsafe.Sizeof(int64(0)) == 8)
   923  	assert(unsafe.Sizeof(float64(0)) == 8)
   924  	assert(unsafe.Sizeof(complex64(0)) == 8)
   925  	assert(unsafe.Sizeof(complex128(0)) == 16)
   926  
   927  	var y0 S0
   928  	assert(unsafe.Sizeof(y0.a) == 1)
   929  	assert(unsafe.Sizeof(y0.b) == 4)
   930  	assert(unsafe.Sizeof(y0.c) == 8)
   931  	assert(unsafe.Sizeof(y0.d) == 1)
   932  	assert(unsafe.Sizeof(y0.e) == 16)
   933  	assert(unsafe.Sizeof(y0) == 40)
   934  
   935  	var y1 S1
   936  	assert(unsafe.Sizeof(y1) == 72)
   937  
   938  	var y2 S2
   939  	assert(unsafe.Sizeof(y2) == 8)
   940  
   941  	var y3 S3
   942  	assert(unsafe.Sizeof(y3) == 16)
   943  
   944  	var y4 S4
   945  	assert(unsafe.Sizeof(y4) == 24)
   946  
   947  	var y5 S5
   948  	assert(unsafe.Sizeof(y5) == 16)
   949  
   950  	var a3 [10]S3
   951  	assert(unsafe.Sizeof(a3) == 160)
   952  
   953  	// test case for issue 5670
   954  	type T struct {
   955  		a int32
   956  		_ int32
   957  		c int32
   958  	}
   959  	assert(unsafe.Sizeof(T{}) == 12)
   960  
   961  	var s []byte
   962  	_ = unsafe.Sizeof(s)
   963  	_ = unsafe.Sizeof(s... /* ERROR "invalid use of ..." */ )
   964  }
   965  
   966  func Sizeof2() {
   967  	f1 := func() (x int64) { return }
   968  	f2 := func() (x, y int64) { return }
   969  	_ = unsafe.Sizeof(f0 /* ERROR "used as value" */ ())
   970  	assert(unsafe.Sizeof(f1()) == 8)
   971  	_ = unsafe.Sizeof(f2()) // ERROR "too many arguments"
   972  }
   973  
   974  func Slice1() {
   975  	var x int
   976  	unsafe.Slice()        // ERROR "not enough arguments"
   977  	unsafe.Slice(1, 2, 3) // ERROR "too many arguments"
   978  	unsafe.Slice(1 /* ERROR "is not a pointer" */ , 2)
   979  	unsafe.Slice(nil /* ERROR "nil is not a pointer" */ , 0)
   980  	unsafe.Slice(&x, "foo" /* ERRORx `cannot convert .* to type int` */ )
   981  	unsafe.Slice(&x, 1.2 /* ERROR "truncated to int" */ )
   982  	unsafe.Slice(&x, - /* ERROR "must not be negative" */ 1)
   983  	unsafe /* ERROR "not used" */ .Slice(&x, 0)
   984  	var _ []byte = unsafe /* ERROR "value of type []int" */ .Slice(&x, 0)
   985  
   986  	var _ []int = unsafe.Slice(&x, 0)
   987  	_ = unsafe.Slice(&x, 1.0)
   988  	_ = unsafe.Slice((*int)(nil), 0)
   989  }
   990  
   991  func SliceData1() {
   992  	var s []int
   993  	unsafe.SliceData(0 /* ERROR "not a slice" */)
   994  	unsafe /* ERROR "not used" */ .SliceData(s)
   995  
   996  	type S []int
   997  	_ = unsafe.SliceData(s)
   998  	_ = unsafe.SliceData(S{})
   999  }
  1000  
  1001  func String1() {
  1002  	var b byte
  1003  	unsafe.String()        // ERROR "not enough arguments"
  1004  	unsafe.String(1, 2, 3) // ERROR "too many arguments"
  1005  	unsafe.String(1 /* ERROR "cannot use 1" */ , 2)
  1006  	unsafe.String(&b, "foo" /* ERRORx `cannot convert .* to type int` */ )
  1007  	unsafe.String(&b, 1.2 /* ERROR "truncated to int" */ )
  1008  	unsafe.String(&b, - /* ERROR "must not be negative" */ 1)
  1009  	unsafe /* ERROR "not used" */ .String(&b, 0)
  1010  	var _ []byte = unsafe /* ERROR "value of type string" */ .String(&b, 0)
  1011  
  1012  	var _ string = unsafe.String(&b, 0)
  1013  	_ = unsafe.String(&b, 1.0)
  1014  	_ = unsafe.String(nil, 0) // here we allow nil as ptr argument (in contrast to unsafe.Slice)
  1015  }
  1016  
  1017  func StringData1() {
  1018  	var s string
  1019  	type S string
  1020  	unsafe.StringData(0 /* ERROR "cannot use 0" */)
  1021  	unsafe.StringData(S /* ERROR "cannot use S" */ ("foo"))
  1022  	unsafe /* ERROR "not used" */ .StringData(s)
  1023  
  1024  	_ = unsafe.StringData(s)
  1025  	_ = unsafe.StringData("foo")
  1026  }
  1027  
  1028  // self-testing only
  1029  func assert1() {
  1030  	var x int
  1031  	assert() /* ERROR "not enough arguments" */
  1032  	assert(1, 2) /* ERROR "too many arguments" */
  1033  	assert("foo" /* ERROR "boolean constant" */ )
  1034  	assert(x /* ERROR "boolean constant" */)
  1035  	assert(true)
  1036  	assert /* ERROR "failed" */ (false)
  1037  	_ = assert(true)
  1038  
  1039  	var s []byte
  1040  	assert(s... /* ERROR "invalid use of ..." */ )
  1041  }
  1042  
  1043  func assert2() {
  1044  	f1 := func() (x bool) { return }
  1045  	f2 := func() (x bool) { return }
  1046  	assert(f0 /* ERROR "used as value" */ ())
  1047  	assert(f1 /* ERROR "boolean constant" */ ())
  1048  	assert(f2 /* ERROR "boolean constant" */ ())
  1049  }
  1050  
  1051  // self-testing only
  1052  func trace1() {
  1053  	// Uncomment the code below to test trace - will produce console output
  1054  	// _ = trace /* ERROR "no value" */ ()
  1055  	// _ = trace(1)
  1056  	// _ = trace(true, 1.2, '\'', "foo", 42i, "foo" <= "bar")
  1057  
  1058  	var s []byte
  1059  	trace(s... /* ERROR "invalid use of ..." */ )
  1060  }
  1061  
  1062  func trace2() {
  1063  	f1 := func() (x int) { return }
  1064  	f2 := func() (x int, y string) { return }
  1065  	f3 := func() (x int, y string, z []int) { return }
  1066  	_ = f1
  1067  	_ = f2
  1068  	_ = f3
  1069  	// Uncomment the code below to test trace - will produce console output
  1070  	// trace(f0())
  1071  	// trace(f1())
  1072  	// trace(f2())
  1073  	// trace(f3())
  1074  	// trace(f0(), 1)
  1075  	// trace(f1(), 1, 2)
  1076  	// trace(f2(), 1, 2, 3)
  1077  	// trace(f3(), 1, 2, 3, 4)
  1078  }
  1079  

View as plain text