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

Forbid reserved variable names for function arguments #11169

Merged
merged 1 commit into from
Nov 29, 2023

Conversation

KAAtheWiseGit
Copy link
Contributor

Works for all arguments and flags. Because the signature parsing doesn't give the spans, it is flags the entire signature.

Also added a constant with reserved variable names.

Fix #11158.

Copy link
Member

@amtoine amtoine left a comment

Choose a reason for hiding this comment

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

i just tried and it addresses the linked issue indeed 👌

could we add a few tests to make sure the example of the issue does not work anymore at parse-time at least? 😇

@KAAtheWiseGit
Copy link
Contributor Author

@amtoine will do! Where should I put them?

@amtoine
Copy link
Member

amtoine commented Nov 28, 2023

will do! Where should I put them?

what i would do is look for a test involving let in = ...!
because it is not allowed, there might be a test for it 😇

@KAAtheWiseGit
Copy link
Contributor Author

@amtoine I mean in folders! I am confused, because there are a bunch of subfolders in test/ and I don't know where this one fits. Should I make a new one?

@KAAtheWiseGit
Copy link
Contributor Author

Found tests/parsing and added these test cases there.

Comment on lines +307 to +319
#[rstest]
#[case("def test [ in ] {}")]
#[case("def test [ in: string ] {}")]
#[case("def test [ nu: int ] {}")]
#[case("def test [ env: record<> ] {}")]
#[case("def test [ --env ] {}")]
#[case("def test [ --nu: int ] {}")]
#[case("def test [ --in (-i): list<any> ] {}")]
#[case("def test [ a: string, b: int, in: table<a: int b: int> ] {}")]
#[case("def test [ env, in, nu ] {}")]
fn parse_function_signature_name_is_builtin_var(#[case] phrase: &str) {
let actual = nu!(phrase);
assert!(actual.err.contains("nu::parser::name_is_builtin_var"))
Copy link
Member

Choose a reason for hiding this comment

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

Thanks for the exhaustive test!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, I discovered rtest and had some fun with it.

@@ -515,6 +526,19 @@ pub fn parse_def(
let mut result = None;

if let (Some(mut signature), Some(block_id)) = (sig.as_signature(), block.as_block()) {
for arg_name in &signature.required_positional {
verify_not_keyword(working_set, &arg_name.name, sig.span);
Copy link
Member

Choose a reason for hiding this comment

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

This may be a future improvement but could we get a more specific span pointing to the part in the signature that is violating the rule?

Copy link
Contributor Author

@KAAtheWiseGit KAAtheWiseGit Nov 29, 2023

Choose a reason for hiding this comment

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

Not without rewriting .as_signature and nu-protocol::signature. Currently the entire module has no notion of spans.

Copy link
Member

Choose a reason for hiding this comment

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

Ah right at this point the information is gone (in the call site this is somewhat recoverable but yeah for Signature this doesn't make as much sense).

In the future we could maybe move that check up earlier in the parse, but having the error at all should suffice for now.

crates/nu-parser/src/parse_keywords.rs Outdated Show resolved Hide resolved
Works for all arguments and flags. Because the signature parsing doesn't
give the spans, it is flags the entire signature.  I also added a
constant with reserved variable names.

Test cases located at `parsing::parse_function_signature_name_is_builtin_var`.

Fix nushell#11158.
Copy link
Member

@sholderbach sholderbach left a comment

Choose a reason for hiding this comment

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

Thanks!

Copy link
Member

@amtoine amtoine left a comment

Choose a reason for hiding this comment

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

thanks for the tests and working on this @KAAtheWiseGit 🙏

let's land the changes and see how it goes 😉

@amtoine amtoine merged commit 0e1322e into nushell:main Nov 29, 2023
19 checks passed
@KAAtheWiseGit KAAtheWiseGit deleted the def-in branch December 1, 2023 18:30
hardfau1t pushed a commit to hardfau1t/nushell that referenced this pull request Dec 14, 2023
Works for all arguments and flags. Because the signature parsing doesn't
give the spans, it is flags the entire signature.

Also added a constant with reserved variable names.

Fix nushell#11158.
dmatos2012 pushed a commit to dmatos2012/nushell that referenced this pull request Feb 20, 2024
Works for all arguments and flags. Because the signature parsing doesn't
give the spans, it is flags the entire signature.

Also added a constant with reserved variable names.

Fix nushell#11158.
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.

Naming a command parameter in breaks
3 participants