Report a syntax error for unclosed brackets in expressions#968
Merged
RobLoach merged 1 commit intoJul 6, 2026
Merged
Conversation
`{{{ foo }}` rendered as `[object Object]` instead of failing. The extra
brace opens an object that never closes, so its group-start token was
left on the shunting-yard stack, flushed into the output, and then
handed straight back by the parser.
Check the stack once the expression is consumed and throw if an opening
`{`, `[` or `(` was never matched. A well formed expression always pops
its start at the matching close, so this only fires on genuinely
unbalanced input.
willrowe
approved these changes
Jul 2, 2026
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.
{{{ foo }}rendered as[object Object]instead of a syntax error (TwigPHP throwsUnexpected "}"here, per @ericmorand on the issue). The extra{opens an object that never closes, so its group-start token is left on the shunting-yard stack, flushed into the output, and handed back by the parser as-is.Twig.expression.compilenow throws if an opening{,[or(is still on the stack once the expression is consumed. Valid expressions always pop their start at the matching close, so it can't fire on well-formed input. (By default twig swallows compile errors and renders empty, which is why the reporter'stry/catchnever fired;rethrow: truesurfaces it like any other syntax error.)Two calls for you: I made the check cover all four grouping constructs, not just the object from the issue, but I'll narrow it to
{if you'd rather. And the message isExpected closing '}'rather than TwigPHP'sUnexpected "}"at the trailing brace; matching that exactly is more work, so say the word.Tests are in
test/test.regression.js.closes #896