...

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

Documentation: internal/types/testdata/check

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

View as plain text