Skip to content

Tests: error decls coverage (§10) #498

Closed

Description

@ehartford

Summary

Add tests for the user-visible presentation surface promised by §10.8 error declarations. The compiler already supports error declarations and error ... from conversion well enough for control-flow tests, but the campaign found Debug/Display behavior unpinned and the Error trait surface absent or incomplete. This issue should become the coverage and verification task for generated error formatting traits, while implementation of missing trait machinery remains with the error-trait issue.

Spec

§10.8 Error Declarations:

error ParseError =
    UnexpectedChar(pos: usize, got: u8)
    UnexpectedEof
    InvalidNumber(pos: usize)

Normative requirement: error declarations automatically implement Error, Debug, and Display (docs/with-specification.md:4397-4407). §10.9 then says error AppError from IoError, ParseError, DbError generates wrapper variants and From implementations (docs/with-specification.md:4408-4416); those wrapper variants should also participate in the same debug/display/error presentation story.

Current state

  • test/behavior/behav_error_decl.w:5-34 covers basic error declarations, generated wrapper variants from error ... from, and ? conversion into the wrapper error. It does not print or format the errors.
  • test/behavior/behav_error_handling.w:6-70 covers matching and ordinary Result flows for error enums. It also does not test Debug, Display, or an Error trait bound.
  • lib/std/traits.w:21-26 declares Debug and Display traits for formatting, with primitive Debug impls beginning at lib/std/traits.w:120.
  • lib/std/result.w:21 defines ContextError[E], and docs/with-specification.md:4331-4342 expects ContextError[E] to implement Error when E: Error; this depends on a coherent Error trait surface.
  • src/MirLower.w:2050-2098 lowers f-string formatting to fmt_to_str, fmt_debug_str, fmt_debug, and fmt_spec intrinsics.
  • src/CodegenDispatch.w:2346-2459 implements debug formatting in codegen. src/CodegenDispatch.w:2359 notes that enums use compiler-generated variant formatting for now, which is likely the current path for error debug output.
  • out/req-campaign/g05.tsv records 10.8.1.1|partial|gap|NEW:error-trait|NEW:tests-error-decls|Debug/Display work, untested; Error absent. That means this issue is test coverage plus verification, not the full trait-design implementation if Error is still missing.

Implementation plan

  1. Add or extend a behavior test, preferably test/behavior/behav_error_decl_formatting.w, focused on error presentation.
  2. Define an error with both payload and payload-less variants, for example error ParseError = Bad(pos: usize) | Eof, and a wrapper error AppError from ParseError.
  3. Exercise debug formatting with f"{err:?}" for payload and payload-less variants. Assert stable strings that include the error type/variant and payload values. If the current compiler-generated enum formatter uses a different but intentional spelling, document that spelling in the test comments and make the issue body precise before implementation.
  4. Exercise display formatting with f"{err}" or an explicit Display.to_string call once the Display surface is implemented for errors. If Display is not implemented yet, keep this test in the issue plan and link the failing expectation to the error-trait implementation issue rather than silently weakening §10.8.
  5. Add a generic helper requiring the eventual Error trait, such as fn accepts_error[E: Error](e: E) -> str, once Error exists in lib/std/traits.w or the appropriate std module. The helper should compile for generated error types and wrapper errors.
  6. Keep test/behavior/behav_error_decl.w as the conversion/control-flow test; do not overload it with formatting assertions unless that makes the suite simpler.

Test plan

  • test/behavior/behav_error_decl_formatting.w with //! expect-stdout: ok:
    • f"{ParseError.Bad(9):?}" or equivalent debug path returns the chosen stable debug spelling.
    • f"{ParseError.Eof:?}" covers payload-less variants.
    • f"{AppError.Parse(ParseError.Bad(9)):?}" covers wrapper variants generated by error ... from.
    • Display formatting for the same values returns the user-facing spelling required by the final Error/Display design.
    • A generic E: Error helper accepts the generated error type and wrapper type after the Error trait exists.
  • If Display or Error is not implemented at the time this issue is picked up, add compile-error tests that capture the current missing trait only in the implementation issue for NEW:error-trait; this coverage issue should close only when the §10.8 contract passes.

Acceptance criteria

  • Error declarations have behavior coverage for generated Debug formatting
  • Error declarations have behavior coverage for generated Display formatting
  • Generated wrapper errors from error ... from are covered
  • Generated error types satisfy the standard Error trait once that trait exists
  • Tests do not paper over a missing Error trait by checking only enum pattern matching
  • with build && with build :fixpoint && with build :test pass

Dependencies

Depends on the error-trait implementation issue referenced in out/req-campaign/g05.tsv as NEW:error-trait if the Error trait is still absent when work starts. Overlaps #434, which owns the implementation side of error declarations and conversions.

Traceability

From the requirements campaign (docs/requirements.md, spec v7.1). Affected requirement IDs: 10.8.1.1.

Activity

  1. ehartford commented on Jul 3, 2026

    @ehartford
    ContributorAuthor

    Closing — error enum Debug ({:?}) and Display ({}) formatting for payload and payload-less variants pinned by behav_error_decl_formatting (bfe7865); the Error/Debug/Display trait surface already covered by spec_ss10_8_error_trait, issue65_*. §10.

  2. added a commit that references this issue on Jul 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions