Source file src/internal/types/testdata/check/expr0.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  // unary expressions
     6  
     7  package expr0
     8  
     9  type mybool bool
    10  
    11  var (
    12  	// bool
    13  	b0 = true
    14  	b1 bool = b0
    15  	b2 = !true
    16  	b3 = !b1
    17  	b4 bool = !true
    18  	b5 bool = !b4
    19  	b6 = +b0 /* ERROR "not defined" */
    20  	b7 = -b0 /* ERROR "not defined" */
    21  	b8 = ^b0 /* ERROR "not defined" */
    22  	b9 = *b0 /* ERROR "cannot indirect" */
    23  	b10 = &true /* ERROR "cannot take address" */
    24  	b11 = &b0
    25  	b12 = <-b0 /* ERROR "cannot receive" */
    26  	b13 = & & /* ERROR "cannot take address" */ b0
    27  	b14 = ~ /* ERROR "cannot use ~ outside of interface or type constraint" */ b0
    28  
    29  	// byte
    30  	_ = byte(0)
    31  	_ = byte(- /* ERROR "overflows" */ 1)
    32  	_ = - /* ERROR "-byte(1) (constant -1 of type byte) overflows byte" */ byte(1) // test for issue 11367
    33  	_ = byte /* ERROR "overflows byte" */ (0) - byte(1)
    34  	_ = ~ /* ERROR "cannot use ~ outside of interface or type constraint (use ^ for bitwise complement)" */ byte(0)
    35  
    36  	// int
    37  	i0 = 1
    38  	i1 int = i0
    39  	i2 = +1
    40  	i3 = +i0
    41  	i4 int = +1
    42  	i5 int = +i4
    43  	i6 = -1
    44  	i7 = -i0
    45  	i8 int = -1
    46  	i9 int = -i4
    47  	i10 = !i0 /* ERROR "not defined" */
    48  	i11 = ^1
    49  	i12 = ^i0
    50  	i13 int = ^1
    51  	i14 int = ^i4
    52  	i15 = *i0 /* ERROR "cannot indirect" */
    53  	i16 = &i0
    54  	i17 = *i16
    55  	i18 = <-i16 /* ERROR "cannot receive" */
    56  	i19 = ~ /* ERROR "cannot use ~ outside of interface or type constraint (use ^ for bitwise complement)" */ i0
    57  
    58  	// uint
    59  	u0 = uint(1)
    60  	u1 uint = u0
    61  	u2 = +1
    62  	u3 = +u0
    63  	u4 uint = +1
    64  	u5 uint = +u4
    65  	u6 = -1
    66  	u7 = -u0
    67  	u8 uint = - /* ERROR "overflows" */ 1
    68  	u9 uint = -u4
    69  	u10 = !u0 /* ERROR "not defined" */
    70  	u11 = ^1
    71  	u12 = ^i0
    72  	u13 uint = ^ /* ERROR "overflows" */ 1
    73  	u14 uint = ^u4
    74  	u15 = *u0 /* ERROR "cannot indirect" */
    75  	u16 = &u0
    76  	u17 = *u16
    77  	u18 = <-u16 /* ERROR "cannot receive" */
    78  	u19 = ^uint(0)
    79  	u20 = ~ /* ERROR "cannot use ~ outside of interface or type constraint (use ^ for bitwise complement)" */ u0
    80  
    81  	// float64
    82  	f0 = float64(1)
    83  	f1 float64 = f0
    84  	f2 = +1
    85  	f3 = +f0
    86  	f4 float64 = +1
    87  	f5 float64 = +f4
    88  	f6 = -1
    89  	f7 = -f0
    90  	f8 float64 = -1
    91  	f9 float64 = -f4
    92  	f10 = !f0 /* ERROR "not defined" */
    93  	f11 = ^1
    94  	f12 = ^i0
    95  	f13 float64 = ^1
    96  	f14 float64 = ^f4 /* ERROR "not defined" */
    97  	f15 = *f0 /* ERROR "cannot indirect" */
    98  	f16 = &f0
    99  	f17 = *u16
   100  	f18 = <-u16 /* ERROR "cannot receive" */
   101  	f19 = ~ /* ERROR "cannot use ~ outside of interface or type constraint" */ f0
   102  
   103  	// complex128
   104  	c0 = complex128(1)
   105  	c1 complex128 = c0
   106  	c2 = +1
   107  	c3 = +c0
   108  	c4 complex128 = +1
   109  	c5 complex128 = +c4
   110  	c6 = -1
   111  	c7 = -c0
   112  	c8 complex128 = -1
   113  	c9 complex128 = -c4
   114  	c10 = !c0 /* ERROR "not defined" */
   115  	c11 = ^1
   116  	c12 = ^i0
   117  	c13 complex128 = ^1
   118  	c14 complex128 = ^c4 /* ERROR "not defined" */
   119  	c15 = *c0 /* ERROR "cannot indirect" */
   120  	c16 = &c0
   121  	c17 = *u16
   122  	c18 = <-u16 /* ERROR "cannot receive" */
   123  	c19 = ~ /* ERROR "cannot use ~ outside of interface or type constraint" */ c0
   124  
   125  	// string
   126  	s0 = "foo"
   127  	s1 = +"foo" /* ERROR "not defined" */
   128  	s2 = -s0 /* ERROR "not defined" */
   129  	s3 = !s0 /* ERROR "not defined" */
   130  	s4 = ^s0 /* ERROR "not defined" */
   131  	s5 = *s4
   132  	s6 = &s4
   133  	s7 = *s6
   134  	s8 = <-s7
   135  	s9 = ~ /* ERROR "cannot use ~ outside of interface or type constraint" */ s0
   136  
   137  	// channel
   138  	ch chan int
   139  	rc <-chan float64
   140  	sc chan <- string
   141  	ch0 = +ch /* ERROR "not defined" */
   142  	ch1 = -ch /* ERROR "not defined" */
   143  	ch2 = !ch /* ERROR "not defined" */
   144  	ch3 = ^ch /* ERROR "not defined" */
   145  	ch4 = *ch /* ERROR "cannot indirect" */
   146  	ch5 = &ch
   147  	ch6 = *ch5
   148  	ch7 = <-ch
   149  	ch8 = <-rc
   150  	ch9 = <-sc /* ERROR "cannot receive" */
   151  	ch10, ok = <-ch
   152  	// ok is of type bool
   153  	ch11, myok = <-ch
   154  	_ mybool = myok /* ERRORx `cannot use .* in variable declaration` */
   155  	ch12 = ~ /* ERROR "cannot use ~ outside of interface or type constraint" */ ch
   156  
   157  )
   158  
   159  // address of composite literals
   160  type T struct{x, y int}
   161  
   162  func f() T { return T{} }
   163  
   164  var (
   165  	_ = &T{1, 2}
   166  	_ = &[...]int{}
   167  	_ = &[]int{}
   168  	_ = &[]int{}
   169  	_ = &map[string]T{}
   170  	_ = &(T{1, 2})
   171  	_ = &((((T{1, 2}))))
   172  	_ = &f /* ERROR "cannot take address" */ ()
   173  )
   174  
   175  // recursive pointer types
   176  type P *P
   177  
   178  var (
   179  	p1 P = new(P)
   180  	p2 P = *p1
   181  	p3 P = &p2
   182  )
   183  
   184  func g() (a, b int) { return }
   185  
   186  func _() {
   187  	_ = -g /* ERROR "multiple-value g" */ ()
   188  	_ = <-g /* ERROR "multiple-value g" */ ()
   189  }
   190  
   191  // ~ is accepted as unary operator only permitted in interface type elements
   192  var (
   193  	_ = ~ /* ERROR "cannot use ~ outside of interface or type constraint" */ 0
   194  	_ = ~ /* ERROR "cannot use ~ outside of interface or type constraint" */ "foo"
   195  	_ = ~ /* ERROR "cannot use ~ outside of interface or type constraint" */ i0
   196  )
   197  

View as plain text