From c0c526951c87c79e0db5e6e32b95bbe0a01347d3 Mon Sep 17 00:00:00 2001 From: Kinsey Favre Date: Thu, 6 Feb 2020 12:25:18 -0600 Subject: [PATCH 1/3] Add Display and Error impls for proc_macro::LexError This should allow LexError to play much nicer with the `?` operator. --- src/libproc_macro/lib.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/libproc_macro/lib.rs b/src/libproc_macro/lib.rs index c51db695a5b15..5ae7640d4b873 100644 --- a/src/libproc_macro/lib.rs +++ b/src/libproc_macro/lib.rs @@ -41,7 +41,7 @@ pub use diagnostic::{Diagnostic, Level, MultiSpan}; use std::ops::{Bound, RangeBounds}; use std::path::PathBuf; use std::str::FromStr; -use std::{fmt, iter, mem}; +use std::{error, fmt, iter, mem}; /// The main type provided by this crate, representing an abstract stream of /// tokens, or, more specifically, a sequence of token trees. @@ -66,6 +66,16 @@ pub struct LexError { _inner: (), } +#[stable(feature = "proc_macro_lib", since = "1.15.0")] +impl fmt::Display for LexError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str("lex error") + } +} + +#[stable(feature = "proc_macro_lib", since = "1.15.0")] +impl error::Error for LexError {} + #[stable(feature = "proc_macro_lib", since = "1.15.0")] impl !Send for LexError {} #[stable(feature = "proc_macro_lib", since = "1.15.0")] From f2b22a136c9129a2f74cd897d8f1ea8ff5386e99 Mon Sep 17 00:00:00 2001 From: Kinsey Favre Date: Sat, 8 Feb 2020 09:55:49 -0600 Subject: [PATCH 2/3] Correct stability attribute for new LexError impls --- src/libproc_macro/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libproc_macro/lib.rs b/src/libproc_macro/lib.rs index 5ae7640d4b873..13bce45243fe1 100644 --- a/src/libproc_macro/lib.rs +++ b/src/libproc_macro/lib.rs @@ -66,14 +66,14 @@ pub struct LexError { _inner: (), } -#[stable(feature = "proc_macro_lib", since = "1.15.0")] +#[stable(feature = "proc_macro_lexerror_impls", since = "1.44.0")] impl fmt::Display for LexError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.write_str("lex error") } } -#[stable(feature = "proc_macro_lib", since = "1.15.0")] +#[stable(feature = "proc_macro_lexerror_impls", since = "1.44.0")] impl error::Error for LexError {} #[stable(feature = "proc_macro_lib", since = "1.15.0")] From 5099ab6e6b80c02f2b16e99c169d318cfd9252c1 Mon Sep 17 00:00:00 2001 From: Kinsey Favre Date: Sat, 8 Feb 2020 10:51:54 -0600 Subject: [PATCH 3/3] Give LexError more descriptive Display impl --- src/libproc_macro/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libproc_macro/lib.rs b/src/libproc_macro/lib.rs index 13bce45243fe1..59ce14c97c0e6 100644 --- a/src/libproc_macro/lib.rs +++ b/src/libproc_macro/lib.rs @@ -69,7 +69,7 @@ pub struct LexError { #[stable(feature = "proc_macro_lexerror_impls", since = "1.44.0")] impl fmt::Display for LexError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.write_str("lex error") + f.write_str("cannot parse string into token stream") } }