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

feat(gnovm): returns error like in Golang when assignment with a function which does not return any value #3049

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
297dd6d
chore: refactor DRY code
hthieu1110 Oct 23, 2024
6e6bcae
wip
Oct 24, 2024
f196044
wip: sync AssignStmt DEFINE with ValueDecl
hthieu1110 Oct 24, 2024
efc7467
Merge remote-tracking branch 'remote/master' into feat/sync-assignstm…
Oct 25, 2024
26792b5
fix: use tupletype for evalStaticTypeOfRaw for CallExpr
Oct 25, 2024
551ab25
wip: sync the general case
Oct 25, 2024
d7401c0
feat: refactor general case code for AssignStmt vs ValueDecl
hthieu1110 Oct 25, 2024
96f279c
chore: remove unused codes + file
hthieu1110 Oct 25, 2024
e54abf8
Merge branch 'master' into feat/sync-assignstmt-valuedecl-1958
hthieu1110 Oct 25, 2024
b050dc6
Merge remote-tracking branch 'remote/master' into feat/sync-assignstm…
hthieu1110 Oct 26, 2024
81b6be5
chore: optimize numNames check
hthieu1110 Oct 26, 2024
1d95d48
chore: add more tests
hthieu1110 Oct 26, 2024
0cf7ca1
Merge remote-tracking branch 'remote/master' into feat/sync-assignstm…
hthieu1110 Oct 28, 2024
edd2287
chore: refactor tests
hthieu1110 Oct 28, 2024
5ecc039
feat: refactor defineOrDecl to raise the same error + add more tests
hthieu1110 Oct 28, 2024
0e80d12
fix: fix tests
hthieu1110 Oct 28, 2024
f3e021f
chore: optimize code
hthieu1110 Oct 30, 2024
f57aafa
Merge remote-tracking branch 'remote/master' into feat/sync-assignstm…
hthieu1110 Oct 30, 2024
2460630
fix: return the same error as go in case of assignment with no return…
hthieu1110 Oct 30, 2024
dc671bd
Merge branch 'master' into feat/cryptic-error-func-return-0-value-1082
hthieu1110 Nov 21, 2024
af39368
chore: refactor code to more readable version
hthieu1110 Nov 21, 2024
d8caf99
Merge remote-tracking branch 'remote/master' into feat/cryptic-error-…
Nov 22, 2024
5ee3df2
feat: handle error when f() not return value like in Golang
Nov 22, 2024
56505b4
chore: make the check more explicit
Nov 22, 2024
6edd434
fix: fix test var34c
Nov 22, 2024
cd49194
Merge branch 'master' into feat/cryptic-error-func-return-0-value-1082
hthieu1110 Nov 22, 2024
877c618
Merge branch 'feat/cryptic-error-func-return-0-value-1082' of https:/…
hthieu1110 Nov 22, 2024
bf9b5bc
Merge remote-tracking branch 'remote/master' into feat/cryptic-error-…
hthieu1110 Nov 22, 2024
ce5717a
fix: refactor
hthieu1110 Nov 22, 2024
fc2b59e
fix: revert and adjust version before refactor
hthieu1110 Nov 22, 2024
97436f8
Merge branch 'master' into feat/cryptic-error-func-return-0-value-1082
hthieu1110 Nov 25, 2024
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
20 changes: 7 additions & 13 deletions gnovm/pkg/gnolang/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -2373,7 +2373,7 @@ func defineOrDecl(
sts := make([]Type, numNames) // static types
tvs := make([]TypedValue, numNames)

if numNames > 1 && len(valueExprs) == 1 {
if numVals == 1 && numNames > 1 {
parseMultipleAssignFromOneExpr(sts, tvs, store, bn, nameExprs, typeExpr, valueExprs[0])
} else {
parseAssignFromExprList(sts, tvs, store, bn, isConst, nameExprs, typeExpr, valueExprs)
Expand Down Expand Up @@ -2410,7 +2410,7 @@ func parseAssignFromExprList(
for _, v := range valueExprs {
if cx, ok := v.(*CallExpr); ok {
tt, ok := evalStaticTypeOfRaw(store, bn, cx).(*tupleType)
if ok && len(tt.Elts) != 1 {
if ok && len(tt.Elts) > 1 {
panic(fmt.Sprintf("multiple-value %s (value of type %s) in single-value context", cx.Func.String(), tt.Elts))
}
}
Expand Down Expand Up @@ -3132,18 +3132,12 @@ func gnoTypeOf(store Store, t Type) Type {
// but rather computes the type OF x.
func evalStaticTypeOf(store Store, last BlockNode, x Expr) Type {
t := evalStaticTypeOfRaw(store, last, x)
if tt, ok := t.(*tupleType); ok {
if len(tt.Elts) != 1 {
panic(fmt.Sprintf(
"evalStaticTypeOf() only supports *CallExpr with 1 result, got %s",
tt.String(),
))
} else {
return tt.Elts[0]
}
} else {
return t

if tt, ok := t.(*tupleType); ok && len(tt.Elts) == 1 {
return tt.Elts[0]
}

return t
}

// like evalStaticTypeOf() but returns the raw *tupleType for *CallExpr.
Expand Down
8 changes: 8 additions & 0 deletions gnovm/pkg/gnolang/type_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -1029,6 +1029,14 @@ func assertValidAssignRhs(store Store, last BlockNode, n Node) {
tt = evalStaticType(store, last, exp)
panic(fmt.Sprintf("%s (type) is not an expression", tt.String()))
}

// Ensures that function used in ValueDecl or AssignStmt must return at least 1 value.
if cx, ok := exp.(*CallExpr); ok {
tType, ok := tt.(*tupleType)
if ok && len(tType.Elts) == 0 {
ltzmaxwell marked this conversation as resolved.
Show resolved Hide resolved
panic(fmt.Sprintf("%s (no value) used as value", cx.Func.String()))
}
}
}
}

Expand Down
10 changes: 10 additions & 0 deletions gnovm/tests/files/assign37.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package main

func f() { }

func main() {
a := f()
}

// Error:
// main/files/assign37.gno:6:2: f<VPBlock(3,0)> (no value) used as value
12 changes: 12 additions & 0 deletions gnovm/tests/files/assign37b.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

import "fmt"

func f() { }

func main() {
a, b := f(), f()
}

// Error:
// main/files/assign37b.gno:8:2: f<VPBlock(3,0)> (no value) used as value
10 changes: 10 additions & 0 deletions gnovm/tests/files/var34.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package main

func f() {}

func main() {
var t = f()
}

// Error:
// main/files/var34.gno:6:6: f<VPBlock(3,0)> (no value) used as value
11 changes: 11 additions & 0 deletions gnovm/tests/files/var34b.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package main

func f() {}

func main() {
var a int
a = f()
}

// Error:
// main/files/var34b.gno:7:2: f<VPBlock(3,0)> (no value) used as value
10 changes: 10 additions & 0 deletions gnovm/tests/files/var34c.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package main

func f() {}

func main() {
var a, b int = f(), 1
}

// Error:
// main/files/var34c.gno:6:6: f<VPBlock(3,0)> (no value) used as value
Loading