Skip to content

Typed errors for label conversion, unalias and verify - #18

Merged
leonbohn merged 1 commit into
mainfrom
typed-errors
Nov 6, 2026
Merged

leonbohn merged 1 commit into
mainfrom
typed-errors

Conversation

@leonbohn

@leonbohn leonbohn commented Nov 6, 2026 •

Copy link
Copy Markdown
Owner

Closes the remaining bullet of #6: the label conversion helpers returned Result<_, String>, so callers had to inspect message text. FromHoaError/ValidationError already implement std::error::Error (0.4.0); this finishes the API.

New types (src/error.rs)

pub enum LabelError {
    ApOutOfRange { ap: u16, count: u16 },
    UnresolvedAlias(String),
}

pub enum AliasError { Undefined(String), Cyclic(String) }

pub enum VerifyError {
    DuplicateState(Id),
    StateCountMismatch { declared: usize, actual: usize },
}

pub struct VerifyErrors(Vec<VerifyError>);   // Deref<[VerifyError]>, Display joins with "; "

All implement Display and std::error::Error, so they work as #[source]/#[from] fields downstream. VerifyErrors keeps verify's existing "report everything at once" behaviour; VerifyError is the single violation to match on.

Breaking changes

item before after
AbstractLabelExpression::try_into_bdd Result<Bdd, String> Result<Bdd, LabelError>
AbstractLabelExpression::try_into_prop Result<HoaExpression, String> Result<HoaExpression, LabelError>
LabelExpression::try_into_hoa_expression Result<HoaExpression, String> Result<HoaExpression, LabelError>
HoaRepresentation::unalias Result<(), String> Result<(), AliasError>
HoaRepresentation::verify Result<(), String> Result<(), VerifyErrors>

Messages keep their old text apart from lower-casing and the state-count wording (`States:` says 3 but the body defines 4).

Behaviour fix

unalias built its substitution map by iterating a HashMap, so for a cyclic alias the reported name changed between runs. It now walks alias_iter(), i.e. file order, so Alias: @x @y / Alias: @y !@x | 0 always reports Cyclic("x"). This matters now that the name is part of a typed, matchable error.

Tests

55 (was 53): unalias_rejects_undefined_and_cyclic_aliases and verify_reports_duplicate_states now assert exact variants, new verify_reports_state_count_mismatch, label::tests::unresolved_alias_is_an_error, and error::tests::conversion_errors_are_std_errors which boxes each type into Box<dyn std::error::Error>.

./scripts/check passes: fmt, clippy -D warnings, rustdoc -D warnings, benches compile, all tests. No benchmark-relevant code paths changed (error construction only), so no before/after table.

Closes #6.

The remaining bullet of #6: the conversion helpers returned
`Result<_, String>`, so callers had to match on message text.

- `LabelError` for `try_into_bdd`, `try_into_prop` and
  `try_into_hoa_expression`: `ApOutOfRange { ap, count }` and
  `UnresolvedAlias(name)`.
- `AliasError` for `unalias`: `Undefined(name)` and `Cyclic(name)`.
- `VerifyError` per violation and `VerifyErrors` (derefs to
  `[VerifyError]`) for `verify`.

All of them implement `std::error::Error`. `unalias` now walks the
aliases in file order, so the alias named in a cycle no longer depends
on `HashMap` iteration order.

Messages are unchanged apart from lower-casing and the state-count
wording.
@leonbohn
leonbohn merged commit 076131c into main Nov 6, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Use typed errors: implement std::error::Error for FromHoaError, replace Result<_, String>

1 participant