Source file test/convert2.go

     1  // errorcheck
     2  
     3  // Copyright 2016 The Go Authors. All rights reserved.
     4  // Use of this source code is governed by a BSD-style
     5  // license that can be found in the LICENSE file.
     6  
     7  // Test various valid and invalid struct assignments and conversions.
     8  // Does not compile.
     9  
    10  package main
    11  
    12  type I interface {
    13  	m()
    14  }
    15  
    16  // conversions between structs
    17  
    18  func _() {
    19  	type S struct{}
    20  	type T struct{}
    21  	var s S
    22  	var t T
    23  	var u struct{}
    24  	s = s
    25  	s = t // ERROR "cannot use .* in assignment|incompatible type"
    26  	s = u
    27  	s = S(s)
    28  	s = S(t)
    29  	s = S(u)
    30  	t = u
    31  	t = T(u)
    32  }
    33  
    34  func _() {
    35  	type S struct{ x int }
    36  	type T struct {
    37  		x int "foo"
    38  	}
    39  	var s S
    40  	var t T
    41  	var u struct {
    42  		x int "bar"
    43  	}
    44  	s = s
    45  	s = t // ERROR "cannot use .* in assignment|incompatible type"
    46  	s = u // ERROR "cannot use .* in assignment|incompatible type"
    47  	s = S(s)
    48  	s = S(t)
    49  	s = S(u)
    50  	t = u // ERROR "cannot use .* in assignment|incompatible type"
    51  	t = T(u)
    52  }
    53  
    54  func _() {
    55  	type E struct{ x int }
    56  	type S struct{ x E }
    57  	type T struct {
    58  		x E "foo"
    59  	}
    60  	var s S
    61  	var t T
    62  	var u struct {
    63  		x E "bar"
    64  	}
    65  	s = s
    66  	s = t // ERROR "cannot use .* in assignment|incompatible type"
    67  	s = u // ERROR "cannot use .* in assignment|incompatible type"
    68  	s = S(s)
    69  	s = S(t)
    70  	s = S(u)
    71  	t = u // ERROR "cannot use .* in assignment|incompatible type"
    72  	t = T(u)
    73  }
    74  
    75  func _() {
    76  	type S struct {
    77  		x struct {
    78  			x int "foo"
    79  		}
    80  	}
    81  	type T struct {
    82  		x struct {
    83  			x int "bar"
    84  		} "foo"
    85  	}
    86  	var s S
    87  	var t T
    88  	var u struct {
    89  		x struct {
    90  			x int "bar"
    91  		} "bar"
    92  	}
    93  	s = s
    94  	s = t // ERROR "cannot use .* in assignment|incompatible type"
    95  	s = u // ERROR "cannot use .* in assignment|incompatible type"
    96  	s = S(s)
    97  	s = S(t)
    98  	s = S(u)
    99  	t = u // ERROR "cannot use .* in assignment|incompatible type"
   100  	t = T(u)
   101  }
   102  
   103  func _() {
   104  	type E1 struct {
   105  		x int "foo"
   106  	}
   107  	type E2 struct {
   108  		x int "bar"
   109  	}
   110  	type S struct{ x E1 }
   111  	type T struct {
   112  		x E2 "foo"
   113  	}
   114  	var s S
   115  	var t T
   116  	var u struct {
   117  		x E2 "bar"
   118  	}
   119  	s = s
   120  	s = t // ERROR "cannot use .* in assignment|incompatible type"
   121  	s = u // ERROR "cannot use .* in assignment|incompatible type"
   122  	s = S(s)
   123  	s = S(t) // ERROR "cannot convert"
   124  	s = S(u) // ERROR "cannot convert"
   125  	t = u    // ERROR "cannot use .* in assignment|incompatible type"
   126  	t = T(u)
   127  }
   128  
   129  func _() {
   130  	type E struct{ x int }
   131  	type S struct {
   132  		f func(struct {
   133  			x int "foo"
   134  		})
   135  	}
   136  	type T struct {
   137  		f func(struct {
   138  			x int "bar"
   139  		})
   140  	}
   141  	var s S
   142  	var t T
   143  	var u struct{ f func(E) }
   144  	s = s
   145  	s = t // ERROR "cannot use .* in assignment|incompatible type"
   146  	s = u // ERROR "cannot use .* in assignment|incompatible type"
   147  	s = S(s)
   148  	s = S(t)
   149  	s = S(u) // ERROR "cannot convert"
   150  	t = u    // ERROR "cannot use .* in assignment|incompatible type"
   151  	t = T(u) // ERROR "cannot convert"
   152  }
   153  
   154  // conversions between pointers to structs
   155  
   156  func _() {
   157  	type S struct{}
   158  	type T struct{}
   159  	var s *S
   160  	var t *T
   161  	var u *struct{}
   162  	s = s
   163  	s = t // ERROR "cannot use .* in assignment|incompatible type"
   164  	s = u // ERROR "cannot use .* in assignment|incompatible type"
   165  	s = (*S)(s)
   166  	s = (*S)(t)
   167  	s = (*S)(u)
   168  	t = u // ERROR "cannot use .* in assignment|incompatible type"
   169  	t = (*T)(u)
   170  }
   171  
   172  func _() {
   173  	type S struct{ x int }
   174  	type T struct {
   175  		x int "foo"
   176  	}
   177  	var s *S
   178  	var t *T
   179  	var u *struct {
   180  		x int "bar"
   181  	}
   182  	s = s
   183  	s = t // ERROR "cannot use .* in assignment|incompatible type"
   184  	s = u // ERROR "cannot use .* in assignment|incompatible type"
   185  	s = (*S)(s)
   186  	s = (*S)(t)
   187  	s = (*S)(u)
   188  	t = u // ERROR "cannot use .* in assignment|incompatible type"
   189  	t = (*T)(u)
   190  }
   191  
   192  func _() {
   193  	type E struct{ x int }
   194  	type S struct{ x E }
   195  	type T struct {
   196  		x E "foo"
   197  	}
   198  	var s *S
   199  	var t *T
   200  	var u *struct {
   201  		x E "bar"
   202  	}
   203  	s = s
   204  	s = t // ERROR "cannot use .* in assignment|incompatible type"
   205  	s = u // ERROR "cannot use .* in assignment|incompatible type"
   206  	s = (*S)(s)
   207  	s = (*S)(t)
   208  	s = (*S)(u)
   209  	t = u // ERROR "cannot use .* in assignment|incompatible type"
   210  	t = (*T)(u)
   211  }
   212  
   213  func _() {
   214  	type S struct {
   215  		x struct {
   216  			x int "foo"
   217  		}
   218  	}
   219  	type T struct {
   220  		x struct {
   221  			x int "bar"
   222  		} "foo"
   223  	}
   224  	var s *S
   225  	var t *T
   226  	var u *struct {
   227  		x struct {
   228  			x int "bar"
   229  		} "bar"
   230  	}
   231  	s = s
   232  	s = t // ERROR "cannot use .* in assignment|incompatible type"
   233  	s = u // ERROR "cannot use .* in assignment|incompatible type"
   234  	s = (*S)(s)
   235  	s = (*S)(t)
   236  	s = (*S)(u)
   237  	t = u // ERROR "cannot use .* in assignment|incompatible type"
   238  	t = (*T)(u)
   239  }
   240  
   241  func _() {
   242  	type E1 struct {
   243  		x int "foo"
   244  	}
   245  	type E2 struct {
   246  		x int "bar"
   247  	}
   248  	type S struct{ x E1 }
   249  	type T struct {
   250  		x E2 "foo"
   251  	}
   252  	var s *S
   253  	var t *T
   254  	var u *struct {
   255  		x E2 "bar"
   256  	}
   257  	s = s
   258  	s = t // ERROR "cannot use .* in assignment|incompatible type"
   259  	s = u // ERROR "cannot use .* in assignment|incompatible type"
   260  	s = (*S)(s)
   261  	s = (*S)(t) // ERROR "cannot convert"
   262  	s = (*S)(u) // ERROR "cannot convert"
   263  	t = u       // ERROR "cannot use .* in assignment|incompatible type"
   264  	t = (*T)(u)
   265  }
   266  
   267  func _() {
   268  	type E struct{ x int }
   269  	type S struct {
   270  		f func(struct {
   271  			x int "foo"
   272  		})
   273  	}
   274  	type T struct {
   275  		f func(struct {
   276  			x int "bar"
   277  		})
   278  	}
   279  	var s *S
   280  	var t *T
   281  	var u *struct{ f func(E) }
   282  	s = s
   283  	s = t // ERROR "cannot use .* in assignment|incompatible type"
   284  	s = u // ERROR "cannot use .* in assignment|incompatible type"
   285  	s = (*S)(s)
   286  	s = (*S)(t)
   287  	s = (*S)(u) // ERROR "cannot convert"
   288  	t = u       // ERROR "cannot use .* in assignment|incompatible type"
   289  	t = (*T)(u) // ERROR "cannot convert"
   290  }
   291  
   292  func _() {
   293  	type E struct{ x int }
   294  	type S struct {
   295  		f func(*struct {
   296  			x int "foo"
   297  		})
   298  	}
   299  	type T struct {
   300  		f func(*struct {
   301  			x int "bar"
   302  		})
   303  	}
   304  	var s *S
   305  	var t *T
   306  	var u *struct{ f func(E) }
   307  	s = s
   308  	s = t // ERROR "cannot use .* in assignment|incompatible type"
   309  	s = u // ERROR "cannot use .* in assignment|incompatible type"
   310  	s = (*S)(s)
   311  	s = (*S)(t)
   312  	s = (*S)(u) // ERROR "cannot convert"
   313  	t = u       // ERROR "cannot use .* in assignment|incompatible type"
   314  	t = (*T)(u) // ERROR "cannot convert"
   315  }
   316  
   317  func _() {
   318  	var s []byte
   319  	_ = ([4]byte)(s)
   320  	_ = (*[4]byte)(s)
   321  
   322  	type A [4]byte
   323  	_ = (A)(s)
   324  	_ = (*A)(s)
   325  
   326  	type P *[4]byte
   327  	_ = (P)(s)
   328  	_ = (*P)(s) // ERROR "cannot convert"
   329  }
   330  

View as plain text