Source file test/typeparam/issue44688.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  // derived & expanded from cmd/compile/internal/types2/testdata/fixedbugs/issue44688.go2
     8  
     9  package main
    10  
    11  type A1[T any] struct {
    12  	val T
    13  }
    14  
    15  func (p *A1[T]) m1(val T) {
    16  	p.val = val
    17  }
    18  
    19  type A2[T any] interface {
    20  	m2(T)
    21  }
    22  
    23  type B1[T any] struct {
    24  	filler int
    25  	*A1[T]
    26  	A2[T]
    27  }
    28  
    29  type B2[T any] interface {
    30  	A2[T]
    31  }
    32  
    33  type ImpA2[T any] struct {
    34  	f T
    35  }
    36  
    37  func (a2 *ImpA2[T]) m2(s T) {
    38  	a2.f = s
    39  }
    40  
    41  type C[T any] struct {
    42  	filler1 int
    43  	filler2 int
    44  	B1[T]
    45  }
    46  
    47  type D[T any] struct {
    48  	filler1 int
    49  	filler2 int
    50  	filler3 int
    51  	C[T]
    52  }
    53  
    54  func test1[T any](arg T) {
    55  	// calling embedded methods
    56  	var b1 B1[T]
    57  	b1.A1 = &A1[T]{}
    58  	b1.A2 = &ImpA2[T]{}
    59  
    60  	b1.A1.m1(arg)
    61  	b1.m1(arg)
    62  
    63  	b1.A2.m2(arg)
    64  	b1.m2(arg)
    65  
    66  	var b2 B2[T]
    67  	b2 = &ImpA2[T]{}
    68  	b2.m2(arg)
    69  
    70  	// a deeper nesting
    71  	var d D[T]
    72  	d.C.B1.A1 = &A1[T]{}
    73  	d.C.B1.A2 = &ImpA2[T]{}
    74  	d.m1(arg)
    75  	d.m2(arg)
    76  
    77  	// calling method expressions
    78  	m1x := B1[T].m1
    79  	m1x(b1, arg)
    80  	// TODO(khr): reenable these.
    81  	//m2x := B2[T].m2
    82  	//m2x(b2, arg)
    83  
    84  	// calling method values
    85  	m1v := b1.m1
    86  	m1v(arg)
    87  	m2v := b1.m2
    88  	m2v(arg)
    89  	b2v := b2.m2
    90  	b2v(arg)
    91  }
    92  
    93  func test2() {
    94  	// calling embedded methods
    95  	var b1 B1[string]
    96  	b1.A1 = &A1[string]{}
    97  	b1.A2 = &ImpA2[string]{}
    98  
    99  	b1.A1.m1("")
   100  	b1.m1("")
   101  
   102  	b1.A2.m2("")
   103  	b1.m2("")
   104  
   105  	var b2 B2[string]
   106  	b2 = &ImpA2[string]{}
   107  	b2.m2("")
   108  
   109  	// a deeper nesting
   110  	var d D[string]
   111  	d.C.B1.A1 = &A1[string]{}
   112  	d.C.B1.A2 = &ImpA2[string]{}
   113  	d.m1("")
   114  	d.m2("")
   115  
   116  	// calling method expressions
   117  	m1x := B1[string].m1
   118  	m1x(b1, "")
   119  	m2x := B2[string].m2
   120  	m2x(b2, "")
   121  
   122  	// calling method values
   123  	m1v := b1.m1
   124  	m1v("")
   125  	m2v := b1.m2
   126  	m2v("")
   127  	b2v := b2.m2
   128  	b2v("")
   129  }
   130  
   131  // actual test case from issue
   132  
   133  type A[T any] struct{}
   134  
   135  func (*A[T]) f(T) {}
   136  
   137  type B[T any] struct{ A[T] }
   138  
   139  func test3() {
   140  	var b B[string]
   141  	b.A.f("")
   142  	b.f("")
   143  }
   144  
   145  func main() {
   146  	test1[string]("")
   147  	test2()
   148  	test3()
   149  }
   150  

View as plain text