Fix parsing of signature inp/out types in predecls (#10642)

# Description
Fixes https://github.com/nushell/nushell/issues/10605 (again).

The loop looking for `[` to determine signature position didn't stop
early enough, so it thought the second `[` denoting the inp/out types
marks the beginning of the signature.

# User-Facing Changes

# Tests + Formatting
adds a new `predecl_signature_multiple_inp_out_types` test

# After Submitting
This commit is contained in:
Jakub Žádník 2023-10-08 13:58:26 +03:00 committed by GitHub
parent 67b5e1bde9
commit 4efccb2b1c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 4 deletions

View File

@ -202,6 +202,7 @@ pub fn parse_def_predecl(working_set: &mut StateWorkingSet, spans: &[Span]) {
.starts_with(&[b'(']) .starts_with(&[b'('])
{ {
signature_pos = Some(pos); signature_pos = Some(pos);
break;
} }
pos += 1; pos += 1;

View File

@ -103,10 +103,10 @@ fn parse_file_relative_to_parsed_file_simple() {
} }
#[test] #[test]
fn predecl_signature_parse() { fn predecl_signature_single_inp_out_type() {
Playground::setup("predecl_signature_parse", |dirs, sandbox| { Playground::setup("predecl_signature_single_inp_out_type", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContentToBeTrimmed( sandbox.with_files(vec![FileWithContentToBeTrimmed(
"spam.nu", "spam1.nu",
" "
def main [] { foo } def main [] { foo }
@ -114,11 +114,33 @@ fn predecl_signature_parse() {
", ",
)]); )]);
let actual = nu!(cwd: dirs.test(), pipeline("nu spam.nu")); let actual = nu!(cwd: dirs.test(), pipeline("nu spam1.nu"));
assert_eq!(actual.out, "foo"); assert_eq!(actual.out, "foo");
}) })
} }
#[test]
fn predecl_signature_multiple_inp_out_types() {
Playground::setup(
"predecl_signature_multiple_inp_out_types",
|dirs, sandbox| {
sandbox.with_files(vec![FileWithContentToBeTrimmed(
"spam2.nu",
"
def main [] { foo }
def foo []: [nothing -> string, string -> string] { 'foo' }
",
)]);
let actual = nu!(cwd: dirs.test(), pipeline("nu spam2.nu"));
assert_eq!(actual.out, "foo");
},
)
}
#[ignore] #[ignore]
#[test] #[test]
fn parse_file_relative_to_parsed_file() { fn parse_file_relative_to_parsed_file() {