Black Lives Matter. Support the Equal Justice Initiative.

Text file src/go/types/testdata/check/typeinst.go2

Documentation: go/types/testdata/check

     1  // Copyright 2019 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  type myInt int
     8  
     9  // Parameterized type declarations
    10  
    11  type T1[P any] P
    12  
    13  type T2[P any] struct {
    14          f P
    15          g int // int should still be in scope chain
    16  }
    17  
    18  type List[P any] []P
    19  
    20  // Alias type declarations cannot have type parameters. Syntax error.
    21  type A1[P any] = /* ERROR cannot be alias */ P
    22  
    23  // But an alias may refer to a generic, uninstantiated type.
    24  type A2 = List
    25  var _ A2[int]
    26  var _ A2 /* ERROR without instantiation */
    27  
    28  type A3 = List[int]
    29  var _ A3
    30  
    31  // Parameterized type instantiations
    32  
    33  var x int
    34  type _ x /* ERROR not a type */ [int]
    35  
    36  type _ int /* ERROR not a generic type */ []
    37  type _ myInt /* ERROR not a generic type */ []
    38  
    39  // TODO(gri) better error messages
    40  type _ T1 /* ERROR got 0 arguments but 1 type parameters */ []
    41  type _ T1[x /* ERROR not a type */ ]
    42  type _ T1 /* ERROR got 2 arguments but 1 type parameters */ [int, float32]
    43  
    44  var _ T2[int] = T2[int]{}
    45  
    46  var _ List[int] = []int{1, 2, 3}
    47  var _ List[[]int] = [][]int{{1, 2, 3}}
    48  var _ List[List[List[int]]]
    49  
    50  // Parameterized types containing parameterized types
    51  
    52  type T3[P any] List[P]
    53  
    54  var _ T3[int] = T3[int](List[int]{1, 2, 3})
    55  
    56  // Self-recursive generic types are not permitted
    57  
    58  type self1[P any] self1 /* ERROR illegal cycle */ [P]
    59  type self2[P any] *self2[P] // this is ok
    60  

View as plain text