-
Notifications
You must be signed in to change notification settings - Fork 77
feat(typeck): implement constant folding for integer literal type preservation #649
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: onbjerg/int-literal-negative
Are you sure you want to change the base?
feat(typeck): implement constant folding for integer literal type preservation #649
Conversation
…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
| 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))), |
There was a problem hiding this comment.
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))), |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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
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:
On top of #648
Stack: