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

0 "numeric errors" will be == to any error created with errors.New #1376

Closed
thehowl opened this issue Nov 16, 2023 · 2 comments · Fixed by #1890
Closed

0 "numeric errors" will be == to any error created with errors.New #1376

thehowl opened this issue Nov 16, 2023 · 2 comments · Fixed by #1890
Assignees
Labels
🐞 bug Something isn't working 📦 🤖 gnovm Issues or PRs gnovm related

Comments

@thehowl
Copy link
Member

thehowl commented Nov 16, 2023

package main

import (
	"errors"
	"strconv"
)

type Error int64

func (e Error) Error() string {
	return "error: " + strconv.Itoa(int(e))
}

var errCmp = errors.New("XXXX")

func main() {
	if Error(0) == errCmp {
		println("what the firetruck?")
	}
}

// Output: what the firetruck?

Discovered while fixing out bugs for #1290.

If the error is Error(1), it works normally.

A workaround is that of making Error a struct [ n int64 }, and its Error method a pointer receiver. Weirdly enough, without the pointer receiver it doesn't work still!

@thehowl thehowl added 🐞 bug Something isn't working 📦 🤖 gnovm Issues or PRs gnovm related labels Nov 16, 2023
@thehowl thehowl added this to the 🚀 main.gno.land (required) milestone Nov 16, 2023
@thehowl thehowl moved this to 🚀 Needed for Launch in 🚀 The Launch [DEPRECATED] Nov 16, 2023
@thehowl
Copy link
Member Author

thehowl commented Nov 16, 2023

The root cause is probably different, related to how we pass around errors as values; but I'm leaving that as a // TODO for the undertaker

@thehowl thehowl changed the title returning a "numeric error" through io.ReadAll makes the error become nil returning a 0 "numeric error" through io.ReadAll makes the error become nil Nov 16, 2023
@thehowl thehowl changed the title returning a 0 "numeric error" through io.ReadAll makes the error become nil 0 "numeric errors" will be == to any error created with errors.New Nov 16, 2023
@ltzmaxwell ltzmaxwell self-assigned this Nov 17, 2023
@ltzmaxwell
Copy link
Contributor

ltzmaxwell commented Dec 11, 2023

Briefly it's because the incorrect type. the vm erroneously treat Error(0) and errCmp with same type of int64, which is the underlying type of Error, thus the value evaluated as int64 are both 0. that is also why Error(1) works. I opened an issue to discuss this #1424 .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐞 bug Something isn't working 📦 🤖 gnovm Issues or PRs gnovm related
Projects
Status: 🚀 Needed for Launch
Development

Successfully merging a pull request may close this issue.

2 participants