github.com/zebozhuang/go@v0.0.0-20200207033046-f8a98f6f5c5d/src/go/types/testdata/expr2.src (about)

     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  // comparisons
     6  
     7  package expr2
     8  
     9  func _bool() {
    10  	const t = true == true
    11  	const f = true == false
    12  	_ = t /* ERROR "cannot compare" */ < f
    13  	_ = 0 /* ERROR "cannot convert" */ == t
    14  	var b bool
    15  	var x, y float32
    16  	b = x < y
    17  	_ = b
    18  	_ = struct{b bool}{x < y}
    19  }
    20  
    21  // corner cases
    22  var (
    23  	v0 = nil /* ERROR "cannot compare" */ == nil
    24  )
    25  
    26  func arrays() {
    27  	// basics
    28  	var a, b [10]int
    29  	_ = a == b
    30  	_ = a != b
    31  	_ = a /* ERROR < not defined */ < b
    32  	_ = a == nil /* ERROR cannot convert */
    33  
    34  	type C [10]int
    35  	var c C
    36  	_ = a == c
    37  
    38  	type D [10]int
    39  	var d D
    40  	_ = c /* ERROR mismatched types */ == d
    41  
    42  	var e [10]func() int
    43  	_ = e /* ERROR == not defined */ == e
    44  }
    45  
    46  func structs() {
    47  	// basics
    48  	var s, t struct {
    49  		x int
    50  		a [10]float32
    51  		_ bool
    52  	}
    53  	_ = s == t
    54  	_ = s != t
    55  	_ = s /* ERROR < not defined */ < t
    56  	_ = s == nil /* ERROR cannot convert */
    57  
    58  	type S struct {
    59  		x int
    60  		a [10]float32
    61  		_ bool
    62  	}
    63  	type T struct {
    64  		x int
    65  		a [10]float32
    66  		_ bool
    67  	}
    68  	var ss S
    69  	var tt T
    70  	_ = s == ss
    71  	_ = ss /* ERROR mismatched types */ == tt
    72  
    73  	var u struct {
    74  		x int
    75  		a [10]map[string]int
    76  	}
    77  	_ = u /* ERROR cannot compare */ == u
    78  }
    79  
    80  func pointers() {
    81  	// nil
    82  	_ = nil /* ERROR == not defined */ == nil
    83  	_ = nil /* ERROR != not defined */ != nil
    84  	_ = nil /* ERROR < not defined */ < nil
    85  	_ = nil /* ERROR <= not defined */ <= nil
    86  	_ = nil /* ERROR > not defined */ > nil
    87  	_ = nil /* ERROR >= not defined */ >= nil
    88  
    89  	// basics
    90  	var p, q *int
    91  	_ = p == q
    92  	_ = p != q
    93  
    94  	_ = p == nil
    95  	_ = p != nil
    96  	_ = nil == q
    97  	_ = nil != q
    98  
    99  	_ = p /* ERROR < not defined */ < q
   100  	_ = p /* ERROR <= not defined */ <= q
   101  	_ = p /* ERROR > not defined */ > q
   102  	_ = p /* ERROR >= not defined */ >= q
   103  
   104  	// various element types
   105  	type (
   106  		S1 struct{}
   107  		S2 struct{}
   108  		P1 *S1
   109  		P2 *S2
   110  	)
   111  	var (
   112  		ps1 *S1
   113  		ps2 *S2
   114  		p1 P1
   115  		p2 P2
   116  	)
   117  	_ = ps1 == ps1
   118  	_ = ps1 /* ERROR mismatched types */ == ps2
   119  	_ = ps2 /* ERROR mismatched types */ == ps1
   120  
   121  	_ = p1 == p1
   122  	_ = p1 /* ERROR mismatched types */ == p2
   123  
   124  	_ = p1 == ps1
   125  }
   126  
   127  func channels() {
   128  	// basics
   129  	var c, d chan int
   130  	_ = c == d
   131  	_ = c != d
   132  	_ = c == nil
   133  	_ = c /* ERROR < not defined */ < d
   134  
   135  	// various element types (named types)
   136  	type (
   137  		C1 chan int
   138  		C1r <-chan int
   139  		C1s chan<- int
   140  		C2 chan float32
   141  	)
   142  	var (
   143  		c1 C1
   144  		c1r C1r
   145  		c1s C1s
   146  		c1a chan int
   147  		c2 C2
   148  	)
   149  	_ = c1 == c1
   150  	_ = c1 /* ERROR mismatched types */ == c1r
   151  	_ = c1 /* ERROR mismatched types */ == c1s
   152  	_ = c1r /* ERROR mismatched types */ == c1s
   153  	_ = c1 == c1a
   154  	_ = c1a == c1
   155  	_ = c1 /* ERROR mismatched types */ == c2
   156  	_ = c1a /* ERROR mismatched types */ == c2
   157  
   158  	// various element types (unnamed types)
   159  	var (
   160  		d1 chan int
   161  		d1r <-chan int
   162  		d1s chan<- int
   163  		d1a chan<- int
   164  		d2 chan float32
   165  	)
   166  	_ = d1 == d1
   167  	_ = d1 == d1r
   168  	_ = d1 == d1s
   169  	_ = d1r /* ERROR mismatched types */ == d1s
   170  	_ = d1 == d1a
   171  	_ = d1a == d1
   172  	_ = d1 /* ERROR mismatched types */ == d2
   173  	_ = d1a /* ERROR mismatched types */ == d2
   174  }
   175  
   176  // for interfaces test
   177  type S1 struct{}
   178  type S11 struct{}
   179  type S2 struct{}
   180  func (*S1) m() int
   181  func (*S11) m() int
   182  func (*S11) n()
   183  func (*S2) m() float32
   184  
   185  func interfaces() {
   186  	// basics
   187  	var i, j interface{ m() int }
   188  	_ = i == j
   189  	_ = i != j
   190  	_ = i == nil
   191  	_ = i /* ERROR < not defined */ < j
   192  
   193  	// various interfaces
   194  	var ii interface { m() int; n() }
   195  	var k interface { m() float32 }
   196  	_ = i == ii
   197  	_ = i /* ERROR mismatched types */ == k
   198  
   199  	// interfaces vs values
   200  	var s1 S1
   201  	var s11 S11
   202  	var s2 S2
   203  
   204  	_ = i == 0 /* ERROR cannot convert */
   205  	_ = i /* ERROR mismatched types */ == s1
   206  	_ = i == &s1
   207  	_ = i == &s11
   208  
   209  	_ = i /* ERROR mismatched types */ == s2
   210  	_ = i /* ERROR mismatched types */ == &s2
   211  }
   212  
   213  func slices() {
   214  	// basics
   215  	var s []int
   216  	_ = s == nil
   217  	_ = s != nil
   218  	_ = s /* ERROR < not defined */ < nil
   219  
   220  	// slices are not otherwise comparable
   221  	_ = s /* ERROR == not defined */ == s
   222  	_ = s /* ERROR < not defined */ < s
   223  }
   224  
   225  func maps() {
   226  	// basics
   227  	var m map[string]int
   228  	_ = m == nil
   229  	_ = m != nil
   230  	_ = m /* ERROR < not defined */ < nil
   231  
   232  	// maps are not otherwise comparable
   233  	_ = m /* ERROR == not defined */ == m
   234  	_ = m /* ERROR < not defined */ < m
   235  }
   236  
   237  func funcs() {
   238  	// basics
   239  	var f func(int) float32
   240  	_ = f == nil
   241  	_ = f != nil
   242  	_ = f /* ERROR < not defined */ < nil
   243  
   244  	// funcs are not otherwise comparable
   245  	_ = f /* ERROR == not defined */ == f
   246  	_ = f /* ERROR < not defined */ < f
   247  }