Skip to content

Commit 6626085

Browse files
committed
add miette errors
1 parent c1414e6 commit 6626085

File tree

7 files changed

+96
-137
lines changed

7 files changed

+96
-137
lines changed

Cargo.toml

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,27 @@
11
[package]
22
name = "descend"
3-
version = "0.1.0"
4-
authors = [
5-
"Luca Carlig <[email protected]>",
6-
"Köpcke <[email protected]>",
7-
]
8-
edition = "2021"
9-
10-
# Allow specific lints globally
11-
[package.metadata.clippy]
12-
unused_variables = "allow"
13-
dead_code = "allow"
14-
unused_imports = "allow"
15-
unreachable_code = "allow"
3+
version.workspace = true
4+
authors.workspace = true
5+
edition.workspace = true
166

177
[dependencies]
18-
clap = { version = "4.5", features = ["derive"] }
19-
insta = "1.43.2"
20-
melior = { version = "0.25", features = ["ods-dialects"] }
21-
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
22-
23-
[dependencies.peg]
24-
version = "0.8.0"
25-
26-
[dependencies.annotate-snippets]
27-
version = "0.9.0"
28-
features = ["color"]
29-
30-
[dependencies.descend_derive]
31-
path = "./descend_derive"
32-
33-
[build-dependencies]
8+
clap = { version = "4.5.50", features = ["derive"] }
349
melior = { version = "0.25", features = ["ods-dialects"] }
10+
miette = { version = "7.6.0", features = ["fancy", "syntect-highlighter"] }
11+
peg = "0.8.5"
12+
descend_derive = { path = "./descend_derive" }
3513

3614
[dev-dependencies]
3715
insta = { version = "1.43.2", features = ["glob"] }
3816

3917
[workspace]
4018
members = ["descend_derive"]
19+
resolver = "3"
20+
21+
[workspace.package]
22+
version = "0.1.0"
23+
authors = [
24+
"Luca Carlig <[email protected]>",
25+
"Köpcke <[email protected]>",
26+
]
27+
edition = "2024"

descend_derive/Cargo.toml

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
[package]
22
name = "descend_derive"
3-
version = "0.1.0"
4-
authors = ["Köpcke <[email protected]>"]
5-
edition = "2018"
6-
7-
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
3+
version.workspace = true
4+
authors.workspace = true
5+
edition.workspace = true
86

97
[lib]
108
proc-macro = true
119

1210
[build-dependencies]
1311

1412
[dependencies]
15-
syn = { version = "1.0", features = ["full"] }
16-
quote = "1.0"
17-
proc-macro2 = "1.0"
18-
include_dir = "0.7"
13+
syn = { version = "2.0.108", features = ["full"] }
14+
quote = "1.0.41"
15+
proc-macro2 = "1.0.103"
16+
include_dir = "0.7.4"

descend_derive/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ pub fn span_derive(attr: TokenStream, input: TokenStream) -> TokenStream {
4646
std::mem::swap(&mut tmp, &mut field.attrs);
4747
tmp = tmp
4848
.into_iter()
49-
.filter(|attr| !attr.path.is_ident(IGNORE_ATTR_NAME))
49+
.filter(|attr| !attr.path().is_ident(IGNORE_ATTR_NAME))
5050
.collect::<Vec<_>>();
5151
std::mem::swap(&mut tmp, &mut field.attrs);
5252
}
@@ -64,7 +64,7 @@ pub fn span_derive(attr: TokenStream, input: TokenStream) -> TokenStream {
6464
for mut field in original_fields.named.clone().into_iter() {
6565
let mut ignored = false;
6666
for attr in field.attrs.iter() {
67-
if attr.path.is_ident(IGNORE_ATTR_NAME) {
67+
if attr.path().is_ident(IGNORE_ATTR_NAME) {
6868
ignored = true;
6969
break;
7070
}

src/error.rs

Lines changed: 24 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
use crate::parser::SourceCode;
2-
use annotate_snippets::display_list::{DisplayList, FormatOptions};
3-
use annotate_snippets::snippet::{Annotation, AnnotationType, Slice, Snippet, SourceAnnotation};
2+
use miette::{Diagnostic, LabeledSpan};
43
use std::fmt::Formatter;
54

65
#[must_use]
6+
#[derive(Debug, Diagnostic)]
7+
#[diagnostic(severity(Error), code("file_io_error"))]
78
pub struct FileIOError<'a> {
89
file_path: &'a str,
910
io_error: std::io::Error,
@@ -18,72 +19,45 @@ impl<'a> FileIOError<'a> {
1819
}
1920

2021
pub fn emit(&self) -> ErrorReported {
21-
println!("{}", self.to_string(self.file_path, &self.io_error));
22+
eprintln!("couldn't read {}: {}", self.file_path, self.io_error);
2223
ErrorReported
2324
}
24-
25-
fn to_string(&self, path_to_file: &str, err: &std::io::Error) -> String {
26-
let label = format!("couldn't read {}: {}", path_to_file, err);
27-
let snippet = Snippet {
28-
title: Some(Annotation {
29-
id: None,
30-
label: Some(&label),
31-
annotation_type: AnnotationType::Error,
32-
}),
33-
footer: vec![],
34-
slices: vec![],
35-
opt: default_format(),
36-
};
37-
DisplayList::from(snippet).to_string()
38-
}
3925
}
4026

41-
impl std::fmt::Debug for FileIOError<'_> {
27+
impl<'a> std::fmt::Display for FileIOError<'a> {
4228
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
43-
write!(f, "{}", self.io_error)
29+
write!(f, "couldn't read {}: {}", self.file_path, self.io_error)
4430
}
4531
}
4632

47-
pub fn default_format() -> FormatOptions {
48-
FormatOptions {
49-
color: true,
50-
anonymized_line_numbers: false,
51-
margin: None,
33+
impl<'a> std::error::Error for FileIOError<'a> {
34+
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
35+
Some(&self.io_error)
5236
}
5337
}
5438

5539
pub fn single_line_snippet<'a>(
5640
source: &'a SourceCode<'a>,
57-
label: &'a str,
41+
_label: &'a str,
5842
annotation: &'a str,
5943
line_num: u32,
6044
begin_column: u32,
6145
end_column: u32,
62-
) -> Snippet<'a> {
63-
let slices = if let Some(line) = source.get_line(line_num) {
64-
vec![Slice {
65-
source: line,
66-
line_start: line_num as usize + 1,
67-
origin: source.file_path,
68-
annotations: vec![SourceAnnotation {
69-
range: (begin_column as usize, end_column as usize),
70-
label: annotation,
71-
annotation_type: AnnotationType::Error,
72-
}],
73-
fold: false,
74-
}]
46+
) -> miette::Report {
47+
if let Some(_line) = source.get_line(line_num) {
48+
let start_offset = source.get_offset(line_num, begin_column);
49+
let end_offset = source.get_offset(line_num, end_column);
50+
51+
miette::miette!(
52+
labels = [LabeledSpan::new_with_span(
53+
Some(annotation.to_string()),
54+
start_offset..end_offset
55+
)],
56+
"{}",
57+
annotation
58+
)
7559
} else {
76-
vec![]
77-
};
78-
Snippet {
79-
title: Some(Annotation {
80-
id: None,
81-
label: Some(label),
82-
annotation_type: AnnotationType::Error,
83-
}),
84-
footer: vec![],
85-
slices,
86-
opt: default_format(),
60+
miette::miette!("{}", annotation)
8761
}
8862
}
8963

src/parser/mod.rs

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -290,13 +290,13 @@ fn replace_struct_idents_with_specific_struct_dtys(struct_dtys: &[StructDecl], i
290290
pub mod error {
291291
use crate::error::ErrorReported;
292292
use crate::parser::{Parser, SourceCode};
293-
use annotate_snippets::display_list::DisplayList;
294-
use annotate_snippets::snippet::Snippet;
293+
use miette::Diagnostic;
295294
use peg::error::ParseError as PegError;
296295
use peg::str::LineCol;
297296

298297
#[must_use]
299-
#[derive(Debug)]
298+
#[derive(Debug, Diagnostic)]
299+
#[diagnostic(severity(Error), code("parse_error"))]
300300
pub struct ParseError<'a> {
301301
parser: &'a Parser<'a>,
302302
err: PegError<LineCol>,
@@ -313,25 +313,33 @@ pub mod error {
313313
u32::try_from(self.err.location.line).expect("Source file is unexpectedly large");
314314
let column_num =
315315
u32::try_from(self.err.location.column).expect("Source file is unexpectedly large");
316-
let snippet = single_line_parse_snippet(
316+
let error = single_line_parse_snippet(
317317
self.parser.source,
318318
&label,
319319
line_num,
320320
column_num,
321321
column_num + 1,
322322
);
323-
println!("{}", DisplayList::from(snippet));
323+
eprintln!("{}", error);
324324
ErrorReported
325325
}
326326
}
327327

328+
impl<'a> std::fmt::Display for ParseError<'a> {
329+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
330+
write!(f, "Parse error: expected {}", self.err.expected)
331+
}
332+
}
333+
334+
impl<'a> std::error::Error for ParseError<'a> {}
335+
328336
fn single_line_parse_snippet<'a>(
329337
source: &'a SourceCode<'a>,
330338
label: &'a str,
331339
line_num: u32,
332340
begin_column: u32,
333341
end_column: u32,
334-
) -> Snippet<'a> {
342+
) -> miette::Report {
335343
// 1-based offsets to 0-based offsets
336344
crate::error::single_line_snippet(
337345
source,
@@ -1361,17 +1369,21 @@ mod tests {
13611369
))))),
13621370
"does not recognize type of unique i32 reference in cpu heap with provenance 'a"
13631371
);
1364-
assert_eq!(descend::ty("&b shrd npu.global [f32;N]"), Ok(Ty::new(TyKind::Data(Box::new(DataTy::new(DataTyKind::Ref(
1365-
Box::new(RefDty::new(
1366-
Provenance::Ident(Ident::new("b")),
1367-
Ownership::Shrd,
1368-
Memory::NpuGm,
1369-
DataTy::new(DataTyKind::Array(
1370-
Box::new(DataTy::new(DataTyKind::Scalar(ScalarTy::F32))),
1371-
Nat::Ident(Ident::new("N"))
1372-
))
1373-
)))))),
1374-
)), "does not recognize type of shared [f32] reference in npu global memory with provenance b");
1372+
assert_eq!(
1373+
descend::ty("&b shrd npu.global [f32;N]"),
1374+
Ok(Ty::new(TyKind::Data(Box::new(DataTy::new(
1375+
DataTyKind::Ref(Box::new(RefDty::new(
1376+
Provenance::Ident(Ident::new("b")),
1377+
Ownership::Shrd,
1378+
Memory::NpuGm,
1379+
DataTy::new(DataTyKind::Array(
1380+
Box::new(DataTy::new(DataTyKind::Scalar(ScalarTy::F32))),
1381+
Nat::Ident(Ident::new("N"))
1382+
))
1383+
)))
1384+
))),)),
1385+
"does not recognize type of shared [f32] reference in npu global memory with provenance b"
1386+
);
13751387
}
13761388

13771389
#[test]

src/parser/source.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ impl<'a> SourceCode<'a> {
6363
}
6464
}
6565

66+
/// Get offset corresponding to line and column (both 0-based)
67+
pub fn get_offset(&self, line_num: u32, col_num: u32) -> usize {
68+
let line_offset = self.line_offsets[line_num as usize] as usize;
69+
line_offset + col_num as usize
70+
}
71+
6672
/// Get line and column (both 0-based) corresponding to an offset in the
6773
/// source string
6874
pub fn get_line_col(&self, offset: u32) -> (u32, u32) {

0 commit comments

Comments
 (0)