From 7de42d6f68631534da36fd2547bdfdaa6e73be17 Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Mon, 15 Mar 2021 22:28:52 -0400 Subject: [PATCH] Don't panic too fast on failed analysis --- bin/convert.rs | 41 ++++++++++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/bin/convert.rs b/bin/convert.rs index 2e687219c3..121c2a3717 100644 --- a/bin/convert.rs +++ b/bin/convert.rs @@ -20,18 +20,22 @@ trait PrettyResult { fn unwrap_pretty(self) -> Self::Target; } +fn print_err(error: impl Error) { + println!("{}:", error); + let mut e = error.source(); + while let Some(source) = e { + println!("\t{}", source); + e = source.source(); + } +} + impl PrettyResult for Result { type Target = T; fn unwrap_pretty(self) -> T { match self { Result::Ok(value) => value, Result::Err(error) => { - println!("{}:", error); - let mut e = error.source(); - while let Some(source) = e { - println!("\t{}", source); - e = source.source(); - } + print_err(error); std::process::exit(1); } } @@ -166,6 +170,16 @@ fn main() { } }; + // validate the IR + #[allow(unused_variables)] + let analysis = match naga::proc::Validator::new().validate(&module) { + Ok(analysis) => Some(analysis), + Err(error) => { + print_err(error); + None + } + }; + let output_path = match output_path { Some(ref string) => string, None => { @@ -174,12 +188,6 @@ fn main() { } }; - // validate the IR - #[allow(unused_variables)] - let analysis = naga::proc::Validator::new() - .validate(&module) - .unwrap_pretty(); - match Path::new(output_path) .extension() .expect("Output has no extension?") @@ -189,14 +197,16 @@ fn main() { #[cfg(feature = "msl-out")] "metal" => { use naga::back::msl; - let (msl, _) = msl::write_string(&module, &analysis, ¶ms.msl).unwrap_pretty(); + let (msl, _) = + msl::write_string(&module, analysis.as_ref().unwrap(), ¶ms.msl).unwrap_pretty(); fs::write(output_path, msl).unwrap(); } #[cfg(feature = "spv-out")] "spv" => { use naga::back::spv; - let spv = spv::write_vec(&module, &analysis, ¶ms.spv).unwrap_pretty(); + let spv = + spv::write_vec(&module, analysis.as_ref().unwrap(), ¶ms.spv).unwrap_pretty(); let bytes = spv .iter() .fold(Vec::with_capacity(spv.len() * 4), |mut v, w| { @@ -225,7 +235,8 @@ fn main() { .unwrap(); let mut writer = - glsl::Writer::new(file, &module, &analysis, ¶ms.glsl).unwrap_pretty(); + glsl::Writer::new(file, &module, analysis.as_ref().unwrap(), ¶ms.glsl) + .unwrap_pretty(); writer .write()