...

Source file src/internal/types/testdata/fixedbugs/issue61879.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  import "fmt"
     8  
     9  type Interface[T any] interface {
    10  	m(Interface[T])
    11  }
    12  
    13  func f[S []Interface[T], T any](S) {}
    14  
    15  func _() {
    16  	var s []Interface[int]
    17  	f(s) // panic here
    18  }
    19  
    20  // Larger example from issue
    21  
    22  type InterfaceA[T comparable] interface {
    23  	setData(string) InterfaceA[T]
    24  }
    25  
    26  type ImplA[T comparable] struct {
    27  	data string
    28  	args []any
    29  }
    30  
    31  func NewInterfaceA[T comparable](args ...any) InterfaceA[T] {
    32  	return &ImplA[T]{
    33  		data: fmt.Sprintf("%v", args...),
    34  		args: args,
    35  	}
    36  }
    37  
    38  func (k *ImplA[T]) setData(data string) InterfaceA[T] {
    39  	k.data = data
    40  	return k
    41  }
    42  
    43  func Foo[M ~map[InterfaceA[T]]V, T comparable, V any](m M) {
    44  	// DO SOMETHING HERE
    45  	return
    46  }
    47  
    48  func Bar() {
    49  	keys := make([]InterfaceA[int], 0, 10)
    50  	m := make(map[InterfaceA[int]]int)
    51  	for i := 0; i < 10; i++ {
    52  		keys = append(keys, NewInterfaceA[int](i))
    53  		m[keys[i]] = i
    54  	}
    55  
    56  	Foo(m) // panic here
    57  }
    58  

View as plain text