pub struct PassError { /* private fields */ }Expand description
The error a Pass returns when it cannot complete its work.
A pass produces a PassError from inside Pass::run
when it hits a condition it cannot transform past — a construct it does not
support, an invariant the input violates, an arithmetic operation that would
overflow. The error carries a human-readable message
explaining what went wrong; the PassManager then
stamps in the name of the pass that failed before returning
it, so the caller always knows which pass halted the pipeline and why.
The reason is stored as a Cow<'static, str>: a static
message ("unsupported construct") costs no allocation, while a computed one
(format!("overflow folding {a} * {b}")) is owned only when it is built.
§Examples
Failing a pass with a static reason:
use pass_lang::{Outcome, Pass, PassError};
struct RejectEmpty;
impl Pass<Vec<i64>> for RejectEmpty {
fn name(&self) -> &'static str { "reject-empty" }
fn run(&mut self, unit: &mut Vec<i64>) -> Result<Outcome, PassError> {
if unit.is_empty() {
return Err(PassError::new("the unit is empty"));
}
Ok(Outcome::Unchanged)
}
}Failing with a computed reason:
use pass_lang::PassError;
let (a, b) = (i64::MAX, 2);
let err = PassError::new(format!("overflow folding {a} * {b}"));
assert_eq!(err.message(), "overflow folding 9223372036854775807 * 2");Implementations§
Source§impl PassError
impl PassError
Sourcepub fn new(message: impl Into<Cow<'static, str>>) -> Self
pub fn new(message: impl Into<Cow<'static, str>>) -> Self
Create an error describing why a pass could not complete.
Call this from inside Pass::run. The message
accepts a string literal (no allocation) or an owned String (a
computed reason). The failing pass’s name is filled in by the
PassManager — you do not need to repeat it.
§Examples
use pass_lang::PassError;
let from_literal = PassError::new("division by zero");
let from_owned = PassError::new(String::from("division by zero"));
assert_eq!(from_literal, from_owned);Sourcepub fn pass(&self) -> &str
pub fn pass(&self) -> &str
The name of the pass that failed, or "" if the error has not yet been
returned through a PassManager.
§Examples
use pass_lang::PassError;
// Before the manager stamps it in, the pass name is empty.
assert_eq!(PassError::new("boom").pass(), "");Trait Implementations§
impl Eq for PassError
Source§impl Error for PassError
impl Error for PassError
1.30.0 · Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
use the Display impl or to_string()