#[non_exhaustive]pub enum ExpandError {
NoMatch {
span: Span,
},
Ambiguous {
rule: usize,
},
RepetitionMismatch {
rule: usize,
},
RecursionLimit {
limit: u32,
},
Budget(Limit),
ContextOverflow,
}Expand description
Why Expander::expand could not expand an
invocation.
A failed expansion leaves the expander exactly as it was: no contexts are
minted and no expansion is recorded. The enum is #[non_exhaustive].
§Examples
use macro_lang::{Context, ExpandError, Expander, Macro, Pattern, Rule};
use intern_lang::Interner;
use token_lang::Span;
use macro_lang::Tree;
let mut names = Interner::new();
// A macro that accepts exactly `a`.
let mac = Macro::new(names.intern("only_a"), vec![Rule {
pattern: vec![Pattern::Token('a')],
template: vec![],
}])?;
let mut expander = Expander::new();
let input = [Tree::token('b', Span::new(9, 10))];
let err = expander.expand(&mac, &input, Span::new(0, 11), Context::ROOT).unwrap_err();
// The error points at the token no rule could get past.
assert_eq!(err, ExpandError::NoMatch { span: Span::new(9, 10) });Variants (Non-exhaustive)§
This enum is marked as non-exhaustive
NoMatch
No rule matched the invocation.
span locates the furthest point any rule reached before failing: the
token no rule could accept, or an empty span at the end of the input if
every rule wanted more. Report it as “no rules expected this token”.
Ambiguous
A rule matched the invocation in more than one way, so the captures are
ambiguous — for example $($a:tt)* $($b:tt)* cannot tell where a
ends. Rules are tried in order, and an ambiguous rule stops the search
rather than falling through. Make the rule’s repetitions unambiguous,
typically with a separator or a literal token between them.
RepetitionMismatch
A template repetition uses metavariables that captured different numbers of repetitions, so they cannot be expanded in lockstep. Match them in the same pattern repetition, or expand them in separate template repetitions.
RecursionLimit
The invocation is nested deeper than the expander’s recursion limit
(Budget::max_depth) — almost always a
macro that expands to an invocation of itself without a base case. Fix
the macro, or raise the limit with
Expander::with_limit.
Budget(Limit)
The expander has used up part of its Budget: it has
already performed max_expansions
expansions, or this one would take the number of tokens written past
max_tokens. Almost always a macro whose
output grows without bound, such as one that doubles its argument on
every round, or one that expands into several calls of itself. Fix the
macro, or raise the budget with
Expander::set_budget. A long-lived host
should call Expander::reset_usage once
per unit of work, or legitimate input eventually exhausts the budget.
Added in 1.1.0.
ContextOverflow
The expander has minted all u32::MAX hygiene contexts it can address.
Contexts are never reclaimed, so this takes billions of expansions;
start a fresh Expander.
Trait Implementations§
Source§impl Clone for ExpandError
impl Clone for ExpandError
impl Copy for ExpandError
Source§impl Debug for ExpandError
impl Debug for ExpandError
Source§impl Display for ExpandError
impl Display for ExpandError
impl Eq for ExpandError
Source§impl Error for ExpandError
impl Error for ExpandError
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()