Skip to content

Parser: report E0953 when 'it' is declared as a parameter or pattern binding (#2382) - #2435

Merged
ehartford merged 1 commit into
mainfrom
issue-2382-it-param
Oct 16, 2026
Merged

ehartford merged 1 commit into
mainfrom
issue-2382-it-param

Conversation

@ehartford

Copy link
Copy Markdown
Contributor

Fixes #2382.

Summary

Declaring a function parameter named it (fn f(it: i32) -> i32: it or fn f[I: Iter[&T]](it: I) -> i32) previously failed with error: expected pattern instead of E0953 ('it' is a reserved keyword and cannot be used as a parameter name).

5 Whys Root Cause

  1. Why did fn f(it: i32) -> i32: it fail with error: expected pattern?
    Parser.parse_param_list (src/Parser.w:9483) dispatched to self.parse_pattern(), which fell through all pattern cases to self.emit_error("expected pattern") (src/Parser.w:8125).
  2. Why did parse_param_list call parse_pattern() on it?
    Parser.param_binding_should_parse_pattern() (src/Parser.w:9358) returned true whenever t != TokenKind.TK_IDENT, and it lexes as TokenKind.TK_KW_IT.
  3. Why did parse_pattern() and parse_param_list() not recognize TK_KW_IT?
    Parenthesized closures (parse_fat_arrow_paren_closure at line 8764) and expect_ident (at line 460) explicitly checked TokenKind.TK_KW_IT and emitted E0953, but parse_param_list, parse_pattern, and unparenthesized single-parameter fat-arrow closures (it => ...) did not.
  4. Why was parse_param_list bypassing expect_ident?
    When commit ddc8767bc introduced param_binding_should_parse_pattern(), its if t != TokenKind.TK_IDENT: return true guard routed every non-TK_IDENT token into parse_pattern() before parse_param_list could reach expect_ident().
  5. Why was this not caught by existing E0953 tests?
    test/behavior/it_keyword_param.w only tested (it) => it + 1 (which goes through parse_fat_arrow_paren_closure), not fn parameters, pattern bindings, or it => ... closures.

Changes

  • Parser.parse_param_list: check TokenKind.TK_KW_IT before param_binding_should_parse_pattern(), emit E0953 ('it' is a reserved keyword and cannot be used as a parameter name), advance, and bind __it so trailing : Type = Default and subsequent parameters parse without cascade errors.
  • Parser.parse_pattern: check TokenKind.TK_KW_IT before the fallback expected pattern error and emit E0953 ('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: route it => expr and move it => expr to parse_fat_arrow_single (emitting E0953) and consume optional : Type after it in (it: T) => expr.
  • Added regression tests in test/compile_errors/err_it_keyword_fn_param.w and test/parser/parser_unit_test.w (verified failing before the fix and passing after).

Verification

  • src/main build :dev + out/bootstrap/bin/with-stage1 test on test/compile_errors/err_it_keyword_fn_param.w, test/parser/parser_unit_test.w, test/behavior/it_keyword_*.w, and test/compile_errors/err_for_pattern_invalid.w: all green.
  • WITH=$PWD/src/main src/main build :gate (pinned seed nightly-20261015-local-1-efcb91c30a6a, commit 71d85556): survey: all targets green.

…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.
@ehartford
ehartford merged commit 2620f6a into main Oct 16, 2026
@ehartford
ehartford deleted the issue-2382-it-param branch October 16, 2026 12:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Parser: a parameter named it fails with "expected pattern" and no hint that it is reserved

1 participant