Text file src/go/printer/testdata/generics.golden

     1  // Copyright 2020 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 generics
     6  
     7  func _[A, B any](a A, b B) int	{}
     8  func _[T any](x, y T) T
     9  
    10  type T[P any] struct{}
    11  type T[P1, P2, P3 any] struct{}
    12  
    13  type T[P C] struct{}
    14  type T[P1, P2, P3 C] struct{}
    15  
    16  type T[P C[P]] struct{}
    17  type T[P1, P2, P3 C[P1, P2, P3]] struct{}
    18  
    19  func f[P any](x P)
    20  func f[P1, P2, P3 any](x1 P1, x2 P2, x3 P3) struct{}
    21  
    22  func f[P interface{}](x P)
    23  func f[P1, P2, P3 interface {
    24  	m1(P1)
    25  	~P2 | ~P3
    26  }](x1 P1, x2 P2, x3 P3) struct{}
    27  func f[P any](T1[P], T2[P]) T3[P]
    28  
    29  func (x T[P]) m()
    30  func (T[P]) m(x T[P]) P
    31  
    32  func _() {
    33  	type _ []T[P]
    34  	var _ []T[P]
    35  	_ = []T[P]{}
    36  }
    37  
    38  // type constraint literals with elided interfaces
    39  func _[P ~int, Q int | string]()	{}
    40  func _[P struct{ f int }, Q *P]()	{}
    41  
    42  // various potentially ambiguous type parameter lists (issue #49482)
    43  type _[P *T,] struct{}
    44  type _[P T | T] struct{}
    45  type _[P T | T | T | T] struct{}
    46  type _[P *T, _ any] struct{}
    47  type _[P *T,] struct{}
    48  type _[P *T,] struct{}
    49  type _[P ~int] struct{}
    50  type _[P *T, _ any] struct{}
    51  type _[P T] struct{}
    52  type _[P T, _ any] struct{}
    53  
    54  type _[P *struct{}] struct{}
    55  type _ [P(*struct{})]struct{}
    56  type _[P []int] struct{}
    57  
    58  // a type literal in an |-expression indicates a type parameter list (blank after type parameter list and type)
    59  type _[P *[]int] struct{}
    60  type _[P *T | T, Q T] struct{}
    61  type _[P *[]T | T] struct{}
    62  type _[P *T | T | T | T | ~T] struct{}
    63  type _[P *T | T | T | ~T | T] struct{}
    64  type _[P *T | T | struct{} | T] struct{}
    65  type _[P <-chan int] struct{}
    66  type _[P *T | struct{} | T] struct{}
    67  
    68  // a trailing comma always indicates a (possibly invalid) type parameter list (blank after type parameter list and type)
    69  type _[P *T,] struct{}
    70  type _[P *T | T,] struct{}
    71  type _[P *T | <-T | T,] struct{}
    72  
    73  // slice/array type declarations (no blank between array length and element type)
    74  type _ []byte
    75  type _ [n]byte
    76  type _ [P(T)]byte
    77  type _ [P((T))]byte
    78  type _ [P * *T]byte
    79  type _ [P * T]byte
    80  type _ [P(*T)]byte
    81  type _ [P(**T)]byte
    82  type _ [P*T - T]byte
    83  type _ [P*T - T]byte
    84  type _ [P*T | T]byte
    85  type _ [P*T | <-T | T]byte
    86  
    87  // equivalent test cases for potentially ambiguous type parameter lists, except
    88  // for function declarations there is no ambiguity (issue #51548)
    89  func _[P *T]()		{}
    90  func _[P *T, _ any]()	{}
    91  func _[P *T]()		{}
    92  func _[P *T, _ any]()	{}
    93  func _[P T]()		{}
    94  func _[P T, _ any]()	{}
    95  
    96  func _[P *struct{}]()	{}
    97  func _[P *struct{}]()	{}
    98  func _[P []int]()	{}
    99  
   100  func _[P T]()	{}
   101  func _[P T]()	{}
   102  func _[P **T]()	{}
   103  func _[P *T]()	{}
   104  func _[P *T]()	{}
   105  func _[P **T]()	{}
   106  func _[P *T]()	{}
   107  
   108  func _[
   109  	P *T,
   110  ]() {
   111  }
   112  

View as plain text