You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
filter / map(x, f, m) over scalar lists emits Rust that does not compile (&bool where bool is expected)
#110
A rule that runs the single-variable filter macro, or the three-argument map(x, filter, expr), over a list of scalars generates a validator that does not compile. Two examples:
error[E0308]: mismatched types: expected `bool`, found `&bool`
error[E0277]: the trait bound `&i64: CelScalar` is not satisfied
Both macros are lowered to Iterator::filter, and its closure takes &&T. element_binding_expr dereferences the element once, which is correct for all, any and map (they take &T) but not inside filter. The binding is therefore &bool where bool is expected, or &i64, &u64 or &f64 where a CelScalar is expected. This affects bool, int, uint and double elements. Strings, bytes, messages and durations still compile because of auto-deref or blanket impls. !b compiles only by accident, because Not is implemented for &bool.
Downstream, we had to rewrite [a, b, c].filter(x, x).size() <= 1 as (a ? 1 : 0) + (b ? 1 : 0) + (c ? 1 : 0) <= 1 to get a schema to build with v0.10.5.
Change
The two filter closures destructure one reference (|&x| …), so the element binding sees &T, as it does in the other comprehensions. Nothing else in the emitted code changes. Message elements also get |&x|, and that is harmless.
Verification
emit_compiles: 7 new fixtures, for filter over a bool list literal and over bool, int, uint and double lists, and for map(x, f, m) over int and bool lists. Before the fix, 6 fail to type-check; after it, all compile.
Runtime test: a new message, acme.v1.FilteredScalars, with 5 message-level rules covering bool, int64, uint32 and double fields and the filter form of map. filter_over_scalar_lists_validates checks owned messages and views, for valid input and for input where every rule fails. It doesn't compile before the fix and passes after it.
Full suite:cargo test --locked --workspace --all-targets passes, along with cargo fmt --check, buf format and buf lint.
Clippy: passes for the changed code. I didn't run hk check --all --slow because mise isn't installed here. Clippy on rustc 1.99.1 reports two assert!-on-emptiness lints in tests/emit_packaging.rs, and these also appear on unmodified main.
Downstream: I regenerated a real schema with the original filter expression. The v0.10.5 plugin gives E0308. With this branch, the crate compiles and its contract tests pass.
Consumers must regenerate validators with the updated plugin to get the fix. The runtime crate is unchanged.
Disclosure: Claude Opus 5.5 found this bug, wrote the fix and its tests, and drafted this description and Discussion.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
A reference fix with regression tests is in amsokol/protovalidate-buffa@8d08e77, on the branch
fix/cel-filter-scalar-lists. It is a 4-line change toemit/cel_compile.rsplus tests. Feel free to take it as is or reimplement it.Problem
A rule that runs the single-variable
filtermacro, or the three-argumentmap(x, filter, expr), over a list of scalars generates a validator that does not compile. Two examples:Both macros are lowered to
Iterator::filter, and its closure takes&&T.element_binding_exprdereferences the element once, which is correct forall,anyandmap(they take&T) but not insidefilter. The binding is therefore&boolwhereboolis expected, or&i64,&u64or&f64where aCelScalaris expected. This affectsbool,int,uintanddoubleelements. Strings, bytes, messages and durations still compile because of auto-deref or blanket impls.!bcompiles only by accident, becauseNotis implemented for&bool.Downstream, we had to rewrite
[a, b, c].filter(x, x).size() <= 1as(a ? 1 : 0) + (b ? 1 : 0) + (c ? 1 : 0) <= 1to get a schema to build with v0.10.5.Change
The two
filterclosures destructure one reference (|&x| …), so the element binding sees&T, as it does in the other comprehensions. Nothing else in the emitted code changes. Message elements also get|&x|, and that is harmless.Verification
emit_compiles: 7 new fixtures, forfilterover aboollist literal and overbool,int,uintanddoublelists, and formap(x, f, m)overintandboollists. Before the fix, 6 fail to type-check; after it, all compile.acme.v1.FilteredScalars, with 5 message-level rules coveringbool,int64,uint32anddoublefields and the filter form ofmap.filter_over_scalar_lists_validateschecks owned messages and views, for valid input and for input where every rule fails. It doesn't compile before the fix and passes after it.cargo test --locked --workspace --all-targetspasses, along withcargo fmt --check,buf formatandbuf lint.hk check --all --slowbecause mise isn't installed here. Clippy on rustc 1.99.1 reports twoassert!-on-emptiness lints intests/emit_packaging.rs, and these also appear on unmodifiedmain.filterexpression. The v0.10.5 plugin givesE0308. With this branch, the crate compiles and its contract tests pass.Consumers must regenerate validators with the updated plugin to get the fix. The runtime crate is unchanged.
Disclosure: Claude Opus 5.5 found this bug, wrote the fix and its tests, and drafted this description and Discussion.
All reactions