...

Source file src/internal/types/testdata/spec/assignability.go

Documentation: internal/types/testdata/spec

     1  // Copyright 2021 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  package assignability
     6  
     7  // See the end of this package for the declarations
     8  // of the types and variables used in these tests.
     9  
    10  // "x's type is identical to T"
    11  func _[TP any](X TP) {
    12  	b = b
    13  	a = a
    14  	l = l
    15  	s = s
    16  	p = p
    17  	f = f
    18  	i = i
    19  	m = m
    20  	c = c
    21  	d = d
    22  
    23  	B = B
    24  	A = A
    25  	L = L
    26  	S = S
    27  	P = P
    28  	F = F
    29  	I = I
    30  	M = M
    31  	C = C
    32  	D = D
    33  	X = X
    34  }
    35  
    36  // "x's type V and T have identical underlying types
    37  // and at least one of V or T is not a named type."
    38  // (here a named type is a type with a name)
    39  func _[TP1, TP2 Interface](X1 TP1, X2 TP2) {
    40  	b = B // ERRORx `cannot use B .* as (int|_Basic.*) value`
    41  	a = A
    42  	l = L
    43  	s = S
    44  	p = P
    45  	f = F
    46  	i = I
    47  	m = M
    48  	c = C
    49  	d = D
    50  
    51  	B = b // ERRORx `cannot use b .* as Basic value`
    52  	A = a
    53  	L = l
    54  	S = s
    55  	P = p
    56  	F = f
    57  	I = i
    58  	M = m
    59  	C = c
    60  	D = d
    61  	X1 = i  // ERRORx `cannot use i .* as TP1 value`
    62  	X1 = X2 // ERRORx `cannot use X2 .* as TP1 value`
    63  }
    64  
    65  // "T is an interface type and x implements T and T is not a type parameter"
    66  func _[TP Interface](X TP) {
    67  	i = d // ERROR "missing method m"
    68  	i = D
    69  	i = X
    70  	X = i // ERRORx `cannot use i .* as TP value`
    71  }
    72  
    73  // "x is a bidirectional channel value, T is a channel type, x's type V and T have identical element types, and at least one of V or T is not a named type"
    74  // (here a named type is a type with a name)
    75  type (
    76  	_SendChan = chan<- int
    77  	_RecvChan = <-chan int
    78  
    79  	SendChan _SendChan
    80  	RecvChan _RecvChan
    81  )
    82  
    83  func _[
    84  	_CC ~_Chan,
    85  	_SC ~_SendChan,
    86  	_RC ~_RecvChan,
    87  
    88  	CC Chan,
    89  	SC SendChan,
    90  	RC RecvChan,
    91  ]() {
    92  	var (
    93  		_ _SendChan = c
    94  		_ _RecvChan = c
    95  		_ _Chan     = c
    96  
    97  		_ _SendChan = C
    98  		_ _RecvChan = C
    99  		_ _Chan     = C
   100  
   101  		_ SendChan = c
   102  		_ RecvChan = c
   103  		_ Chan     = c
   104  
   105  		_ SendChan = C // ERRORx `cannot use C .* as SendChan value`
   106  		_ RecvChan = C // ERRORx `cannot use C .* as RecvChan value`
   107  		_ Chan     = C
   108  		_ Chan     = make /* ERRORx `cannot use make\(chan Basic\) .* as Chan value` */ (chan Basic)
   109  	)
   110  
   111  	var (
   112  		_ _CC = C // ERRORx `cannot use C .* as _CC value`
   113  		_ _SC = C // ERRORx `cannot use C .* as _SC value`
   114  		_ _RC = C // ERRORx `cannot use C .* as _RC value`
   115  
   116  		_ CC = _CC /* ERRORx `cannot use _CC\(nil\) .* as CC value` */ (nil)
   117  		_ SC = _CC /* ERRORx `cannot use _CC\(nil\) .* as SC value` */ (nil)
   118  		_ RC = _CC /* ERRORx `cannot use _CC\(nil\) .* as RC value` */ (nil)
   119  
   120  		_ CC = C // ERRORx `cannot use C .* as CC value`
   121  		_ SC = C // ERRORx `cannot use C .* as SC value`
   122  		_ RC = C // ERRORx `cannot use C .* as RC value`
   123  	)
   124  }
   125  
   126  // "x's type V is not a named type and T is a type parameter, and x is assignable to each specific type in T's type set."
   127  func _[
   128  	TP0 any,
   129  	TP1 ~_Chan,
   130  	TP2 ~chan int | ~chan byte,
   131  ]() {
   132  	var (
   133  		_ TP0 = c // ERRORx `cannot use c .* as TP0 value`
   134  		_ TP0 = C // ERRORx `cannot use C .* as TP0 value`
   135  		_ TP1 = c
   136  		_ TP1 = C // ERRORx `cannot use C .* as TP1 value`
   137  		_ TP2 = c // ERRORx `.* cannot assign (chan int|_Chan.*) to chan byte`
   138  	)
   139  }
   140  
   141  // "x's type V is a type parameter and T is not a named type, and values x' of each specific type in V's type set are assignable to T."
   142  func _[
   143  	TP0 Interface,
   144  	TP1 ~_Chan,
   145  	TP2 ~chan int | ~chan byte,
   146  ](X0 TP0, X1 TP1, X2 TP2) {
   147  	i = X0
   148  	I = X0
   149  	c = X1
   150  	C = X1 // ERRORx `cannot use X1 .* as Chan value`
   151  	c = X2 // ERRORx `.* cannot assign chan byte \(in TP2\) to (chan int|_Chan.*)`
   152  }
   153  
   154  // "x is the predeclared identifier nil and T is a pointer, function, slice, map, channel, or interface type"
   155  func _[TP Interface](X TP) {
   156  	b = nil // ERROR "cannot use nil"
   157  	a = nil // ERROR "cannot use nil"
   158  	l = nil
   159  	s = nil // ERROR "cannot use nil"
   160  	p = nil
   161  	f = nil
   162  	i = nil
   163  	m = nil
   164  	c = nil
   165  	d = nil // ERROR "cannot use nil"
   166  
   167  	B = nil // ERROR "cannot use nil"
   168  	A = nil // ERROR "cannot use nil"
   169  	L = nil
   170  	S = nil // ERROR "cannot use nil"
   171  	P = nil
   172  	F = nil
   173  	I = nil
   174  	M = nil
   175  	C = nil
   176  	D = nil // ERROR "cannot use nil"
   177  	X = nil // ERROR "cannot use nil"
   178  }
   179  
   180  // "x is an untyped constant representable by a value of type T"
   181  func _[
   182  	Int8 ~int8,
   183  	Int16 ~int16,
   184  	Int32 ~int32,
   185  	Int64 ~int64,
   186  	Int8_16 ~int8 | ~int16,
   187  ](
   188  	i8 Int8,
   189  	i16 Int16,
   190  	i32 Int32,
   191  	i64 Int64,
   192  	i8_16 Int8_16,
   193  ) {
   194  	b = 42
   195  	b = 42.0
   196  	// etc.
   197  
   198  	i8 = -1 << 7
   199  	i8 = 1<<7 - 1
   200  	i16 = -1 << 15
   201  	i16 = 1<<15 - 1
   202  	i32 = -1 << 31
   203  	i32 = 1<<31 - 1
   204  	i64 = -1 << 63
   205  	i64 = 1<<63 - 1
   206  
   207  	i8_16 = -1 << 7
   208  	i8_16 = 1<<7 - 1
   209  	i8_16 = - /* ERRORx `cannot use .* as Int8_16` */ 1 << 15
   210  	i8_16 = 1 /* ERRORx `cannot use .* as Int8_16` */ <<15 - 1
   211  }
   212  
   213  // proto-types for tests
   214  
   215  type (
   216  	_Basic     = int
   217  	_Array     = [10]int
   218  	_Slice     = []int
   219  	_Struct    = struct{ f int }
   220  	_Pointer   = *int
   221  	_Func      = func(x int) string
   222  	_Interface = interface{ m() int }
   223  	_Map       = map[string]int
   224  	_Chan      = chan int
   225  
   226  	Basic     _Basic
   227  	Array     _Array
   228  	Slice     _Slice
   229  	Struct    _Struct
   230  	Pointer   _Pointer
   231  	Func      _Func
   232  	Interface _Interface
   233  	Map       _Map
   234  	Chan      _Chan
   235  	Defined   _Struct
   236  )
   237  
   238  func (Defined) m() int
   239  
   240  // proto-variables for tests
   241  
   242  var (
   243  	b _Basic
   244  	a _Array
   245  	l _Slice
   246  	s _Struct
   247  	p _Pointer
   248  	f _Func
   249  	i _Interface
   250  	m _Map
   251  	c _Chan
   252  	d _Struct
   253  
   254  	B Basic
   255  	A Array
   256  	L Slice
   257  	S Struct
   258  	P Pointer
   259  	F Func
   260  	I Interface
   261  	M Map
   262  	C Chan
   263  	D Defined
   264  )
   265  

View as plain text