You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
use thiserror::Error;#[derive(Debug,Clone,Error,PartialEq)]pubenumError{First,Second,}
produces this compiler error
error[E0277]: `Error` doesn't implement `std::fmt::Display`
--> src/lib.rs:3:24
|
3 | #[derive(Debug, Clone, Error, PartialEq)]
| ^^^^^ `Error` cannot be formatted with the default formatter
|
= help: the trait `std::fmt::Display` is not implemented for `Error`
= note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
= note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)
error: aborting due to previous error
For more information about this error, try `rustc --explain E0277`.
error: could not compile `terr`.
To learn more, run the command again with --verbose.
It is not immediately obvious that you have to add attributes to the enum variants.
use thiserror::Error;#[derive(Debug,Clone,Error,PartialEq)]pubenumError{#[error("first")]First,#[error("second")]Second,}
Suggestion
A better error message would be something like this
error[E0277]: `Error` doesn't implement `std::fmt::Display`
--> src/lib.rs:4:10
|
4 | pub enum Error {
| ^^^^^ `Error` cannot be formatted with the default formatter
|
= help: the trait `std::fmt::Display` is not implemented for `Error`
= note: use the `#[error("...")]` attribute on variants to derive `std::fmt::Display`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0277`.
error: could not compile `terr`.
To learn more, run the command again with --verbose.
I know that the suggested error message is not possible (or is it?), because proc-macros operate on tokens and can therefore not know if a type implements std::fmt::Display.
It is still possible to improve the error message (a little) by returning this in the else clause:
Some(quote_spanned!{
ty.span() => struct _AssertDisplay where #ty: ::core::fmt::Display;
})
which should result in this compiler error
error[E0277]: `Error` doesn't implement `std::fmt::Display`
--> src/lib.rs:4:10
|
4 | pub enum Error {
| ^^^^^ `Error` cannot be formatted with the default formatter
|
= help: the trait `std::fmt::Display` is not implemented for `Error`
= note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
error: aborting due to previous error
For more information about this error, try `rustc --explain E0277`.
error: could not compile `terr`.
To learn more, run the command again with --verbose.
The text was updated successfully, but these errors were encountered:
Problem
This code
produces this compiler error
It is not immediately obvious that you have to add attributes to the enum variants.
Suggestion
A better error message would be something like this
Implementation
The error happens, because this if clause
thiserror/impl/src/expand.rs
Line 226 in f866fa9
in
thiserror/impl/src/expand.rs
Line 124 in f866fa9
returns
None
, ifinput.has_display()
isfalse
(to support structs, which implementstd::fmt::Display
manually?).thiserror/impl/src/expand.rs
Line 266 in f866fa9
I know that the suggested error message is not possible (or is it?), because
proc-macro
s operate on tokens and can therefore not know if a type implementsstd::fmt::Display
.It is still possible to improve the error message (a little) by returning this in the else clause:
thiserror/impl/src/expand.rs
Line 266 in f866fa9
which should result in this compiler error
The text was updated successfully, but these errors were encountered: