Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[move-compiler] Improved declaration handling after a parse error #20498

Merged
merged 8 commits into from
Dec 6, 2024

Conversation

awelc
Copy link
Contributor

@awelc awelc commented Dec 3, 2024

Description

This PR finesses parsing resilience for a specific situation which creates questionable user experience in the IDE. This specific case occurs when a valid sequence item in on the following line from parsing error. Consider the following example:

    public struct SomeStruct has drop, copy {}

    public fun bar(param: SomeStruct): SomeStruct {
        param
    }

    public fun test7(param: SomeStruct): SomeStruct {
        param.b
        param.bar()
    }

The param.b expression is invalid (as the programmer is still typing) but this should not be be the reason to not compile the following param.bar() incorrectly (and generate another error about wrong return value.

The following example shows a scenario that's even worse:

     let v =
     let _tmp1 = 42;
     let _tmp2 = _tmp1 * param;
     let _tmp3 = _tmp1 + param;

Here, when a programmer starts typing an incomplete (and unparsable) let v = , if we don't compile declaration of _tmp1 correctly, it would lead to unnecessary cascading errors to appear due to _tmp1 being undefined.

Test plan

Additional tests have been added. All tests (including the modified ones) must pass


Release notes

Check each box that your changes affect. If none of the boxes relate to your changes, release notes aren't required.

For each box you select, include information after the relevant heading that describes the impact of your changes that a user might notice and any actions they must take to implement updates.

  • Protocol:
  • Nodes (Validators and Full nodes):
  • Indexer:
  • JSON-RPC:
  • GraphQL:
  • CLI: Compiler might generate slightly different errors as a result of the compilation process being more permissive with respect to parsing errors.
  • Rust SDK:
  • REST API:

Copy link

vercel bot commented Dec 3, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
sui-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 6, 2024 1:43am
3 Skipped Deployments
Name Status Preview Comments Updated (UTC)
multisig-toolkit ⬜️ Ignored (Inspect) Visit Preview Dec 6, 2024 1:43am
sui-kiosk ⬜️ Ignored (Inspect) Visit Preview Dec 6, 2024 1:43am
sui-typescript-docs ⬜️ Ignored (Inspect) Visit Preview Dec 6, 2024 1:43am

@@ -1546,6 +1563,9 @@ fn parse_sequence(context: &mut Context) -> Result<Sequence, Box<Diagnostic>> {
seq.push(item);
last_semicolon_loc = Some(current_token_loc(context.tokens));
if let Err(diag) = consume_token(context.tokens, Tok::Semicolon) {
if continue_sequence_after_error(context, diag.as_ref().clone()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'll leave for @cgswords to review, but personally, I would love to see some exploration on using newline information. I think it feels potentially a more "general" solution, but who knows
e.g.

x.foo
//  ^
//  CURSOR HERE
x.bar();

just looking for let doesn't help here but seeing that the next token is on another line helps a lot.

@@ -1503,6 +1503,45 @@ fn parse_sequence_item(context: &mut Context) -> Result<SequenceItem, Box<Diagno
))
}

// Checks if parsing of a sequence should continue after encountering an error.
fn continue_sequence_after_error(
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: should_continue_sequence_parsing or similar?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed to should_continue_sequence_after_error

fn continue_sequence_after_error(
context: &mut Context,
diag: Diagnostic,
last_token_preceded_by_eol: bool,
Copy link
Contributor

Choose a reason for hiding this comment

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

Small nit: since we already take context, we do not strictly need to pass this as well. And it would be weird to pass anything other than context.tokens.last_token_preceded_by_eol().

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, this wasn't very smart - I simply slapped another parameter when updating the previous version without thinking too much. Thanks! And fixed!

error[E01002]: unexpected token
┌─ tests/move_2024/ide_mode/seq_item_after_incomplete.move:25:9
25 │ let _tmp1 = 42;
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we also suppress this error in IDE mode? Maybe as a follow-up PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure if we should, though... It's a legitimate parsing error and I can imagine someone forgetting a semicolon at the end of the line and being surprised that the IDE does not show a squiggly but the actual build in CLI fails...

Copy link
Contributor

@cgswords cgswords left a comment

Choose a reason for hiding this comment

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

LGTM modulo some small nits.

@awelc awelc temporarily deployed to sui-typescript-aws-kms-test-env December 6, 2024 01:42 — with GitHub Actions Inactive
@awelc awelc merged commit 1e7f26c into main Dec 6, 2024
52 checks passed
@awelc awelc deleted the aw/compiler-parse-fail-before-let branch December 6, 2024 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.

3 participants