Skip to content

Fix optimizer reading a non-constant map literal property as () - #1219

Merged
schungx merged 1 commit into
rhaiscript:mainfrom
waw666waw666:fix-optimizer-map-literal-non-constant-property
Nov 18, 2026
Merged

schungx merged 1 commit into
rhaiscript:mainfrom
waw666waw666:fix-optimizer-map-literal-non-constant-property

Conversation

@waw666waw666

Copy link
Copy Markdown
Contributor

Since the map literal template change (c8d99c6, "Optimize map literals"), accessing a property directly on a map literal can return () once optimization is on, if the property's value is not a literal at parse time:

let mut engine = Engine::new(); // default OptimizationLevel::Simple
engine.eval::<Dynamic>("#{ a: 42, b: { 1 } }.b")?;      // () — expected 1
engine.eval::<Dynamic>(r#"#{ b: { 1 } }["b"]"#)?;      // ()
engine.eval::<Dynamic>("#{ a: if true { 1 } else { 2 } }.a")?; // ()
engine.eval::<Dynamic>("#{ a: 42, b: (() ?? 1) }.b")?; // ()

With OptimizationLevel::None all of these return the right value.

Cause

The map template now holds every key, with () as a placeholder for values that are not constant (those stay in the expression list). The map.prop and map["prop"] arms in optimize_expr check m.1.contains_key(..) first, which is always true for any key of the literal, so a pure but not-yet-constant value (a block, if/switch on a constant, ??, ...) is replaced by its () placeholder. These arms run before the map's own entries are optimized and moved into the template, so the value never gets the chance to become a constant there.

Fix

Look the key up among the non-constant expressions first and promote that expression; only fall back to the template value when the key isn't there, and to () when the key doesn't exist at all. Same change for the property and the string-index arms.

Tests

Added cases to test_map_template_evaluation_and_isolation in tests/maps.rs (runs under None/Simple/Full). They fail on main and pass with the fix. cargo test --tests and cargo test --tests --features internals,grain,bin-features (including the grain differential suite) pass; cargo fmt --all -- --check is clean.

Found by running random scripts with OptimizationLevel::None vs Simple/Full and comparing results.

The template of a map literal holds every key, with () standing in for
values that are not constant. When optimizing #{...}.prop or
#{...}["prop"], the optimizer checked the template first, so a property
whose value is pure but only becomes constant later (e.g. a block, an
if/switch on a constant, or ??) was replaced by ().

Look among the non-constant values first and only fall back to the
template.
@schungx schungx added the bug label Nov 18, 2026
@schungx
schungx merged commit 6f442b7 into rhaiscript:main Nov 18, 2026
53 of 55 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants