Source file src/internal/types/testdata/fixedbugs/issue60377.go

     1  // Copyright 2023 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 p
     6  
     7  // The type parameter P is not used in interface T1.
     8  // T1 is a defined parameterized interface type which
     9  // can be assigned to any other interface with the same
    10  // methods. We cannot infer a type argument in this case
    11  // because any type would do.
    12  
    13  type T1[P any] interface{ m() }
    14  
    15  func g[P any](T1[P]) {}
    16  
    17  func _() {
    18  	var x T1[int]
    19  	g /* ERROR "cannot infer P" */ (x)
    20  	g[int](x)    // int is ok for P
    21  	g[string](x) // string is also ok for P!
    22  }
    23  
    24  // This is analogous to the above example,
    25  // but uses two interface types of the same structure.
    26  
    27  type T2[P any] interface{ m() }
    28  
    29  func _() {
    30  	var x T2[int]
    31  	g /* ERROR "cannot infer P" */ (x)
    32  	g[int](x)    // int is ok for P
    33  	g[string](x) // string is also ok for P!
    34  }
    35  
    36  // Analogous to the T2 example but using an unparameterized interface T3.
    37  
    38  type T3 interface{ m() }
    39  
    40  func _() {
    41  	var x T3
    42  	g /* ERROR "cannot infer P" */ (x)
    43  	g[int](x)    // int is ok for P
    44  	g[string](x) // string is also ok for P!
    45  }
    46  
    47  // The type parameter P is not used in struct S.
    48  // S is a defined parameterized (non-interface) type which can only
    49  // be assigned to another type S with the same type argument.
    50  // Therefore we can infer a type argument in this case.
    51  
    52  type S[P any] struct{}
    53  
    54  func g4[P any](S[P]) {}
    55  
    56  func _() {
    57  	var x S[int]
    58  	g4(x)      // we can infer int for P
    59  	g4[int](x) // int is the correct type argument
    60  	g4[string](x /* ERROR "cannot use x (variable of struct type S[int]) as S[string] value in argument to g4[string]" */)
    61  }
    62  
    63  // This is similar to the first example but here T1 is a component
    64  // of a func type. In this case types must match exactly: P must
    65  // match int.
    66  
    67  func g5[P any](func(T1[P])) {}
    68  
    69  func _() {
    70  	var f func(T1[int])
    71  	g5(f)
    72  	g5[int](f)
    73  	g5[string](f /* ERROR "cannot use f (variable of type func(T1[int])) as func(T1[string]) value in argument to g5[string]" */)
    74  }
    75  
    76  // This example would fail if we were to infer the type argument int for P
    77  // exactly because any type argument would be ok for the first argument.
    78  // Choosing the wrong type would cause the second argument to not match.
    79  
    80  type T[P any] interface{}
    81  
    82  func g6[P any](T[P], P) {}
    83  
    84  func _() {
    85  	var x T[int]
    86  	g6(x, 1.2)
    87  	g6(x, "")
    88  }
    89  

View as plain text