Source file test/typeparam/issue48538.go

     1  // compile
     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  // Testing composite literal for a type param constrained to be a struct or a map.
     8  
     9  package p
    10  
    11  type C interface {
    12  	~struct{ b1, b2 string }
    13  }
    14  
    15  func f[T C]() T {
    16  	return T{
    17  		b1: "a",
    18  		b2: "b",
    19  	}
    20  }
    21  
    22  func f2[T ~struct{ b1, b2 string }]() T {
    23  	return T{
    24  		b1: "a",
    25  		b2: "b",
    26  	}
    27  }
    28  
    29  type D interface {
    30  	map[string]string | S
    31  }
    32  
    33  type S map[string]string
    34  
    35  func g[T D]() T {
    36  	b1 := "foo"
    37  	b2 := "bar"
    38  	return T{
    39  		b1: "a",
    40  		b2: "b",
    41  	}
    42  }
    43  
    44  func g2[T map[string]string]() T {
    45  	b1 := "foo"
    46  	b2 := "bar"
    47  	return T{
    48  		b1: "a",
    49  		b2: "b",
    50  	}
    51  }
    52  
    53  func g3[T S]() T {
    54  	b1 := "foo"
    55  	b2 := "bar"
    56  	return T{
    57  		b1: "a",
    58  		b2: "b",
    59  	}
    60  }
    61  

View as plain text