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

fix(gnovm): Improve Error Message in evalStaticTypeOf function #2695

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ type IndexExpr struct { // X[Index]
Attributes
X Expr // expression
Index Expr // index expression
HasOK bool // if true, is form: `value, ok := <X>[<Key>]
HasOK bool // if true, is form: `value, ok := <X>[<Key>]`
}

type SelectorExpr struct { // X.Sel
Expand Down
33 changes: 25 additions & 8 deletions gnovm/pkg/gnolang/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -2449,16 +2449,33 @@ 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]
loc := last.GetLocation()
funcName := getFunctionName(x)
valueCount := len(tt.Elts)

var msg string
if valueCount == 0 {
msg = fmt.Sprintf("%s: %s() (no value) used as value", loc, funcName)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe the message could be improved here. "no value" doesn't seem as clear as saying something like "function does not return a value".

} else {
msg = fmt.Sprintf("%s: %s() (%d values) used as single value", loc, funcName, valueCount)
}

panic(fmt.Errorf("%s\nHint: Ensure the function returns a single value, or use multiple assignment", msg))
Comment on lines +2458 to +2463
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This hint may not be very effective if it corresponds to one of the messages above, such as:

panic: main/func.gno:3:1: f() (no value) used as value
Hint: Ensure the function returns a single value, or use multiple assignment.

I mean in this case, "or use multiple assignment" is superfluous。

Copy link
Contributor

@ltzmaxwell ltzmaxwell Aug 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or not, this check should be checked somewhere other than evalStaticTypeOf?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well I think this part feels odd to me(the original logic in evalStaticTypeOf:

if len(tt.Elts) != 1 {
			panic(fmt.Sprintf(
				"evalStaticTypeOf() only supports *CallExpr with 1 result, got %s",
				tt.String(),
			))

at the very least for case like:

n := f2()

func f2() (string, int) {
	return "hey", 0
}

the check can happened in *AssignStmt, to identify a mismatch on LHS and RHS.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Writing so much just for the sake of discussion.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes @ltzmaxwell I think this check should be done in AssignStmt and ValueDecl in trans_enter

}
return tt.Elts[0]
}
return t
}

// getFunctionName attempts to extract the function name from the expression
func getFunctionName(x Expr) string {
switch expr := x.(type) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a nitpick, but it is a bit strange to have a switch with only one case

case *CallExpr:
if id, ok := expr.Func.(*NameExpr); ok {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

another name other than id?

return string(id.Name)
}
} else {
return t
}
return "<unknown function>"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this happen?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't happend. I added it just in case.

}

// like evalStaticTypeOf() but returns the raw *tupleType for *CallExpr.
Expand Down
59 changes: 59 additions & 0 deletions gnovm/pkg/gnolang/preprocess_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,62 @@ func main() {
initStaticBlocks(store, pn, n)
Preprocess(store, pn, n)
}

func TestEvalStaticTypeOf_MultipleValues(t *testing.T) {
pn := NewPackageNode("main", "main", nil)
pv := pn.NewPackage()
store := gonativeTestStore(pn, pv)
store.SetBlockNode(pn)

const src = `package main
func multipleReturns() (int, string) {
return 1, "hello"
}
func main() {
x := multipleReturns()
}`
n := MustParseFile("main.go", src)

initStaticBlocks(store, pn, n)

defer func() {
err := recover()
assert.NotNil(t, err, "Expected panic")
errMsg := fmt.Sprint(err)
assert.Contains(t, errMsg, "multipleReturns() (2 values) used as single value", "Unexpected error message")
assert.Contains(t, errMsg, "Hint: Ensure the function returns a single value, or use multiple assignment", "Missing hint in error message")
}()

Preprocess(store, pn, n)
}

func TestEvalStaticTypeOf_NoValue(t *testing.T) {
pn := NewPackageNode("main", "main", nil)
pv := pn.NewPackage()
store := gonativeTestStore(pn, pv)
store.SetBlockNode(pn)

const src = `package main

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

func f() {
println("hello!")
}
`
n := MustParseFile("main.go", src)

initStaticBlocks(store, pn, n)

defer func() {
err := recover()
assert.NotNil(t, err, "Expected panic")
errMsg := fmt.Sprint(err)
assert.Contains(t, errMsg, "f() (no value) used as value", "Unexpected error message")
assert.Contains(t, errMsg, "Hint: Ensure the function returns a single value, or use multiple assignment", "Missing hint in error message")
}()

Preprocess(store, pn, n)
}
Loading