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 1 commit into
base: master
Choose a base branch
from

Conversation

notJoon
Copy link
Member

@notJoon notJoon commented Aug 14, 2024

Description

Closes #1082

Enhanced the error message in evalStaticTypeOf function to match with go's error message and provide more informative feeback.

Example of Errors

Sample code (taken from linked issue)

package main

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

func f() {
	println("hello!")
}

Before

panic: evalStaticTypeOf() only supports *CallExpr with 1 result, got ()

After

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

Go version (go playground)

./prog.go:4:2: declared and not used: n
./prog.go:4:7: f() (no value) used as value

Further Works

I think it would be good to fix getTypeOf function as well, but I can't think of a case where it would cause panic.

Contributors' checklist...
  • Added new tests, or not needed, or not feasible
  • Provided an example (e.g. screenshot) to aid review or the PR is self-explanatory
  • Updated the official documentation or not needed
  • No breaking changes were made, or a BREAKING CHANGE: xxx message was included in the description
  • Added references to related issues and PRs
  • Provided any useful hints for running manual tests
  • Added new benchmarks to generated graphs, if any. More info here.

@github-actions github-actions bot added the 📦 🤖 gnovm Issues or PRs gnovm related label Aug 14, 2024
Copy link

codecov bot commented Aug 14, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 58.80%. Comparing base (0e3c050) to head (cf9a3fd).
Report is 19 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2695      +/-   ##
==========================================
+ Coverage   54.99%   58.80%   +3.80%     
==========================================
  Files         595      561      -34     
  Lines       79775    74792    -4983     
==========================================
+ Hits        43872    43978     +106     
+ Misses      32581    27495    -5086     
+ Partials     3322     3319       -3     
Flag Coverage Δ
contribs/gnodev 61.40% <ø> (+35.40%) ⬆️
contribs/gnofaucet 14.46% <ø> (ø)
gno.land 64.75% <ø> (+0.59%) ⬆️
misc/genstd 80.54% <ø> (ø)
misc/logos 19.88% <ø> (+2.50%) ⬆️
tm2 62.01% <ø> (+7.51%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@notJoon notJoon changed the title feat(gnovm): Improve Error Message in evalStaticTypeOf function fix(gnovm): Improve Error Message in evalStaticTypeOf function Aug 14, 2024
}
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.

func getFunctionName(x Expr) string {
switch expr := x.(type) {
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?

Comment on lines +2458 to +2463
msg = fmt.Sprintf("%s: %s() (no value) used as value", loc, funcName)
} 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))
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
Contributor

@deelawn deelawn left a comment

Choose a reason for hiding this comment

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

A few small suggestions, but looks good to me overall.


// 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


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".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
📦 🤖 gnovm Issues or PRs gnovm related
Projects
Status: In Progress
Status: In Review
Status: In Review
3 participants