Skip to content

fix: a conversion that can change the value is refused where the lambda is written - #130

Merged
CaffeinatedCoder merged 1 commit into
mainfrom
fix/narrowing-cast-refused
Nov 14, 2026
Merged

CaffeinatedCoder merged 1 commit into
mainfrom
fix/narrowing-cast-refused

Conversation

@CaffeinatedCoder

Copy link
Copy Markdown
Owner

Closes #103

Problem

LambdaTranslator took every Convert node off and wrote what was under it. That is right for the conversions the compiler adds and wrong for one that changes the value:

pg.Exclusion(pg.Overlaps(x => x.Period), where: x => (int)x.Amount == 2, name: "ex_cast")   // Amount is a decimal

was stored as {Amount} = 2 and reached the database as WHERE ("Amount" = 2). For Amount = 2.5 the lambda is true and the predicate false: the row is outside the constraint, and nothing was refused. The same on the value's side: x.Rank == (int)limit with a captured limit of 2.5 was written as {Rank} = 2.5.

Change

  • LambdaTranslator.SilentConversions is a table like StringMethods and LiteralTypes: the pairs of different types whose conversion is left out of a template. IsSilentConversion(from, to) is the whole rule, decided from the two types alone.
  • One pass over the lambda, ahead of the dispatch (ConversionCheck), refuses every other conversion of what the lambda reads from the entity, with NotSupportedException and a message that names the conversion and says what to write. The dispatch takes a conversion off in eight places (a comparison's operand, a null test, a concatenation, …); a check in each would be one forgotten. The conversion lambda of a ?? (x.Score ?? x.Price) is held to the same table, and so is ConvertChecked.
  • A conversion of a value alone is C#'s to do, and its result is written.
  • An enum in a comparison keeps its own refusal ("compares an enum"); outside one, (int)x.Status is refused as a conversion.
  • Nothing renders a cast (The lambda factories leave the kernel: Predicate.Of and KeyTarget.Of take no provider and spell SQL as Npgsql does #112, Declarations bound to the model at finalization and stored as segments, not as a string to scan #114).

Left out of the template, as before

to Nullable<T>, and back ((int)x.Score is x.Score.Value) structural
to a type the source already is: itself, object, an interface, Enum structural
sbyte → short, int, long; short → int, long; int → long table
byte → short, ushort, int, uint, long, ulong; ushort → int, uint, long, ulong; uint → long, ulong table
char → int table; not a widening, see below

Refused: everything else. To an integer from decimal, double, float; to a narrower integer or one of the other sign (long → int, int → uint, ulong → long); any type to float or double, including int → double and float → double; any type to decimal; char to anything but int; an enum to its number outside a comparison; a downcast; a user-defined conversion.

What the compiler emits for comparisons the tests already held (each is a row of A_conversion_that_keeps_every_value_is_left_out_as_before, which states the conversions in its tree and the template it had): a short, byte, sbyte or ushort against anything is promoted to int; int against long to long; uint against int both to long; byte against uint to uint; a column against a nullable one is lifted; + on a string and a number boxes the number to object; a char is compared as an int. char → int is the one case that stays accepted and is no plain widening: the tree of x.Grade == 'A' is the tree of (int)x.Grade == 65, and the comparison writes the constant back as the char.

The parity sweep crosses the table. A row's conversions are read from the tree the compiler built (ParityCatalogue.ConversionsIn), AcceptedConstructs lists the table, and the catalogue adds, for every pair, a column of the source type against a column and a sample of the target type, bare and with the cast written out. ParityRow got an sbyte, a ushort and a uint column for it.

Tests

LambdaTranslatorTests, five new tests and the table's list in The_accepted_constructs_are_in_the_tables; PgExclusionDeclarationTests.A_cast_that_can_change_the_value_fails_at_the_declaration. Locally: build without a warning; kernel suite 522 passed with PostgreSQL 18 (TranslatorParityLiveTests included); PostgreSQL package 193 passed; SchemaKit.ApiProbes 125 passed.

Each rule broken in a scratch copy, and the tests that fail (non-integration runs):

Broken Fails
the pass in front of the dispatch removed A_cast_that_can_change_the_value_is_refused_where_it_is_written, Every_other_conversion_between_numbers_is_refused, A_pair_of_numeric_types_is_silent_exactly_when_the_table_has_it, and the PostgreSQL package's A_cast_that_can_change_the_value_fails_at_the_declaration. A_conversion_that_keeps_every_value_is_left_out_as_before passes, which is what "as before" rests on
short → int taken out of the table A_conversion_that_keeps_every_value_is_left_out_as_before, The_accepted_constructs_are_in_the_tables, the sweep's "Every construct the translator accepts is compared with EF Core at least once"
int → double added to the table Every_other_conversion_between_numbers_is_refused, The_accepted_constructs_are_in_the_tables, the sweep's "Every typed filter and expression renders as EF Core's query…"
a captured value's conversion taken off again, not evaluated A_conversion_of_a_captured_value_is_done_before_the_value_is_written
a converted captured char not put back as a char the same test, and the sweep's "Every typed filter…"
ConvertChecked not held to the table Every_other_conversion_between_numbers_is_refused
the enum's own refusal taken out of the pass What_a_predicate_cannot_say_is_refused, Every_other_conversion_between_numbers_is_refused
Nullable<T> not looked through A_conversion_that_keeps_every_value_is_left_out_as_before, A_pair_of_numeric_types_is_silent_exactly_when_the_table_has_it, the sweep's "No waiver outlives the disagreement it waives"
the ?? conversion lambda not checked A_cast_that_can_change_the_value_is_refused_where_it_is_written
the sweep not reading conversions from the trees "Every construct the translator accepts is compared with EF Core at least once"

What the issue did not expect

Decisions in this PR that are not confirmed

  1. Refused, not rendered. No cast is written as SQL: that needs the provider's spelling (The lambda factories leave the kernel: Predicate.Of and KeyTarget.Of take no provider and spell SQL as Npgsql does #112, Declarations bound to the model at finalization and stored as segments, not as a string to scan #114).
  2. int → decimal, int → double and float → double are refused, although each keeps every value and the compiler adds them unasked: x.Rank > 2.5m, x.Rank > 2.5, x.Ratio > 0.5 and a comparison of an int column with a decimal one were accepted until now and are refused. The reason is the task's rule (integer widenings only) and that how a database types the two sides is its own rule (SQL Server reads 0.5 as a decimal and converts it to real). The message says to write the value in the property's type (0.5f). Each is one row of the table if the maintainer wants it back.
  3. char → int stays silent everywhere, not only as a comparison's operand, so E(x => (int)x.Grade) is the column.
  4. Nullable<T> → T stays silent, as .Value is in ValueMembers.
  5. The check is a pass of its own in front of the dispatch, so a lambda with a refused conversion and another untranslatable construct reports the conversion.
  6. The waiver block above, and the three columns added to ParityRow.

🤖 Generated with Claude Code

…da is written

Every Convert node was left out of the template, so (int)x.Amount == 2 on a
decimal was stored as {Amount} = 2 and the constraint covered other rows than
the lambda names (#103).

The conversions left out are now a table, SilentConversions: the widenings
between integer types, and char to int for the comparison of a char. A
Nullable<T> on either side is looked through, and a conversion to a type the
source already is needs no entry. One pass over the lambda refuses every other
conversion of what it reads from the entity, the conversion lambda of a ??
included, ahead of the dispatch. A conversion of a captured value is done by
C# and its result written: (int)limit was written as 2.5.

The parity sweep reads the conversions from the trees the compiler built and
crosses the table with a column and a sample of the target type. EF Core writes
a widening between two store types as a cast, which the translator does not
render yet: 432 rows waived as answering alike, checked live (#125).

Closes #103

Co-Authored-By: Claude Fable 5.2 <[email protected]>
@CaffeinatedCoder CaffeinatedCoder added this to the 11.0.0 milestone Nov 14, 2026
@CaffeinatedCoder CaffeinatedCoder added bug Something isn't working silent Answers wrongly without throwing; the fix ships as a minor, never a patch package/core Affects CodoMetis.EFCore.SchemaKit (the kernel) severity/medium Inconsistency or failure in a narrow scenario area/expression-indexes Expression, typed lambda and JSON member indexes labels Nov 14, 2026
@CaffeinatedCoder
CaffeinatedCoder merged commit 2720f7c into main Nov 14, 2026
7 checks passed
@CaffeinatedCoder
CaffeinatedCoder deleted the fix/narrowing-cast-refused branch November 14, 2026 09:55
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/medium Inconsistency or failure in a narrow scenario silent Answers wrongly without throwing; the fix ships as a minor, never a patch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A narrowing cast in a lambda is left out of the template: (int)x.Amount == 2 is stored as {Amount} = 2

1 participant