compiler: allow to omit types for nested slices

This commit is contained in:
Evgenii Stratonikov 2020-06-24 19:25:08 +03:00
parent 53f2e130c0
commit 5a615d8178
2 changed files with 26 additions and 9 deletions

View file

@ -594,15 +594,11 @@ func (c *codegen) Visit(node ast.Node) ast.Visitor {
return nil
case *ast.CompositeLit:
typ := c.typeOf(n.Type).Underlying()
switch n.Type.(type) {
case *ast.Ident, *ast.SelectorExpr, *ast.MapType:
switch typ.(type) {
case *types.Struct:
c.convertStruct(n)
case *types.Map:
c.convertMap(n)
}
switch typ := c.typeOf(n).Underlying().(type) {
case *types.Struct:
c.convertStruct(n)
case *types.Map:
c.convertMap(n)
default:
ln := len(n.Elts)
// ByteArrays needs a different approach than normal arrays.

View file

@ -222,6 +222,27 @@ var sliceTestCases = []testCase{
}`,
big.NewInt(42),
},
{
"nested slice omitted type (slice)",
`package foo
func Main() int {
a := [][]int{{1, 2}, {3, 4}}
a[1][0] = 42
return a[1][0]
}`,
big.NewInt(42),
},
{
"nested slice omitted type (struct)",
`package foo
type pair struct { a, b int }
func Main() int {
a := []pair{{a: 1, b: 2}, {a: 3, b: 4}}
a[1].a = 42
return a[1].a
}`,
big.NewInt(42),
},
}
func TestSliceOperations(t *testing.T) {