...

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

Documentation: internal/types/testdata/check

     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 /* ERRORx `operator .* not defined` */ < f
    13  	_ = 0 == t /* ERROR "mismatched types untyped int and untyped bool" */
    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 == nil // ERROR "operator == not defined on untyped 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 "mismatched types" */
    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 == d /* ERROR "mismatched types" */
    41  
    42  	var e [10]func() int
    43  	_ = e /* ERROR "[10]func() int cannot be compared" */ == 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 "mismatched types" */
    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 == tt /* ERROR "mismatched types" */
    72  
    73  	var u struct {
    74  		x int
    75  		a [10]map[string]int
    76  	}
    77  	_ = u /* ERROR "cannot be compared" */ == u
    78  }
    79  
    80  func pointers() {
    81  	// nil
    82  	_ = nil == nil // ERROR "operator == not defined on untyped nil"
    83  	_ = nil != nil // ERROR "operator != not defined on untyped 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 == ps2 /* ERROR "mismatched types" */
   119  	_ = ps2 == ps1 /* ERROR "mismatched types" */
   120  
   121  	_ = p1 == p1
   122  	_ = p1 == p2 /* ERROR "mismatched types" */
   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 == c1r /* ERROR "mismatched types" */
   151  	_ = c1 == c1s /* ERROR "mismatched types" */
   152  	_ = c1r == c1s /* ERROR "mismatched types" */
   153  	_ = c1 == c1a
   154  	_ = c1a == c1
   155  	_ = c1 == c2 /* ERROR "mismatched types" */
   156  	_ = c1a == c2 /* ERROR "mismatched types" */
   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 == d1s /* ERROR "mismatched types" */
   170  	_ = d1 == d1a
   171  	_ = d1a == d1
   172  	_ = d1 == d2 /* ERROR "mismatched types" */
   173  	_ = d1a == d2 /* ERROR "mismatched types" */
   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 == k /* ERROR "mismatched types" */
   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 == s1 /* ERROR "mismatched types" */
   206  	_ = i == &s1
   207  	_ = i == &s11
   208  
   209  	_ = i == s2 /* ERROR "mismatched types" */
   210  	_ = i == & /* ERROR "mismatched types" */ s2
   211  
   212  	// issue #28164
   213  	// testcase from issue
   214  	_ = interface{}(nil) == [ /* ERROR "slice can only be compared to nil" */ ]int(nil)
   215  
   216  	// related cases
   217  	var e interface{}
   218  	var s []int
   219  	var x int
   220  	_ = e == s // ERROR "slice can only be compared to nil"
   221  	_ = s /* ERROR "slice can only be compared to nil" */ == e
   222  	_ = e /* ERROR "operator < not defined on interface" */ < x
   223  	_ = x < e // ERROR "operator < not defined on interface"
   224  }
   225  
   226  func slices() {
   227  	// basics
   228  	var s []int
   229  	_ = s == nil
   230  	_ = s != nil
   231  	_ = s /* ERROR "< not defined" */ < nil
   232  
   233  	// slices are not otherwise comparable
   234  	_ = s /* ERROR "slice can only be compared to nil" */ == s
   235  	_ = s /* ERROR "< not defined" */ < s
   236  }
   237  
   238  func maps() {
   239  	// basics
   240  	var m map[string]int
   241  	_ = m == nil
   242  	_ = m != nil
   243  	_ = m /* ERROR "< not defined" */ < nil
   244  
   245  	// maps are not otherwise comparable
   246  	_ = m /* ERROR "map can only be compared to nil" */ == m
   247  	_ = m /* ERROR "< not defined" */ < m
   248  }
   249  
   250  func funcs() {
   251  	// basics
   252  	var f func(int) float32
   253  	_ = f == nil
   254  	_ = f != nil
   255  	_ = f /* ERROR "< not defined" */ < nil
   256  
   257  	// funcs are not otherwise comparable
   258  	_ = f /* ERROR "func can only be compared to nil" */ == f
   259  	_ = f /* ERROR "< not defined" */ < f
   260  }
   261  

View as plain text