Skip to content

Conversation

@onbjerg
Copy link
Contributor

@onbjerg onbjerg commented Dec 17, 2025

When both operands of a binary operation are IntLiteral, evaluate the expression at compile time to preserve literal type semantics. This allows expressions like -(1 + 2) to work correctly, since the result remains an IntLiteral that can be negated.

Changes:

  • Extended IntScalar with explicit sign tracking and signed arithmetic
  • Added try_eval_int_literal_binop to evaluate literal binary expressions
  • Updated eval_array_len to reject negative array lengths

On top of #648

Stack:

…servation

When both operands of a binary operation are IntLiteral, evaluate the
expression at compile time to preserve literal type semantics. This
allows expressions like `-(1 + 2)` to work correctly, since the result
remains an IntLiteral that can be negated.

Changes:
- Extended IntScalar with explicit sign tracking and signed arithmetic
- Added try_eval_int_literal_binop to evaluate literal binary expressions
- Updated eval_array_len to reject negative array lengths
@onbjerg onbjerg requested a review from DaniPopes as a code owner December 17, 2025 00:57
@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
BitXor => Self::new(self.data ^ r.data),
Shr => Self::new(self.data.wrapping_shr(r.data.try_into().unwrap_or(usize::MAX))),
Shl => Self::new(self.data.wrapping_shl(r.data.try_into().unwrap_or(usize::MAX))),
Sar => Self::new(self.data.arithmetic_shr(r.data.try_into().unwrap_or(usize::MAX))),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is >>> which only works on fixed types, we should rename this to sometihng so we dont forget its useless

BitOr => Self::new(self.data | r.data),
BitAnd => Self::new(self.data & r.data),
BitXor => Self::new(self.data ^ r.data),
Shr => Self::new(self.data.wrapping_shr(r.data.try_into().unwrap_or(usize::MAX))),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be arithmetic_shr depending on sign

}

/// Returns whether the value is negative.
pub fn is_negative(&self) -> bool {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should probably be is_signed and be a constant value. then data should be I256 and is_negative will be I256's is_negative (last bit set). this way all ops are easier to keep track of, since they become regular two's complement

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.

3 participants