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

[wasi-common] Use thiserror proc macros for auto From impls #758

Merged
merged 4 commits into from
Jan 6, 2020

Conversation

kubkon
Copy link
Member

@kubkon kubkon commented Jan 3, 2020

This commit refactors wasi_common::error::Error by using #[from]
proc macro to autoderive From for wrapped errors.

Jakub Konka added 2 commits January 3, 2020 11:30
This commit refactors `wasi_common::error::Error` by using `#[from]`
proc macro to autoderive `From` for wrapped errors.
@kubkon kubkon added the wasi:impl Issues pertaining to WASI implementation in Wasmtime label Jan 3, 2020
@@ -97,38 +97,23 @@ impl WasiError {
impl fmt::Display for WasiError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
_ => write!(f, "{:?}", self),
_ => write!(f, "{}", self),
Copy link
Member

Choose a reason for hiding this comment

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

Can this whole fmt::Display impl be removed entirely now?

Copy link
Member Author

Choose a reason for hiding this comment

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

We can get rid of it, but we still need WasiError to derive fmt::Display which however can be done more efficiently using #[error(..)] attribute. See 3b392db.

@@ -97,38 +97,23 @@ impl WasiError {
impl fmt::Display for WasiError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
_ => write!(f, "{:?}", self),
_ => write!(f, "{}", self),
Copy link
Member

Choose a reason for hiding this comment

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

Ditto.

Copy link
Member Author

Choose a reason for hiding this comment

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

LIke above.

Copy link
Member

@sunfishcode sunfishcode left a comment

Choose a reason for hiding this comment

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

Ah, thanks for simplifying that. I imagine we'll eventually want to do the equivalent of strerror for WASI error codes and display that, but that's not something the current code does, so this seems good for now.

@kubkon
Copy link
Member Author

kubkon commented Jan 3, 2020

Ah, thanks for simplifying that. I imagine we'll eventually want to do the equivalent of strerror for WASI error codes and display that, but that's not something the current code does, so this seems good for now.

Agreed! Currently, whenever some unexpected error happens, all we get is the error code which can be quite tricky to "parse" mentally ;-)

use thiserror::Error;

#[derive(Clone, Copy, Debug, Error, Eq, PartialEq)]
#[repr(u16)]
#[error("{}", self)]
Copy link

Choose a reason for hiding this comment

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

This seems unlikely to work. You are implementing Display for WasiError by calling Display for WasiError.

#[test]
fn test_display_wasi_error() {
    WasiError::ESUCCESS.to_string();
}
thread 'error::test_display_wasi_error' has overflowed its stack
fatal runtime error: stack overflow

Did you maybe mean #[error("{}", *self as __wasi_errno_t)] or #[error("{:?}", self)]?

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, I missed that one, thanks for the pointer! Yeah, I definitely meant #[error("{}", *self as __wasi_errno_t)]. I'll provide a fix ASAP.

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmm, on second thought, I think I prefer the second option of #[error("{:?}", self)] since it displays error value that's closer to its actual string representation rather than displaying the raw error code; that is, println!("err={}", err)

  • with #[error("{}", *self as wasi::__wasi_errno_t)] produces err=28,
  • whereas with #[error("{:?}", self)], a nicer looking and easier to reason about err=EINVAL

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed in 1b42cf4

@alexcrichton
Copy link
Member

👍

data-pup pushed a commit to data-pup/wasmtime that referenced this pull request Jan 8, 2020
…alliance#758)

* Use thiserror proc macros for auto From impls

This commit refactors `wasi_common::error::Error` by using `#[from]`
proc macro to autoderive `From` for wrapped errors.

* Back port changes to snapshot0

* Auto impl Display for WasiError

* Fix stack overflow when auto generating Display for WasiError
@kubkon kubkon deleted the error_refactor branch January 16, 2020 09:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wasi:impl Issues pertaining to WASI implementation in Wasmtime
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants