Skip to content

Bug: user-defined unary neg fails in codegen (§11.7) #464

Closed

Description

@ehartford

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

  1. Add a unary operator resolution helper in SemaCheck.w, analogous to check_binary_operator_method, for UOP_NEGATE and method neg.
  2. 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 applicable neg method on the operand type.
  3. Record resolved unary operator method calls in a new sidecar map, similar to operator_method_calls, keyed by unary AST node.
  4. Update MirLower.lower_expr/lower_un_op to lower a resolved unary method call instead of RvalueKind primitive negate.
  5. Update codegen only if MIR needs a new call shape; prefer reusing normal method-call lowering so neg participates in auto-referencing and effects consistently.
  6. Add optional Neg trait 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 = -x calls the method
  • chained -(-x) works
  • generic function using -x checks at instantiation

Add compile-error cases:

  • test/compile_errors/err_operator_neg_no_impl.w
  • test/compile_errors/err_operator_neg_bad_return_or_signature.w
  • keep/extend unsigned primitive negative diagnostic coverage.

Acceptance criteria

  • User-defined unary - resolves to neg and lowers as a method call.
  • Primitive signed numeric negate remains unchanged.
  • Unsigned negate remains a compile-time error.
  • Missing or inapplicable neg methods fail in sema, not codegen.
  • with build && with build :fixpoint && with build :test pass.

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

  1. ehartford commented on Jun 13, 2026

    @ehartford
    ContributorAuthor

    Implemented in b503570. Unary - now keeps primitive signed numeric negation, preserves the unsigned diagnostic, resolves user-defined neg methods 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.

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