Skip to content

Conversation

@onbjerg
Copy link
Contributor

@onbjerg onbjerg commented Dec 17, 2025

Add implicit type coercion for integer literals (IntLiteral) to typed integers (uint/int). The coercion rules are:

  • IntLiteral -> uint: Allowed if the literal is non-negative and fits in the target size (size.bits() <= target.bits())

  • IntLiteral -> int: Allowed with strict inequality (size.bits() < target.bits()) for non-negative values due to TypeSize rounding, and non-strict for negative values

TypeSize stores ceil(bit_len/8), so int_literal[1] covers 0-255. This means we can't distinguish edge cases like 127 (fits in int8) from 128 (doesn't fit), so we conservatively require int16+ for int_literal[1].

Note: Negative literal support requires additional work in the type checker to propagate negativity through unary negation. This is provided in a follow up.

Supercedes #564 and closes #627

Stack:

Blocked by #650

Add implicit type coercion for integer literals (IntLiteral) to typed
integers (uint/int). The coercion rules are:

- IntLiteral -> uint: Allowed if the literal is non-negative and fits
  in the target size (size.bits() <= target.bits())

- IntLiteral -> int: Allowed with strict inequality (size.bits() <
  target.bits()) for non-negative values due to TypeSize rounding,
  and non-strict for negative values

TypeSize stores ceil(bit_len/8), so int_literal[1] covers 0-255. This
means we can't distinguish edge cases like 127 (fits in int8) from 128
(doesn't fit), so we conservatively require int16+ for int_literal[1].

Note: Negative literal support requires additional work in the type
checker to propagate negativity through unary negation.
@onbjerg onbjerg added C-enhancement Category: an issue proposing an enhancement or a PR with one A-sema Area: semantic analysis labels Dec 17, 2025
@onbjerg onbjerg marked this pull request as ready for review December 17, 2025 00:58
@onbjerg onbjerg requested a review from DaniPopes as a code owner December 17, 2025 00:58
@onbjerg onbjerg added the S-blocked Status: this cannot more forward until something else changes label Dec 17, 2025
@onbjerg onbjerg marked this pull request as draft December 17, 2025 01:10
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 S-blocked Status: this cannot more forward until something else changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Integer literal implicit conversion

2 participants