Skip to content

Conversation

@rtfeldman
Copy link
Contributor

When using !3 (the bang/not operator on a numeric literal), the compiler would compile successfully but panic at runtime with "unreachable code". The issue was that the type checker allowed a flex var to have both from_numeral and not constraints without detecting the incompatibility. At runtime, the numeric literal would default to Dec, but Dec has no not method, causing the panic.

The fix adds validation to detect when a flex var with from_numeral constraint (indicating a numeric type) also has constraints that are incompatible with numeric types (specifically the not method which only exists on Bool). When this incompatibility is detected, a proper MISSING METHOD type error is reported at compile time.

  • Add isMethodIncompatibleWithNumeric to identify Bool-only methods
  • Add checkFlexVarConstraintCompatibility to validate constraint compatibility
  • Call the new check after type-checking expressions
  • Add snapshot test to verify the fix

Fixes #8885

Co-authored by Claude Opus 4.5

When using `!3` (the bang/not operator on a numeric literal), the compiler
would compile successfully but panic at runtime with "unreachable code".

The issue was that the type checker allowed a flex var to have both
`from_numeral` and `not` constraints without detecting the incompatibility.
At runtime, the numeric literal would default to Dec, but Dec has no `not`
method, causing the panic.

The fix adds validation in checkFlexVarConstraintCompatibility to detect
when a flex var with `from_numeral` constraint (indicating a numeric type)
also has constraints that are incompatible with numeric types (specifically
the `not` method which only exists on Bool). When this incompatibility is
detected, a proper MISSING METHOD type error is reported at compile time.

- Add isMethodIncompatibleWithNumeric to identify Bool-only methods
- Add checkFlexVarConstraintCompatibility to validate constraint compatibility
- Call the new check after type-checking expressions in checkExprRepl
- Also check in checkDeferredStaticDispatchConstraints for the flex case
- Add snapshot test bang_on_numeric_literal.md to verify the fix

Fixes #8885

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
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.

panic: reached unreachable code

2 participants