Make IR the default evaluator (#13718)

# Description

Makes IR the default evaluator, in preparation to remove the non-IR
evaluator in a future release.

# User-Facing Changes

* Remove `NU_USE_IR` option
* Add `NU_DISABLE_IR` option
* IR is enabled unless `NU_DISABLE_IR` is set

# After Submitting
- [ ] release notes
This commit is contained in:
Devyn Cairns 2024-09-15 14:54:38 -07:00 committed by GitHub
parent c535c24d03
commit 9ca0fb772d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
19 changed files with 77 additions and 57 deletions

View File

@ -46,8 +46,8 @@ fn setup_stack_and_engine_from_command(command: &str) -> (Stack, EngineState) {
let mut stack = Stack::new(); let mut stack = Stack::new();
// Support running benchmarks with IR mode // Support running benchmarks without IR mode
stack.use_ir = std::env::var_os("NU_USE_IR").is_some(); stack.use_ir = std::env::var_os("NU_DISABLE_IR").is_none();
evaluate_commands( evaluate_commands(
&commands, &commands,

View File

@ -288,9 +288,9 @@ fn loop_iteration(ctx: LoopContext) -> (bool, Stack, Reedline) {
if let Err(err) = engine_state.merge_env(&mut stack, cwd) { if let Err(err) = engine_state.merge_env(&mut stack, cwd) {
report_shell_error(engine_state, &err); report_shell_error(engine_state, &err);
} }
// Check whether $env.NU_USE_IR is set, so that the user can change it in the REPL // Check whether $env.NU_DISABLE_IR is set, so that the user can change it in the REPL
// Temporary while IR eval is optional // Temporary while IR eval is optional
stack.use_ir = stack.has_env_var(engine_state, "NU_USE_IR"); stack.use_ir = !stack.has_env_var(engine_state, "NU_DISABLE_IR");
perf!("merge env", start_time, use_color); perf!("merge env", start_time, use_color);
start_time = std::time::Instant::now(); start_time = std::time::Instant::now();

View File

@ -70,7 +70,7 @@ impl Command for Describe {
Example { Example {
description: "Describe the type of a record in a detailed way", description: "Describe the type of a record in a detailed way",
example: example:
"{shell:'true', uwu:true, features: {bugs:false, multiplatform:true, speed: 10}, fib: [1 1 2 3 5 8], on_save: {|x| print $'Saving ($x)'}, first_commit: 2019-05-10, my_duration: (4min + 20sec)} | describe -d", "{shell:'true', uwu:true, features: {bugs:false, multiplatform:true, speed: 10}, fib: [1 1 2 3 5 8], on_save: {|x| $'Saving ($x)'}, first_commit: 2019-05-10, my_duration: (4min + 20sec)} | describe -d",
result: Some(Value::test_record(record!( result: Some(Value::test_record(record!(
"type" => Value::test_string("record"), "type" => Value::test_string("record"),
"columns" => Value::test_record(record!( "columns" => Value::test_record(record!(

View File

@ -83,7 +83,7 @@ impl Command for Do {
let eval_block_with_early_return = get_eval_block_with_early_return(engine_state); let eval_block_with_early_return = get_eval_block_with_early_return(engine_state);
// Applies to all block evaluation once set true // Applies to all block evaluation once set true
callee_stack.use_ir = caller_stack.has_env_var(engine_state, "NU_USE_IR"); callee_stack.use_ir = !caller_stack.has_env_var(engine_state, "NU_DISABLE_IR");
let result = eval_block_with_early_return(engine_state, &mut callee_stack, block, input); let result = eval_block_with_early_return(engine_state, &mut callee_stack, block, input);

View File

@ -77,18 +77,18 @@ impl Command for Try {
fn examples(&self) -> Vec<Example> { fn examples(&self) -> Vec<Example> {
vec![ vec![
Example { Example {
description: "Try to run a missing command", description: "Try to run a division by zero",
example: "try { asdfasdf }", example: "try { 1 / 0 }",
result: None, result: None,
}, },
Example { Example {
description: "Try to run a missing command", description: "Try to run a division by zero and return a string instead",
example: "try { asdfasdf } catch { 'missing' }", example: "try { 1 / 0 } catch { 'divided by zero' }",
result: Some(Value::test_string("missing")), result: Some(Value::test_string("divided by zero")),
}, },
Example { Example {
description: "Try to run a missing command and report the message", description: "Try to run a division by zero and report the message",
example: "try { asdfasdf } catch { |err| $err.msg }", example: "try { 1 / 0 } catch { |err| $err.msg }",
result: None, result: None,
}, },
] ]

View File

@ -1,10 +1,7 @@
use itertools::Itertools; use itertools::Itertools;
use nu_engine::command_prelude::*; use nu_engine::{command_prelude::*, compile};
use nu_protocol::{ use nu_protocol::{
ast::Block, ast::Block, debugger::WithoutDebug, engine::StateWorkingSet, report_shell_error, Range,
debugger::WithoutDebug,
engine::{StateDelta, StateWorkingSet},
report_shell_error, Range,
}; };
use std::{ use std::{
sync::Arc, sync::Arc,
@ -77,16 +74,25 @@ pub fn eval_pipeline_without_terminal_expression(
cwd: &std::path::Path, cwd: &std::path::Path,
engine_state: &mut Box<EngineState>, engine_state: &mut Box<EngineState>,
) -> Option<Value> { ) -> Option<Value> {
let (mut block, delta) = parse(src, engine_state); let (mut block, mut working_set) = parse(src, engine_state);
if block.pipelines.len() == 1 { if block.pipelines.len() == 1 {
let n_expressions = block.pipelines[0].elements.len(); let n_expressions = block.pipelines[0].elements.len();
Arc::make_mut(&mut block).pipelines[0] // Modify the block to remove the last element and recompile it
.elements {
.truncate(&n_expressions - 1); let mut_block = Arc::make_mut(&mut block);
mut_block.pipelines[0].elements.truncate(n_expressions - 1);
mut_block.ir_block = Some(compile(&working_set, mut_block).expect(
"failed to compile block modified by eval_pipeline_without_terminal_expression",
));
}
working_set.add_block(block.clone());
engine_state
.merge_delta(working_set.render())
.expect("failed to merge delta");
if !block.pipelines[0].elements.is_empty() { if !block.pipelines[0].elements.is_empty() {
let empty_input = PipelineData::empty(); let empty_input = PipelineData::empty();
Some(eval_block(block, empty_input, cwd, engine_state, delta)) Some(eval_block(block, empty_input, cwd, engine_state))
} else { } else {
Some(Value::nothing(Span::test_data())) Some(Value::nothing(Span::test_data()))
} }
@ -96,28 +102,30 @@ pub fn eval_pipeline_without_terminal_expression(
} }
} }
pub fn parse(contents: &str, engine_state: &EngineState) -> (Arc<Block>, StateDelta) { pub fn parse<'engine>(
contents: &str,
engine_state: &'engine EngineState,
) -> (Arc<Block>, StateWorkingSet<'engine>) {
let mut working_set = StateWorkingSet::new(engine_state); let mut working_set = StateWorkingSet::new(engine_state);
let output = nu_parser::parse(&mut working_set, None, contents.as_bytes(), false); let output = nu_parser::parse(&mut working_set, None, contents.as_bytes(), false);
if let Some(err) = working_set.parse_errors.first() { if let Some(err) = working_set.parse_errors.first() {
panic!("test parse error in `{contents}`: {err:?}") panic!("test parse error in `{contents}`: {err:?}");
} }
(output, working_set.render()) if let Some(err) = working_set.compile_errors.first() {
panic!("test compile error in `{contents}`: {err:?}");
}
(output, working_set)
} }
pub fn eval_block( pub fn eval_block(
block: Arc<Block>, block: Arc<Block>,
input: PipelineData, input: PipelineData,
cwd: &std::path::Path, cwd: &std::path::Path,
engine_state: &mut Box<EngineState>, engine_state: &EngineState,
delta: StateDelta,
) -> Value { ) -> Value {
engine_state
.merge_delta(delta)
.expect("Error merging delta");
let mut stack = Stack::new().capture(); let mut stack = Stack::new().capture();
stack.add_env_var("PWD".to_string(), Value::test_string(cwd.to_string_lossy())); stack.add_env_var("PWD".to_string(), Value::test_string(cwd.to_string_lossy()));
@ -191,8 +199,11 @@ fn eval(
cwd: &std::path::Path, cwd: &std::path::Path,
engine_state: &mut Box<EngineState>, engine_state: &mut Box<EngineState>,
) -> Value { ) -> Value {
let (block, delta) = parse(contents, engine_state); let (block, working_set) = parse(contents, engine_state);
eval_block(block, input, cwd, engine_state, delta) engine_state
.merge_delta(working_set.render())
.expect("failed to merge delta");
eval_block(block, input, cwd, engine_state)
} }
pub struct DebuggableValue<'a>(pub &'a Value); pub struct DebuggableValue<'a>(pub &'a Value);

View File

@ -137,7 +137,7 @@ mod test {
use nu_cmd_lang::eval_pipeline_without_terminal_expression; use nu_cmd_lang::eval_pipeline_without_terminal_expression;
use crate::Metadata; use crate::{Get, Metadata};
use super::*; use super::*;
@ -157,6 +157,7 @@ mod test {
working_set.add_decl(Box::new(ToCsv {})); working_set.add_decl(Box::new(ToCsv {}));
working_set.add_decl(Box::new(Metadata {})); working_set.add_decl(Box::new(Metadata {}));
working_set.add_decl(Box::new(Get {}));
working_set.render() working_set.render()
}; };

View File

@ -161,7 +161,7 @@ fn json_list(input: &[Value]) -> Result<Vec<nu_json::Value>, ShellError> {
mod test { mod test {
use nu_cmd_lang::eval_pipeline_without_terminal_expression; use nu_cmd_lang::eval_pipeline_without_terminal_expression;
use crate::Metadata; use crate::{Get, Metadata};
use super::*; use super::*;
@ -182,6 +182,7 @@ mod test {
working_set.add_decl(Box::new(ToJson {})); working_set.add_decl(Box::new(ToJson {}));
working_set.add_decl(Box::new(Metadata {})); working_set.add_decl(Box::new(Metadata {}));
working_set.add_decl(Box::new(Get {}));
working_set.render() working_set.render()
}; };

View File

@ -335,7 +335,7 @@ fn get_padded_string(text: String, desired_length: usize, padding_character: cha
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::Metadata; use crate::{Get, Metadata};
use super::*; use super::*;
use nu_cmd_lang::eval_pipeline_without_terminal_expression; use nu_cmd_lang::eval_pipeline_without_terminal_expression;
@ -474,6 +474,7 @@ mod tests {
working_set.add_decl(Box::new(ToMd {})); working_set.add_decl(Box::new(ToMd {}));
working_set.add_decl(Box::new(Metadata {})); working_set.add_decl(Box::new(Metadata {}));
working_set.add_decl(Box::new(Get {}));
working_set.render() working_set.render()
}; };

View File

@ -275,7 +275,7 @@ where
mod test { mod test {
use nu_cmd_lang::eval_pipeline_without_terminal_expression; use nu_cmd_lang::eval_pipeline_without_terminal_expression;
use crate::Metadata; use crate::{Get, Metadata};
use super::*; use super::*;
@ -296,6 +296,7 @@ mod test {
working_set.add_decl(Box::new(ToMsgpack {})); working_set.add_decl(Box::new(ToMsgpack {}));
working_set.add_decl(Box::new(Metadata {})); working_set.add_decl(Box::new(Metadata {}));
working_set.add_decl(Box::new(Get {}));
working_set.render() working_set.render()
}; };

View File

@ -107,7 +107,7 @@ mod test {
use super::*; use super::*;
use nu_cmd_lang::eval_pipeline_without_terminal_expression; use nu_cmd_lang::eval_pipeline_without_terminal_expression;
use crate::Metadata; use crate::{Get, Metadata};
#[test] #[test]
fn test_examples() { fn test_examples() {
@ -126,6 +126,7 @@ mod test {
working_set.add_decl(Box::new(ToNuon {})); working_set.add_decl(Box::new(ToNuon {}));
working_set.add_decl(Box::new(Metadata {})); working_set.add_decl(Box::new(Metadata {}));
working_set.add_decl(Box::new(Get {}));
working_set.render() working_set.render()
}; };

View File

@ -144,7 +144,7 @@ fn update_metadata(metadata: Option<PipelineMetadata>) -> Option<PipelineMetadat
mod test { mod test {
use nu_cmd_lang::eval_pipeline_without_terminal_expression; use nu_cmd_lang::eval_pipeline_without_terminal_expression;
use crate::Metadata; use crate::{Get, Metadata};
use super::*; use super::*;
@ -165,6 +165,7 @@ mod test {
working_set.add_decl(Box::new(ToText {})); working_set.add_decl(Box::new(ToText {}));
working_set.add_decl(Box::new(Metadata {})); working_set.add_decl(Box::new(Metadata {}));
working_set.add_decl(Box::new(Get {}));
working_set.render() working_set.render()
}; };

View File

@ -102,7 +102,7 @@ fn to_tsv(
mod test { mod test {
use nu_cmd_lang::eval_pipeline_without_terminal_expression; use nu_cmd_lang::eval_pipeline_without_terminal_expression;
use crate::Metadata; use crate::{Get, Metadata};
use super::*; use super::*;
@ -123,6 +123,7 @@ mod test {
working_set.add_decl(Box::new(ToTsv {})); working_set.add_decl(Box::new(ToTsv {}));
working_set.add_decl(Box::new(Metadata {})); working_set.add_decl(Box::new(Metadata {}));
working_set.add_decl(Box::new(Get {}));
working_set.render() working_set.render()
}; };

View File

@ -514,7 +514,7 @@ impl Job {
mod test { mod test {
use nu_cmd_lang::eval_pipeline_without_terminal_expression; use nu_cmd_lang::eval_pipeline_without_terminal_expression;
use crate::Metadata; use crate::{Get, Metadata};
use super::*; use super::*;
@ -535,6 +535,7 @@ mod test {
working_set.add_decl(Box::new(ToXml {})); working_set.add_decl(Box::new(ToXml {}));
working_set.add_decl(Box::new(Metadata {})); working_set.add_decl(Box::new(Metadata {}));
working_set.add_decl(Box::new(Get {}));
working_set.render() working_set.render()
}; };

View File

@ -125,7 +125,7 @@ fn to_yaml(input: PipelineData, head: Span) -> Result<PipelineData, ShellError>
mod test { mod test {
use nu_cmd_lang::eval_pipeline_without_terminal_expression; use nu_cmd_lang::eval_pipeline_without_terminal_expression;
use crate::Metadata; use crate::{Get, Metadata};
use super::*; use super::*;
@ -146,6 +146,7 @@ mod test {
working_set.add_decl(Box::new(ToYaml {})); working_set.add_decl(Box::new(ToYaml {}));
working_set.add_decl(Box::new(Metadata {})); working_set.add_decl(Box::new(Metadata {}));
working_set.add_decl(Box::new(Get {}));
working_set.render() working_set.render()
}; };

View File

@ -78,7 +78,7 @@ impl Stack {
active_overlays: vec![DEFAULT_OVERLAY_NAME.to_string()], active_overlays: vec![DEFAULT_OVERLAY_NAME.to_string()],
arguments: ArgumentStack::new(), arguments: ArgumentStack::new(),
error_handlers: ErrorHandlerStack::new(), error_handlers: ErrorHandlerStack::new(),
use_ir: false, use_ir: true,
recursion_count: 0, recursion_count: 0,
parent_stack: None, parent_stack: None,
parent_deletions: vec![], parent_deletions: vec![],

View File

@ -290,12 +290,12 @@ pub fn nu_run_test(opts: NuOpts, commands: impl AsRef<str>, with_std: bool) -> O
.stdout(Stdio::piped()) .stdout(Stdio::piped())
.stderr(Stdio::piped()); .stderr(Stdio::piped());
// Explicitly set NU_USE_IR // Explicitly set NU_DISABLE_IR
if let Some(use_ir) = opts.use_ir { if let Some(use_ir) = opts.use_ir {
if use_ir { if !use_ir {
command.env("NU_USE_IR", "1"); command.env("NU_DISABLE_IR", "1");
} else { } else {
command.env_remove("NU_USE_IR"); command.env_remove("NU_DISABLE_IR");
} }
} }

View File

@ -26,8 +26,8 @@ pub(crate) fn run_commands(
let mut stack = Stack::new(); let mut stack = Stack::new();
let start_time = std::time::Instant::now(); let start_time = std::time::Instant::now();
if stack.has_env_var(engine_state, "NU_USE_IR") { if stack.has_env_var(engine_state, "NU_DISABLE_IR") {
stack.use_ir = true; stack.use_ir = false;
} }
// if the --no-config-file(-n) option is NOT passed, load the plugin file, // if the --no-config-file(-n) option is NOT passed, load the plugin file,
@ -115,8 +115,8 @@ pub(crate) fn run_file(
trace!("run_file"); trace!("run_file");
let mut stack = Stack::new(); let mut stack = Stack::new();
if stack.has_env_var(engine_state, "NU_USE_IR") { if stack.has_env_var(engine_state, "NU_DISABLE_IR") {
stack.use_ir = true; stack.use_ir = false;
} }
// if the --no-config-file(-n) option is NOT passed, load the plugin file, // if the --no-config-file(-n) option is NOT passed, load the plugin file,
@ -184,8 +184,8 @@ pub(crate) fn run_repl(
let mut stack = Stack::new(); let mut stack = Stack::new();
let start_time = std::time::Instant::now(); let start_time = std::time::Instant::now();
if stack.has_env_var(engine_state, "NU_USE_IR") { if stack.has_env_var(engine_state, "NU_DISABLE_IR") {
stack.use_ir = true; stack.use_ir = false;
} }
if parsed_nu_cli_args.no_config_file.is_none() { if parsed_nu_cli_args.no_config_file.is_none() {

View File

@ -244,9 +244,9 @@ pub fn nu_repl() {
engine_state.add_env_var("PWD".into(), Value::test_string(cwd.to_string_lossy())); engine_state.add_env_var("PWD".into(), Value::test_string(cwd.to_string_lossy()));
engine_state.add_env_var("PATH".into(), Value::test_string("")); engine_state.add_env_var("PATH".into(), Value::test_string(""));
// Enable IR in tests if set // Disable IR in tests if set
if std::env::var_os("NU_USE_IR").is_some() { if std::env::var_os("NU_DISABLE_IR").is_some() {
Arc::make_mut(&mut top_stack).use_ir = true; Arc::make_mut(&mut top_stack).use_ir = false;
} }
let mut last_output = String::new(); let mut last_output = String::new();