Source file src/internal/types/testdata/check/compliterals3.go

     1  // Copyright 2026 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Some additional checks for inferred composite literal types
     6  
     7  package comp_literals
     8  
     9  type S1 struct{
    10         x int
    11         y float64
    12         s string
    13  }
    14  
    15  var (
    16         _ = { /* ERROR "missing type in composite literal" */ }
    17         _ []int = {}
    18         _ struct{} = {}
    19        _ [10]byte = {1, 2, 3, 9: 10}
    20         _ map[string]int = {"foo": 0, "bar": 1}
    21  )
    22  
    23  // var (
    24  //        _ = struct{ f struct { f int }}{{1}}
    25  // )
    26  
    27  func _() []int {
    28         return nil
    29         return {}
    30         return {1, 2, 3}
    31  }
    32  
    33  func _() S1 {
    34         return {}
    35         return {1, 2, "3"}
    36         return {1.0, 2, ""}
    37         return {s: "foo"}
    38  }
    39  
    40  func _() []S1 {
    41         return {}
    42         return {{}}
    43         return {{x: 1}, {y: 2}, {1, 2.0, "3"}}
    44  }
    45  
    46  func f(s []int) {
    47         s = {}
    48         s = {1, 2, 3}
    49         s = {0: 0, 1: 1}
    50         s = {"foo" /* ERRORx "cannot use .* as int value" */ }
    51  }
    52  
    53  // func _() {
    54  //        f({})
    55  // }
    56  
    57  type S2 struct {
    58         f func(x int)
    59  }
    60  
    61  func g1[T any](x T) {}
    62  
    63  var _ S2 = { g1 }
    64  

View as plain text