Repository navigation
Description
Summary
Fix unary - on user-defined types so it dispatches to neg like §11.7 specifies. Binary user-defined operators already have method resolution; unary negate currently passes sema too broadly and then codegen treats it as a primitive negate.
Spec
§11.7 maps unary - to method neg, and shows -a -> Neg.neg(&a). The optional prelude trait is Neg[Output] with fn neg(self: Self) -> Output, though the operator hook is fixed method-name dispatch on the concrete type.
Current state
Binary operator method dispatch is implemented in Sema.check_binary_operator_method at src/SemaCheck.w:3767-3800, with callers for comparison, @, and arithmetic at src/SemaCheck.w:4109-4170. Unary negate does not use that path: Sema.check_unary returns the operand type for UOP_NEGATE and only rejects unsigned integer operands (src/SemaCheck.w:4235-4251). MirBuilder.lower_expr lowers every non-? unary operator through lower_un_op at src/MirLower.w:7785-7790, so codegen sees a primitive unary op even when sema accepted a user type.
Implementation plan
- Add a unary operator resolution helper in
SemaCheck.w, analogous tocheck_binary_operator_method, forUOP_NEGATEand methodneg. - For numeric signed operands, keep the existing primitive path. For unsigned operands, keep
cannot negate an unsigned value. For non-numeric operands, look for an applicablenegmethod on the operand type. - Record resolved unary operator method calls in a new sidecar map, similar to
operator_method_calls, keyed by unary AST node. - Update
MirLower.lower_expr/lower_un_opto lower a resolved unary method call instead ofRvalueKindprimitive negate. - Update codegen only if MIR needs a new call shape; prefer reusing normal method-call lowering so
negparticipates in auto-referencing and effects consistently. - Add optional
Negtrait declaration through Prelude operator traits Add/Sub/Mul/Div/MatMul/Neg (§11.7) #469, but do not require explicit trait bounds for unbounded generics.
Test plan
Add test/spec/spec_ss11_7_unary_neg_dispatch.w:
- user type with
fn neg(self: &Self) -> Self let y = -xcalls the method- chained
-(-x)works - generic function using
-xchecks at instantiation
Add compile-error cases:
test/compile_errors/err_operator_neg_no_impl.wtest/compile_errors/err_operator_neg_bad_return_or_signature.w- keep/extend unsigned primitive negative diagnostic coverage.
Acceptance criteria
- User-defined unary
-resolves tonegand lowers as a method call. - Primitive signed numeric negate remains unchanged.
- Unsigned negate remains a compile-time error.
- Missing or inapplicable
negmethods fail in sema, not codegen. with build && with build :fixpoint && with build :testpass.
Dependencies
#469 should add the optional Neg prelude trait, but fixed method-name dispatch can be fixed independently.
Traceability
From the requirements campaign (docs/requirements.md, spec v7.1). Affected requirement IDs: 11.7.1.30.
Activity
ehartford commentedon Jun 13, 2026
Implemented in b503570. Unary
-now keeps primitive signed numeric negation, preserves the unsigned diagnostic, resolves user-definednegmethods in sema, and lowers resolved unary operator calls through normal method-call MIR. Added positive dispatch coverage, generic instantiation coverage, and missing/bad-signature/unsigned compile-error coverage. Verification passed: build, fixpoint, full test suite, and test-green.