Skip to content

[SwiftParser] Accept keyword-named decl references after a module selector in atStartOfExpression - #3406

Merged
rintaro merged 1 commit into
swiftlang:mainfrom
no-hup:module-selector-keyword-expr-start
Sep 9, 2026
Merged

rintaro merged 1 commit into
swiftlang:mainfrom
no-hup:module-selector-keyword-expr-start

Conversation

@no-hup

@no-hup no-hup commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

Fixes #3387.

Problem

atStartOfExpression() consumes a module selector and then recursively tests whether the following token can begin an unqualified expression. Lexer-classified keywords such as as and is fail that check, so valid code the compiler accepts:

func use(_ x: Int) {
  Module::as(x)
  Module::is(x)
}

is parsed at statement level as unexpected code instead of a FunctionCallExprSyntax, with a spurious diagnostic.

Fix

parseDeclReferenceBase() already accepts any lexer-classified keyword after a module selector and remaps it to an identifier. This teaches the lookahead the same rule: after consuming the module selector tokens, a lexer-classified keyword counts as the start of an expression. Six lines, no behavior change for code without module selectors.

Testing

Added testModuleSelectorKeywordNameAtStartOfStatement to ModuleSelectorTests covering Module::as(x) and Module::is(x) as bare statements plus the issue's function-body reproducer, asserting the FunctionCallExprSyntax substructure with the module-selected DeclReferenceExprSyntax callee and no diagnostics.

  • Without the fix, all three cases fail (no FunctionCallExprSyntax; one diagnostic each) — matching the issue.
  • With the fix, the full test suite passes (SKIP_LONG_TESTS=1, Swift 6.3 / aarch64 Linux).

…ector in atStartOfExpression

'atStartOfExpression()' consumes a module selector and then recursively
tests whether the following token can begin an unqualified expression, so
lexer-classified keywords such as 'as' and 'is' fail the check and valid
code like 'Module::as(x)' at statement level is parsed as unexpected code.

'parseDeclReferenceBase()' already accepts any lexer-classified keyword
after a module selector and remaps it to an identifier; teach the
lookahead the same rule so the two agree.

Fixes swiftlang#3387
@no-hup

no-hup commented Sep 9, 2026

Copy link
Copy Markdown
Contributor Author

This has been open a couple of weeks with no CI run yet. Whenever someone has a moment, could a maintainer kick off @swift-ci please test? Happy to rebase or adjust anything if needed.

@rintaro

rintaro commented Sep 9, 2026

Copy link
Copy Markdown
Member

@swift-ci Please test

@rintaro rintaro left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delay. This looks good to me. Thank you!

@rintaro
rintaro enabled auto-merge September 9, 2026 19:19
@rintaro
rintaro merged commit b01ab99 into swiftlang:main Sep 9, 2026
42 checks passed
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.

[SwiftParser] Module-selected keyword references such as Module::as(x) are parsed as unexpected code

2 participants