Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

experiment: support eliding for struct fields [DON'T REVIEW] #1389

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions examples/gno.land/p/demo/aaa/aaa.gno
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 1 addition & 0 deletions examples/gno.land/p/demo/aaa/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module gno.land/p/demo/aaa
41 changes: 34 additions & 7 deletions gnovm/pkg/gnolang/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -3262,20 +3266,23 @@ func elideCompositeElements(clx *CompositeLitExpr, clt Type) {
}
*/
case *ArrayType:
fmt.Println("ArrayType:")
et := clt.Elt
el := len(clx.Elts)
for i := 0; i < el; i++ {
kvx := &clx.Elts[i]
elideCompositeExpr(&kvx.Value, et)
}
case *SliceType:
fmt.Println("SliceType:")
et := clt.Elt
el := len(clx.Elts)
for i := 0; i < el; i++ {
kvx := &clx.Elts[i]
elideCompositeExpr(&kvx.Value, et)
}
case *MapType:
fmt.Println("MapType:")
kt := clt.Key
vt := clt.Value
el := len(clx.Elts)
Expand All @@ -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
Expand All @@ -3312,6 +3319,26 @@ func elideCompositeElements(clx *CompositeLitExpr, clt Type) {
"unexpected composite lit type %s",
clt.String()))
}

fmt.Println("\n\n <xxx> \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 <xxx> \n\n")
}

// if *vx is composite lit type, fill in elided type.
Expand Down
Loading