...

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

Documentation: internal/types/testdata/check

     1  // Copyright 2021 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 typeInference
     6  
     7  // As of issue #51527, type-type inference has been disabled.
     8  
     9  // basic inference
    10  type Tb[P ~*Q, Q any] int
    11  
    12  func _() {
    13  	var x Tb /* ERROR "not enough type arguments for type Tb: have 1, want 2" */ [*int]
    14  	var y Tb[*int, int]
    15  	x = y /* ERRORx `cannot use y .* in assignment` */
    16  	_ = x
    17  }
    18  
    19  // recursive inference
    20  type Tr[A any, B *C, C *D, D *A] int
    21  
    22  func _() {
    23  	var x Tr /* ERROR "not enough type arguments for type Tr: have 1, want 4" */ [string]
    24  	var y Tr[string, ***string, **string, *string]
    25  	var z Tr[int, ***int, **int, *int]
    26  	x = y /* ERRORx `cannot use y .* in assignment` */
    27  	x = z // ERRORx `cannot use z .* as Tr`
    28  	_ = x
    29  }
    30  
    31  // other patterns of inference
    32  type To0[A any, B []A] int
    33  type To1[A any, B struct{ a A }] int
    34  type To2[A any, B [][]A] int
    35  type To3[A any, B [3]*A] int
    36  type To4[A any, B any, C struct {
    37  	a A
    38  	b B
    39  }] int
    40  
    41  func _() {
    42  	var _ To0 /* ERROR "not enough type arguments for type To0: have 1, want 2" */ [int]
    43  	var _ To1 /* ERROR "not enough type arguments for type To1: have 1, want 2" */ [int]
    44  	var _ To2 /* ERROR "not enough type arguments for type To2: have 1, want 2" */ [int]
    45  	var _ To3 /* ERROR "not enough type arguments for type To3: have 1, want 2" */ [int]
    46  	var _ To4 /* ERROR "not enough type arguments for type To4: have 2, want 3" */ [int, string]
    47  }
    48  
    49  // failed inference
    50  type Tf0[A, B any] int
    51  type Tf1[A any, B ~struct {
    52  	a A
    53  	c C
    54  }, C any] int
    55  
    56  func _() {
    57  	var _ Tf0 /* ERROR "not enough type arguments for type Tf0: have 1, want 2" */ [int]
    58  	var _ Tf1 /* ERROR "not enough type arguments for type Tf1: have 1, want 3" */ [int]
    59  }
    60  

View as plain text