ensure that when nu evaluates files, it allows early returns (#7415)

# Description

Fixes #7301.

# User-Facing Changes

`return` can now be used in scripts without explicit `def main`.

# Tests + Formatting

Don't forget to add tests that cover your changes. (I'm not sure how to
test this.)

Make sure you've run and fixed any issues with these commands:

- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass

# After Submitting

If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
This commit is contained in:
pwygab
2023-02-02 07:02:27 +08:00
committed by GitHub
parent c130ca1bc6
commit 24d7227e27
10 changed files with 56 additions and 8 deletions

View File

@ -41,6 +41,7 @@ pub fn read_plugin_file(
&contents,
&plugin_filename,
PipelineData::empty(),
false,
);
}
}
@ -93,6 +94,7 @@ pub fn eval_config_contents(
&contents,
&config_filename,
PipelineData::empty(),
false,
);
// Merge the environment in case env vars changed in the config

View File

@ -106,13 +106,21 @@ pub fn evaluate_file(
&file,
file_path_str,
PipelineData::empty(),
true,
) {
std::process::exit(1);
}
if !eval_source(engine_state, stack, args.as_bytes(), "<commandline>", input) {
if !eval_source(
engine_state,
stack,
args.as_bytes(),
"<commandline>",
input,
true,
) {
std::process::exit(1);
}
} else if !eval_source(engine_state, stack, &file, file_path_str, input) {
} else if !eval_source(engine_state, stack, &file, file_path_str, input, true) {
std::process::exit(1);
}

View File

@ -145,6 +145,7 @@ pub fn evaluate_repl(
s.item.as_bytes(),
&format!("entry #{entry_num}"),
PipelineData::empty(),
false,
);
engine_state.merge_env(stack, get_guaranteed_cwd(engine_state, stack))?;
}
@ -472,6 +473,7 @@ pub fn evaluate_repl(
s.as_bytes(),
&format!("entry #{entry_num}"),
PipelineData::empty(),
false,
);
}
let cmd_duration = start_time.elapsed();

View File

@ -1,5 +1,5 @@
use crate::repl::eval_hook;
use nu_engine::eval_block;
use nu_engine::{eval_block, eval_block_with_early_return};
use nu_parser::{escape_quote_string, lex, parse, unescape_unquote_string, Token, TokenContents};
use nu_protocol::engine::StateWorkingSet;
use nu_protocol::CliError;
@ -203,6 +203,7 @@ pub fn eval_source(
source: &[u8],
fname: &str,
input: PipelineData,
allow_return: bool,
) -> bool {
let start_time = std::time::Instant::now();
@ -230,7 +231,13 @@ pub fn eval_source(
return false;
}
match eval_block(engine_state, stack, &block, input, false, false) {
let b = if allow_return {
eval_block_with_early_return(engine_state, stack, &block, input, false, false)
} else {
eval_block(engine_state, stack, &block, input, false, false)
};
match b {
Ok(pipeline_data) => {
let config = engine_state.get_config();
let result;