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

     1  // Copyright 2026 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  // This file checks error messages for cases where function types were inferred from context.
     6  //
     7  // The function doc strings refer to the type checker functions that contain the relevant
     8  // newTarget/Hint calls (there's a chance that they may change, so this needs to be kept
     9  // in mind when looking for the calls).
    10  //
    11  // Note that most descriptor strings provided to newTarget/Hint never make it into an error
    12  // message (because the descriptors are not used). This test ensures that we will notice if
    13  // that changes.
    14  
    15  package p
    16  
    17  // This is a map with an invalid key, but we can still test type inference against the key.
    18  // (The type checker reports an error but keeps working with that key type.)
    19  type M map[F /* ERROR "invalid map key type" */ ]int
    20  type F func(int, string)
    21  
    22  func f[T any](T, T) {}
    23  
    24  // Checker.assignment
    25  func _() {
    26  	var m M
    27  	delete(m, f /* ERROR "cannot use f[int] (value of type func(int, int)) as F value in argument to delete" */ [int])
    28  }
    29  
    30  // Checker.assignVar
    31  func _() {
    32  	var v1 F
    33  	v1 = f /* ERROR "inferred type func(int, int) for func(T, T) does not match type F of v1" */
    34  	_ = v1
    35  
    36  	var v2 func(int, string)
    37  	v2 = f /* ERROR "inferred type func(int, int) for func(T, T) does not match type func(int, string) of v2" */
    38  	_ = v2
    39  
    40  	type A = func(int, string)
    41  	var v3 A
    42  	v3 = f /* ERROR "inferred type func(int, int) for func(T, T) does not match type A of v3" */
    43  	_ = v3
    44  
    45  	var a []F
    46  	var i, j int
    47  	a[i+j] = f /* ERROR "inferred type func(int, int) for func(T, T) does not match type F of a[i + j]" */
    48  }
    49  
    50  // Checker.initVars
    51  func _() F {
    52  	return f /* ERROR "cannot use f[int] (value of type func(int, int)) as F value in return statement" */ [int]
    53  }
    54  
    55  // Checker.callExpr
    56  var _ = F(f /* ERROR "cannot convert f[int] (value of type func(int, int)) to type F" */ [int])
    57  
    58  // Checker.callExpr
    59  func g(F) {}
    60  func _() {
    61  	g(f /* ERROR "cannot use f[int] (value of type func(int, int)) as F value in argument to g" */ [int])
    62  }
    63  
    64  // Checker.callExpr
    65  func h(int, F) {}
    66  func _() {
    67  	h(1, f /* ERROR "cannot use f[int] (value of type func(int, int)) as F value in argument to h" */ [int])
    68  }
    69  
    70  // Checker.varDecl
    71  var _ F = f /* ERROR "cannot use f[int] (value of type func(int, int)) as F value in variable declaration" */ [int]
    72  
    73  // Checker.indexExpr
    74  func _() {
    75  	var m M
    76  	_ = m[f /* ERROR "cannot use f[int] (value of type func(int, int)) as F value in map key" */ [int]]
    77  }
    78  
    79  // Checker.indexExpr
    80  func _() {
    81  	var m M
    82  	m[f /* ERROR "cannot use f[int] (value of type func(int, int)) as F value in map key" */ [int]] = 1
    83  }
    84  
    85  // Checker.compositeLit
    86  type S struct{ f F }
    87  
    88  var _ = S{f: f /* ERROR "cannot use f[int] (value of type func(int, int)) as F value in struct literal" */ [int]}
    89  
    90  // Checker.compositeLit
    91  var _ = S{f /* ERROR "cannot use f[int] (value of type func(int, int)) as F value in struct literal" */ [int]}
    92  
    93  // Checker.compositeLit
    94  var _ = M{f /* ERROR "cannot use f[int] (value of type func(int, int)) as F value in map literal" */ [int]: 1}
    95  
    96  // Checker.compositeLit
    97  var _ = map[int]F{1: f /* ERROR "cannot use f[int] (value of type func(int, int)) as F value in map literal" */ [int]}
    98  
    99  // Checker.indexElts
   100  var _ = []F{f /* ERROR "cannot use f[int] (value of type func(int, int)) as F value in array or slice literal" */ [int]}
   101  
   102  // Checker.stmt
   103  func _() {
   104  	var c chan F
   105  	c <- f /* ERROR "cannot use f[int] (value of type func(int, int)) as F value in send" */ [int]
   106  }
   107  

View as plain text