github.com/JimmyHuang454/JLS-go@v0.0.0-20230831150107-90d536585ba0/internal/types/testdata/fixedbugs/issue51257.go (about)

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