Repository navigation
Parser: report E0953 when 'it' is declared as a parameter or pattern binding (#2382) - #2435
Merged
Merged
Conversation
…binding (#2382) Parser.param_binding_should_parse_pattern routed any non-TK_IDENT token (including TK_KW_IT) to parse_pattern, which fell through to 'expected pattern' instead of E0953 ('it' is a reserved keyword and cannot be used as a parameter name). Handle TK_KW_IT in parse_param_list, parse_pattern, and single-parameter fat-arrow closures so parameter and pattern bindings naming 'it' report E0953 and recover cleanly without cascading errors.
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 #2382.
Summary
Declaring a function parameter named
it(fn f(it: i32) -> i32: itorfn f[I: Iter[&T]](it: I) -> i32) previously failed witherror: expected patterninstead ofE0953('it' is a reserved keyword and cannot be used as a parameter name).5 Whys Root Cause
fn f(it: i32) -> i32: itfail witherror: expected pattern?Parser.parse_param_list(src/Parser.w:9483) dispatched toself.parse_pattern(), which fell through all pattern cases toself.emit_error("expected pattern")(src/Parser.w:8125).parse_param_listcallparse_pattern()onit?Parser.param_binding_should_parse_pattern()(src/Parser.w:9358) returnedtruewhenevert != TokenKind.TK_IDENT, anditlexes asTokenKind.TK_KW_IT.parse_pattern()andparse_param_list()not recognizeTK_KW_IT?Parenthesized closures (
parse_fat_arrow_paren_closureat line 8764) andexpect_ident(at line 460) explicitly checkedTokenKind.TK_KW_ITand emittedE0953, butparse_param_list,parse_pattern, and unparenthesized single-parameter fat-arrow closures (it => ...) did not.parse_param_listbypassingexpect_ident?When commit
ddc8767bcintroducedparam_binding_should_parse_pattern(), itsif t != TokenKind.TK_IDENT: return trueguard routed every non-TK_IDENTtoken intoparse_pattern()beforeparse_param_listcould reachexpect_ident().E0953tests?test/behavior/it_keyword_param.wonly tested(it) => it + 1(which goes throughparse_fat_arrow_paren_closure), notfnparameters, pattern bindings, orit => ...closures.Changes
Parser.parse_param_list: checkTokenKind.TK_KW_ITbeforeparam_binding_should_parse_pattern(), emitE0953('it' is a reserved keyword and cannot be used as a parameter name), advance, and bind__itso trailing: Type = Defaultand subsequent parameters parse without cascade errors.Parser.parse_pattern: checkTokenKind.TK_KW_ITbefore the fallbackexpected patternerror and emitE0953('it' is a reserved keyword and cannot be used as an identifier).Parser.parse_primary/Parser.parse_fat_arrow_single/Parser.parse_fat_arrow_paren_closure: routeit => exprandmove it => exprtoparse_fat_arrow_single(emittingE0953) and consume optional: Typeafteritin(it: T) => expr.test/compile_errors/err_it_keyword_fn_param.wandtest/parser/parser_unit_test.w(verified failing before the fix and passing after).Verification
src/main build :dev+out/bootstrap/bin/with-stage1 testontest/compile_errors/err_it_keyword_fn_param.w,test/parser/parser_unit_test.w,test/behavior/it_keyword_*.w, andtest/compile_errors/err_for_pattern_invalid.w: all green.WITH=$PWD/src/main src/main build :gate(pinned seednightly-20261015-local-1-efcb91c30a6a, commit71d85556):survey: all targets green.