Skip to content

Commit e8a0c90

Browse files
authored
Merge pull request #265 from dtolnay/lexerror
Implement Display and Error for LexError
2 parents 5245816 + 0093bbb commit e8a0c90

File tree

4 files changed

+31
-0
lines changed

4 files changed

+31
-0
lines changed

build.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ fn main() {
6969
println!("cargo:rustc-cfg=no_bind_by_move_pattern_guard");
7070
}
7171

72+
if version.minor >= 44 {
73+
println!("cargo:rustc-cfg=lexerror_display");
74+
}
75+
7276
if version.minor >= 45 {
7377
println!("cargo:rustc-cfg=hygiene");
7478
}

src/fallback.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,12 @@ impl FromStr for TokenStream {
148148
}
149149
}
150150

151+
impl Display for LexError {
152+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
153+
f.write_str("cannot parse string into token stream")
154+
}
155+
}
156+
151157
impl Display for TokenStream {
152158
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
153159
let mut joint = false;

src/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ mod imp;
105105

106106
use crate::marker::Marker;
107107
use std::cmp::Ordering;
108+
use std::error::Error;
108109
use std::fmt::{self, Debug, Display};
109110
use std::hash::{Hash, Hasher};
110111
use std::iter::FromIterator;
@@ -254,6 +255,14 @@ impl Debug for LexError {
254255
}
255256
}
256257

258+
impl Display for LexError {
259+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
260+
Display::fmt(&self.inner, f)
261+
}
262+
}
263+
264+
impl Error for LexError {}
265+
257266
/// The source file of a given `Span`.
258267
///
259268
/// This type is semver exempt and not exposed by default.

src/wrapper.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,18 @@ impl Debug for LexError {
264264
}
265265
}
266266

267+
impl Display for LexError {
268+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
269+
match self {
270+
#[cfg(lexerror_display)]
271+
LexError::Compiler(e) => Display::fmt(e, f),
272+
#[cfg(not(lexerror_display))]
273+
LexError::Compiler(_e) => Display::fmt(&fallback::LexError, f),
274+
LexError::Fallback(e) => Display::fmt(e, f),
275+
}
276+
}
277+
}
278+
267279
#[derive(Clone)]
268280
pub(crate) enum TokenTreeIter {
269281
Compiler(proc_macro::token_stream::IntoIter),

0 commit comments

Comments
 (0)