Skip to content

Commit 9891ea7

Browse files
committed
Implement traits for parser error structs
Implement `Debug`, `Display` and `Error` for `FatalError` and `ExplicitBug`
1 parent af1c39c commit 9891ea7

File tree

1 file changed

+29
-8
lines changed

1 file changed

+29
-8
lines changed

src/libsyntax/diagnostic.rs

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,14 @@ pub use self::RenderSpan::*;
1313
pub use self::ColorConfig::*;
1414
use self::Destination::*;
1515

16-
use codemap::{COMMAND_LINE_SP, COMMAND_LINE_EXPN, Pos, Span};
17-
use codemap;
16+
use codemap::{self, COMMAND_LINE_SP, COMMAND_LINE_EXPN, Pos, Span};
1817
use diagnostics;
1918

2019
use std::cell::{RefCell, Cell};
21-
use std::cmp;
22-
use std::fmt;
20+
use std::{cmp, error, fmt};
2321
use std::io::prelude::*;
2422
use std::io;
25-
use term::WriterWrapper;
26-
use term;
23+
use term::{self, WriterWrapper};
2724
use libc;
2825

2926
/// maximum number of lines we will print for each error; arbitrary.
@@ -83,15 +80,39 @@ pub trait Emitter {
8380
/// Used as a return value to signify a fatal error occurred. (It is also
8481
/// used as the argument to panic at the moment, but that will eventually
8582
/// not be true.)
86-
#[derive(Copy, Clone)]
83+
#[derive(Copy, Clone, Debug)]
8784
#[must_use]
8885
pub struct FatalError;
8986

87+
impl fmt::Display for FatalError {
88+
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
89+
write!(f, "parser fatal error")
90+
}
91+
}
92+
93+
impl error::Error for FatalError {
94+
fn description(&self) -> &str {
95+
"The parser has encountered a fatal error"
96+
}
97+
}
98+
9099
/// Signifies that the compiler died with an explicit call to `.bug`
91100
/// or `.span_bug` rather than a failed assertion, etc.
92-
#[derive(Copy, Clone)]
101+
#[derive(Copy, Clone, Debug)]
93102
pub struct ExplicitBug;
94103

104+
impl fmt::Display for ExplicitBug {
105+
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
106+
write!(f, "parser internal bug")
107+
}
108+
}
109+
110+
impl error::Error for ExplicitBug {
111+
fn description(&self) -> &str {
112+
"The parser has encountered an internal bug"
113+
}
114+
}
115+
95116
/// A span-handler is like a handler but also
96117
/// accepts span information for source-location
97118
/// reporting.

0 commit comments

Comments
 (0)