1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
use std::{error, fmt, result};
use codespan_reporting::diagnostic::Diagnostic;
use arret_syntax::span::{FileId, Span};
use crate::mir::inliner::ApplyCookie;
use crate::reporting::{new_primary_label, LocTrace};
#[derive(Debug, PartialEq)]
pub struct Panic {
loc_trace: LocTrace,
message: String,
}
impl Panic {
pub fn new(span: Span, message: String) -> Panic {
Panic {
loc_trace: span.into(),
message,
}
}
}
#[derive(Debug, PartialEq)]
pub enum Error {
Panic(Panic),
AbortRecursion(ApplyCookie),
Diverged,
}
pub type Result<T> = result::Result<T, Error>;
impl Error {
pub fn with_macro_invocation_span(self, span: Span) -> Error {
match self {
Error::Panic(Panic { loc_trace, message }) => Error::Panic(Panic {
loc_trace: loc_trace.with_macro_invocation(span),
message,
}),
other => other,
}
}
}
impl From<Error> for Diagnostic<FileId> {
fn from(error: Error) -> Self {
if let Error::Panic(panic) = error {
let diagnostic = Diagnostic::error()
.with_message(panic.message)
.with_labels(vec![new_primary_label(
panic.loc_trace.origin(),
"panicked here",
)]);
return panic.loc_trace.label_macro_invocation(diagnostic);
}
panic!(
"attempted to convert an internal {:?} flow control error to a diagnostic",
error
);
}
}
impl From<Error> for Vec<Diagnostic<FileId>> {
fn from(error: Error) -> Self {
vec![error.into()]
}
}
impl error::Error for Panic {}
impl fmt::Display for Panic {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.message)
}
}