Skip to content

Fix: missing parse error when extra tokens are given to let bindings - #12238

Merged
fdncred merged 1 commit into
nushell:mainfrom
yizhepku:fix-let-binding-extra-token
Mar 21, 2024
Merged

fdncred merged 1 commit into
nushell:mainfrom
yizhepku:fix-let-binding-extra-token

Conversation

@yizhepku

@yizhepku yizhepku commented Mar 19, 2024 •

Copy link
Copy Markdown
Contributor

Manual checks are added to parse_let, parse_mut, and parse_const. parse_var_with_opt_type is also fixed to update spans_idx correctly. Fixes #12125.

It's technically a fix, but I'd rather not merge this directly. I'm making this PR to bring into attention the code quality of the parser code. For example:

  • Inconsistent usage of spans_idx. What is its purpose, and which parsing functions need it? I suspect it's possible to remove the usage of spans_idx entirely.
  • Lacking documentation for top-level functions. What does mutable mean for parse_var_with_opt_type()?
  • Inconsistent error reporting. Usage of both working_set.error() and working_set.parse_errors.push(). Using ParseError::Expected for an invalid variable name when there's ParseError::VariableNotValid (from parser.rs:5237). Checking variable names manually when there's is_variable() (from parser.rs:2905).
  • span() is a terrible name for a function that flattens a bunch of spans into one (from nu-protocal/src/span.rs:92). The top-level comment (Used when you have a slice of spans of at least size 1) doesn't help either.

I've only looked at a small portion of the parser code; I expect there are a lot more. These issues made it much harder to fix a simple bug like #12125. I believe we should invest some effort to cleanup the parser code, which will ease maintainance in the future. I'll willing to help if there is interest.

@yizhepku
yizhepku marked this pull request as draft March 19, 2024 14:54
@yizhepku
yizhepku marked this pull request as ready for review March 19, 2024 15:05
@fdncred

fdncred commented Mar 19, 2024

Copy link
Copy Markdown
Contributor

Thanks for looking into this! It would be great to have some tests that ensure your fix stays fixed.

@yizhepku

Copy link
Copy Markdown
Contributor Author

I've added a simple test case.

@WindSoilder WindSoilder 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.

Thank you for making some changes to improve nushell! I just leave some comment

Comment thread crates/nu-parser/src/parser.rs Resolved
Comment thread tests/parsing/mod.rs Resolved
Comment thread crates/nu-parser/src/parser.rs Resolved
Comment thread tests/parsing/mod.rs

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.

Can you also add tests for const a int = 2, mut a string = "3"?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Will do. But I suspect these tests will be obsolete if we refactor the parser code. I think my current fix is hacky.

@yizhepku
yizhepku requested a review from WindSoilder March 20, 2024 19:34
Manual checks are added to `parse_let`, `parse_mut`, and `parse_const`.

@WindSoilder WindSoilder 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.

Thanks! LGTM


Regards to your question, generally I agree with your point, maybe we can improve some parser code, I remember there is a pr to make it more structured too

@fdncred
fdncred merged commit ef05d14 into nushell:main Mar 21, 2024
@fdncred

fdncred commented Mar 21, 2024

Copy link
Copy Markdown
Contributor

Thanks!

@fdncred fdncred added the notes:fixes Noted in "Bug fixes" section label Mar 21, 2024
@yizhepku
yizhepku deleted the fix-let-binding-extra-token branch March 21, 2024 17:21
@hustcer hustcer added this to the v0.92.0 milestone Mar 22, 2024
sholderbach pushed a commit that referenced this pull request Apr 16, 2024
# Description
A little refactor that use `working_set.error` rather than
`working_set.parse_errors.push`, which is reported here:
#12238

> Inconsistent error reporting. Usage of both working_set.error() and
working_set.parse_errors.push(). Using ParseError::Expected for an
invalid variable name when there's ParseError::VariableNotValid (from
parser.rs:5237). Checking variable names manually when there's
is_variable() (from parser.rs:2905).

# User-Facing Changes
NaN

# Tests + Formatting
Done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

notes:fixes Noted in "Bug fixes" section

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Let, mut, const bindings don't parse error when more than one ident is passed in

4 participants