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

WIP fix(gnovm): correct const type deduction #2948

Draft
wants to merge 10 commits into
base: master
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion gnovm/pkg/gnolang/gno_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ func BenchmarkIfStatement(b *testing.B) {
func main() {
for i:=0; i<10000; i++ {
if i > 10 {

}
}
}`
Expand Down
33 changes: 30 additions & 3 deletions gnovm/pkg/gnolang/go2gno.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ func ParseFile(filename string, body string) (fn *FileNode, err error) {
}()
// parse with Go2Gno.
fn = Go2Gno(fs, f).(*FileNode)
//fmt.Println("---fn after transpile: ", fn)
fn.Name = Name(filename)
return fn, nil
}
Expand Down Expand Up @@ -408,6 +409,7 @@ func Go2Gno(fs *token.FileSet, gon ast.Node) (n Node) {
VarName: toName(as.Lhs[0].(*ast.Ident)),
}
case *ast.ExprStmt:
println("---ExprStmt")
return &SwitchStmt{
Init: toStmt(fs, gon.Init),
X: toExpr(fs, as.X.(*ast.TypeAssertExpr).X),
Expand Down Expand Up @@ -457,6 +459,7 @@ func Go2Gno(fs *token.FileSet, gon ast.Node) (n Node) {
case *ast.GenDecl:
panic("unexpected *ast.GenDecl; use toDecls(fs,) instead")
case *ast.File:
//println("---ast.File")
pkgName := Name(gon.Name.Name)
decls := make([]Decl, 0, len(gon.Decls))
for _, d := range gon.Decls {
Expand Down Expand Up @@ -767,14 +770,22 @@ func toDecls(fs *token.FileSet, gd *ast.GenDecl) (ds Decls) {
for _, id := range s.Names {
names = append(names, *Nx(toName(id)))
}
if s.Type == nil {
if s.Type == nil && s.Values == nil { // inherit declared type
tipe = lastType
} else {
tipe = toExpr(fs, s.Type)
lastType = tipe
lastType = toExpr(fs, s.Type)
}

if nx, ok := lastType.(*NameExpr); ok {
if !isPrimitiveType(string(nx.Name)) {
tipe = lastType
}
}

if s.Values == nil {
// inherit type from last values
values = copyExprs(lastValues)
// if lastType is declaredType, inherit it
} else {
values = toExprs(fs, s.Values)
lastValues = values
Expand Down Expand Up @@ -919,3 +930,19 @@ func toSwitchClauseStmt(fs *token.FileSet, cc *ast.CaseClause) SwitchClauseStmt
Body: toStmts(fs, cc.Body),
}
}
func isPrimitiveType(typeStr string) bool {
// List of primitive type prefixes
primitiveTypes := []string{
"int", "int8", "int16", "int32", "int64",
"uint", "uint8", "uint16", "uint32", "uint64",
"float32", "float64", "string", "bool",
}

for _, t := range primitiveTypes {
if strings.HasPrefix(typeStr, t) {
return true
}
}

return false
}
14 changes: 8 additions & 6 deletions gnovm/pkg/gnolang/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -2233,7 +2233,7 @@ func Preprocess(store Store, ctx BlockNode, n Node) Node {
}
}
}
// evaluate types and convert consts.

if n.Type != nil {
// only a single type can be specified.
nt := evalStaticType(store, last, n.Type)
Expand All @@ -2245,12 +2245,14 @@ func Preprocess(store Store, ctx BlockNode, n Node) Node {
checkOrConvertType(store, last, &n.Values[i], nt, false)
}
} else if n.Const {
// derive static type from values.
for i, vx := range n.Values {
vt := evalStaticTypeOf(store, last, vx)
sts[i] = vt
if n.Values != nil {
// derive static type from values.
for i, vx := range n.Values {
vt := evalStaticTypeOf(store, last, vx)
sts[i] = vt
}
}
} else {
} else { // not const, type is nil
// convert n.Value to default type.
for i, vx := range n.Values {
convertIfConst(store, last, vx)
Expand Down
43 changes: 43 additions & 0 deletions gnovm/tests/files/const23.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package main

type Integer int

const (
i0 Integer = 0

i int32 = 1
s = "world"
c = 1 << iota

s2 = "hey"
d, e = 1, "hello"

m, n

b = 1 + 1
u = +1
x, y = i0, "hello"
)

func main() {
println(i)
println(s)
println(c)
println(s2)
println(d, e)
println(m, n)
println(b)
println(u)
println(x, y)
}

// Output:
// 1
// world
// 8
// hey
// 1 hello
// 1 hello
// 2
// 1
// (0 main.Integer) hello
14 changes: 14 additions & 0 deletions gnovm/tests/files/const24.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package main

const (
s int = 1
g = "world"
c = 1 << iota
)

func main() {
println(c)
}

// Output:
// 4
14 changes: 14 additions & 0 deletions gnovm/tests/files/const25.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package main

const (
d, e = 1, "hello"
m
)

func main() {
println(d, e)
println(m)
}

// Error:
// main/files/const25.gno:5:2: assignment mismatch: 1 variable(s) but 2 value(s)
14 changes: 14 additions & 0 deletions gnovm/tests/files/const25a.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package main

const (
d, e = 1, "hello"
m, n, l
)

func main() {
println(d, e)
println(m)
}

// Error:
// main/files/const25a.gno:5:2: assignment mismatch: 3 variable(s) but 2 value(s)
15 changes: 15 additions & 0 deletions gnovm/tests/files/const26.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package main

const (
d = "hello"
m
)

func main() {
println(d)
println(m)
}

// Output:
// hello
// hello
Loading