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. # Enable support for the Souper harvester.
souper-harvest = ["souper-ir", "souper-ir/stringify"] 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"] isle-errors = ["cranelift-isle/fancy-errors"]
# Put ISLE generated files in isle_generated_code/, for easier # 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 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 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. 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 This will give pretty-printed errors with source context.
errors with source context.
Additionally, the `cranelift-codegen-meta` crate will automatically generate Additionally, the `cranelift-codegen-meta` crate will automatically generate
ISLE `extern` declarations and helpers for working with CLIF. The code that does 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, f: &mut std::fmt::Formatter,
diagnostics: Vec<Diagnostic<usize>>, diagnostics: Vec<Diagnostic<usize>>,
) -> std::fmt::Result { ) -> std::fmt::Result {
let line_starts: Vec<Vec<_>> = self let line_ends: Vec<Vec<_>> = self
.file_texts .file_texts
.iter() .iter()
.map(|text| { .map(|text| text.match_indices('\n').map(|(i, _)| i + 1).collect())
let mut end = 0;
text.split_inclusive('\n')
.map(|line| {
let start = end;
end += line.len();
start
})
.collect()
})
.collect(); .collect();
let pos = |file_id: usize, offset| { let pos = |file_id: usize, offset| {
let starts = &line_starts[file_id]; let ends = &line_ends[file_id];
let line = starts.partition_point(|&start| start <= offset); let line0 = ends.partition_point(|&end| end <= offset);
let text = &self.file_texts[file_id]; let text = &self.file_texts[file_id];
let line_range = starts[line - 1]..starts.get(line).copied().unwrap_or(text.len()); let start = line0.checked_sub(1).map_or(0, |prev| ends[prev]);
let col = offset - line_range.start + 1; let end = ends.get(line0).copied().unwrap_or(text.len());
let col = offset - start + 1;
format!( format!(
"{}:{}:{}: {}", "{}:{}:{}: {}",
self.filenames[file_id], line, col, &text[line_range] self.filenames[file_id],
line0 + 1,
col,
&text[start..end]
) )
}; };
for diagnostic in diagnostics { for diagnostic in diagnostics {
@@ -190,6 +185,7 @@ impl Errors {
for note in diagnostic.notes { for note in diagnostic.notes {
writeln!(f, "{}", note)?; writeln!(f, "{}", note)?;
} }
writeln!(f)?;
} }
Ok(()) Ok(())
} }
@@ -219,10 +215,9 @@ impl Span {
Span { Span {
from: pos, from: pos,
// This is a slight hack (we don't actually look at the // This is a slight hack (we don't actually look at the
// file to find line/col of next char); but the span // file to find line/col of next char); but the `to`
// aspect, vs. just the starting point, is only relevant // position only matters for pretty-printed errors and only
// for miette and when miette is enabled we use only the // the offset is used in that case.
// `offset` here to provide its SourceSpans.
to: Pos { to: Pos {
file: pos.file, file: pos.file,
offset: pos.offset + 1, 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 { let arg_tys = match arg_tys {
Ok(a) => a, Ok(a) => a,
Err(_) => { Err(_) => {