diff --git a/examples/gno.land/p/demo/aaa/aaa.gno b/examples/gno.land/p/demo/aaa/aaa.gno new file mode 100644 index 00000000000..33832d45edf --- /dev/null +++ b/examples/gno.land/p/demo/aaa/aaa.gno @@ -0,0 +1,24 @@ +// Experiment with eliding + +package aaa + +type Foo struct { + a string + b int +} + +type Bar struct { + c string + d Foo +} + +func Experiment() { + // a := [3]T{0: "a", 1: "b", 2: "c"} + // var a []Foo = []Foo{{"A", 1}, {"B", 2}} + var b []Bar = []Bar{ + {"A", {"A", 1}}, + {"B", {"B", 2}}, + } + // _ = a + _ = b +} diff --git a/examples/gno.land/p/demo/aaa/gno.mod b/examples/gno.land/p/demo/aaa/gno.mod new file mode 100644 index 00000000000..8106cc55552 --- /dev/null +++ b/examples/gno.land/p/demo/aaa/gno.mod @@ -0,0 +1 @@ +module gno.land/p/demo/aaa diff --git a/gnovm/pkg/gnolang/preprocess.go b/gnovm/pkg/gnolang/preprocess.go index 1a1edc41222..a03281f3bb5 100644 --- a/gnovm/pkg/gnolang/preprocess.go +++ b/gnovm/pkg/gnolang/preprocess.go @@ -3245,6 +3245,10 @@ func fileNameOf(n BlockNode) Name { } func elideCompositeElements(clx *CompositeLitExpr, clt Type) { + oldBaseOfClt := baseOf(clt) + oldClx := *clx + oldClt := clt + switch clt := baseOf(clt).(type) { /* case *PointerType: @@ -3262,6 +3266,7 @@ func elideCompositeElements(clx *CompositeLitExpr, clt Type) { } */ case *ArrayType: + fmt.Println("ArrayType:") et := clt.Elt el := len(clx.Elts) for i := 0; i < el; i++ { @@ -3269,6 +3274,7 @@ func elideCompositeElements(clx *CompositeLitExpr, clt Type) { elideCompositeExpr(&kvx.Value, et) } case *SliceType: + fmt.Println("SliceType:") et := clt.Elt el := len(clx.Elts) for i := 0; i < el; i++ { @@ -3276,6 +3282,7 @@ func elideCompositeElements(clx *CompositeLitExpr, clt Type) { elideCompositeExpr(&kvx.Value, et) } case *MapType: + fmt.Println("MapType:") kt := clt.Key vt := clt.Value el := len(clx.Elts) @@ -3285,19 +3292,19 @@ func elideCompositeElements(clx *CompositeLitExpr, clt Type) { elideCompositeExpr(&kvx.Value, vt) } case *StructType: + fmt.Println("StructType:") // Struct fields cannot be elided in Go for // legibility, but Gno could support them (e.g. for // certain tagged struct fields). // TODO: support eliding. - for _, kvx := range clx.Elts { - vx := kvx.Value - if vclx, ok := vx.(*CompositeLitExpr); ok { - if vclx.Type == nil { - panic("types cannot be elided in composite literals for struct types") - } - } + el := len(clx.Elts) + for i := 0; i < el; i++ { + kvx := &clx.Elts[i] + ft := clt.Fields[i].Type + elideCompositeExpr(&kvx.Value, ft) } case *NativeType: + fmt.Println("NativeType:") // TODO: support eliding. for _, kvx := range clx.Elts { vx := kvx.Value @@ -3312,6 +3319,26 @@ func elideCompositeElements(clx *CompositeLitExpr, clt Type) { "unexpected composite lit type %s", clt.String())) } + + fmt.Println("\n\n \n") + + fmt.Println("oldBaseOfClt:") + fmt.Println("-", oldBaseOfClt) + fmt.Println("+", baseOf(clt)) + + fmt.Println("\n --- \n") + + fmt.Println("clt:") + fmt.Println("-", oldClt) + fmt.Println("+", clt) + + fmt.Println("\n --- \n") + + fmt.Println("clx:") + fmt.Println("-", oldClx) + fmt.Println("+", clx) + + fmt.Println("\n \n\n") } // if *vx is composite lit type, fill in elided type.