Skip to content

Commit ad34387

Browse files
Merge #6640
6640: Implement `Display` for macro expansion errors r=jonas-schievink a=jonas-schievink Co-authored-by: Jonas Schievink <[email protected]>
2 parents db061fb + 2c85db8 commit ad34387

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

crates/mbe/src/lib.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ mod subtree_source;
1212
#[cfg(test)]
1313
mod tests;
1414

15+
use std::fmt;
16+
1517
pub use tt::{Delimiter, Punct};
1618

1719
use crate::{
@@ -42,6 +44,20 @@ impl From<tt::ExpansionError> for ExpandError {
4244
}
4345
}
4446

47+
impl fmt::Display for ExpandError {
48+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
49+
match self {
50+
ExpandError::NoMatchingRule => f.write_str("no rule matches input tokens"),
51+
ExpandError::UnexpectedToken => f.write_str("unexpected token in input"),
52+
ExpandError::BindingError(e) => f.write_str(e),
53+
ExpandError::ConversionError => f.write_str("could not convert tokens"),
54+
ExpandError::InvalidRepeat => f.write_str("invalid repeat expression"),
55+
ExpandError::ProcMacroError(e) => write!(f, "{}", e),
56+
ExpandError::Other(e) => f.write_str(e),
57+
}
58+
}
59+
}
60+
4561
pub use crate::syntax_bridge::{
4662
ast_to_token_tree, parse_to_token_tree, syntax_node_to_token_tree, token_tree_to_syntax_node,
4763
TokenMap,

crates/tt/src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,17 @@ pub enum ExpansionError {
240240
ExpansionError(String),
241241
}
242242

243+
impl fmt::Display for ExpansionError {
244+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
245+
match self {
246+
ExpansionError::IOError(e) => write!(f, "I/O error: {}", e),
247+
ExpansionError::JsonError(e) => write!(f, "JSON decoding error: {}", e),
248+
ExpansionError::Unknown(e) => write!(f, "{}", e),
249+
ExpansionError::ExpansionError(e) => write!(f, "proc macro returned error: {}", e),
250+
}
251+
}
252+
}
253+
243254
pub trait TokenExpander: Debug + Send + Sync + RefUnwindSafe {
244255
fn expand(&self, subtree: &Subtree, attrs: Option<&Subtree>)
245256
-> Result<Subtree, ExpansionError>;

0 commit comments

Comments
 (0)