Source file test/typeparam/issue50417.go

     1  // run
     2  
     3  // Copyright 2022 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  func main() {}
    10  
    11  // Field accesses through type parameters are disabled
    12  // until we have a more thorough understanding of the
    13  // implications on the spec. See issue #51576.
    14  
    15  /*
    16  type Sf struct {
    17  	f int
    18  }
    19  
    20  func f0[P Sf](p P) {
    21  	_ = p.f
    22  	p.f = 0
    23  }
    24  
    25  func f0t[P ~struct{ f int }](p P) {
    26  	_ = p.f
    27  	p.f = 0
    28  }
    29  
    30  var _ = f0[Sf]
    31  var _ = f0t[Sf]
    32  
    33  func f1[P interface {
    34  	~struct{ f int }
    35  	m()
    36  }](p P) {
    37  	_ = p.f
    38  	p.f = 0
    39  	p.m()
    40  }
    41  
    42  var _ = f1[Sfm]
    43  
    44  type Sm struct{}
    45  
    46  func (Sm) m() {}
    47  
    48  type Sfm struct {
    49  	f int
    50  }
    51  
    52  func (Sfm) m() {}
    53  
    54  func f2[P interface {
    55  	Sfm
    56  	m()
    57  }](p P) {
    58  	_ = p.f
    59  	p.f = 0
    60  	p.m()
    61  }
    62  
    63  var _ = f2[Sfm]
    64  
    65  // special case: core type is a named pointer type
    66  
    67  type PSfm *Sfm
    68  
    69  func f3[P interface{ PSfm }](p P) {
    70  	_ = p.f
    71  	p.f = 0
    72  }
    73  
    74  var _ = f3[PSfm]
    75  
    76  // special case: core type is an unnamed pointer type
    77  
    78  func f4[P interface{ *Sfm }](p P) {
    79  	_ = p.f
    80  	p.f = 0
    81  }
    82  
    83  var _ = f4[*Sfm]
    84  
    85  type A int
    86  type B int
    87  type C float64
    88  
    89  type Int interface {
    90  	*Sf | A
    91  	*Sf | B
    92  }
    93  
    94  func f5[P Int](p P) {
    95  	_ = p.f
    96  	p.f = 0
    97  }
    98  
    99  var _ = f5[*Sf]
   100  
   101  type Int2 interface {
   102  	*Sf | A
   103  	any
   104  	*Sf | C
   105  }
   106  
   107  func f6[P Int2](p P) {
   108  	_ = p.f
   109  	p.f = 0
   110  }
   111  
   112  var _ = f6[*Sf]
   113  
   114  type Int3 interface {
   115  	Sf
   116  	~struct{ f int }
   117  }
   118  
   119  func f7[P Int3](p P) {
   120  	_ = p.f
   121  	p.f = 0
   122  }
   123  
   124  var _ = f7[Sf]
   125  
   126  type Em1 interface {
   127  	*Sf | A
   128  }
   129  
   130  type Em2 interface {
   131  	*Sf | B
   132  }
   133  
   134  type Int4 interface {
   135  	Em1
   136  	Em2
   137  	any
   138  }
   139  
   140  func f8[P Int4](p P) {
   141  	_ = p.f
   142  	p.f = 0
   143  }
   144  
   145  var _ = f8[*Sf]
   146  */
   147  

View as plain text