From 5a615d81787c561803aa5b08ff0764bdb5388048 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Wed, 24 Jun 2020 19:25:08 +0300 Subject: [PATCH] compiler: allow to omit types for nested slices --- pkg/compiler/codegen.go | 14 +++++--------- pkg/compiler/slice_test.go | 21 +++++++++++++++++++++ 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/pkg/compiler/codegen.go b/pkg/compiler/codegen.go index 3df5ffa43..7093acae3 100644 --- a/pkg/compiler/codegen.go +++ b/pkg/compiler/codegen.go @@ -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. diff --git a/pkg/compiler/slice_test.go b/pkg/compiler/slice_test.go index b90283a2a..2dfe9adbc 100644 --- a/pkg/compiler/slice_test.go +++ b/pkg/compiler/slice_test.go @@ -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) {