Source file test/typeparam/issue50642.go

     1  // run
     2  
     3  // Copyright 2021 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 main
     8  
     9  import "fmt"
    10  
    11  type Temp[T any] struct {
    12  }
    13  
    14  var temp, temp1 any
    15  var ch any
    16  
    17  func (it Temp[T]) HasNext() bool {
    18  	var ok bool
    19  	temp1 = <-ch.(chan T)
    20  	// test conversion of T to interface{} during an OAS2RECV
    21  	temp, ok = <-ch.(chan T)
    22  	return ok
    23  }
    24  
    25  type MyInt int
    26  
    27  func (i MyInt) String() string {
    28  	return "a"
    29  }
    30  
    31  type Stringer interface {
    32  	String() string
    33  }
    34  
    35  type Temp2[T Stringer] struct {
    36  }
    37  
    38  var temp2 Stringer
    39  
    40  func (it Temp2[T]) HasNext() string {
    41  	var x map[int]T
    42  
    43  	var ok bool
    44  	// test conversion of T to Stringer during an OAS2MAPR
    45  	temp2, ok = x[43]
    46  	_ = ok
    47  	return temp2.String()
    48  }
    49  
    50  func main() {
    51  	ch1 := make(chan int, 2)
    52  	ch1 <- 5
    53  	ch1 <- 6
    54  	ch = ch1
    55  	iter := Temp[int]{}
    56  	iter.HasNext()
    57  
    58  	iter2 := Temp2[MyInt]{}
    59  	if got, want := iter2.HasNext(), "a"; got != want {
    60  		panic(fmt.Sprintf("got %v, want %v", got, want))
    61  	}
    62  
    63  }
    64  

View as plain text