Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 19, 2025

Boolean literals (true, false) and null in switch expression when clauses were being tokenized as lambda parameter names instead of their respective literal types.

Changes

  • Grammar: Moved #boolean-literal and #null-literal patterns before #anonymous-method-expression in the expression patterns list to ensure these literal expressions are matched before lambda parameter detection
  • Tests: Added test coverage for boolean literals in when clauses

The fix prioritizes boolean and null literal matching while preserving lambda expression functionality by keeping other literal patterns (like #tuple-literal) in their original positions.

Example

Before this fix, true and false in the following code were incorrectly highlighted as parameter names:

_ = (1, '2', "3") switch
{
    (var a, 'b', _) when a > 0 == true => 1,
    var _ when false => 3,
    var _ when true => 4,
};

The lambda expression pattern was matching <identifier> => patterns before the grammar could recognize boolean keywords as literals.

Original prompt

This section details on the original issue you should resolve

<issue_title>boolean literals in when clause of switch statement</issue_title>
<issue_description>## Details

What editor are you seeing the problem in? (e.g. Atom, Visual Studio Code, etc.)
vscode
What version of the editor are you using?
1.88.1
What color theme are you using?
darkplus

Repro

_ = (1, '2', "3") switch
{
    (var a, 'b', _) when a > 0 == true => 1,
    (object, _, string s) when s.Length > 2 == false => 2,
    var _ when false => 3,
    var _ when true => 4,
};

code

Please provide a code example and (optionally) a screenshot demonstrating the problem.</issue_description>

<agent_instructions>In the example, true and false are not classified by the grammar.</agent_instructions>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

- Add test case for boolean literals (true/false) in switch expression when clauses
- Fix anonymous-method-expression pattern to exclude boolean and null keywords
- This ensures true/false are correctly classified as constant.language.boolean instead of parameter names

Co-authored-by: JoeRobich <[email protected]>
Copilot AI changed the title [WIP] Fix boolean literals in when clause of switch statement Fix boolean literal highlighting in switch expression when clauses Dec 20, 2025
Copilot AI requested a review from JoeRobich December 20, 2025 00:05
((?:\b(?:async|static)\b\s*)*)
(?:
(@?[_[:alpha:]][_[:alnum:]]*)\b|
(?!\b(?:true|false|null)\b)(@?[_[:alpha:]][_[:alnum:]]*)\b|
Copy link
Member

Choose a reason for hiding this comment

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

@copilot we shouldn't update the anonymous-method-express. true and false are literal expressions. Perhaps we should move literal higher in the list of expression patterns.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in e2ff38b. Moved #boolean-literal and #null-literal before #anonymous-method-expression in the expression patterns list. I kept the rest of #literal (including #tuple-literal) in its original position to avoid breaking lambda expressions with parenthesized parameters like (x, y) =>.

Instead of modifying the anonymous-method-expression pattern, move #boolean-literal and #null-literal higher in the expression patterns list to ensure they are matched before lambda expressions. This prevents true/false/null from being incorrectly interpreted as lambda parameter names while preserving all lambda expression functionality.

Co-authored-by: JoeRobich <[email protected]>
@JoeRobich JoeRobich marked this pull request as ready for review December 20, 2025 02:11
@JoeRobich JoeRobich requested a review from dibarbet December 20, 2025 02:11
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.

boolean literals in when clause of switch statement

2 participants