fix: handle leading tilde on else if chain tags - #768
Merged
Conversation
`{{~else if cond}}` panicked during template compilation with
`unreachable: leading_tilde_to_omit_whitespace`.
The grammar places the optional leading tilde of `invert_chain_tag`
before `invert_tag_item`, but the compile code called `parse_name`
(before `parse_expression`) to consume `invert_tag_item`, so the
leading tilde was handed to `parse_name` and hit its `unreachable!`
arm. The plain `{{~else}}` (`invert_tag`) case already worked because
it skips `parse_name` and `parse_expression` handles the tilde itself.
Consume the optional leading tilde before `parse_name` for
`invert_chain_tag`, and fold its `omit_pre_ws` semantics into the
parsed expression so preceding whitespace is stripped consistently
with `{{~else}}`.
Fixes #766.
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.
Fixes #766.
Summary
{{~else if cond}}panicked during template compilation:Root cause
The grammar places the optional leading tilde of
invert_chain_tagbefore
invert_tag_item:But the compile code called
parse_name(to consumeinvert_tag_item)before
parse_expression, so when a leading~was present it washanded to
parse_name, which only expectsidentifier/invert_tag_item/reference/subexpression— hence theunreachable!arm.The plain
{{~else}}(invert_tag) case already worked, because itskips
parse_nameand goes straight toparse_expression, whichhandles the leading tilde itself. Only
invert_chain_tagwith aleading tilde was broken (trailing tildes worked, since
parse_expression's loop consumes them).Fix
Consume the optional leading tilde before
parse_nameforinvert_chain_tag, and fold itsomit_pre_wssemantics into theparsed expression so preceding whitespace is stripped consistently
with
{{~else}}.Tests
src/grammar.rs: grammar acceptance cases for{{~else if foo}},{{else if foo~}},{{~else if foo~}}.src/template.rs: unit test reproducing the original panic.tests/whitespace.rs: rendering tests for leading / trailing / bothtildes and the falsy fallback, confirming behaviour matches
{{~else}}.All existing tests,
cargo fmt --check, andcargo clippy --testspass.