Skip to content

fix: a conversion in a checked context is translated as it is unchecked - #149

Merged
CaffeinatedCoder merged 1 commit into
mainfrom
fix/convert-checked
Nov 16, 2026
Merged

CaffeinatedCoder merged 1 commit into
mainfrom
fix/convert-checked

Conversation

@CaffeinatedCoder

Copy link
Copy Markdown
Owner

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 as ConvertChecked, the ones it adds unasked included. LambdaTranslator matched ExpressionType.Convert in nine places, so in such a project

where: x => x.Small == 5      // Small is a short

was 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 a ConvertChecked. 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 (and Unconverted for 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.
  • No rule changes. Of what a lambda reads from the entity only a conversion of SilentConversions gets 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 with checkOverflow: true and 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 of A_conversion_that_keeps_every_value_is_left_out_as_before inside checked(…). 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 a ConvertChecked and 1,100 of those a template, so the second compilation cannot quietly be an unchecked one.
  • With the translator of main under these tests: both unit tests fail, and the sweep test reports the 1,242 rows.
  • On this branch: the kernel's 661 tests pass, the live sweep on PostgreSQL 18 included.

🤖 Generated with Claude Code

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]>
@CaffeinatedCoder CaffeinatedCoder added this to the 11.0.0 milestone Nov 16, 2026
@CaffeinatedCoder CaffeinatedCoder added bug Something isn't working package/core Affects CodoMetis.EFCore.SchemaKit (the kernel) severity/low Cosmetic, message or documentation-level difference area/expression-indexes Expression, typed lambda and JSON member indexes labels Nov 16, 2026
@CaffeinatedCoder
CaffeinatedCoder merged commit 2da51b9 into main Nov 16, 2026
10 checks passed
@CaffeinatedCoder
CaffeinatedCoder deleted the fix/convert-checked branch November 16, 2026 15:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/expression-indexes Expression, typed lambda and JSON member indexes bug Something isn't working package/core Affects CodoMetis.EFCore.SchemaKit (the kernel) severity/low Cosmetic, message or documentation-level difference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

In a checked context every promoted comparison is refused: the compiler writes ConvertChecked, and the translator knows Convert

1 participant