Repository navigation
C# -> VB: Truncate towards zero when casting a fractional number to an integer - #1330
Merged
GrahamTheCoder merged 2 commits intoNov 24, 2026
Conversation
The constant path returned a bare literal, so e.g. `object o = (long)2.5;` became `Dim o As Object = 2`, boxing an Integer instead of a Long. Route the folded constant through the same cast as the non-constant path; the simplifier still drops the cast where it's redundant (e.g. Const initializers). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019FEr8GNqY4EeHaJnrFj4NL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Builds on #1329 (fixes #1021).
Problem
C# truncates float/double/decimal → integral casts. VB's
CIntetc. round to the nearest integer instead. For the full description, see #1329.Review of that PR found a bug in its constant path: a folded cast was emitted as a bare literal, so
object o = (long)2.5;becameDim o As Object = 2. That boxes anInteger, not aLong. The same applies wherever the type matters, such as overload resolution.Solution
CLng(2)). The simplifier still removes the cast where it is redundant, soConst TruncatedConstant As Integer = 0is unchanged.TruncateFractionalSource, which runs once before the destination-type switch.(int)double?,(int?)d) and enum destinations still round.boxedConstantcase added toCastFractionalToInteger_TruncatedConstantAndBoxedValueAsync; it failed before the follow-up)Testing
Process
Generated by Claude Code