Skip to content

Implement ? operator for error propagation#3

Merged
ochsec merged 1 commit intomainfrom
feature/question-operator
Mar 2, 2026
Merged

Implement ? operator for error propagation#3
ochsec merged 1 commit intomainfrom
feature/question-operator

Conversation

@ochsec
Copy link
Owner

@ochsec ochsec commented Mar 2, 2026

Summary

  • Add postfix ? operator that unwraps Result/Option values, returning early on Err/None — mirrors Rust's ? operator
  • Handles interaction with pipe operator (x |> F? |> G?) by unwrapping Propagate during pipe desugaring, then re-wrapping
  • Binds tighter than binary ops and pipes (1 + x? parses as 1 + (x?))

Changes

  • Lexer: Question token for ?
  • AST: Propagate { expr } expression variant
  • Parser: Postfix ? handling after primary expressions, function calls, and binary op operands; pipe desugaring updated to handle Propagate-wrapped RHS
  • Codegen: Propagate { expr } transpiles to (expr)? in Rust
  • Type inference: Verifies ? is applied to Option<T> or Result<T, E>, returns unwrapped T
  • 14 new tests covering lexer, parser, and codegen
  • Example file compiler/examples/error_propagation.w

Test plan

  • cargo build passes
  • cargo test — all 198 tests pass (14 new + 184 existing)
  • Verified ? works with: bare identifiers, function calls, pipe chains, binary ops, Some/Ok expressions

🤖 Generated with Claude Code

@ochsec ochsec force-pushed the feature/question-operator branch from 2a514a8 to 5776195 Compare March 2, 2026 00:47
@ochsec ochsec changed the title Add ? operator and lambda shorthand syntax (x -> body) Implement ? operator for error propagation Mar 2, 2026
Add postfix ? operator that unwraps Result/Option values, returning
early on Err/None — mirrors Rust's ? operator. Handles interaction
with pipe operator (x |> F? |> G?) and binds tighter than binary ops.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@ochsec ochsec force-pushed the feature/question-operator branch from 5776195 to b027ad4 Compare March 2, 2026 00:50
@ochsec ochsec merged commit 1a19dd7 into main Mar 2, 2026
1 check 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.

1 participant