Source file src/internal/types/testdata/spec/untyped_lit.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  package untyped_lit
     6  
     7  // Type definitions used across tests.
     8  
     9  type (
    10  	_Basic     = int
    11  	_Array     = [10]int
    12  	_Slice     = []int
    13  	_Struct    = struct{ f int }
    14  	_Pointer   = *int
    15  	_PointerS  = *_Struct
    16  	_Func      = func(int) string
    17  	_Interface = interface{ m() int }
    18  	_Map       = map[string]int
    19  	_Chan      = chan int
    20  
    21  	Basic     _Basic
    22  	Array     _Array
    23  	Slice     _Slice
    24  	Struct    _Struct
    25  	Pointer   _Pointer
    26  	PointerS  _PointerS
    27  	Func      _Func
    28  	Interface _Interface
    29  	Map       _Map
    30  	Chan      _Chan
    31  
    32  	GArray[E any]             [10]E
    33  	GSlice[E any]             []E
    34  	GStruct[E any]            struct{ f E }
    35  	GMap[K comparable, V any] map[K]V
    36  )
    37  
    38  // ----------------------------------------------------------------------------
    39  // Assignability across target types T:
    40  // x is an untyped composite literal and T(x) is a valid composite literal of type T.
    41  
    42  func _() {
    43  	// alias / unnamed types
    44  	var (
    45  		_ _Basic     = { /* ERROR "invalid composite literal type _Basic" */ }
    46  		_ _Array     = {}
    47  		_ _Array     = {1, 2, 3}
    48  		_ _Slice     = {}
    49  		_ _Slice     = {1, 2, 3}
    50  		_ _Struct    = {}
    51  		_ _Struct    = {1}
    52  		_ _Struct    = {f: 1}
    53  		_ _Pointer   = { /* ERROR "invalid composite literal type _Pointer" */ }
    54  		_ _PointerS  = {}
    55  		_ _PointerS  = {1}
    56  		_ _PointerS  = {f: 1}
    57  		_ _Func      = { /* ERROR "invalid composite literal type _Func" */ }
    58  		_ _Interface = { /* ERROR "invalid composite literal type _Interface" */ }
    59  		_ any        = { /* ERROR "invalid composite literal type any" */ }
    60  		_ _Map       = {}
    61  		_ _Map       = {"a": 1}
    62  		_ _Chan      = { /* ERROR "invalid composite literal type _Chan" */ }
    63  	)
    64  
    65  	// defined types
    66  	var (
    67  		_ Basic     = { /* ERROR "invalid composite literal type Basic" */ }
    68  		_ Array     = {}
    69  		_ Array     = {1, 2, 3}
    70  		_ Slice     = {}
    71  		_ Slice     = {1, 2, 3}
    72  		_ Struct    = {}
    73  		_ Struct    = {1}
    74  		_ Struct    = {f: 1}
    75  		_ Pointer   = { /* ERROR "invalid composite literal type Pointer" */ }
    76  		_ PointerS  = {}
    77  		_ PointerS  = {1}
    78  		_ PointerS  = {f: 1}
    79  		_ Func      = { /* ERROR "invalid composite literal type Func" */ }
    80  		_ Interface = { /* ERROR "invalid composite literal type Interface" */ }
    81  		_ Map       = {}
    82  		_ Map       = {"a": 1}
    83  		_ Chan      = { /* ERROR "invalid composite literal type Chan" */ }
    84  	)
    85  
    86  	// instantiated generic types
    87  	var (
    88  		_ GArray[int]       = {}
    89  		_ GArray[int]       = {1, 2, 3}
    90  		_ GSlice[int]       = {}
    91  		_ GSlice[int]       = {1, 2, 3}
    92  		_ GStruct[int]      = {}
    93  		_ GStruct[int]      = {1}
    94  		_ GStruct[int]      = {f: 1}
    95  		_ GMap[string, int] = {}
    96  		_ GMap[string, int] = {"a": 1}
    97  	)
    98  }
    99  
   100  // Type parameter target types T.
   101  func _[
   102  	P0 any,
   103  	PB ~int,
   104  	PA ~[10]int,
   105  	PA2 [10]int | Array,
   106  	PA_bad [10]int | [20]int,
   107  	PL ~[]int,
   108  	PL2 []int | Slice,
   109  	PLA_bad []int | [10]int,
   110  	PS ~struct{ f int },
   111  	PS2 struct{ f int } | Struct,
   112  	PS_bad struct{ f int } | struct{ g int },
   113  	PP ~*int,
   114  	PPS ~*_Struct,
   115  	PF ~func(int) string,
   116  	PI Interface,
   117  	PM ~map[string]int,
   118  	PM2 map[string]int | Map,
   119  	PC ~chan int,
   120  ]() {
   121  	var (
   122  		_ P0      = { /* ERROR "invalid composite literal type P0" */ }
   123  		_ PB      = { /* ERROR "invalid composite literal type PB" */ }
   124  		_ PA      = {}
   125  		_ PA      = {1, 2, 3}
   126  		_ PA2     = {0: 1, 9: 2}
   127  		_ PA_bad  = { /* ERROR "invalid composite literal type PA_bad (no common underlying type)" */ }
   128  		_ PL      = {}
   129  		_ PL      = {1, 2, 3}
   130  		_ PL2     = {0: 1, 2: 3}
   131  		_ PLA_bad = { /* ERROR "invalid composite literal type PLA_bad (no common underlying type)" */ }
   132  		_ PS      = {}
   133  		_ PS      = {1}
   134  		_ PS      = {f: 1}
   135  		_ PS2     = {f: 1}
   136  		_ PS_bad  = { /* ERROR "invalid composite literal type PS_bad (no common underlying type)" */ }
   137  		_ PP      = { /* ERROR "invalid composite literal type PP" */ }
   138  		_ PPS     = {}
   139  		_ PPS     = {f: 1}
   140  		_ PF      = { /* ERROR "invalid composite literal type PF" */ }
   141  		_ PI      = { /* ERROR "invalid composite literal type PI" */ }
   142  		_ PM      = {}
   143  		_ PM      = {"a": 1}
   144  		_ PM2     = {"a": 1}
   145  		_ PC      = { /* ERROR "invalid composite literal type PC" */ }
   146  	)
   147  }
   148  
   149  // ----------------------------------------------------------------------------
   150  // Validity of literal elements for each composite type kind.
   151  
   152  func _() {
   153  	// struct literals (including promoted fields)
   154  	type Inner struct {
   155  		a, b int
   156  	}
   157  	type Outer struct {
   158  		Inner
   159  		x int
   160  		s string
   161  	}
   162  
   163  	var (
   164  		_ Outer = {}
   165  		_ Outer = {{1, 2}, 3, "foo"}
   166  		_ Outer = {Inner: {1, 2}, x: 3, s: "foo"}
   167  		_ Outer = {a: 1, b: 2, x: 3, s: "foo"}
   168  		_ Outer = {{1, 2}, 3} // ERROR "too few values"
   169  		_ Outer = {{1, 2}, 3, "foo", 4 /* ERROR "too many values" */}
   170  		_ Outer = {x: 3, "foo" /* ERROR "mixture of field:value and value elements" */}
   171  		_ Outer = {{1, 2}, x /* ERROR "mixture of field:value and value elements" */ : 3, "foo"}
   172  		_ Outer = {z /* ERROR "unknown field z" */ : 1}
   173  		_ Outer = {x: 1, x /* ERROR "duplicate field name x" */ : 2}
   174  		_ Outer = {Inner: {1, 2}, a /* ERROR "cannot specify promoted field a and enclosing embedded field Inner" */ : 1}
   175  		_ Outer = {x: "foo" /* ERRORx `cannot use "foo" .* as int value` */}
   176  	)
   177  
   178  	// array literals
   179  	var (
   180  		_ [3]int = {}
   181  		_ [3]int = {1, 2, 3}
   182  		_ [3]int = {0: 1, 2: 3}
   183  		_ [3]int = {1: 2, 3}
   184  		_ [3]int = {1, 2, 3, 4 /* ERROR "index 3 is out of bounds" */}
   185  		_ [3]int = {3 /* ERROR "index 3 out of bounds" */ : 1}
   186  		_ [3]int = {- /* ERROR "index -1 (constant of type int) must not be negative" */ 1: 1}
   187  		_ [3]int = {"a" /* ERRORx `cannot convert "a" .* to type int` */ : 1}
   188  		_ [3]int = {0: 1, 0 /* ERROR "duplicate index 0" */ : 2}
   189  		_ [3]int = {1: 1, 0: 2, 3 /* ERROR "duplicate index 1" */}
   190  		_ [3]int = {"a" /* ERRORx `cannot use "a" .* as int value` */}
   191  	)
   192  
   193  	// slice literals
   194  	var (
   195  		_ []int = {}
   196  		_ []int = {1, 2, 3}
   197  		_ []int = {0: 1, 10: 2}
   198  		_ []int = {- /* ERROR "index -1 (constant of type int) must not be negative" */ 1: 1}
   199  		_ []int = {"a" /* ERRORx `cannot convert "a" .* to type int` */ : 1}
   200  		_ []int = {0: 1, 0 /* ERROR "duplicate index 0" */ : 2}
   201  		_ []int = {"a" /* ERRORx `cannot use "a" .* as int value` */}
   202  	)
   203  
   204  	// map literals
   205  	k := "key"
   206  	var (
   207  		_ map[string]int = {}
   208  		_ map[string]int = {"a": 1, "b": 2}
   209  		_ map[string]int = {k: 1}
   210  		_ map[string]int = {1 /* ERROR "missing key in map literal" */}
   211  		_ map[string]int = {"a": 1, "a" /* ERROR `duplicate key "a"` */ : 2}
   212  		_ map[string]int = {1 /* ERRORx `cannot use 1 .* as string value` */ : 1}
   213  		_ map[string]int = {"a": "b" /* ERRORx `cannot use "b" .* as int value` */}
   214  	)
   215  }
   216  
   217  // ----------------------------------------------------------------------------
   218  // Contexts where untyped composite literals may appear.
   219  
   220  // 1. Variable declarations and short variable declarations
   221  func _() {
   222  	var _ = { /* ERROR "missing type in composite literal" */ }
   223  	var _ = { /* ERROR "missing type in composite literal" */ 1, 2}
   224  	var _ = { /* ERROR "missing type in composite literal" */ f: 1}
   225  
   226  	var s Struct = {1}
   227  	var s1, l1 = Struct{1}, Slice{1, 2}
   228  	var s2, s3 Struct = {1}, {f: 2}
   229  	_, _, _, _ = s, s1, s2, s3
   230  	_ = l1
   231  
   232  	x := { /* ERROR "missing type in composite literal" */ }
   233  	_ = x
   234  
   235  	// Redeclared variable in short variable declaration already has a type.
   236  	var r Struct
   237  	r, y := {1}, 42
   238  	_, _ = r, y
   239  }
   240  
   241  // 2. Constant declarations
   242  func _() {
   243  	const _ Struct /* ERROR "invalid constant type Struct" */ = {}
   244  	const _ int = { /* ERROR "missing type in composite literal" */ }
   245  	const _ = { /* ERROR "missing type in composite literal" */ }
   246  }
   247  
   248  // 3. Assignments
   249  func _() {
   250  	_ = { /* ERROR "missing type in composite literal" */ }
   251  
   252  	var (
   253  		s   Struct
   254  		a   Array
   255  		l   Slice
   256  		m   Map
   257  		p   PointerS
   258  		box struct{ s Struct }
   259  		arr [1]Struct
   260  		ptr *Struct
   261  		mp  map[string]Struct
   262  	)
   263  
   264  	s = {}
   265  	s = {1}
   266  	s = {f: 1}
   267  	a = {1, 2, 3, 9: 10}
   268  	l = {1, 2, 3}
   269  	m = {"a": 1, "b": 2}
   270  	p = {f: 1}
   271  
   272  	// multiple assignment
   273  	s, l, m = {1}, {1, 2}, {"a": 1}
   274  
   275  	// assignment to addressable operands and map index
   276  	box.s = {1}
   277  	arr[0] = {f: 2}
   278  	*ptr = {3}
   279  	mp["k"] = {4}
   280  
   281  	_, _, _, _, _, _, _ = s, a, l, m, p, box, arr
   282  }
   283  
   284  // 4. Return statements
   285  func _() Struct {
   286  	return {}
   287  	return {1}
   288  	return {f: 1}
   289  }
   290  
   291  func _() (Struct, Slice, Map) {
   292  	return {1}, {1, 2, 3}, {"a": 1}
   293  }
   294  
   295  func _() (s Struct, a Array, l Slice, m Map) {
   296  	return {f: 1}, {0: 1}, {1, 2}, {"a": 1}
   297  }
   298  
   299  // 5. Function and method calls
   300  type Recv struct{ x int }
   301  
   302  func (Recv) m(s Struct, rest ...Slice) {}
   303  
   304  func _() {
   305  	f := func(s Struct, a Array, l Slice, m Map) {}
   306  	f({}, {}, {}, {})
   307  	f({1}, {1, 2}, {3, 4}, {"a": 5})
   308  
   309  	fv := func(x int, rest ...Struct) {}
   310  	fv(0)
   311  	fv(0, {})
   312  	fv(0, {1}, {f: 2})
   313  
   314  	var r Recv
   315  	r.m({1}, {1, 2}, {3, 4})
   316  	Recv.m({1}, {2}, {1, 2}, {3, 4})
   317  }
   318  
   319  func fg[P any](x P)               {}
   320  func fgc[P ~struct{ f int }](x P) {}
   321  
   322  func _() {
   323  	fg[Struct]({})
   324  	fg[Struct]({1})
   325  	fg[Slice]({1, 2, 3})
   326  	fg[Map]({"a": 1})
   327  
   328  	fgc[Struct]({1})
   329  	fgc({f: 1})
   330  }
   331  
   332  // 6. Nested composite literals
   333  func _() {
   334  	type Nested struct {
   335  		s Struct
   336  		a [2]Struct
   337  		l []Struct
   338  		m map[Struct]Slice
   339  		p *Struct
   340  	}
   341  
   342  	// typed outer literal with untyped inner literals
   343  	_ = Nested{
   344  		s: {1},
   345  		a: {{1}, {f: 2}},
   346  		l: {{1}, {f: 2}},
   347  		m: {{1}: {1, 2}, {f: 2}: {}},
   348  		p: {f: 3},
   349  	}
   350  	_ = Nested{
   351  		{1},
   352  		{{1}, {f: 2}},
   353  		{{1}, {f: 2}},
   354  		{{1}: {1, 2}, {f: 2}: {}},
   355  		{f: 3},
   356  	}
   357  
   358  	// untyped outer literal with untyped inner literals
   359  	var _ Nested = {
   360  		s: {1},
   361  		a: {{1}, {f: 2}},
   362  		l: {{1}, {f: 2}},
   363  		m: {{1}: {1, 2}, {f: 2}: {}},
   364  		p: {f: 3},
   365  	}
   366  	var _ Nested = {
   367  		{1},
   368  		{{1}, {f: 2}},
   369  		{{1}, {f: 2}},
   370  		{{1}: {1, 2}, {f: 2}: {}},
   371  		{f: 3},
   372  	}
   373  
   374  	// [...]T array literal with untyped element literals
   375  	_ = [...]Struct{{}, {1}, {f: 2}}
   376  	_ = [...]*Struct{{}, {1}, {f: 2}}
   377  
   378  	// generic function value inside untyped struct literal (reverse type inference)
   379  	type FuncHolder struct {
   380  		fn func(int)
   381  	}
   382  	var _ FuncHolder = {fg}
   383  	var _ FuncHolder = {fn: fg}
   384  }
   385  
   386  // 7. Map index expressions
   387  func _[M ~map[Struct]Slice](gm M) {
   388  	var m map[Struct]Slice
   389  	_ = m[{}]
   390  	_ = m[{1}]
   391  	_ = m[{f: 1}]
   392  	v, ok := m[{f: 1}]
   393  	_, _ = v, ok
   394  	m[{1}] = {1, 2, 3}
   395  
   396  	var counts map[Struct]int
   397  	counts[{1}]++
   398  	counts[{f: 2}] += 10
   399  
   400  	_ = gm[{}]
   401  	gm[{1}] = {1, 2, 3}
   402  }
   403  
   404  // 8. Channel send statements
   405  func _[C ~chan Struct](gch C) {
   406  	var ch chan Struct
   407  	var sch chan<- Struct
   408  	ch <- {}
   409  	ch <- {1}
   410  	sch <- {f: 1}
   411  	gch <- {1}
   412  
   413  	select {
   414  	case ch <- {1}:
   415  	case sch <- {f: 2}:
   416  	default:
   417  	}
   418  }
   419  
   420  // 9. Conversions T(x)
   421  func _[P ~struct{ f int }]() {
   422  	_ = _Array({1, 2, 3})
   423  	_ = _Slice({1, 2, 3})
   424  	_ = _Struct({1})
   425  	_ = _Struct({f: 1})
   426  	_ = _PointerS({f: 1})
   427  	_ = _Map({"a": 1})
   428  
   429  	_ = Array({1, 2, 3})
   430  	_ = Slice({1, 2, 3})
   431  	_ = Struct({1})
   432  	_ = Struct({f: 1})
   433  	_ = PointerS({f: 1})
   434  	_ = Map({"a": 1})
   435  
   436  	_ = ([3]int)({1, 2, 3})
   437  	_ = ([]int)({1, 2, 3})
   438  	_ = (struct{ f int })({f: 1})
   439  	_ = (*struct{ f int })({f: 1})
   440  	_ = (map[string]int)({"a": 1})
   441  
   442  	_ = P({})
   443  	_ = P({1})
   444  	_ = P({f: 1})
   445  
   446  	// selector, indexing, and slicing on converted untyped composite literals
   447  	_ = Struct({f: 1}).f
   448  	_ = Slice({1, 2, 3})[0]
   449  	_ = Slice({1, 2, 3})[1:3]
   450  	_ = Map({"a": 1})["a"]
   451  }
   452  
   453  // 10. Other expressions and statements (parentheses, unary &, variadic ..., comparisons, switch, built-ins)
   454  func _() {
   455  	var (
   456  		s  Struct
   457  		sl []Struct
   458  		l  Slice
   459  		m  map[Struct]int
   460  	)
   461  
   462  	// parenthesized untyped composite literals (type inference does not see through parentheses)
   463  	var _ Struct = ({ /* ERROR "missing type in composite literal" */ 1})
   464  
   465  	// address of untyped composite literal (target type does not propagate through &)
   466  	var _ *Struct = &{ /* ERROR "missing type in composite literal" */ 1}
   467  
   468  	// variadic call with ... (currently target type is element type rather than slice type)
   469  	fv := func(x int, rest ...int) {}
   470  	fv(0, { /* ERROR "invalid composite literal type int" */ 1, 2, 3}...)
   471  
   472  	// comparisons
   473  	// TODO(gri) these should work
   474  	_ = s == { /* ERROR "missing type in composite literal" */ 1}
   475  	_ = { /* ERROR "missing type in composite literal" */ 1} == s
   476  
   477  	// switch cases
   478  	// TODO(gri) these should work
   479  	switch s {
   480  	case { /* ERROR "missing type in composite literal" */ 1}:
   481  	}
   482  
   483  	// built-in functions where untyped composite literals are allowed
   484  	// TODO(gri) there should work
   485  	_ = append(sl, { /* ERROR "missing type in composite literal" */ 1})
   486  	_ = copy(l, { /* ERROR "missing type in composite literal" */ 1, 2, 3})
   487  	delete(m, { /* ERROR "missing type in composite literal" */ 1})
   488  
   489  	// built-in functions where untyped composite literals are not allowed
   490  	_ = new({ /* ERROR "missing type in composite literal" */ })
   491  	_ = len({ /* ERROR "missing type in composite literal" */ })
   492  	_ = cap({ /* ERROR "missing type in composite literal" */ })
   493  	clear({ /* ERROR "missing type in composite literal" */ })
   494  	panic({ /* ERROR "missing type in composite literal" */ })
   495  	print({ /* ERROR "missing type in composite literal" */ })
   496  	println({ /* ERROR "missing type in composite literal" */ })
   497  }
   498  

View as plain text