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

     1  // Copyright 2020 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  // This file tests built-in calls on generic types.
     6  
     7  package builtins
     8  
     9  import "unsafe"
    10  
    11  // clear
    12  
    13  func _[T any](x T) {
    14  	clear(x /* ERROR "cannot clear x" */)
    15  }
    16  
    17  func _[T ~map[int]string | ~[]byte](x T) {
    18  	clear(x)
    19  }
    20  
    21  func _[T ~map[int]string | ~[]byte | ~*[10]int | string](x T) {
    22  	clear(x /* ERROR "cannot clear x" */)
    23  }
    24  
    25  // close
    26  
    27  type C0 interface{ int }
    28  type C1 interface{ chan int }
    29  type C2 interface{ chan int | <-chan int }
    30  type C3 interface{ chan int | chan float32 }
    31  type C4 interface{ chan int | chan<- int }
    32  type C5[T any] interface{ ~chan T | chan<- T }
    33  
    34  func _[T any](ch T) {
    35  	close(ch /* ERROR "cannot close non-channel" */)
    36  }
    37  
    38  func _[T C0](ch T) {
    39  	close(ch /* ERROR "cannot close non-channel" */)
    40  }
    41  
    42  func _[T C1](ch T) {
    43  	close(ch)
    44  }
    45  
    46  func _[T C2](ch T) {
    47  	close(ch /* ERROR "cannot close receive-only channel" */)
    48  }
    49  
    50  func _[T C3](ch T) {
    51  	close(ch)
    52  }
    53  
    54  func _[T C4](ch T) {
    55  	close(ch)
    56  }
    57  
    58  func _[T C5[X], X any](ch T) {
    59  	close(ch)
    60  }
    61  
    62  // copy
    63  
    64  func _[T any](x, y T) {
    65  	copy(x /* ERROR "invalid copy: argument must be a slice; have x (variable of type T constrained by any)" */ , y)
    66  }
    67  
    68  func _[T ~[]byte](x, y T) {
    69  	copy(x, y)
    70  	copy(x, "foo")
    71  	copy("foo" /* ERROR "argument must be a slice; have \"foo\" (untyped string constant)" */ , y)
    72  
    73  	var x2 []byte
    74  	copy(x2, y) // element types are identical
    75  	copy(y, x2) // element types are identical
    76  
    77  	type myByte byte
    78  	var x3 []myByte
    79  	copy(x3 /* ERROR "different element types" */ , y)
    80  	copy(y /* ERROR "different element types" */ , x3)
    81  }
    82  
    83  func _[T ~[]E, E any](x T, y []E) {
    84  	copy(x, y)
    85  	copy(x /* ERROR "arguments x (variable of type T constrained by ~[]E) and \"foo\" (untyped string constant) have different element types E and byte" */ , "foo")
    86  }
    87  
    88  func _[T ~string](x []byte, y T) {
    89  	copy(x, y)
    90  	copy([ /* ERROR "arguments []int{} (value of type []int) and y (variable of type T constrained by ~string) have different element types int and byte" */ ]int{}, y)
    91  	copy(y /* ERROR "argument must be a slice; have y (variable of type T constrained by ~string)" */ , x)
    92  }
    93  
    94  func _[T ~[]byte|~string](x T, y []byte) {
    95  	copy(x /* ERROR "argument must be a slice; have x (variable of type T constrained by ~[]byte | ~string)" */ , y)
    96  	copy(y, x)
    97  }
    98  
    99  type L0 []int
   100  type L1 []int
   101  
   102  func _[T L0 | L1](x, y T) {
   103  	copy(x, y)
   104  }
   105  
   106  // delete
   107  
   108  type M0 interface{ int }
   109  type M1 interface{ map[string]int }
   110  type M2 interface { map[string]int | map[string]float64 }
   111  type M3 interface{ map[string]int | map[rune]int }
   112  type M4[K comparable, V any] interface{ map[K]V | map[rune]V }
   113  
   114  func _[T any](m T) {
   115  	delete(m /* ERROR "not a map" */, "foo")
   116  }
   117  
   118  func _[T M0](m T) {
   119  	delete(m /* ERROR "not a map" */, "foo")
   120  }
   121  
   122  func _[T M1](m T) {
   123  	delete(m, "foo")
   124  }
   125  
   126  func _[T M2](m T) {
   127  	delete(m, "foo")
   128  	delete(m, 0 /* ERRORx `cannot use .* as string` */)
   129  }
   130  
   131  func _[T M3](m T) {
   132  	delete(m /* ERROR "must have identical key types" */, "foo")
   133  }
   134  
   135  func _[T M4[rune, V], V any](m T) {
   136  	delete(m, 'k')
   137  }
   138  
   139  func _[T M4[K, V], K comparable, V any](m T) {
   140  	delete(m /* ERROR "must have identical key types" */, "foo")
   141  }
   142  
   143  // make
   144  
   145  type myChan chan int
   146  
   147  func _[
   148  	A1 ~[10]byte,
   149  	A2 ~[]byte | ~[10]byte,
   150  
   151  	S1 ~[]int,
   152  	S2 ~[]int | ~chan int,
   153  
   154  	M1 ~map[string]int,
   155  	M2 ~map[string]int | ~chan int,
   156  
   157  	C1 ~chan int,
   158  	C2 ~chan int | ~chan string,
   159  	C3 chan int | myChan,     // single underlying type
   160  	C4 chan int | chan<- int, // channels may have different (non-conflicting) directions
   161  	C5 <-chan int | chan<- int,
   162  ]() {
   163  	type A0 [10]byte
   164  	_ = make([ /* ERROR "cannot make [10]byte: type must be slice, map, or channel" */ 10]byte)
   165  	_ = make(A1 /* ERROR "cannot make A1: type must be slice, map, or channel" */ )
   166  	_ = make(A2 /* ERROR "cannot make A2: type must be slice, map, or channel" */ )
   167  
   168  	type S0 []int
   169  	_ = make([]int, 10)
   170  	_ = make(S0, 10)
   171  	_ = make(S1, 10)
   172  	_ = make() /* ERROR "not enough arguments" */
   173  	_ = make /* ERROR "expects 2 or 3 arguments" */ (S1)
   174  	_ = make(S1, 10, 20)
   175  	_ = make /* ERROR "expects 2 or 3 arguments" */ (S1, 10, 20, 30)
   176  	_ = make(S2 /* ERROR "cannot make S2: []int and chan int have different underlying types" */ , 10)
   177  
   178  	type M0 map[string]int
   179  	_ = make(map[string]int)
   180  	_ = make(M0)
   181  	_ = make(M1)
   182  	_ = make(M1, 10)
   183  	_ = make/* ERROR "expects 1 or 2 arguments" */(M1, 10, 20)
   184  	_ = make(M2 /* ERROR "cannot make M2: map[string]int and chan int have different underlying types" */ )
   185  
   186  	type C0 chan int
   187  	_ = make(chan int)
   188  	_ = make(C0)
   189  	_ = make(C1)
   190  	_ = make(C1, 10)
   191  	_ = make/* ERROR "expects 1 or 2 arguments" */(C1, 10, 20)
   192  	_ = make(C2 /* ERROR "cannot make C2: channels chan int and chan string have different element types" */ )
   193  	_ = make(C3)
   194  	_ = make(C4)
   195  	_ = make(C5 /* ERROR "cannot make C5: channels <-chan int and chan<- int have conflicting directions" */ )
   196  }
   197  
   198  // max
   199  
   200  func _[
   201  	P1 ~int|~float64,
   202  	P2 ~int|~string|~uint,
   203  	P3 ~int|bool,
   204  ]() {
   205  	var x1 P1
   206  	_ = max(x1)
   207  	_ = max(x1, x1)
   208  	_ = max(1, x1, 2)
   209  	const _ = max /* ERROR "max(1, x1, 2) (value of type P1 constrained by ~int | ~float64) is not constant" */ (1, x1, 2)
   210  
   211  	var x2 P2
   212  	_ = max(x2)
   213  	_ = max(x2, x2)
   214  	_ = max(1, 2 /* ERROR "cannot convert 2 (untyped int constant) to type P2" */, x2) // error at 2 because max is 2
   215  
   216  	_ = max(x1, x2 /* ERROR "mismatched types P1 (previous argument) and P2 (type of x2)" */ )
   217  }
   218  
   219  // min
   220  
   221  func _[
   222  	P1 ~int|~float64,
   223  	P2 ~int|~string|~uint,
   224  	P3 ~int|bool,
   225  ]() {
   226  	var x1 P1
   227  	_ = min(x1)
   228  	_ = min(x1, x1)
   229  	_ = min(1, x1, 2)
   230  	const _ = min /* ERROR "min(1, x1, 2) (value of type P1 constrained by ~int | ~float64) is not constant" */ (1, x1, 2)
   231  
   232  	var x2 P2
   233  	_ = min(x2)
   234  	_ = min(x2, x2)
   235  	_ = min(1 /* ERROR "cannot convert 1 (untyped int constant) to type P2" */ , 2, x2) // error at 1 because min is 1
   236  
   237  	_ = min(x1, x2 /* ERROR "mismatched types P1 (previous argument) and P2 (type of x2)" */ )
   238  }
   239  
   240  // unsafe.Alignof
   241  
   242  func _[T comparable]() {
   243  	var (
   244  		b int64
   245  		a [10]T
   246  		s struct{ f T }
   247  		p *T
   248  		l []T
   249  		f func(T)
   250  		i interface{ m() T }
   251  		c chan T
   252  		m map[T]T
   253  		t T
   254  	)
   255  
   256  	const bb = unsafe.Alignof(b)
   257  	assert(bb == 8)
   258  	const _ = unsafe /* ERROR "not constant" */ .Alignof(a)
   259  	const _ = unsafe /* ERROR "not constant" */ .Alignof(s)
   260  	const pp = unsafe.Alignof(p)
   261  	assert(pp == 8)
   262  	const ll = unsafe.Alignof(l)
   263  	assert(ll == 8)
   264  	const ff = unsafe.Alignof(f)
   265  	assert(ff == 8)
   266  	const ii = unsafe.Alignof(i)
   267  	assert(ii == 8)
   268  	const cc = unsafe.Alignof(c)
   269  	assert(cc == 8)
   270  	const mm = unsafe.Alignof(m)
   271  	assert(mm == 8)
   272  	const _ = unsafe /* ERROR "not constant" */ .Alignof(t)
   273  }
   274  
   275  // unsafe.Offsetof
   276  
   277  func _[T comparable]() {
   278  	var (
   279  		b struct{ _, f int64 }
   280  		a struct{ _, f [10]T }
   281  		s struct{ _, f struct{ f T } }
   282  		p struct{ _, f *T }
   283  		l struct{ _, f []T }
   284  		f struct{ _, f func(T) }
   285  		i struct{ _, f interface{ m() T } }
   286  		c struct{ _, f chan T }
   287  		m struct{ _, f map[T]T }
   288  		t struct{ _, f T }
   289  	)
   290  
   291  	const bb = unsafe.Offsetof(b.f)
   292  	assert(bb == 8)
   293  	const _ = unsafe /* ERROR "not constant" */ .Alignof(a)
   294  	const _ = unsafe /* ERROR "not constant" */ .Alignof(s)
   295  	const pp = unsafe.Offsetof(p.f)
   296  	assert(pp == 8)
   297  	const ll = unsafe.Offsetof(l.f)
   298  	assert(ll == 24)
   299  	const ff = unsafe.Offsetof(f.f)
   300  	assert(ff == 8)
   301  	const ii = unsafe.Offsetof(i.f)
   302  	assert(ii == 16)
   303  	const cc = unsafe.Offsetof(c.f)
   304  	assert(cc == 8)
   305  	const mm = unsafe.Offsetof(m.f)
   306  	assert(mm == 8)
   307  	const _ = unsafe /* ERROR "not constant" */ .Alignof(t)
   308  }
   309  
   310  // unsafe.Sizeof
   311  
   312  func _[T comparable]() {
   313  	var (
   314  		b int64
   315  		a [10]T
   316  		s struct{ f T }
   317  		p *T
   318  		l []T
   319  		f func(T)
   320  		i interface{ m() T }
   321  		c chan T
   322  		m map[T]T
   323  		t T
   324  	)
   325  
   326  	const bb = unsafe.Sizeof(b)
   327  	assert(bb == 8)
   328  	const _ = unsafe /* ERROR "not constant" */ .Alignof(a)
   329  	const _ = unsafe /* ERROR "not constant" */ .Alignof(s)
   330  	const pp = unsafe.Sizeof(p)
   331  	assert(pp == 8)
   332  	const ll = unsafe.Sizeof(l)
   333  	assert(ll == 24)
   334  	const ff = unsafe.Sizeof(f)
   335  	assert(ff == 8)
   336  	const ii = unsafe.Sizeof(i)
   337  	assert(ii == 16)
   338  	const cc = unsafe.Sizeof(c)
   339  	assert(cc == 8)
   340  	const mm = unsafe.Sizeof(m)
   341  	assert(mm == 8)
   342  	const _ = unsafe /* ERROR "not constant" */ .Alignof(t)
   343  }
   344  

View as plain text