...

Source file src/internal/types/testdata/fixedbugs/issue51257.go

Documentation: internal/types/testdata/fixedbugs

     1  // Copyright 2022 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  func f[_ comparable]() {}
     8  
     9  type S1 struct{ x int }
    10  type S2 struct{ x any }
    11  type S3 struct{ x [10]interface{ m() } }
    12  
    13  func _[P1 comparable, P2 S2]() {
    14  	_ = f[S1]
    15  	_ = f[S2]
    16  	_ = f[S3]
    17  
    18  	type L1 struct { x P1 }
    19  	type L2 struct { x P2 }
    20  	_ = f[L1]
    21  	_ = f[L2 /* ERROR "L2 does not satisfy comparable" */ ]
    22  }
    23  
    24  
    25  // example from issue
    26  
    27  type Set[T comparable] map[T]struct{}
    28  
    29  func NewSetFromSlice[T comparable](items []T) *Set[T] {
    30  	s := Set[T]{}
    31  
    32  	for _, item := range items {
    33  		s[item] = struct{}{}
    34  	}
    35  
    36  	return &s
    37  }
    38  
    39  type T struct{ x any }
    40  
    41  func main() {
    42  	NewSetFromSlice([]T{
    43  		{"foo"},
    44  		{5},
    45  	})
    46  }
    47  

View as plain text