Source file src/internal/types/testdata/check/compliterals2.go
1 // -lang=go1.27 2 3 // Copyright 2026 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 // This is a replica of compliterals1.go at Go 1.27 to ensure error 8 // messages are reported as expected. 9 10 package comp_literals 11 12 type T struct {} 13 14 // var declaration 15 func _() { 16 var _ T = { /* ERROR "requires go1.28 or later" */ } 17 } 18 19 // assignment 20 func _() { 21 var x T 22 x = { /* ERROR "requires go1.28 or later" */ } 23 _ = x // "use" it 24 } 25 26 // // argument to function call 27 // func _() { 28 // f := func(_ T) {} 29 // f({ /* ERROR "requires go1.28 or later" */ }) 30 // } 31 32 // // argument to function call with variadic arguments 33 // func _() { 34 // f := func(_ ...T) {} 35 // f({ /* ERROR "requires go1.28 or later" */ }, { /* ERROR "requires go1.28 or later" */ }) 36 // } 37 38 // map index expression 39 func _() { 40 var x map[T]int 41 _ = x[{ /* ERROR "requires go1.28 or later" */ }] 42 } 43 44 // map index expression through a type parameter 45 func _[P map[T]int](x P) { 46 _ = x[{ /* ERROR "requires go1.28 or later" */ }] 47 } 48 49 // // value of a struct literal with keys / values 50 // func _() { 51 // type S struct { 52 // f T 53 // } 54 // _ = S{f: { /* ERROR "requires go1.28 or later" */ }} 55 // } 56 57 // // value of a struct literal without keys / values 58 // func _() { 59 // type S struct { 60 // f T 61 // } 62 // _ = S{{ /* ERROR "requires go1.28 or later" */ }} 63 // } 64 65 // values sent to a channel 66 func _() { 67 var x chan<- T 68 x <- { /* ERROR "requires go1.28 or later" */ } 69 } 70 71 // argument to conversion 72 func _() { 73 _ = T({ /* ERROR "requires go1.28 or later" */ }) 74 } 75 76 // The below are all covered by the inference mechanism used before 77 // Go 1.28 (AKA "hints"). They are included here for completeness. 78 79 // keys of a map literal 80 func _() { 81 type M map[T]int 82 _ = M{{}: 42} 83 } 84 85 // values of a map literal 86 func _() { 87 type M map[int]T 88 _ = M{42: {}} 89 } 90 91 // elements in an array literal 92 func _() { 93 _ = [42]T{{}} 94 } 95 96 // elements in a slice literal 97 func _() { 98 _ = []T{{}} 99 } 100