Skip to content

[v2] feat(diagnostics): Improve unexpected terminal diagnostic on identifiers#1848

Open
teofr wants to merge 2 commits into
mainfrom
teofr/improve-identifier-diagnostic
Open

[v2] feat(diagnostics): Improve unexpected terminal diagnostic on identifiers#1848
teofr wants to merge 2 commits into
mainfrom
teofr/improve-identifier-diagnostic

Conversation

@teofr

@teofr teofr commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

Certain keywords can be parsed as either the keyword itself, or as an identifier.

This PR tries to improve on the UnexpectedTerminal diagnostic when these keywords are likely expected only on their capacity as identifiers.

This is a heuristic, so (as pointed by Copilot below) in some cases omitting certain terminals is not perfect, but I think it's worth it in the general case.

Check the difference on the newly added test between the first and second commit for an example.

@teofr teofr requested a review from Copilot June 9, 2026 13:54
@changeset-bot

changeset-bot Bot commented Jun 9, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: dff2b3f

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR improves Solidity v2 parser diagnostics by reducing noise in UnexpectedTerminal expectation lists when unreserved keywords are likely being treated as identifiers, and updates diagnostics snapshot tests accordingly.

Changes:

  • Adjusts parsing error expectation conversion to drop certain unreserved keyword terminals when Identifier is among the expected terminals.
  • Adds/updates diagnostics snapshot test cases for unexpected_terminal (including a new “expecting_identifier” scenario) and refreshes related expected outputs.
  • Updates the generated Rust test harness to run the split unexpected_terminal snapshot suites.

Reviewed changes

Copilot reviewed 3 out of 10 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
crates/solidity-v2/outputs/cargo/parser/src/parser/mod.rs Implements the heuristic that filters unreserved keyword terminals out of expectation sets when Identifier is expected.
crates/solidity-v2/outputs/cargo/tests/src/diagnostics_output/snapshots.generated.rs Updates generated snapshot test registration to run the new unexpected_terminal/* sub-suites.
crates/solidity-v2/testing/snapshots/diagnostics_output/syntax/unexpected_terminal/semicolon/input.sol Adds semicolon-based unexpected-terminal snapshot input.
crates/solidity-v2/testing/snapshots/diagnostics_output/syntax/unexpected_terminal/semicolon/generated/solc/0.8.0-failure.txt Adds expected solc diagnostic output snapshot for the new semicolon case.
crates/solidity-v2/testing/snapshots/diagnostics_output/syntax/unexpected_terminal/semicolon/generated/slang/0.8.0-failure.txt Adds expected slang diagnostic output snapshot for the new semicolon case.
crates/solidity-v2/testing/snapshots/diagnostics_output/syntax/unexpected_terminal/expecting_identifier/input.sol Adds new snapshot input targeting the “keyword-as-identifier” ambiguity (public used as a name).
crates/solidity-v2/testing/snapshots/diagnostics_output/syntax/unexpected_terminal/expecting_identifier/generated/solc/0.8.0-failure.txt Adds expected solc diagnostic output snapshot for the new “expecting_identifier” case.
crates/solidity-v2/testing/snapshots/diagnostics_output/syntax/unexpected_terminal/expecting_identifier/generated/slang/0.8.0-failure.txt Adds expected slang diagnostic output snapshot for the new “expecting_identifier” case.
crates/solidity-v2/testing/snapshots/diagnostics_output/syntax/unexpected_terminal/generated/slang/0.8.0-failure.txt Removes the prior combined unexpected-terminal slang snapshot output in favor of the split test directories.
crates/solidity-v2/testing/snapshots/diagnostics_output/syntax/unexpected_eof/generated/slang/0.8.0-failure.txt Updates expected slang unexpected-EOF snapshot output (reflecting the filtered expectation set).

Comment on lines +127 to +131
if expected.contains(&TerminalKind::Identifier) {
expected.remove(&TerminalKind::AtKeyword);
expected.remove(&TerminalKind::ErrorKeyword);
expected.remove(&TerminalKind::FromKeyword);
expected.remove(&TerminalKind::GlobalKeyword);
@github-actions

github-actions Bot commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

🐰 Bencher Report

Branchteofr/improve-identifier-diagnostic
Testbedci

⚠️ WARNING: Truncated view!

The full continuous benchmarking report exceeds the maximum length allowed on this platform.

⚠️ WARNING: No Threshold found!

Without a Threshold, no Alerts will ever be generated.

🐰 View full continuous benchmarking report in Bencher

@teofr teofr force-pushed the teofr/improve-identifier-diagnostic branch from 5ce020f to dff2b3f Compare June 9, 2026 14:25
@teofr teofr marked this pull request as ready for review June 9, 2026 14:37
@teofr teofr requested review from a team as code owners June 9, 2026 14:37

@ggiraldez ggiraldez left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think the change is good, but I have one minor concern with maintainability.

Comment on lines +128 to +134
expected.remove(&TerminalKind::AtKeyword);
expected.remove(&TerminalKind::ErrorKeyword);
expected.remove(&TerminalKind::FromKeyword);
expected.remove(&TerminalKind::GlobalKeyword);
expected.remove(&TerminalKind::LayoutKeyword);
expected.remove(&TerminalKind::RevertKeyword);
expected.remove(&TerminalKind::TransientKeyword);

@ggiraldez ggiraldez Jun 9, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Would it be possible to generate this list of unreserved keywords from the language definition? This is bound to get out of sync with it as the language evolves.

From a quick look at the templates, it should be easy to generate TerminalKind::is_unreserved_keyword() and then run

expected.retain(|t| !t.is_unreserved_keyword());

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.

3 participants