Skip to content

Commit a916f06

Browse files
committed
fix: Handle try expression as pass-through instead of negation
The `try` operator in Zig unwraps an error union. When passed to create_unary_op, it was falling through to the default case which returned UnaryOp::Minus, causing values to be negated. Now `try` is handled specially to just return the operand unchanged, which is correct behavior for the simple case where we're not implementing full error handling semantics. Test: `try riskyOp()` now returns 77 instead of -77
1 parent 8a9f0f8 commit a916f06

1 file changed

Lines changed: 7 additions & 0 deletions

File tree

crates/zyn_peg/src/runtime.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,6 +1100,13 @@ impl AstHostFunctions for TypedAstBuilder {
11001100

11011101
let operand_expr = self.get_expr(operand).unwrap_or_else(|| self.inner.int_literal(0, span));
11021102

1103+
// Handle special cases that aren't true unary ops
1104+
match op.to_lowercase().as_str() {
1105+
// `try` unwraps error union - for now just pass through the value
1106+
"try" => return operand,
1107+
_ => {}
1108+
}
1109+
11031110
let unary_op = Self::string_to_unary_op(op);
11041111
let result_type = Type::Primitive(PrimitiveType::I32);
11051112

0 commit comments

Comments
 (0)