diff --git a/crates/nu-command/tests/commands/def.rs b/crates/nu-command/tests/commands/def.rs index 0ec13c0511..c66869b479 100644 --- a/crates/nu-command/tests/commands/def.rs +++ b/crates/nu-command/tests/commands/def.rs @@ -292,3 +292,37 @@ fn def_env_wrapped_no_help() { let actual = nu!("def --wrapped foo [...rest] { echo $rest }; foo -h | to json --raw"); assert_eq!(actual.out, r#"["-h"]"#); } + +#[test] +fn def_recursive_func_should_work() { + let actual = nu!("def bar [] { let x = 1; ($x | foo) }; def foo [] { foo }"); + assert!(actual.err.is_empty()); + + let actual = nu!(r#" +def recursive [c: int] { + if ($c == 0) { return } + if ($c mod 2 > 0) { + $in | recursive ($c - 1) + } else { + recursive ($c - 1) + } +}"#); + assert!(actual.err.is_empty()); +} + +#[test] +fn export_def_recursive_func_should_work() { + let actual = nu!("export def bar [] { let x = 1; ($x | foo) }; export def foo [] { foo }"); + assert!(actual.err.is_empty()); + + let actual = nu!(r#" +export def recursive [c: int] { + if ($c == 0) { return } + if ($c mod 2 > 0) { + $in | recursive ($c - 1) + } else { + recursive ($c - 1) + } +}"#); + assert!(actual.err.is_empty()); +} diff --git a/crates/nu-engine/src/compile/call.rs b/crates/nu-engine/src/compile/call.rs index e928b63826..4112e889b1 100644 --- a/crates/nu-engine/src/compile/call.rs +++ b/crates/nu-engine/src/compile/call.rs @@ -71,6 +71,9 @@ pub(crate) fn compile_call( "return" => { return compile_return(working_set, builder, call, redirect_modes, io_reg); } + "def" | "export def" => { + return builder.load_empty(io_reg); + } _ => (), } }