Skip to content

Conversation

@onbjerg
Copy link
Contributor

@onbjerg onbjerg commented Dec 17, 2025

Implement proper handling of negative integer literals in the type checker. Previously, negative literals like -42 were broken because:

  1. The parser represents them as unary negation applied to a positive literal, so type_of_lit only sees the positive value
  2. The negativity flag in IntLiteral was always false

This commit fixes the issue by:

  • Allowing unary negation on IntLiteral types (they can always be negated since the result is just a negative literal)
  • Propagating the negativity flag when applying unary negation to an IntLiteral, flipping neg from false to true
  • Not propagating the expected type through negation when targeting signed types, to avoid premature type mismatch errors on the inner expression

Also simplifies the coercion rule to use strict inequality for both positive and negative literals, since TypeSize rounding means we can't reliably distinguish edge cases in either direction.

On top of #647

Supercedes #566 (will mark @mablr as a co-author to commend effort) and closes #560

Stack:

Implement proper handling of negative integer literals in the type
checker. Previously, negative literals like -42 were broken because:

1. The parser represents them as unary negation applied to a positive
   literal, so type_of_lit only sees the positive value
2. The negativity flag in IntLiteral was always false

This commit fixes the issue by:

- Allowing unary negation on IntLiteral types (they can always be
  negated since the result is just a negative literal)
- Propagating the negativity flag when applying unary negation to an
  IntLiteral, flipping neg from false to true
- Not propagating the expected type through negation when targeting
  signed types, to avoid premature type mismatch errors on the inner
  expression

Also simplifies the coercion rule to use strict inequality for both
positive and negative literals, since TypeSize rounding means we can't
reliably distinguish edge cases in either direction.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-sema Area: semantic analysis C-enhancement Category: an issue proposing an enhancement or a PR with one

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants