Repository navigation
fix: a conversion in a checked context is translated as it is unchecked - #149
Merged
Merged
Conversation
In a checked context, which a project sets for all of its code with CheckForOverflowUnderflow, the compiler writes the numeric conversions of an expression tree as ConvertChecked, the ones it adds unasked included. The translator matched Convert, so x.Small == 5 on a short column was refused there, and with it every comparison the compiler promotes. Every place that takes a conversion off now asks one helper, which knows both nodes. Of what a lambda reads from the entity only a conversion that keeps every value gets that far, and that one cannot overflow, so the two nodes mean the same there. The parity sweep compiles its rows a second time with overflow checks and holds each template, and each refusal, to the unchecked one: 1,242 of 3,499 rows differed before. Closes #126 Co-Authored-By: Claude Fable 5.2 <[email protected]>
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.
Closes #126
Problem
In a checked context, which a project sets for all of its code with
<CheckForOverflowUnderflow>true</CheckForOverflowUnderflow>, the compiler writes the numeric conversions of an expression tree asConvertChecked, the ones it adds unasked included.LambdaTranslatormatchedExpressionType.Convertin nine places, so in such a projectwas refused with "The expression 'ConvertChecked(x.Small, Int32)' has no translation in a declaration", and with it every comparison the compiler promotes. Nothing reached a database wrongly; the lambda could not be written.
Measured on
main: the parity sweep's 3,499 rows compiled a second time with overflow checks. 1,307 trees hold aConvertChecked. 1,242 rows are translated differently from their unchecked compilation: 1,206 are refused where the unchecked row has a template, and 36 are refused either way with a message that names the conversion and not the construct under it.Change
LambdaTranslator: one helper,Converted(andUnconvertedfor the loops), knows both nodes, and every place that takes a conversion off asks it:VisitPredicate,Visit,RefuseEnum,IsConvertedChar,IsNullValue,ComparesNullable,NullCheck,CanBeNull,ConcatOperand, and the check in front of the dispatch, which knew both already.SilentConversionsgets past that check, and a widening cannot overflow, so the two nodes mean the same wherever one is taken off. A conversion that can change the value is refused as before (A narrowing cast in a lambda is left out of the template: (int)x.Amount == 2 is stored as {Amount} = 2 #103). A conversion of a captured value is still C#'s to do, checked or not.ParitySweep.InACheckedContext(): the sweep's rows compiled withcheckOverflow: trueand translated, each beside its unchecked twin.CLAUDE.md: the rule, in the translator's section.Not in it: #125 (a widening EF Core writes as a cast) and #127 (a float literal), which wait for the provider's spelling.
Tests
LambdaTranslatorTests.In_a_checked_context_the_same_conversions_are_left_out: the rows ofA_conversion_that_keeps_every_value_is_left_out_as_beforeinsidechecked(…). Each states the conversions the compiler wrote, with their node, and has the template it has unchecked.In_a_checked_context_every_rule_reads_through_the_conversion: null compensation, a negation pushed down, a null test, a concatenation with a nullable operand, the enum's own refusal and A narrowing cast in a lambda is left out of the template: (int)x.Amount == 2 is stored as {Amount} = 2 #103's refusal, each equal to its unchecked form.TranslatorParitySweepTests.A_checked_context_changes_no_row: no row of the sweep differs, and at least 1,200 rows have aConvertCheckedand 1,100 of those a template, so the second compilation cannot quietly be an unchecked one.mainunder these tests: both unit tests fail, and the sweep test reports the 1,242 rows.🤖 Generated with Claude Code