modernc.org/gc@v1.0.1-0.20240304020402-f0dba7c97c2b/testdata/errchk/issue15292/0.go (about)

     1  // errorcheck
     2  
     3  // Copyright 2009 The GC Authors. All rights reserved.
     4  // Use of this source code is governed by a BSD-style
     5  // license that can be found in the LICENSE file.
     6  
     7  // Based on proposal by Ian Lance Taylor available at
     8  //
     9  //  https://github.com/golang/proposal/blob/master/design/15292-generics.md
    10  
    11  // For now only a syntax check.
    12  
    13  package p
    14  
    15  import (
    16  	"sort"
    17  )
    18  
    19  // Dummy types.
    20  type ( //TODO-
    21  	t int
    22  	u int
    23  	a int
    24  	b int
    25  	keytype int
    26  	valtype int
    27  )
    28  
    29  type _ sort.Interface«int»
    30  
    31  /*
    32  	https://github.com/golang/proposal/blob/master/design/15292/2010-06-type-functions.md
    33  */
    34  
    35  type Vector«t» []t
    36  type Vector2«t, u» []struct{t; u}
    37  type VectorInt Vector«int»
    38  type VectorInt2 Vector2«int, string»
    39  
    40  func Bound(v Vector«int»)
    41  func Unbound«t»(v Vector«t»)
    42  var UnboundInt = Unbound«int»
    43  
    44  var fn = func(Vector«int»)() {}
    45  var fn2 = func(v Vector«int») {}
    46  var fn3 = func«t»(Vector«t») {} // ERROR "anonymous functions cannot have generic type parameters"
    47  
    48  func (v Vector«int») BoundMethod(x int)
    49  func (v Vector«t») UnboundMethod«t»(x t)
    50  
    51  // func Generic(v t type) t
    52  func Generic«t»(v t) t
    53  
    54  // func Head(v Vector(t type)) {
    55  //         var first t
    56  //         first = v[0]
    57  // }
    58  func Head«t»(v Vector«t») {
    59  	var first t
    60  	first = v[0]
    61  }
    62  
    63  // func SetHead(v Vector(t type), e t) t {
    64  //         v[0] = e
    65  //         return e
    66  // }
    67  func SetHead«t»(v Vector«t», e t) t {
    68  	v[0] = e
    69  	return e
    70  }
    71  
    72  // generic(t) func SetHead(v Vector(t), e t) t { ... }
    73  func SetHead2«t»(v Vector«t», e t) t {}
    74  
    75  // func Choose(which bool, a t type, b t) t
    76  func Choose«t»(which bool, a, b t) t
    77  
    78  // func Repeat(x t type, n int) Slice(t) {
    79  //         a := make(Slice(t), n)
    80  //         for i := range a {
    81  //                 a[i] = x
    82  //         }
    83  //         return a
    84  // }
    85  type Slice«t» []t
    86  func Repeat«t»(x t, n int) Slice«t» {
    87  	a := make(Slice«t», n)
    88  	for i := range a {
    89  		a[i] = x
    90  	}
    91  	return a
    92  }
    93  
    94  // type Pair(a, b) struct {
    95  //         first a
    96  //         second b
    97  // }
    98  // func Sum(a Pair(Vector(t type), Vector(t))) Vector(t)
    99  type Pair«a, b» struct {
   100  	first a
   101  	second b
   102  }
   103  func Sum«t»(a Pair«Vector«t», Vector«t»») Vector«t»
   104  
   105  // func (v Vector(t type)) At(int i) t {
   106  //         return v[i]
   107  // }
   108  // 
   109  // func (v Vector(t type)) Set(i int, x t) {
   110  //         v[i] = x
   111  // }
   112  func (v Vector«t») At«t»(i int) t {
   113  	return v[i]
   114  }
   115  
   116  func (v Vector«t») Set«t»(i int, x t) {
   117  	v[i] = x
   118  }
   119  
   120  // package hashmap
   121  // 
   122  // type bucket(keytype, valtype) struct {
   123  //         next *bucket(keytype, valtype)
   124  //         key keytype
   125  //         val valtype
   126  // }
   127  // 
   128  // type Hashfn(keytype) func(keytype) uint
   129  // 
   130  // type Eqfn(keytype) func(keytype, keytype) bool
   131  // 
   132  // type Hashmap(keytype, valtype) struct {
   133  //         hashfn Hashfn(keytype)
   134  //         eqfn Eqtype(keytype)
   135  //         buckets []bucket(keytype, valtype)
   136  //         entries int
   137  // }
   138  // 
   139  // func New(hashfn Hashfn(keytype type), eqfn Eqfn(keytype),
   140  //          _ valtype type) *Hashmap(keytype, valtype) {
   141  //         return &Hashmap(k, v){hashfn, eqfn,
   142  //                 make([]bucket(keytype, valtype), 16),
   143  //                 0}
   144  // }
   145  // 
   146  // // Note that the dummy valtype parameter in the New function
   147  // // exists only to get valtype into the function signature.
   148  // // This feels wrong.
   149  // 
   150  // func (p *Hashmap(keytype type, vvaltype type))
   151  //           Lookup(key keytype) (found bool, val valtype) {
   152  //         h := p.hashfn(key) % len(p.buckets)
   153  //         for b := buckets[h]; b != nil; b = b.next {
   154  //                 if p.eqfn(key, b.key) {
   155  //                         return true, b.val
   156  //                 }
   157  //         }
   158  //         return
   159  // }
   160  type bucket«keytype, valtype» struct {
   161  	next *bucket«keytype, valtype»
   162  	key keytype
   163  	val valtype
   164  }
   165  
   166  type Hashfn«keytype» func(keytype) uint
   167  
   168  type Eqfn«keytype» func(keytype, keytype) bool
   169  
   170  type Hashmap«keytype, valtype» struct {
   171  	hashfn Hashfn«keytype»
   172  	eqfn Eqfn«keytype»
   173  	buckets []bucket«keytype, valtype»
   174  	entries int
   175  }
   176  
   177  func New«keytype, valtype»(hashfn Hashfn«keytype», eqfn Eqfn«keytype») *Hashmap«keytype, valtype» {
   178  	return &Hashmap«keytype, valtype»{
   179  		hashfn,
   180  		eqfn,
   181  		make([]bucket«keytype, valtype», 16),
   182  		0,
   183  	}
   184  }
   185  
   186  func (p *Hashmap«keytype, vvaltype») Lookup(key keytype) (found bool, val valtype) {
   187  	h := p.hashfn(key) % len(p.buckets)
   188  	for b := p.buckets[h]; b != nil; b = b.next {
   189  		if p.eqfn(key, b.key) {
   190  			return true, b.val
   191  		}
   192  	}
   193  	return
   194  }
   195  
   196  // Concepts: Try to get away w/o them. (???)
   197  
   198  // type PrintableVector(t Stringer) []t
   199  // func Concat(p PrintableVector(t type)) string {
   200  //         s := ""
   201  //         for _, v := range p {
   202  //                 s += v.String()
   203  //         }
   204  //         return s
   205  // }
   206  func Concat(p Vector«t») string {
   207  	s := ""
   208  	for _, v := range p {
   209  		s += v.String()
   210  	}
   211  	return s
   212  }
   213  
   214  // type Lesser(t) interface {
   215  //         Less(t) bool
   216  // }
   217  // func Min(a, b t type Lesser(t)) t {
   218  //         if a.Less(b) {
   219  //                 return a
   220  //         }
   221  //         return b
   222  // }
   223  type Lesser«t» interface {
   224  	Less(t) bool
   225  }
   226  func Min«t»(a, b t) t {
   227  	// Type switch optimized away at compile time if t is known to be/not to be a Lesser«t».
   228  	switch x := a.(type) {
   229  	case Lesser«t»:
   230  		if x.Less(b) {
   231  			return a
   232  		}
   233  
   234  		return b
   235  	default:
   236  		if a < b {
   237  			return a
   238  		}
   239  
   240  		return b
   241  	}
   242  }
   243  
   244  // type Addable(t) interface {
   245  //         Binary+(t) t
   246  // }
   247  // type AddableSlice(t Addable(t)) []t
   248  // func Sum(v AddableSlice) t {
   249  //         var sum t
   250  //         for _, v := range v {
   251  //                 sum = sum + v
   252  //         }
   253  //         return sum
   254  // }
   255  type Addable«t» interface {
   256  	Add(t) t
   257  }
   258  func Sum244«t»(v Vector«t») t {
   259  	// Type switch optimized away at compile time if t is known to be/not to be a Addable«t».
   260  	switch t.(type) {
   261  	case Addable«t»:
   262  		var sum t
   263  		for _, v := range v {
   264  		        sum = sum.Add(v)
   265  		}
   266  		return sum
   267  	default:
   268  		var sum t
   269  		for _, v := range v {
   270  		        sum = sum + v
   271  		}
   272  		return sum
   273  	}
   274  }
   275  
   276  /*
   277  	https://github.com/golang/proposal/blob/master/design/15292/2011-03-gen.md
   278  */
   279  
   280  //gen [T] type Vector []T
   281  type Vector265«t» []t
   282  
   283  // type VectorInt Vector[int]
   284  // var v1 Vector[int]
   285  // var v2 Vector[float32]
   286  // gen [T1, T2] type Pair struct { first T1; second T2 }
   287  // var v3 Pair[int, string]
   288  type VectorInt272 Vector«int»
   289  var v273 Vector«int»
   290  var v274 Vector«float32»
   291  type Pair275«t, u» struct { first t; second u }
   292  var v276 Pair«int, string»
   293  
   294  // gen [T] func SetHead(v Vector[T], e T) T {
   295  //     v[0] = e
   296  //     return e
   297  // }
   298  func SetHead282«t»(v Vector«t», e t) t {
   299  	v[0] = e
   300  	return e
   301  }
   302  
   303  // gen [T1, T2] (
   304  // 	type Pair struct { first T1; second T2 }
   305  // 
   306  // 	func MakePair(first T1, second T2) Pair {
   307  // 	    return &Pair{first, second}
   308  // 	}
   309  // )
   310  type Pair297«t, u» struct { first t; second u }
   311  
   312  func MakePair299«t, u»(first t, second t) Pair297«t, u» {
   313  	return &Pair297«t, u»{first, second}
   314  }
   315  
   316  // var MakeIntPair = MakePair[int, int]
   317  // var IntPairZero = MakeIntPair(0, 0)
   318  var MakeIntPair305 = MakePair299«int, int»
   319  var IntPairZero306 = MakePair299«int, int»(0, 0)
   320  var IntPairZero307 = MakeIntPair305(0, 0)
   321  
   322  // gen [T] func (v *Vector[T]) SetHeadMethod(e T) T {
   323  //     v[0] = e
   324  //     return e
   325  // }
   326  func (v *Vector«t») SetHeadMethod313«t»(e t) t {
   327  	(*v)[0] = e
   328  	return e
   329  }
   330  
   331  // gen [T, T2] func (v *Vector[T]) Transform(f func(T) T2) Vector[T2]
   332  func (v *Vector«t, u») Transform«t, u»(f func(t) u) Vector«u»
   333  
   334  // var v1 = MakePair[int, int](0, 0)
   335  // var v2 = MakePair(0, 0)         // [int, int] deduced.
   336  var v323 = MakePair299«int, int»(0, 0)
   337  var v324 = MakePair299(0, 0)         // [int, int] deduced.
   338  
   339  // gen [T] func SliceAverage(a []T) T {
   340  //     s := T(0)
   341  //     for _, v = range a {
   342  //         s += v
   343  //     }
   344  //     return s / len(a)
   345  // }
   346  func SliceAverage«t»(a []t) t {
   347  	s := t(0)
   348  	// or: var s t
   349  	for _, v := range a {
   350  		s += v
   351  	}
   352  	return s / len(a)
   353  }
   354  
   355  // gen [T] type Number interface {
   356  //     Plus(T) T
   357  //     Divide(T) T
   358  // }
   359  // 
   360  // gen [T Number[T]] func SliceAverage(a []T) T {
   361  //     s := 0.[T]
   362  //     for _, v = range a {
   363  //         s = s.Plus(v)
   364  //     }
   365  //     return s.Divide(len(a))
   366  // }
   367  type Number354«t» interface {
   368  	Plus(t) t
   369  	Divide(t) t
   370  }
   371  
   372  func SliceAverage359«t»(a []t) t {
   373  	// Type switch optimized away at compile time if t is known to be/not to be a Lesser«t».
   374  	switch t.(type) {
   375  	case Number354«t»:
   376  		var s t
   377  		for _, v = range a {
   378  			s = s.Plus(v)
   379  		}
   380  		return s.Divide(t(len(a)))
   381  	default:
   382  		var s t
   383  		for _, v := range a {
   384  			s += v
   385  		}
   386  		return s / len(a)
   387  	}
   388  }
   389  
   390  /*
   391  	https://github.com/golang/proposal/blob/master/design/15292/2013-10-gen.md
   392  */
   393  
   394  // var v1 = MakePair[int, int](0, 0)
   395  // var v2 = MakePair(0, 0)         // [int, int] deduced.
   396  // var v3 = Pair{0, 0}         // [int, int] deduced.
   397  var v386 = MakePair299«int, int»(0, 0)
   398  var v387 = MakePair299(0, 0)         // «int, int» deduced.
   399  var v388 = Pair297{0, 0}         // «int, int» deduced.
   400  
   401  // func SortSlice(s []T, less func(T, T) bool) {
   402  //     sort.Sort(&sorter{s, less})
   403  // }
   404  // 
   405  // type sorter struct {
   406  //     s []T
   407  //     less func(T, T) bool
   408  // }
   409  // 
   410  // func (s *sorter) Len() int { return len(s.s) }
   411  // func (s *sorter) Less(i, j int) bool { return s.less(s[i], s[j]) }
   412  // func (s *sorter) Swap(i, j int) { s.s[i], s.s[j] = s.s[j], s.s[i] }
   413  // 
   414  // ) // End gen
   415  func SortSlice«t»(s []t, less func(t, t) bool) {
   416      sort.Sort(&sorter{s, less})
   417  }
   418  
   419  type sorter«t» struct {
   420      s []t
   421      less func(t, t) bool
   422  }
   423  
   424  func (s *sorter«t») Len«t»() int           { return len(s.s) }
   425  func (s *sorter«t») Less«t»(i, j int) bool { return s.less(s.s[i], s.s[j]) }
   426  func (s *sorter«t») Swap«t»(i, j int)      { s.s[i], s.s[j] = s.s[j], s.s[i] }
   427  
   428  // gen [T] func SortNumericSlice(s []T) {
   429  //     SortSlice(s, func(a, b T) bool { return a < b })
   430  // }
   431  func SortNumericSlice«t»(s []t) {
   432      SortSlice(s, func(a, b t) bool { return a < b })
   433  }
   434  
   435  // gen [T] func Merge(a, b <-chan T) <-chan T {
   436  //     c := make(chan T)
   437  //     go func(a, b chan T) {
   438  //         for a != nil && b != nil {
   439  //             select {
   440  //             case v, ok := <-a:
   441  //                 if ok {
   442  //                     c <- v
   443  //                 } else {
   444  //                     a = nil
   445  //                 }
   446  //             case v, ok := <-b:
   447  //                 if ok {
   448  //                     c <- v
   449  //                 } else {
   450  //                     b = nil
   451  //                 }
   452  //             }
   453  //         }
   454  //         close(c)
   455  //     }(a, b)
   456  //     return c
   457  // }
   458  func Merge«t»(a, b <-chan t) <-chan t {
   459  	c := make(chan t)
   460  	go func(a, b chan t) {
   461  		for a != nil && b != nil {
   462  			select {
   463  			case v, ok := <-a:
   464  				if ok {
   465  					c <- v
   466  				} else {
   467  					a = nil
   468  				}
   469  			case v, ok := <-b:
   470  				if ok {
   471  					c <- v
   472  				} else {
   473  					b = nil
   474  				}
   475  			}
   476  		}
   477  		close(c)
   478  	}(a, b)
   479  	return c
   480  }
   481  
   482  // type [T] List struct { element T; next *List[T] }
   483  type List«t» struct { element t; next *List«t» }
   484  
   485  // type ListInt List[int]
   486  // var v1 List[int]
   487  // var v2 List[float]
   488  // type (
   489  // [T1, T2] MyMap map[T1]T2
   490  // [T3] MyChan chan T3
   491  // )
   492  // var v3 MyMap[int, string]
   493  type ListInt List«int»
   494  var v483 List«int»
   495  var v484 List«float»
   496  type (
   497  	MyMap«t, u» map[t]u
   498  	MyChan«t» chan t
   499  )
   500  var v489 MyMap«int, string»
   501  
   502  // func [T] Push(l *List[T], e T) *List[T] {
   503  //     return &List[T]{e, l}
   504  // }
   505  func Push«t»(l *List«t», e t) *List«t» {
   506      return &List«t»{e, l}
   507  }
   508  
   509  // func [T] (v *List[T]) Push(e T) {
   510  //     *v = &List[T]{e, v}
   511  // }
   512  func (v *List«t») Push«t»(e t) {
   513      *v = &List«t»{e, v}
   514  }
   515  
   516  // type [T] SortableSlice []T
   517  // func [T] (v SortableSlice[T]) Len() int { return len(v) }
   518  // func [T] (v SortableSlice[T]) Swap(i, j int) {
   519  //     v[i], v[j] = v[j], v[i]
   520  // }
   521  // func [T] (v SortableSlice[T]) Less(i, j int) bool {
   522  //     return v[i].Less(v[j])
   523  // }
   524  // func [T] (v SortableSlice[T]) Sort() {
   525  //     sort.Sort(v)
   526  // }
   527  type SortableSlice«t» []t
   528  func (v SortableSlice«t») Len«t»() int { return len(v) }
   529  func (v SortableSlice«t») Swap«t»(i, j int) {
   530  	v[i], v[j] = v[j], v[i]
   531  }
   532  func (v SortableSlice«t») Less«t»(i, j int) bool {
   533  	// Type switch optimized away at compile time if possible.
   534  	switch t.(type) {
   535  	case Lesser«t»:
   536  		return v[i].Less(v[j])
   537  	default:
   538  		return v[i] < v[j]
   539  	}
   540  }
   541  func (v SortableSlice«t») Sort«t»() {
   542  	sort.Sort(v)
   543  }