Repository navigation
Conversation
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.
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.
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:With
OptimizationLevel::Noneall 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). Themap.propandmap["prop"]arms inoptimize_exprcheckm.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/switchon 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_isolationintests/maps.rs(runs under None/Simple/Full). They fail on main and pass with the fix.cargo test --testsandcargo test --tests --features internals,grain,bin-features(including thegraindifferential suite) pass;cargo fmt --all -- --checkis clean.Found by running random scripts with
OptimizationLevel::NonevsSimple/Fulland comparing results.