diff --git a/build.rs b/build.rs index 8bf3d2cd..b247d874 100644 --- a/build.rs +++ b/build.rs @@ -69,6 +69,10 @@ fn main() { println!("cargo:rustc-cfg=no_bind_by_move_pattern_guard"); } + if version.minor >= 44 { + println!("cargo:rustc-cfg=lexerror_display"); + } + if version.minor >= 45 { println!("cargo:rustc-cfg=hygiene"); } diff --git a/src/fallback.rs b/src/fallback.rs index 949b9a5f..8900c5ff 100644 --- a/src/fallback.rs +++ b/src/fallback.rs @@ -148,6 +148,12 @@ impl FromStr for TokenStream { } } +impl Display for LexError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.write_str("cannot parse string into token stream") + } +} + impl Display for TokenStream { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let mut joint = false; diff --git a/src/lib.rs b/src/lib.rs index 09c72802..2d23e5fc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -105,6 +105,7 @@ mod imp; use crate::marker::Marker; use std::cmp::Ordering; +use std::error::Error; use std::fmt::{self, Debug, Display}; use std::hash::{Hash, Hasher}; use std::iter::FromIterator; @@ -254,6 +255,14 @@ impl Debug for LexError { } } +impl Display for LexError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + Display::fmt(&self.inner, f) + } +} + +impl Error for LexError {} + /// The source file of a given `Span`. /// /// This type is semver exempt and not exposed by default. diff --git a/src/wrapper.rs b/src/wrapper.rs index 8ab224d7..3df044af 100644 --- a/src/wrapper.rs +++ b/src/wrapper.rs @@ -264,6 +264,18 @@ impl Debug for LexError { } } +impl Display for LexError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self { + #[cfg(lexerror_display)] + LexError::Compiler(e) => Display::fmt(e, f), + #[cfg(not(lexerror_display))] + LexError::Compiler(_e) => Display::fmt(&fallback::LexError, f), + LexError::Fallback(e) => Display::fmt(e, f), + } + } +} + #[derive(Clone)] pub(crate) enum TokenTreeIter { Compiler(proc_macro::token_stream::IntoIter),