cranelift-isle: Minor error-handling cleanups (#5338)

- Remove remaining references to Miette
- Borrow implementation of `line_starts` from codespan-reporting
- Clean up a use of `Result` that no longer conflicts with a local
  definition
- When printing plain errors, add a blank line between errors for
  readability
This commit is contained in:
Jamey Sharp
2022-11-28 19:07:05 -08:00
committed by GitHub
parent a5a0645aff
commit ff5abfd993
4 changed files with 18 additions and 24 deletions

View File

@@ -99,7 +99,7 @@ incremental-cache = [
# Enable support for the Souper harvester.
souper-harvest = ["souper-ir", "souper-ir/stringify"]
# Provide fancy Miette-produced errors for ISLE.
# Report any ISLE errors in pretty-printed style.
isle-errors = ["cranelift-isle/fancy-errors"]
# Put ISLE generated files in isle_generated_code/, for easier

View File

@@ -40,8 +40,7 @@ could cause significant confusion.
If there are any errors during ISLE compilation (e.g., a type mismatch), you
will see a basic error message with a file, line number, and one-line error. To
see a more detailed output with context, `--features isle-errors` can be used.
This will leverage the `miette` error-reporting library to give pretty-printed
errors with source context.
This will give pretty-printed errors with source context.
Additionally, the `cranelift-codegen-meta` crate will automatically generate
ISLE `extern` declarations and helpers for working with CLIF. The code that does

View File

@@ -157,29 +157,24 @@ impl Errors {
f: &mut std::fmt::Formatter,
diagnostics: Vec<Diagnostic<usize>>,
) -> std::fmt::Result {
let line_starts: Vec<Vec<_>> = self
let line_ends: Vec<Vec<_>> = self
.file_texts
.iter()
.map(|text| {
let mut end = 0;
text.split_inclusive('\n')
.map(|line| {
let start = end;
end += line.len();
start
})
.collect()
})
.map(|text| text.match_indices('\n').map(|(i, _)| i + 1).collect())
.collect();
let pos = |file_id: usize, offset| {
let starts = &line_starts[file_id];
let line = starts.partition_point(|&start| start <= offset);
let ends = &line_ends[file_id];
let line0 = ends.partition_point(|&end| end <= offset);
let text = &self.file_texts[file_id];
let line_range = starts[line - 1]..starts.get(line).copied().unwrap_or(text.len());
let col = offset - line_range.start + 1;
let start = line0.checked_sub(1).map_or(0, |prev| ends[prev]);
let end = ends.get(line0).copied().unwrap_or(text.len());
let col = offset - start + 1;
format!(
"{}:{}:{}: {}",
self.filenames[file_id], line, col, &text[line_range]
self.filenames[file_id],
line0 + 1,
col,
&text[start..end]
)
};
for diagnostic in diagnostics {
@@ -190,6 +185,7 @@ impl Errors {
for note in diagnostic.notes {
writeln!(f, "{}", note)?;
}
writeln!(f)?;
}
Ok(())
}
@@ -219,10 +215,9 @@ impl Span {
Span {
from: pos,
// This is a slight hack (we don't actually look at the
// file to find line/col of next char); but the span
// aspect, vs. just the starting point, is only relevant
// for miette and when miette is enabled we use only the
// `offset` here to provide its SourceSpans.
// file to find line/col of next char); but the `to`
// position only matters for pretty-printed errors and only
// the offset is used in that case.
to: Pos {
file: pos.file,
offset: pos.offset + 1,

View File

@@ -1176,7 +1176,7 @@ impl TermEnv {
()
})
})
.collect::<std::result::Result<Vec<_>, _>>();
.collect::<Result<Vec<_>, _>>();
let arg_tys = match arg_tys {
Ok(a) => a,
Err(_) => {