...

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

Documentation: internal/types/testdata/check

     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 "copy expects slice arguments" */ , y)
    66  }
    67  
    68  func _[T ~[]byte](x, y T) {
    69  	copy(x, y)
    70  	copy(x, "foo")
    71  	copy("foo" /* ERROR "expects slice arguments" */ , 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 "different element types" */ , "foo")
    86  }
    87  
    88  func _[T ~string](x []byte, y T) {
    89  	copy(x, y)
    90  	copy(y /* ERROR "expects slice arguments" */ , x)
    91  }
    92  
    93  func _[T ~[]byte|~string](x T, y []byte) {
    94  	copy(x /* ERROR "expects slice arguments" */ , y)
    95  	copy(y, x)
    96  }
    97  
    98  type L0 []int
    99  type L1 []int
   100  
   101  func _[T L0 | L1](x, y T) {
   102  	copy(x, y)
   103  }
   104  
   105  // delete
   106  
   107  type M0 interface{ int }
   108  type M1 interface{ map[string]int }
   109  type M2 interface { map[string]int | map[string]float64 }
   110  type M3 interface{ map[string]int | map[rune]int }
   111  type M4[K comparable, V any] interface{ map[K]V | map[rune]V }
   112  
   113  func _[T any](m T) {
   114  	delete(m /* ERROR "not a map" */, "foo")
   115  }
   116  
   117  func _[T M0](m T) {
   118  	delete(m /* ERROR "not a map" */, "foo")
   119  }
   120  
   121  func _[T M1](m T) {
   122  	delete(m, "foo")
   123  }
   124  
   125  func _[T M2](m T) {
   126  	delete(m, "foo")
   127  	delete(m, 0 /* ERRORx `cannot use .* as string` */)
   128  }
   129  
   130  func _[T M3](m T) {
   131  	delete(m /* ERROR "must have identical key types" */, "foo")
   132  }
   133  
   134  func _[T M4[rune, V], V any](m T) {
   135  	delete(m, 'k')
   136  }
   137  
   138  func _[T M4[K, V], K comparable, V any](m T) {
   139  	delete(m /* ERROR "must have identical key types" */, "foo")
   140  }
   141  
   142  // make
   143  
   144  type myChan chan int
   145  
   146  func _[
   147  	S1 ~[]int,
   148  	S2 ~[]int | ~chan int,
   149  
   150  	M1 ~map[string]int,
   151  	M2 ~map[string]int | ~chan int,
   152  
   153  	C1 ~chan int,
   154  	C2 ~chan int | ~chan string,
   155  	C3 chan int | myChan, // single underlying type
   156  ]() {
   157  	type S0 []int
   158  	_ = make([]int, 10)
   159  	_ = make(S0, 10)
   160  	_ = make(S1, 10)
   161  	_ = make() /* ERROR "not enough arguments" */
   162  	_ = make /* ERROR "expects 2 or 3 arguments" */ (S1)
   163  	_ = make(S1, 10, 20)
   164  	_ = make /* ERROR "expects 2 or 3 arguments" */ (S1, 10, 20, 30)
   165  	_ = make(S2 /* ERROR "cannot make S2: no core type" */ , 10)
   166  
   167  	type M0 map[string]int
   168  	_ = make(map[string]int)
   169  	_ = make(M0)
   170  	_ = make(M1)
   171  	_ = make(M1, 10)
   172  	_ = make/* ERROR "expects 1 or 2 arguments" */(M1, 10, 20)
   173  	_ = make(M2 /* ERROR "cannot make M2: no core type" */ )
   174  
   175  	type C0 chan int
   176  	_ = make(chan int)
   177  	_ = make(C0)
   178  	_ = make(C1)
   179  	_ = make(C1, 10)
   180  	_ = make/* ERROR "expects 1 or 2 arguments" */(C1, 10, 20)
   181  	_ = make(C2 /* ERROR "cannot make C2: no core type" */ )
   182  	_ = make(C3)
   183  }
   184  
   185  // max
   186  
   187  func _[
   188  	P1 ~int|~float64,
   189  	P2 ~int|~string|~uint,
   190  	P3 ~int|bool,
   191  ]() {
   192  	var x1 P1
   193  	_ = max(x1)
   194  	_ = max(x1, x1)
   195  	_ = max(1, x1, 2)
   196  	const _ = max /* ERROR "max(1, x1, 2) (value of type P1 constrained by ~int | ~float64) is not constant" */ (1, x1, 2)
   197  
   198  	var x2 P2
   199  	_ = max(x2)
   200  	_ = max(x2, x2)
   201  	_ = max(1, 2 /* ERROR "cannot convert 2 (untyped int constant) to type P2" */, x2) // error at 2 because max is 2
   202  
   203  	_ = max(x1, x2 /* ERROR "mismatched types P1 (previous argument) and P2 (type of x2)" */ )
   204  }
   205  
   206  // min
   207  
   208  func _[
   209  	P1 ~int|~float64,
   210  	P2 ~int|~string|~uint,
   211  	P3 ~int|bool,
   212  ]() {
   213  	var x1 P1
   214  	_ = min(x1)
   215  	_ = min(x1, x1)
   216  	_ = min(1, x1, 2)
   217  	const _ = min /* ERROR "min(1, x1, 2) (value of type P1 constrained by ~int | ~float64) is not constant" */ (1, x1, 2)
   218  
   219  	var x2 P2
   220  	_ = min(x2)
   221  	_ = min(x2, x2)
   222  	_ = min(1 /* ERROR "cannot convert 1 (untyped int constant) to type P2" */ , 2, x2) // error at 1 because min is 1
   223  
   224  	_ = min(x1, x2 /* ERROR "mismatched types P1 (previous argument) and P2 (type of x2)" */ )
   225  }
   226  
   227  // unsafe.Alignof
   228  
   229  func _[T comparable]() {
   230  	var (
   231  		b int64
   232  		a [10]T
   233  		s struct{ f T }
   234  		p *T
   235  		l []T
   236  		f func(T)
   237  		i interface{ m() T }
   238  		c chan T
   239  		m map[T]T
   240  		t T
   241  	)
   242  
   243  	const bb = unsafe.Alignof(b)
   244  	assert(bb == 8)
   245  	const _ = unsafe /* ERROR "not constant" */ .Alignof(a)
   246  	const _ = unsafe /* ERROR "not constant" */ .Alignof(s)
   247  	const pp = unsafe.Alignof(p)
   248  	assert(pp == 8)
   249  	const ll = unsafe.Alignof(l)
   250  	assert(ll == 8)
   251  	const ff = unsafe.Alignof(f)
   252  	assert(ff == 8)
   253  	const ii = unsafe.Alignof(i)
   254  	assert(ii == 8)
   255  	const cc = unsafe.Alignof(c)
   256  	assert(cc == 8)
   257  	const mm = unsafe.Alignof(m)
   258  	assert(mm == 8)
   259  	const _ = unsafe /* ERROR "not constant" */ .Alignof(t)
   260  }
   261  
   262  // unsafe.Offsetof
   263  
   264  func _[T comparable]() {
   265  	var (
   266  		b struct{ _, f int64 }
   267  		a struct{ _, f [10]T }
   268  		s struct{ _, f struct{ f T } }
   269  		p struct{ _, f *T }
   270  		l struct{ _, f []T }
   271  		f struct{ _, f func(T) }
   272  		i struct{ _, f interface{ m() T } }
   273  		c struct{ _, f chan T }
   274  		m struct{ _, f map[T]T }
   275  		t struct{ _, f T }
   276  	)
   277  
   278  	const bb = unsafe.Offsetof(b.f)
   279  	assert(bb == 8)
   280  	const _ = unsafe /* ERROR "not constant" */ .Alignof(a)
   281  	const _ = unsafe /* ERROR "not constant" */ .Alignof(s)
   282  	const pp = unsafe.Offsetof(p.f)
   283  	assert(pp == 8)
   284  	const ll = unsafe.Offsetof(l.f)
   285  	assert(ll == 24)
   286  	const ff = unsafe.Offsetof(f.f)
   287  	assert(ff == 8)
   288  	const ii = unsafe.Offsetof(i.f)
   289  	assert(ii == 16)
   290  	const cc = unsafe.Offsetof(c.f)
   291  	assert(cc == 8)
   292  	const mm = unsafe.Offsetof(m.f)
   293  	assert(mm == 8)
   294  	const _ = unsafe /* ERROR "not constant" */ .Alignof(t)
   295  }
   296  
   297  // unsafe.Sizeof
   298  
   299  func _[T comparable]() {
   300  	var (
   301  		b int64
   302  		a [10]T
   303  		s struct{ f T }
   304  		p *T
   305  		l []T
   306  		f func(T)
   307  		i interface{ m() T }
   308  		c chan T
   309  		m map[T]T
   310  		t T
   311  	)
   312  
   313  	const bb = unsafe.Sizeof(b)
   314  	assert(bb == 8)
   315  	const _ = unsafe /* ERROR "not constant" */ .Alignof(a)
   316  	const _ = unsafe /* ERROR "not constant" */ .Alignof(s)
   317  	const pp = unsafe.Sizeof(p)
   318  	assert(pp == 8)
   319  	const ll = unsafe.Sizeof(l)
   320  	assert(ll == 24)
   321  	const ff = unsafe.Sizeof(f)
   322  	assert(ff == 8)
   323  	const ii = unsafe.Sizeof(i)
   324  	assert(ii == 16)
   325  	const cc = unsafe.Sizeof(c)
   326  	assert(cc == 8)
   327  	const mm = unsafe.Sizeof(m)
   328  	assert(mm == 8)
   329  	const _ = unsafe /* ERROR "not constant" */ .Alignof(t)
   330  }
   331  

View as plain text