Skip to content

feat(parser): add support for parsing return pipelines as subexpressions#17786

Open
fdncred wants to merge 3 commits intonushell:mainfrom
fdncred:return_with_pipeline
Open

feat(parser): add support for parsing return pipelines as subexpressions#17786
fdncred wants to merge 3 commits intonushell:mainfrom
fdncred:return_with_pipeline

Conversation

@fdncred
Copy link
Copy Markdown
Contributor

@fdncred fdncred commented Mar 12, 2026

This PR allows the return command to have a pipeline. Previously the parser would interpret return x | y as (return x) | y. Now it interprets it as return (x | y)

closes #13648

Release notes summary - What our users need to know

Allow the return command to have a pipeline. Previously the parser would interpret return x | y as (return x) | y. Now it interprets it as return (x | y).

Example from issue after this PR

 def nu-dirs [] { return $nu | transpose k v | where k like 'dir' }
 nu-dirs
╭─#─┬──────────k───────────┬─────────────────────v─────────────────────╮
 0  default-config-dir    C:\Users\username\AppData\Roaming\nushell 
 1  home-dir              C:\Users\username                         
 2  data-dir              C:\Users\username\AppData\Roaming\nushell 
 3  cache-dir             C:\Users\username\AppData\Local\nushell   
 4  vendor-autoload-dirs  [list 2 items]                            
 5  user-autoload-dirs    [list 1 item]                             
 6  temp-dir              D:\Temp                                   
╰─#─┴──────────k───────────┴─────────────────────v─────────────────────╯

Before (incorrect rounding)

 def t [] { return 1.04 | math round }
 t
1.04
 1.04 | math round
1

After (correct rounding)

 def t [] { return 1.04 | math round }
 t
1

Tasks after submitting

N/A

@github-actions github-actions bot added the A:parser Issues related to parsing label Mar 12, 2026
Comment thread crates/nu-parser/src/parser.rs Outdated
Comment thread crates/nu-parser/src/parser.rs Outdated
@cptpiepmatz cptpiepmatz added notes:ready Indicates Ready for Release notes notes:other Noted in "Other changes" section labels Mar 17, 2026
Comment thread crates/nu-parser/src/lite_parser.rs Outdated
TokenContents::Pipe => {
pipeline.push(&mut command);
command.pipe = Some(token.span);
// If this command begins with `return` and doesn't have its own redirection, we want to treat the rest of the pipeline as the
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.

Is the special treatment of redirection the reason not to put this check in parse_builtin_commands?

Actually I don't understand why we allow redirection s in keyword return.

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.

Ok, I think I get the difference between let and return here, there's an assignment mode when the lite parser sees the = sign.

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.

TBH I'm afraid this is still too hacky. If there's not yet enough utilities to make it a straightforward implementation, I think we should discuss whether this is a sensible choice to make.

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.

ok, this is my last attempt to make this thing work. Let me know what you think.

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.

ok, this is my last attempt to make this thing work. Let me know what you think.

I feel it would be better if we introduce a new parsing mode for the first element of a pipeline.

At the end of the day, typing a pair of parentheses for return isn't that annoying IMHO. Let's wait for other people's opinions, shall we?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A:parser Issues related to parsing notes:other Noted in "Other changes" section notes:ready Indicates Ready for Release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

"Return into pipe" is allowed but doesn't use the pipe

3 participants