...

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

Documentation: internal/types/testdata/fixedbugs

     1  // Copyright 2023 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  // Simplified (representative) test case.
     8  
     9  func _() {
    10  	f(R1{})
    11  }
    12  
    13  func f[T any](R[T]) {}
    14  
    15  type R[T any] interface {
    16  	m(R[T])
    17  }
    18  
    19  type R1 struct{}
    20  
    21  func (R1) m(R[int]) {}
    22  
    23  // Test case from issue.
    24  
    25  func _() {
    26  	r := newTestRules()
    27  	NewSet(r)
    28  	r2 := newTestRules2()
    29  	NewSet(r2)
    30  }
    31  
    32  type Set[T any] struct {
    33  	rules Rules[T]
    34  }
    35  
    36  func NewSet[T any](rules Rules[T]) Set[T] {
    37  	return Set[T]{
    38  		rules: rules,
    39  	}
    40  }
    41  
    42  func (s Set[T]) Copy() Set[T] {
    43  	return NewSet(s.rules)
    44  }
    45  
    46  type Rules[T any] interface {
    47  	Hash(T) int
    48  	Equivalent(T, T) bool
    49  	SameRules(Rules[T]) bool
    50  }
    51  
    52  type testRules struct{}
    53  
    54  func newTestRules() Rules[int] {
    55  	return testRules{}
    56  }
    57  
    58  func (r testRules) Hash(val int) int {
    59  	return val % 16
    60  }
    61  
    62  func (r testRules) Equivalent(val1 int, val2 int) bool {
    63  	return val1 == val2
    64  }
    65  
    66  func (r testRules) SameRules(other Rules[int]) bool {
    67  	_, ok := other.(testRules)
    68  	return ok
    69  }
    70  
    71  type testRules2 struct{}
    72  
    73  func newTestRules2() Rules[string] {
    74  	return testRules2{}
    75  }
    76  
    77  func (r testRules2) Hash(val string) int {
    78  	return 16
    79  }
    80  
    81  func (r testRules2) Equivalent(val1 string, val2 string) bool {
    82  	return val1 == val2
    83  }
    84  
    85  func (r testRules2) SameRules(other Rules[string]) bool {
    86  	_, ok := other.(testRules2)
    87  	return ok
    88  }
    89  

View as plain text