cranelift-isle: Rewrite error reporting (#5318)

There were several issues with ISLE's existing error reporting
implementation.

- When using Miette for more readable error reports, it would panic if
  errors were reported from multiple files in the same run.
- Miette is pretty heavy-weight for what we're doing, with a lot of
  dependencies.
- The `Error::Errors` enum variant led to normalization steps in many
  places, to avoid using that variant to represent a single error.

This commit:
- replaces Miette with codespan-reporting
- gets rid of a bunch of cargo-vet exemptions
- replaces the `Error::Errors` variant with a new `Errors` type
- removes source info from `Error` variants so they're easy to construct
- adds source info only when formatting `Errors`
- formats `Errors` with a custom `Debug` impl
- shares common code between ISLE's callers, islec and cranelift-codegen
- includes a source snippet even with fancy-errors disabled

I tried to make this a series of smaller commits but I couldn't find any
good split points; everything was too entangled with everything else.
This commit is contained in:
Jamey Sharp
2022-11-23 14:20:48 -08:00
committed by GitHub
parent 48ee42efc2
commit 044b57f334
18 changed files with 342 additions and 548 deletions

View File

@@ -7,7 +7,6 @@ license = "Apache-2.0 WITH LLVM-exception"
publish = false
[dependencies]
cranelift-isle = { version = "*", path = "../isle/", features = ["miette-errors", "logging"] }
cranelift-isle = { version = "*", path = "../isle/", features = ["fancy-errors", "logging"] }
env_logger = { workspace = true }
miette = { version = "5.1.0", features = ["fancy"] }
clap = { workspace = true }

View File

@@ -1,6 +1,6 @@
use clap::Parser;
use cranelift_isle::{compile, lexer, parser};
use miette::{Context, IntoDiagnostic, Result};
use cranelift_isle::compile;
use cranelift_isle::error::Errors;
use std::{
default::Default,
fs,
@@ -20,33 +20,19 @@ struct Opts {
inputs: Vec<PathBuf>,
}
fn main() -> Result<()> {
fn main() -> Result<(), Errors> {
let _ = env_logger::try_init();
let _ = miette::set_hook(Box::new(|_| {
Box::new(
miette::MietteHandlerOpts::new()
// `miette` mistakenly uses braille-optimized output for emacs's
// `M-x shell`.
.force_graphical(true)
.build(),
)
}));
let opts = Opts::parse();
let lexer = lexer::Lexer::from_files(opts.inputs)?;
let defs = parser::parse(lexer)?;
let code = compile::compile(&defs, &Default::default())?;
let code = compile::from_files(opts.inputs, &Default::default())?;
let stdout = io::stdout();
let (mut output, output_name): (Box<dyn Write>, _) = match &opts.output {
Some(f) => {
let output = Box::new(
fs::File::create(f)
.into_diagnostic()
.with_context(|| format!("failed to create '{}'", f.display()))?,
);
let output =
Box::new(fs::File::create(f).map_err(|e| {
Errors::from_io(e, format!("failed to create '{}'", f.display()))
})?);
(output, f.display().to_string())
}
None => {
@@ -57,8 +43,7 @@ fn main() -> Result<()> {
output
.write_all(code.as_bytes())
.into_diagnostic()
.with_context(|| format!("failed to write to '{}'", output_name))?;
.map_err(|e| Errors::from_io(e, format!("failed to write to '{}'", output_name)))?;
Ok(())
}