Skip to main content

Pass

Trait Pass 

Source
pub trait Pass<T> {
    // Required methods
    fn name(&self) -> &'static str;
    fn run(&mut self, unit: &mut T) -> Result<Outcome, PassError>;
}
Expand description

A single transform or analysis over a unit of type T — the plugin seam.

Implement this trait to define a pass. The unit T is whatever the pass rewrites: an intermediate representation, a single function, a module, an abstract syntax tree, or a struct bundling an IR with the diagnostics and analysis state the pass needs. The manager is generic over T and never inspects it — a pass is the only thing trusted to read or mutate the unit.

A pass is registered with PassManager::add and run in registration order. It must be 'static: it may own state across runs, but it may not borrow from outside the manager.

§Contract

  • name returns a stable, static identifier used in the Report and in error context. It must not change between runs of the same pass.
  • run transforms unit in place and reports an Outcome: Changed only when the unit was actually modified, so a fixpoint loop can terminate. A pass that cannot proceed returns a PassError instead of panicking.

§Examples

A pass that drops zero entries from a list, reporting whether it removed any:

use pass_lang::{Outcome, Pass, PassError};

struct DropZeros;

impl Pass<Vec<i64>> for DropZeros {
    fn name(&self) -> &'static str {
        "drop-zeros"
    }

    fn run(&mut self, unit: &mut Vec<i64>) -> Result<Outcome, PassError> {
        let before = unit.len();
        unit.retain(|&x| x != 0);
        Ok(Outcome::from_changed(unit.len() != before))
    }
}

let mut unit = vec![0, 1, 0, 2];
assert_eq!(DropZeros.run(&mut unit).unwrap(), Outcome::Changed);
assert_eq!(unit, vec![1, 2]);
// Running again changes nothing.
assert_eq!(DropZeros.run(&mut unit).unwrap(), Outcome::Unchanged);

Required Methods§

Source

fn name(&self) -> &'static str

A stable, static name for this pass, used in reports and error context.

Source

fn run(&mut self, unit: &mut T) -> Result<Outcome, PassError>

Run the pass over unit, transforming it in place.

Return Outcome::Changed if and only if the unit was modified, so the manager’s fixpoint loop can tell when the pipeline has settled. Return a PassError — never a panic — if the pass cannot proceed; the manager stops the pipeline and reports which pass failed.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§