From c96e792358eb1600ca4d1d37e4ae9543c53d2c3c Mon Sep 17 00:00:00 2001 From: Erich Gubler Date: Wed, 26 Apr 2023 15:02:35 -0400 Subject: [PATCH 1/6] =?UTF-8?q?refactor(diag):=20ensure=20consistent=20'?= =?UTF-8?q?=E2=80=A6'=20style=20for=20`ValidationError::EntryPoint`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/valid/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/valid/mod.rs b/src/valid/mod.rs index 2c83e1a8d7..0b68c24c67 100644 --- a/src/valid/mod.rs +++ b/src/valid/mod.rs @@ -211,7 +211,7 @@ pub enum ValidationError { name: String, source: FunctionError, }, - #[error("Entry point {name} at {stage:?} is invalid")] + #[error("Entry point '{name}' at {stage:?} is invalid")] EntryPoint { stage: crate::ShaderStage, name: String, From e9ccfc9d181086f4e6c7acf2d1c952cc1de4c4e6 Mon Sep 17 00:00:00 2001 From: Erich Gubler Date: Wed, 26 Apr 2023 15:33:30 -0400 Subject: [PATCH 2/6] refactor(diag)!: remove space from `print_err` between `:` and `\n` --- cli/src/bin/naga.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/src/bin/naga.rs b/cli/src/bin/naga.rs index b121dd52db..b9d8d98096 100644 --- a/cli/src/bin/naga.rs +++ b/cli/src/bin/naga.rs @@ -166,7 +166,7 @@ fn print_err(error: &dyn Error) { let mut e = error.source(); if e.is_some() { - eprintln!(": "); + eprintln!(":"); } else { eprintln!(); } From 48f77ffff675d76cb69278144a4683aaad880b8f Mon Sep 17 00:00:00 2001 From: Erich Gubler Date: Wed, 26 Apr 2023 15:37:54 -0400 Subject: [PATCH 3/6] refactor(diag)!: use 8 spaces manually instead of tab for `print_err` --- cli/src/bin/naga.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/src/bin/naga.rs b/cli/src/bin/naga.rs index b9d8d98096..dd05f049ae 100644 --- a/cli/src/bin/naga.rs +++ b/cli/src/bin/naga.rs @@ -172,7 +172,7 @@ fn print_err(error: &dyn Error) { } while let Some(source) = e { - eprintln!("\t{source}"); + eprintln!(" {source}"); e = source.source(); } } From 4bf80ab21d0e7e8e96d2e952444319b7dcac8c11 Mon Sep 17 00:00:00 2001 From: Erich Gubler Date: Wed, 26 Apr 2023 15:40:36 -0400 Subject: [PATCH 4/6] refactor(cli): add explicit writer arg. for `print_err` --- cli/src/bin/naga.rs | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/cli/src/bin/naga.rs b/cli/src/bin/naga.rs index dd05f049ae..e3b6663e71 100644 --- a/cli/src/bin/naga.rs +++ b/cli/src/bin/naga.rs @@ -1,7 +1,13 @@ #![allow(clippy::manual_strip)] #[allow(unused_imports)] use std::fs; -use std::{error::Error, fmt, io::Read, path::Path, str::FromStr}; +use std::{ + error::Error, + fmt, + io::{stderr, Read, Write}, + path::Path, + str::FromStr, +}; /// Translate shaders to different formats. #[derive(argh::FromArgs, Debug, Clone)] @@ -161,18 +167,18 @@ trait PrettyResult { fn unwrap_pretty(self) -> Self::Target; } -fn print_err(error: &dyn Error) { - eprint!("{error}"); +fn print_err(out: &mut dyn Write, error: &dyn Error) { + write!(out, "{error}").unwrap(); let mut e = error.source(); if e.is_some() { - eprintln!(":"); + writeln!(out, ":").unwrap(); } else { - eprintln!(); + writeln!(out).unwrap(); } while let Some(source) = e { - eprintln!(" {source}"); + writeln!(out, " {source}").unwrap(); e = source.source(); } } @@ -183,7 +189,7 @@ impl PrettyResult for Result { match self { Result::Ok(value) => value, Result::Err(error) => { - print_err(&error); + print_err(&mut stderr().lock(), &error); std::process::exit(1); } } @@ -192,7 +198,7 @@ impl PrettyResult for Result { fn main() { if let Err(e) = run() { - print_err(e.as_ref()); + print_err(&mut stderr().lock(), e.as_ref()); std::process::exit(1); } } @@ -340,7 +346,7 @@ fn run() -> Result<(), Box> { let filename = input_path.file_name().and_then(std::ffi::OsStr::to_str); emit_annotated_error(&error, filename.unwrap_or("input"), &input); } - print_err(&error); + print_err(&mut stderr().lock(), &error); None } }; @@ -362,8 +368,6 @@ fn run() -> Result<(), Box> { .ok_or(CliError("Output filename not valid unicode"))? { "txt" => { - use std::io::Write; - let mut file = fs::File::create(output_path)?; writeln!(file, "{module:#?}")?; if let Some(ref info) = info { From cf371ed548f07b604e7017db865c1aeaf34855ba Mon Sep 17 00:00:00 2001 From: Erich Gubler Date: Wed, 26 Apr 2023 15:41:53 -0400 Subject: [PATCH 5/6] refactor(cli): generate `Diagnostic::message` from `print_err` inside `emit_annotated_error` --- cli/src/bin/naga.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/cli/src/bin/naga.rs b/cli/src/bin/naga.rs index e3b6663e71..e64e3c0673 100644 --- a/cli/src/bin/naga.rs +++ b/cli/src/bin/naga.rs @@ -345,8 +345,9 @@ fn run() -> Result<(), Box> { if let Some(input) = input_text { let filename = input_path.file_name().and_then(std::ffi::OsStr::to_str); emit_annotated_error(&error, filename.unwrap_or("input"), &input); + } else { + print_err(&mut stderr().lock(), &error); } - print_err(&mut stderr().lock(), &error); None } }; @@ -557,7 +558,14 @@ pub fn emit_annotated_error(ann_err: &WithSpan, filename: &str, sou let config = codespan_reporting::term::Config::default(); let writer = StandardStream::stderr(ColorChoice::Auto); - let diagnostic = Diagnostic::error().with_labels( + let mut msg_buf = Vec::new(); + print_err(&mut msg_buf, ann_err); + let msg = String::from_utf8(msg_buf) + .unwrap() + // NOTE: `replace` is intended to correct the alignment of multi-line messages against the + // `error: ` prefix, which is 7 characters. + .replace("\n", "\n "); + let diagnostic = Diagnostic::error().with_message(msg).with_labels( ann_err .spans() .map(|(span, desc)| { From 9ab3994a19472da4e155b5f7cb9fb5e3d4ce3920 Mon Sep 17 00:00:00 2001 From: Erich Gubler Date: Wed, 26 Apr 2023 16:37:59 -0400 Subject: [PATCH 6/6] fixup! refactor(cli): generate `Diagnostic::message` from `print_err` inside `emit_annotated_error` --- cli/src/bin/naga.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/src/bin/naga.rs b/cli/src/bin/naga.rs index e64e3c0673..e146c60ed7 100644 --- a/cli/src/bin/naga.rs +++ b/cli/src/bin/naga.rs @@ -564,7 +564,7 @@ pub fn emit_annotated_error(ann_err: &WithSpan, filename: &str, sou .unwrap() // NOTE: `replace` is intended to correct the alignment of multi-line messages against the // `error: ` prefix, which is 7 characters. - .replace("\n", "\n "); + .replace('\n', "\n "); let diagnostic = Diagnostic::error().with_message(msg).with_labels( ann_err .spans()