Source file test/typeparam/issue48042.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 (
    10  	"fmt"
    11  	"reflect"
    12  )
    13  
    14  type G[T any] interface {
    15  	g() func()(*T)
    16  }
    17  type Foo[T any] struct {
    18  
    19  }
    20  // OCALL
    21  func (l *Foo[T]) f1() (*T) {
    22  	return g[T]()()
    23  }
    24  // OCALLFUNC
    25  func (l *Foo[T]) f2() (*T) {
    26  	var f = g[T]
    27  	return f()()
    28  }
    29  // OCALLMETH
    30  func (l *Foo[T]) f3() (*T) {
    31  	return l.g()()
    32  }
    33  // OCALLINTER
    34  func (l *Foo[T]) f4() (*T) {
    35  	var g G[T] = l
    36  	return g.g()()
    37  }
    38  // ODYNAMICDOTTYPE
    39  func (l *Foo[T]) f5() (*T) {
    40  	var x interface{}
    41  	x = g[T]
    42  	return x.(func()func()(*T))()()
    43  }
    44  func (l *Foo[T]) g() func() (*T) {
    45  	return func() (*T) {
    46  		t := new(T)
    47  		reflect.ValueOf(t).Elem().SetInt(100)
    48  		return t
    49  	}
    50  }
    51  func g[T any]() func() (*T) {
    52  	return func() (*T) {
    53  		t := new(T)
    54  		reflect.ValueOf(t).Elem().SetInt(100)
    55  		return t
    56  	}
    57  }
    58  
    59  func main() {
    60  	foo := Foo[int]{}
    61  	// Make sure the function conversion is correct
    62  	if n := *(foo.f1()) ; n != 100{
    63  		panic(fmt.Sprintf("%v",n))
    64  	}
    65  	if n := *(foo.f2()) ; n != 100{
    66  		panic(fmt.Sprintf("%v",n))
    67  	}
    68  	if n := *(foo.f3()) ; n != 100{
    69  		panic(fmt.Sprintf("%v",n))
    70  	}
    71  	if n := *(foo.f4()) ; n != 100{
    72  		panic(fmt.Sprintf("%v",n))
    73  	}
    74  	if n := *(foo.f5()) ; n != 100{
    75  		panic(fmt.Sprintf("%v",n))
    76  	}
    77  }

View as plain text