From 1b89ccf25b7fc0698125b8751716053f9a5e2c92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20=C5=BD=C3=A1dn=C3=ADk?= Date: Mon, 27 Sep 2021 20:36:44 +0300 Subject: [PATCH 01/17] Add comment --- crates/nu-parser/src/parse_keywords.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/nu-parser/src/parse_keywords.rs b/crates/nu-parser/src/parse_keywords.rs index e516da309b..088bf57ac5 100644 --- a/crates/nu-parser/src/parse_keywords.rs +++ b/crates/nu-parser/src/parse_keywords.rs @@ -309,6 +309,7 @@ pub fn parse_module( if err.is_none() { let decl_name = + // parts[1] is safe since it's checked in parse_def already working_set.get_span_contents(pipeline.commands[0].parts[1]); let decl_id = working_set From 561feff36550d00ad77545d865d271380eb55433 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20=C5=BD=C3=A1dn=C3=ADk?= Date: Tue, 28 Sep 2021 20:29:38 +0300 Subject: [PATCH 02/17] Introduce 'export' keyword --- crates/nu-parser/src/parse_keywords.rs | 53 ++++++++++++++++++++++++-- 1 file changed, 49 insertions(+), 4 deletions(-) diff --git a/crates/nu-parser/src/parse_keywords.rs b/crates/nu-parser/src/parse_keywords.rs index 088bf57ac5..7c8ed36b6c 100644 --- a/crates/nu-parser/src/parse_keywords.rs +++ b/crates/nu-parser/src/parse_keywords.rs @@ -16,6 +16,13 @@ use crate::{ pub fn parse_def_predecl(working_set: &mut StateWorkingSet, spans: &[Span]) { let name = working_set.get_span_contents(spans[0]); + // handle "export def" same as "def" + let (name, spans) = if name == b"export" && spans.len() >= 2 { + (working_set.get_span_contents(spans[1]), &spans[1..]) + } else { + (name, spans) + }; + if name == b"def" && spans.len() >= 4 { let (name_expr, ..) = parse_string(working_set, spans[1]); let name = name_expr.as_string(); @@ -218,6 +225,38 @@ pub fn parse_alias( ) } +pub fn parse_export( + working_set: &mut StateWorkingSet, + spans: &[Span], +) -> (Statement, Option) { + let bytes = working_set.get_span_contents(spans[0]); + + if bytes == b"export" && spans.len() >= 3 { + let export_name = working_set.get_span_contents(spans[1]); + + match export_name { + b"def" => parse_def(working_set, &spans[1..]), + _ => ( + garbage_statement(spans), + Some(ParseError::Expected( + // TODO: Fill in more as they come + "def keyword".into(), + spans[1], + )), + ), + } + } else { + ( + garbage_statement(spans), + Some(ParseError::UnknownState( + // TODO: fill in more as they come + "Expected structure: export def [] {}".into(), + span(spans), + )), + ) + } +} + pub fn parse_module( working_set: &mut StateWorkingSet, spans: &[Span], @@ -307,16 +346,21 @@ pub fn parse_module( b"def" => { let (stmt, err) = parse_def(working_set, &pipeline.commands[0].parts); + (stmt, err) + } + b"export" => { + let (stmt, err) = + parse_export(working_set, &pipeline.commands[0].parts); + if err.is_none() { let decl_name = - // parts[1] is safe since it's checked in parse_def already - working_set.get_span_contents(pipeline.commands[0].parts[1]); + // parts[2] is safe since it's checked in parse_def already + working_set.get_span_contents(pipeline.commands[0].parts[2]); let decl_id = working_set .find_decl(decl_name) .expect("internal error: failed to find added declaration"); - // TODO: Later, we want to put this behind 'export' exports.push((decl_name.into(), decl_id)); } @@ -325,7 +369,8 @@ pub fn parse_module( _ => ( garbage_statement(&pipeline.commands[0].parts), Some(ParseError::Expected( - "def".into(), + // TODO: Fill in more as they com + "def or export keyword".into(), pipeline.commands[0].parts[0], )), ), From 93521da9d8c64827d950147b4f2138033420f92d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20=C5=BD=C3=A1dn=C3=ADk?= Date: Tue, 28 Sep 2021 21:03:53 +0300 Subject: [PATCH 03/17] Add 'export def' command --- crates/nu-command/src/default_context.rs | 10 ++++++- crates/nu-command/src/export_def.rs | 35 ++++++++++++++++++++++++ crates/nu-parser/src/parse_keywords.rs | 35 +++++++++++++++++++++++- 3 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 crates/nu-command/src/export_def.rs diff --git a/crates/nu-command/src/default_context.rs b/crates/nu-command/src/default_context.rs index 609d0cc683..4140ca1a4d 100644 --- a/crates/nu-command/src/default_context.rs +++ b/crates/nu-command/src/default_context.rs @@ -6,7 +6,7 @@ use nu_protocol::{ }; use crate::{ - Alias, Benchmark, BuildString, Def, Do, Each, External, For, From, FromJson, Git, GitCheckout, + Alias, Benchmark, BuildString, Def, Do, Each, ExportDef, External, For, From, FromJson, Git, GitCheckout, If, Length, Let, LetEnv, Lines, ListGitBranches, Ls, Module, Sys, Table, Use, Where, }; @@ -20,12 +20,20 @@ pub fn create_default_context() -> Rc> { Signature::build("where").required("cond", SyntaxShape::RowCondition, "condition"); working_set.add_decl(sig.predeclare()); + working_set.add_decl(Box::new(If)); + + working_set.add_decl(Box::new(Let)); + + working_set.add_decl(Box::new(LetEnv)); + working_set.add_decl(Box::new(Alias)); working_set.add_decl(Box::new(Benchmark)); working_set.add_decl(Box::new(BuildString)); + working_set.add_decl(Box::new(Def)); working_set.add_decl(Box::new(Do)); working_set.add_decl(Box::new(Each)); + working_set.add_decl(Box::new(ExportDef)); working_set.add_decl(Box::new(External)); working_set.add_decl(Box::new(For)); working_set.add_decl(Box::new(From)); diff --git a/crates/nu-command/src/export_def.rs b/crates/nu-command/src/export_def.rs new file mode 100644 index 0000000000..b82418c486 --- /dev/null +++ b/crates/nu-command/src/export_def.rs @@ -0,0 +1,35 @@ +use nu_protocol::ast::Call; +use nu_protocol::engine::{Command, EvaluationContext}; +use nu_protocol::{Signature, SyntaxShape, Value}; + +pub struct ExportDef; + +impl Command for ExportDef { + fn name(&self) -> &str { + "export def" + } + + fn usage(&self) -> &str { + "Define a custom command and export it from a module" + } + + fn signature(&self) -> nu_protocol::Signature { + Signature::build("export def") + .required("target", SyntaxShape::String, "definition name") + .required("params", SyntaxShape::Signature, "parameters") + .required( + "block", + SyntaxShape::Block(Some(vec![])), + "body of the definition", + ) + } + + fn run( + &self, + _context: &EvaluationContext, + call: &Call, + _input: Value, + ) -> Result { + Ok(Value::Nothing { span: call.head }) + } +} diff --git a/crates/nu-parser/src/parse_keywords.rs b/crates/nu-parser/src/parse_keywords.rs index 7c8ed36b6c..787de1b53b 100644 --- a/crates/nu-parser/src/parse_keywords.rs +++ b/crates/nu-parser/src/parse_keywords.rs @@ -235,7 +235,40 @@ pub fn parse_export( let export_name = working_set.get_span_contents(spans[1]); match export_name { - b"def" => parse_def(working_set, &spans[1..]), + b"def" => { + let (stmt, err) = parse_def(working_set, &spans[1..]); + + let export_def_decl_id = working_set + .find_decl(b"export def") + .expect("internal error: missing 'export def' command"); + + // Trying to warp the 'def' call into the 'export def' in a very clumsy way + let stmt = if let Statement::Pipeline(ref pipe) = stmt { + if !pipe.expressions.is_empty() { + if let Expr::Call(ref call) = pipe.expressions[0].expr { + let mut call = call.clone(); + + call.head = span(&spans[0..=1]); + call.decl_id = export_def_decl_id; + + Statement::Pipeline(Pipeline::from_vec(vec![Expression { + expr: Expr::Call(call), + span: span(spans), + ty: Type::Unknown, + custom_completion: None, + }])) + } else { + stmt + } + } else { + stmt + } + } else { + stmt + }; + + (stmt, err) + } _ => ( garbage_statement(spans), Some(ParseError::Expected( From 3cbf99053f938c0ab23cdb84ff65b81da1465b51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20=C5=BD=C3=A1dn=C3=ADk?= Date: Tue, 28 Sep 2021 21:12:46 +0300 Subject: [PATCH 04/17] Throw an error if using export outside of module --- crates/nu-parser/src/errors.rs | 8 ++++++++ crates/nu-parser/src/parser.rs | 4 ++++ 2 files changed, 12 insertions(+) diff --git a/crates/nu-parser/src/errors.rs b/crates/nu-parser/src/errors.rs index 58123aac0d..f55fed2da7 100644 --- a/crates/nu-parser/src/errors.rs +++ b/crates/nu-parser/src/errors.rs @@ -57,6 +57,14 @@ pub enum ParseError { #[diagnostic(code(nu::parser::expected_keyword), url(docsrs))] ExpectedKeyword(String, #[label("expected {0}")] Span), + #[error("Unexpected keyword.")] + #[diagnostic( + code(nu::parser::unexpected_keyword), + url(docsrs), + help("'export' keyword is allowed only in a module.") + )] + UnexpectedKeyword(String, #[label("unexpected {0}")] Span), + #[error("Multiple rest params.")] #[diagnostic(code(nu::parser::multiple_rest_params), url(docsrs))] MultipleRestParams(#[label = "multiple rest params"] Span), diff --git a/crates/nu-parser/src/parser.rs b/crates/nu-parser/src/parser.rs index 59508cdfe2..daff7c6054 100644 --- a/crates/nu-parser/src/parser.rs +++ b/crates/nu-parser/src/parser.rs @@ -2585,6 +2585,10 @@ pub fn parse_statement( b"alias" => parse_alias(working_set, spans), b"module" => parse_module(working_set, spans), b"use" => parse_use(working_set, spans), + b"export" => ( + garbage_statement(spans), + Some(ParseError::UnexpectedKeyword("export".into(), spans[0])), + ), _ => { let (expr, err) = parse_expression(working_set, spans); (Statement::Pipeline(Pipeline::from_vec(vec![expr])), err) From 7488254cca6ff6140f8fe4c8439f6a6295da204e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20=C5=BD=C3=A1dn=C3=ADk?= Date: Tue, 28 Sep 2021 23:18:48 +0300 Subject: [PATCH 05/17] Implement a rough version of 'hide' 'hide' command is used to undefine custom commands --- crates/nu-command/src/core_commands/use_.rs | 2 +- crates/nu-command/src/default_context.rs | 9 +- crates/nu-parser/src/parse_keywords.rs | 63 +++++++++++++- crates/nu-parser/src/parser.rs | 3 +- crates/nu-protocol/src/engine/engine_state.rs | 86 ++++++++++++++++++- 5 files changed, 150 insertions(+), 13 deletions(-) diff --git a/crates/nu-command/src/core_commands/use_.rs b/crates/nu-command/src/core_commands/use_.rs index 30b5e3d0b0..3cfae2e49d 100644 --- a/crates/nu-command/src/core_commands/use_.rs +++ b/crates/nu-command/src/core_commands/use_.rs @@ -14,7 +14,7 @@ impl Command for Use { } fn signature(&self) -> nu_protocol::Signature { - Signature::build("use").required("module_name", SyntaxShape::String, "module name") + Signature::build("use").required("pattern", SyntaxShape::String, "import pattern") } fn run( diff --git a/crates/nu-command/src/default_context.rs b/crates/nu-command/src/default_context.rs index 4140ca1a4d..42b30cf877 100644 --- a/crates/nu-command/src/default_context.rs +++ b/crates/nu-command/src/default_context.rs @@ -7,7 +7,7 @@ use nu_protocol::{ use crate::{ Alias, Benchmark, BuildString, Def, Do, Each, ExportDef, External, For, From, FromJson, Git, GitCheckout, - If, Length, Let, LetEnv, Lines, ListGitBranches, Ls, Module, Sys, Table, Use, Where, + Hide, If, Length, Let, LetEnv, Lines, ListGitBranches, Ls, Module, Sys, Table, Use, Where, }; pub fn create_default_context() -> Rc> { @@ -29,8 +29,8 @@ pub fn create_default_context() -> Rc> { working_set.add_decl(Box::new(Alias)); working_set.add_decl(Box::new(Benchmark)); working_set.add_decl(Box::new(BuildString)); - - working_set.add_decl(Box::new(Def)); + + working_set.add_decl(Box::new(Def)); working_set.add_decl(Box::new(Do)); working_set.add_decl(Box::new(Each)); working_set.add_decl(Box::new(ExportDef)); @@ -38,6 +38,7 @@ pub fn create_default_context() -> Rc> { working_set.add_decl(Box::new(For)); working_set.add_decl(Box::new(From)); working_set.add_decl(Box::new(FromJson)); + working_set.add_decl(Box::new(Hide)); working_set.add_decl(Box::new(If)); working_set.add_decl(Box::new(Length)); working_set.add_decl(Box::new(Let)); @@ -48,7 +49,7 @@ pub fn create_default_context() -> Rc> { working_set.add_decl(Box::new(Sys)); working_set.add_decl(Box::new(Table)); working_set.add_decl(Box::new(Use)); - working_set.add_decl(Box::new(Where)); + working_set.add_decl(Box::new(Where)); // This is a WIP proof of concept working_set.add_decl(Box::new(ListGitBranches)); diff --git a/crates/nu-parser/src/parse_keywords.rs b/crates/nu-parser/src/parse_keywords.rs index 787de1b53b..c27c66d637 100644 --- a/crates/nu-parser/src/parse_keywords.rs +++ b/crates/nu-parser/src/parse_keywords.rs @@ -472,8 +472,6 @@ pub fn parse_use( let mut error = None; let bytes = working_set.get_span_contents(spans[0]); - // TODO: Currently, this directly imports the module's definitions into the current scope. - // Later, we want to put them behind the module's name and add selective importing if bytes == b"use" && spans.len() >= 2 { let (module_name_expr, err) = parse_string(working_set, spans[1]); error = error.or(err); @@ -482,8 +480,6 @@ pub fn parse_use( error = error.or(err); let exports = if let Some(block_id) = working_set.find_module(&import_pattern.head) { - // TODO: Since we don't use the Block at all, we might just as well create a separate - // Module that holds only the exports, without having Blocks in the way. working_set.get_block(block_id).exports.clone() } else { return ( @@ -571,6 +567,65 @@ pub fn parse_use( } } +pub fn parse_hide( + working_set: &mut StateWorkingSet, + spans: &[Span], +) -> (Statement, Option) { + let mut error = None; + let bytes = working_set.get_span_contents(spans[0]); + + if bytes == b"hide" && spans.len() >= 2 { + let (name_expr, err) = parse_string(working_set, spans[1]); + error = error.or(err); + + let name_bytes: Vec = working_set.get_span_contents(spans[1]).into(); + + // TODO: Do the import pattern stuff for bulk-hiding + // TODO: move this error into error = error.or pattern + let _decl_id = if let Some(id) = working_set.find_decl(&name_bytes) { + id + } else { + return ( + garbage_statement(spans), + Some(ParseError::UnknownCommand(spans[1])), + ); + }; + + // Hide the definitions + working_set.hide_decl(name_bytes); + + // Create the Hide command call + let hide_decl_id = working_set + .find_decl(b"hide") + .expect("internal error: missing hide command"); + + let call = Box::new(Call { + head: spans[0], + decl_id: hide_decl_id, + positional: vec![name_expr], + named: vec![], + }); + + ( + Statement::Pipeline(Pipeline::from_vec(vec![Expression { + expr: Expr::Call(call), + span: span(spans), + ty: Type::Unknown, + custom_completion: None, + }])), + error, + ) + } else { + ( + garbage_statement(spans), + Some(ParseError::UnknownState( + "Expected structure: hide ".into(), + span(spans), + )), + ) + } +} + pub fn parse_let( working_set: &mut StateWorkingSet, spans: &[Span], diff --git a/crates/nu-parser/src/parser.rs b/crates/nu-parser/src/parser.rs index daff7c6054..d3464869f4 100644 --- a/crates/nu-parser/src/parser.rs +++ b/crates/nu-parser/src/parser.rs @@ -14,7 +14,7 @@ use nu_protocol::{ }; use crate::parse_keywords::{ - parse_alias, parse_def, parse_def_predecl, parse_let, parse_module, parse_use, + parse_alias, parse_def, parse_def_predecl, parse_hide, parse_let, parse_module, parse_use, }; #[derive(Debug, Clone)] @@ -2589,6 +2589,7 @@ pub fn parse_statement( garbage_statement(spans), Some(ParseError::UnexpectedKeyword("export".into(), spans[0])), ), + b"hide" => parse_hide(working_set, spans), _ => { let (expr, err) = parse_expression(working_set, spans); (Statement::Pipeline(Pipeline::from_vec(vec![expr])), err) diff --git a/crates/nu-protocol/src/engine/engine_state.rs b/crates/nu-protocol/src/engine/engine_state.rs index f466318970..9ddb768449 100644 --- a/crates/nu-protocol/src/engine/engine_state.rs +++ b/crates/nu-protocol/src/engine/engine_state.rs @@ -18,6 +18,7 @@ pub struct ScopeFrame { decls: HashMap, DeclId>, aliases: HashMap, Vec>, modules: HashMap, BlockId>, + hiding: HashMap, usize>, // defines what is being hidden and its "hiding strength" } impl ScopeFrame { @@ -27,6 +28,7 @@ impl ScopeFrame { decls: HashMap::new(), aliases: HashMap::new(), modules: HashMap::new(), + hiding: HashMap::new(), } } @@ -81,6 +83,9 @@ impl EngineState { for item in first.modules.into_iter() { last.modules.insert(item.0, item.1); } + for item in first.hiding.into_iter() { + last.hiding.insert(item.0, item.1); + } } } @@ -124,9 +129,23 @@ impl EngineState { } pub fn find_decl(&self, name: &[u8]) -> Option { + let mut hiding_strength = 0; + // println!("state: starting finding {}", String::from_utf8_lossy(&name)); + for scope in self.scope.iter().rev() { + // println!("hiding map: {:?}", scope.hiding); + // check if we're hiding the declin this scope + if let Some(strength) = scope.hiding.get(name) { + hiding_strength += strength; + } + if let Some(decl_id) = scope.decls.get(name) { - return Some(*decl_id); + // if we're hiding this decl, do not return it and reduce the hiding strength + if hiding_strength > 0 { + hiding_strength -= 1; + } else { + return Some(*decl_id); + } } } @@ -243,10 +262,12 @@ impl StateDelta { } pub fn enter_scope(&mut self) { + // println!("enter scope"); self.scope.push(ScopeFrame::new()); } pub fn exit_scope(&mut self) { + // println!("exit scope"); self.scope.pop(); } } @@ -280,6 +301,7 @@ impl<'a> StateWorkingSet<'a> { pub fn add_decl(&mut self, decl: Box) -> DeclId { let name = decl.name().as_bytes().to_vec(); + // println!("adding {}", String::from_utf8_lossy(&name)); self.delta.decls.push(decl); let decl_id = self.num_decls() - 1; @@ -289,11 +311,36 @@ impl<'a> StateWorkingSet<'a> { .scope .last_mut() .expect("internal error: missing required scope frame"); + + // reset "hiding strength" to 0 => not hidden + if let Some(strength) = scope_frame.hiding.get_mut(&name) { + *strength = 0; + // println!(" strength: {}", strength); + } + scope_frame.decls.insert(name, decl_id); decl_id } + pub fn hide_decl(&mut self, name: Vec) { + let scope_frame = self + .delta + .scope + .last_mut() + .expect("internal error: missing required scope frame"); + + if let Some(strength) = scope_frame.hiding.get_mut(&name) { + *strength += 1; + // println!("hiding {}, strength: {}", String::from_utf8_lossy(&name), strength); + } else { + // println!("hiding {}, strength: 1", String::from_utf8_lossy(&name)); + scope_frame.hiding.insert(name, 1); + } + + // println!("hiding map: {:?}", scope_frame.hiding); + } + pub fn add_block(&mut self, block: Block) -> BlockId { self.delta.blocks.push(block); @@ -401,15 +448,48 @@ impl<'a> StateWorkingSet<'a> { } pub fn find_decl(&self, name: &[u8]) -> Option { + let mut hiding_strength = 0; + // println!("set: starting finding {}", String::from_utf8_lossy(&name)); + for scope in self.delta.scope.iter().rev() { + // println!("delta frame"); + // println!("hiding map: {:?}", scope.hiding); + // check if we're hiding the declin this scope + if let Some(strength) = scope.hiding.get(name) { + hiding_strength += strength; + // println!(" was hiding, strength {}", hiding_strength); + } + if let Some(decl_id) = scope.decls.get(name) { - return Some(*decl_id); + // if we're hiding this decl, do not return it and reduce the hiding strength + if hiding_strength > 0 { + hiding_strength -= 1; + // println!(" decl found, strength {}", hiding_strength); + } else { + // println!(" decl found, return"); + return Some(*decl_id); + } } } for scope in self.permanent_state.scope.iter().rev() { + // println!("perma frame"); + // println!("hiding map: {:?}", scope.hiding); + // check if we're hiding the declin this scope + if let Some(strength) = scope.hiding.get(name) { + hiding_strength += strength; + // println!(" was hiding, strength {}", hiding_strength); + } + if let Some(decl_id) = scope.decls.get(name) { - return Some(*decl_id); + // if we're hiding this decl, do not return it and reduce the hiding strength + if hiding_strength > 0 { + hiding_strength -= 1; + // println!(" decl found, strength {}", hiding_strength); + } else { + // println!(" decl found, return"); + return Some(*decl_id); + } } } From 244289c9012e1936b525f7c74ad7186176b9f1d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20=C5=BD=C3=A1dn=C3=ADk?= Date: Tue, 28 Sep 2021 23:32:15 +0300 Subject: [PATCH 06/17] Add missing file --- crates/nu-command/src/hide.rs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 crates/nu-command/src/hide.rs diff --git a/crates/nu-command/src/hide.rs b/crates/nu-command/src/hide.rs new file mode 100644 index 0000000000..9c9d611e12 --- /dev/null +++ b/crates/nu-command/src/hide.rs @@ -0,0 +1,28 @@ +use nu_protocol::ast::Call; +use nu_protocol::engine::{Command, EvaluationContext}; +use nu_protocol::{Signature, SyntaxShape, Value}; + +pub struct Hide; + +impl Command for Hide { + fn name(&self) -> &str { + "hide" + } + + fn usage(&self) -> &str { + "Hide definitions in the current scope" + } + + fn signature(&self) -> nu_protocol::Signature { + Signature::build("hide").required("pattern", SyntaxShape::String, "import pattern") + } + + fn run( + &self, + _context: &EvaluationContext, + call: &Call, + _input: Value, + ) -> Result { + Ok(Value::Nothing { span: call.head }) + } +} From 8ed6afe1e568e18efc3e27cf6aba38d049f7f7a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20=C5=BD=C3=A1dn=C3=ADk?= Date: Tue, 28 Sep 2021 23:40:03 +0300 Subject: [PATCH 07/17] Fix tests failing without export --- src/tests.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/tests.rs b/src/tests.rs index f47daaee27..77e888a893 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -346,7 +346,7 @@ fn better_block_types() -> TestResult { #[test] fn module_imports_1() -> TestResult { run_test( - r#"module foo { def a [] { 1 }; def b [] { 2 } }; use foo; foo.a"#, + r#"module foo { export def a [] { 1 }; def b [] { 2 } }; use foo; foo.a"#, "1", ) } @@ -354,7 +354,7 @@ fn module_imports_1() -> TestResult { #[test] fn module_imports_2() -> TestResult { run_test( - r#"module foo { def a [] { 1 }; def b [] { 2 } }; use foo.a; a"#, + r#"module foo { export def a [] { 1 }; def b [] { 2 } }; use foo.a; a"#, "1", ) } @@ -362,7 +362,7 @@ fn module_imports_2() -> TestResult { #[test] fn module_imports_3() -> TestResult { run_test( - r#"module foo { def a [] { 1 }; def b [] { 2 } }; use foo.*; b"#, + r#"module foo { export def a [] { 1 }; export def b [] { 2 } }; use foo.*; b"#, "2", ) } @@ -370,7 +370,7 @@ fn module_imports_3() -> TestResult { #[test] fn module_imports_4() -> TestResult { fail_test( - r#"module foo { def a [] { 1 }; def b [] { 2 } }; use foo.c"#, + r#"module foo { export def a [] { 1 }; export def b [] { 2 } }; use foo.c"#, "not find import", ) } @@ -378,7 +378,7 @@ fn module_imports_4() -> TestResult { #[test] fn module_imports_5() -> TestResult { run_test( - r#"module foo { def a [] { 1 }; def b [] { 2 }; def c [] { 3 } }; use foo.[a, c]; c"#, + r#"module foo { export def a [] { 1 }; def b [] { 2 }; export def c [] { 3 } }; use foo.[a, c]; c"#, "3", ) } From aa06a71e1ffd7e7babf46a886ebdd1744817d04d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20=C5=BD=C3=A1dn=C3=ADk?= Date: Wed, 29 Sep 2021 21:56:59 +0300 Subject: [PATCH 08/17] Move new commands to the new structure --- crates/nu-command/src/{ => core_commands}/export_def.rs | 0 crates/nu-command/src/{ => core_commands}/hide.rs | 0 crates/nu-command/src/core_commands/mod.rs | 4 ++++ 3 files changed, 4 insertions(+) rename crates/nu-command/src/{ => core_commands}/export_def.rs (100%) rename crates/nu-command/src/{ => core_commands}/hide.rs (100%) diff --git a/crates/nu-command/src/export_def.rs b/crates/nu-command/src/core_commands/export_def.rs similarity index 100% rename from crates/nu-command/src/export_def.rs rename to crates/nu-command/src/core_commands/export_def.rs diff --git a/crates/nu-command/src/hide.rs b/crates/nu-command/src/core_commands/hide.rs similarity index 100% rename from crates/nu-command/src/hide.rs rename to crates/nu-command/src/core_commands/hide.rs diff --git a/crates/nu-command/src/core_commands/mod.rs b/crates/nu-command/src/core_commands/mod.rs index ac2d4af449..a16b748afd 100644 --- a/crates/nu-command/src/core_commands/mod.rs +++ b/crates/nu-command/src/core_commands/mod.rs @@ -1,6 +1,8 @@ mod alias; mod def; mod do_; +mod export_def; +mod hide; mod if_; mod let_; mod module; @@ -9,6 +11,8 @@ mod use_; pub use alias::Alias; pub use def::Def; pub use do_::Do; +pub use export_def::ExportDef; +pub use hide::Hide; pub use if_::If; pub use let_::Let; pub use module::Module; From 2af8116f509832112eb88953bda3f365769001c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20=C5=BD=C3=A1dn=C3=ADk?= Date: Fri, 1 Oct 2021 22:29:24 +0300 Subject: [PATCH 09/17] Fix hiding logic; Fix hiding with predecls * Hiding logic is simplified and fixed so you can hide and unhide the same def repeatedly. * Separates predeclared ids into its own data structure to protect them from hiding. Otherwise, you could hide the predeclared variable and the actual def would panic. --- crates/nu-parser/src/parse_keywords.rs | 35 +++-- crates/nu-protocol/src/engine/engine_state.rs | 135 +++++++++--------- 2 files changed, 89 insertions(+), 81 deletions(-) diff --git a/crates/nu-parser/src/parse_keywords.rs b/crates/nu-parser/src/parse_keywords.rs index c27c66d637..3195acd785 100644 --- a/crates/nu-parser/src/parse_keywords.rs +++ b/crates/nu-parser/src/parse_keywords.rs @@ -42,7 +42,7 @@ pub fn parse_def_predecl(working_set: &mut StateWorkingSet, spans: &[Span]) { signature.name = name; let decl = signature.predeclare(); - working_set.add_decl(decl); + working_set.add_predecl(decl); } } } @@ -95,15 +95,15 @@ pub fn parse_def( call.positional.push(block); if let (Some(name), Some(mut signature), Some(block_id)) = - (name, signature, block_id) + (&name, signature, block_id) { let decl_id = working_set - .find_decl(name.as_bytes()) + .find_predecl(name.as_bytes()) .expect("internal error: predeclaration failed to add definition"); let declaration = working_set.get_decl_mut(decl_id); - signature.name = name; + signature.name = name.clone(); *declaration = signature.into_block_command(block_id); } @@ -118,6 +118,19 @@ pub fn parse_def( } working_set.exit_scope(); + if let Some(name) = name { + // It's OK if it returns None: The decl was already merged in previous parse + // pass. + working_set.merge_predecl(name.as_bytes()); + } else { + error = error.or_else(|| { + Some(ParseError::UnknownState( + "Could not get string from string expression".into(), + *name_span, + )) + }); + } + call } else { let err_span = Span { @@ -581,18 +594,10 @@ pub fn parse_hide( let name_bytes: Vec = working_set.get_span_contents(spans[1]).into(); // TODO: Do the import pattern stuff for bulk-hiding - // TODO: move this error into error = error.or pattern - let _decl_id = if let Some(id) = working_set.find_decl(&name_bytes) { - id - } else { - return ( - garbage_statement(spans), - Some(ParseError::UnknownCommand(spans[1])), - ); - }; - // Hide the definitions - working_set.hide_decl(name_bytes); + if working_set.hide_decl(&name_bytes).is_none() { + error = error.or_else(|| Some(ParseError::UnknownCommand(spans[1]))); + } // Create the Hide command call let hide_decl_id = working_set diff --git a/crates/nu-protocol/src/engine/engine_state.rs b/crates/nu-protocol/src/engine/engine_state.rs index 9ddb768449..e1dcb7024d 100644 --- a/crates/nu-protocol/src/engine/engine_state.rs +++ b/crates/nu-protocol/src/engine/engine_state.rs @@ -1,7 +1,10 @@ use super::Command; use crate::{ast::Block, BlockId, DeclId, Span, Type, VarId}; use core::panic; -use std::{collections::HashMap, slice::Iter}; +use std::{ + collections::{HashMap, HashSet}, + slice::Iter, +}; pub struct EngineState { files: Vec<(String, usize, usize)>, @@ -18,7 +21,7 @@ pub struct ScopeFrame { decls: HashMap, DeclId>, aliases: HashMap, Vec>, modules: HashMap, BlockId>, - hiding: HashMap, usize>, // defines what is being hidden and its "hiding strength" + hiding: HashSet, } impl ScopeFrame { @@ -28,7 +31,7 @@ impl ScopeFrame { decls: HashMap::new(), aliases: HashMap::new(), modules: HashMap::new(), - hiding: HashMap::new(), + hiding: HashSet::new(), } } @@ -84,7 +87,7 @@ impl EngineState { last.modules.insert(item.0, item.1); } for item in first.hiding.into_iter() { - last.hiding.insert(item.0, item.1); + last.hiding.insert(item); } } } @@ -129,21 +132,13 @@ impl EngineState { } pub fn find_decl(&self, name: &[u8]) -> Option { - let mut hiding_strength = 0; - // println!("state: starting finding {}", String::from_utf8_lossy(&name)); + let mut hiding: HashSet = HashSet::new(); for scope in self.scope.iter().rev() { - // println!("hiding map: {:?}", scope.hiding); - // check if we're hiding the declin this scope - if let Some(strength) = scope.hiding.get(name) { - hiding_strength += strength; - } + hiding.extend(&scope.hiding); if let Some(decl_id) = scope.decls.get(name) { - // if we're hiding this decl, do not return it and reduce the hiding strength - if hiding_strength > 0 { - hiding_strength -= 1; - } else { + if !hiding.contains(decl_id) { return Some(*decl_id); } } @@ -242,9 +237,10 @@ pub struct StateWorkingSet<'a> { pub struct StateDelta { files: Vec<(String, usize, usize)>, pub(crate) file_contents: Vec, - vars: Vec, // indexed by VarId - decls: Vec>, // indexed by DeclId - blocks: Vec, // indexed by BlockId + vars: Vec, // indexed by VarId + decls: Vec>, // indexed by DeclId + blocks: Vec, // indexed by BlockId + predecls: HashMap, DeclId>, // this should get erased after every def call pub scope: Vec, } @@ -262,12 +258,10 @@ impl StateDelta { } pub fn enter_scope(&mut self) { - // println!("enter scope"); self.scope.push(ScopeFrame::new()); } pub fn exit_scope(&mut self) { - // println!("exit scope"); self.scope.pop(); } } @@ -280,6 +274,7 @@ impl<'a> StateWorkingSet<'a> { file_contents: vec![], vars: vec![], decls: vec![], + predecls: HashMap::new(), blocks: vec![], scope: vec![ScopeFrame::new()], }, @@ -301,7 +296,6 @@ impl<'a> StateWorkingSet<'a> { pub fn add_decl(&mut self, decl: Box) -> DeclId { let name = decl.name().as_bytes().to_vec(); - // println!("adding {}", String::from_utf8_lossy(&name)); self.delta.decls.push(decl); let decl_id = self.num_decls() - 1; @@ -312,33 +306,63 @@ impl<'a> StateWorkingSet<'a> { .last_mut() .expect("internal error: missing required scope frame"); - // reset "hiding strength" to 0 => not hidden - if let Some(strength) = scope_frame.hiding.get_mut(&name) { - *strength = 0; - // println!(" strength: {}", strength); - } - scope_frame.decls.insert(name, decl_id); decl_id } - pub fn hide_decl(&mut self, name: Vec) { - let scope_frame = self + pub fn add_predecl(&mut self, decl: Box) { + let name = decl.name().as_bytes().to_vec(); + + self.delta.decls.push(decl); + let decl_id = self.num_decls() - 1; + + self.delta.predecls.insert(name, decl_id); + } + + pub fn find_predecl(&mut self, name: &[u8]) -> Option { + self.delta.predecls.get(name).copied() + } + + pub fn merge_predecl(&mut self, name: &[u8]) -> Option { + if let Some(decl_id) = self.delta.predecls.remove(name) { + let scope_frame = self + .delta + .scope + .last_mut() + .expect("internal error: missing required scope frame"); + + scope_frame.decls.insert(name.into(), decl_id); + + return Some(decl_id); + } + + None + } + + pub fn hide_decl(&mut self, name: &[u8]) -> Option { + // Since we can mutate scope frames in delta, remove the id directly + for scope in self.delta.scope.iter_mut().rev() { + if let Some(decl_id) = scope.decls.remove(name) { + return Some(decl_id); + } + } + + // We cannot mutate the permanent state => store the information in the current scope frame + let last_scope_frame = self .delta .scope .last_mut() .expect("internal error: missing required scope frame"); - if let Some(strength) = scope_frame.hiding.get_mut(&name) { - *strength += 1; - // println!("hiding {}, strength: {}", String::from_utf8_lossy(&name), strength); - } else { - // println!("hiding {}, strength: 1", String::from_utf8_lossy(&name)); - scope_frame.hiding.insert(name, 1); + for scope in self.permanent_state.scope.iter().rev() { + if let Some(decl_id) = scope.decls.get(name) { + last_scope_frame.hiding.insert(*decl_id); + return Some(*decl_id); + } } - // println!("hiding map: {:?}", scope_frame.hiding); + None } pub fn add_block(&mut self, block: Block) -> BlockId { @@ -448,46 +472,25 @@ impl<'a> StateWorkingSet<'a> { } pub fn find_decl(&self, name: &[u8]) -> Option { - let mut hiding_strength = 0; - // println!("set: starting finding {}", String::from_utf8_lossy(&name)); + let mut hiding: HashSet = HashSet::new(); + + if let Some(decl_id) = self.delta.predecls.get(name) { + return Some(*decl_id); + } for scope in self.delta.scope.iter().rev() { - // println!("delta frame"); - // println!("hiding map: {:?}", scope.hiding); - // check if we're hiding the declin this scope - if let Some(strength) = scope.hiding.get(name) { - hiding_strength += strength; - // println!(" was hiding, strength {}", hiding_strength); - } + hiding.extend(&scope.hiding); if let Some(decl_id) = scope.decls.get(name) { - // if we're hiding this decl, do not return it and reduce the hiding strength - if hiding_strength > 0 { - hiding_strength -= 1; - // println!(" decl found, strength {}", hiding_strength); - } else { - // println!(" decl found, return"); - return Some(*decl_id); - } + return Some(*decl_id); } } for scope in self.permanent_state.scope.iter().rev() { - // println!("perma frame"); - // println!("hiding map: {:?}", scope.hiding); - // check if we're hiding the declin this scope - if let Some(strength) = scope.hiding.get(name) { - hiding_strength += strength; - // println!(" was hiding, strength {}", hiding_strength); - } + hiding.extend(&scope.hiding); if let Some(decl_id) = scope.decls.get(name) { - // if we're hiding this decl, do not return it and reduce the hiding strength - if hiding_strength > 0 { - hiding_strength -= 1; - // println!(" decl found, strength {}", hiding_strength); - } else { - // println!(" decl found, return"); + if !hiding.contains(decl_id) { return Some(*decl_id); } } From 25b05dec9e612da9116b09f0c2e234e7eef8f2b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20=C5=BD=C3=A1dn=C3=ADk?= Date: Fri, 1 Oct 2021 23:16:27 +0300 Subject: [PATCH 10/17] Fix panic on double def; Tests; Double def error * Fixes a panic with defining two commands with the same name caused by declaration not found after predeclaration. * Adds a new error if a custom command is defined more than once in one block. * Add some tests --- crates/nu-parser/src/errors.rs | 4 ++++ crates/nu-parser/src/parse_keywords.rs | 10 +++++++--- crates/nu-parser/src/parser.rs | 8 +++++--- crates/nu-protocol/src/engine/engine_state.rs | 8 ++------ 4 files changed, 18 insertions(+), 12 deletions(-) diff --git a/crates/nu-parser/src/errors.rs b/crates/nu-parser/src/errors.rs index f55fed2da7..444873d1ae 100644 --- a/crates/nu-parser/src/errors.rs +++ b/crates/nu-parser/src/errors.rs @@ -77,6 +77,10 @@ pub enum ParseError { #[diagnostic(code(nu::parser::module_not_found), url(docsrs))] ModuleNotFound(#[label = "module not found"] Span), + #[error("Duplicate command definition within a block.")] + #[diagnostic(code(nu::parser::duplicate_command_def), url(docsrs))] + DuplicateCommandDef(#[label = "defined more than once"] Span), + #[error("Unknown command.")] #[diagnostic( code(nu::parser::unknown_command), diff --git a/crates/nu-parser/src/parse_keywords.rs b/crates/nu-parser/src/parse_keywords.rs index 3195acd785..6eb40daad0 100644 --- a/crates/nu-parser/src/parse_keywords.rs +++ b/crates/nu-parser/src/parse_keywords.rs @@ -13,7 +13,7 @@ use crate::{ ParseError, }; -pub fn parse_def_predecl(working_set: &mut StateWorkingSet, spans: &[Span]) { +pub fn parse_def_predecl(working_set: &mut StateWorkingSet, spans: &[Span]) -> Option { let name = working_set.get_span_contents(spans[0]); // handle "export def" same as "def" @@ -42,9 +42,13 @@ pub fn parse_def_predecl(working_set: &mut StateWorkingSet, spans: &[Span]) { signature.name = name; let decl = signature.predeclare(); - working_set.add_predecl(decl); + if working_set.add_predecl(decl).is_some() { + return Some(ParseError::DuplicateCommandDef(spans[1])); + } } } + + None } pub fn parse_def( @@ -98,7 +102,7 @@ pub fn parse_def( (&name, signature, block_id) { let decl_id = working_set - .find_predecl(name.as_bytes()) + .find_decl(name.as_bytes()) .expect("internal error: predeclaration failed to add definition"); let declaration = working_set.get_decl_mut(decl_id); diff --git a/crates/nu-parser/src/parser.rs b/crates/nu-parser/src/parser.rs index d3464869f4..79213fdf7b 100644 --- a/crates/nu-parser/src/parser.rs +++ b/crates/nu-parser/src/parser.rs @@ -2606,16 +2606,18 @@ pub fn parse_block( working_set.enter_scope(); } + let mut error = None; + // Pre-declare any definition so that definitions // that share the same block can see each other for pipeline in &lite_block.block { if pipeline.commands.len() == 1 { - parse_def_predecl(working_set, &pipeline.commands[0].parts); + if let Some(err) = parse_def_predecl(working_set, &pipeline.commands[0].parts) { + error = error.or(Some(err)); + } } } - let mut error = None; - let block: Block = lite_block .block .iter() diff --git a/crates/nu-protocol/src/engine/engine_state.rs b/crates/nu-protocol/src/engine/engine_state.rs index e1dcb7024d..0653ed73c9 100644 --- a/crates/nu-protocol/src/engine/engine_state.rs +++ b/crates/nu-protocol/src/engine/engine_state.rs @@ -311,17 +311,13 @@ impl<'a> StateWorkingSet<'a> { decl_id } - pub fn add_predecl(&mut self, decl: Box) { + pub fn add_predecl(&mut self, decl: Box) -> Option { let name = decl.name().as_bytes().to_vec(); self.delta.decls.push(decl); let decl_id = self.num_decls() - 1; - self.delta.predecls.insert(name, decl_id); - } - - pub fn find_predecl(&mut self, name: &[u8]) -> Option { - self.delta.predecls.get(name).copied() + self.delta.predecls.insert(name, decl_id) } pub fn merge_predecl(&mut self, name: &[u8]) -> Option { From 891d79d2aae3f9479223ce26dee39add166c7bdb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20=C5=BD=C3=A1dn=C3=ADk?= Date: Fri, 1 Oct 2021 23:25:48 +0300 Subject: [PATCH 11/17] Fmt and misc fixes after rebase --- crates/nu-command/src/default_context.rs | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/crates/nu-command/src/default_context.rs b/crates/nu-command/src/default_context.rs index 42b30cf877..a2c888e269 100644 --- a/crates/nu-command/src/default_context.rs +++ b/crates/nu-command/src/default_context.rs @@ -6,8 +6,9 @@ use nu_protocol::{ }; use crate::{ - Alias, Benchmark, BuildString, Def, Do, Each, ExportDef, External, For, From, FromJson, Git, GitCheckout, - Hide, If, Length, Let, LetEnv, Lines, ListGitBranches, Ls, Module, Sys, Table, Use, Where, + Alias, Benchmark, BuildString, Def, Do, Each, ExportDef, External, For, From, FromJson, Git, + GitCheckout, Hide, If, Length, Let, LetEnv, Lines, ListGitBranches, Ls, Module, Sys, Table, + Use, Where, }; pub fn create_default_context() -> Rc> { @@ -20,17 +21,10 @@ pub fn create_default_context() -> Rc> { Signature::build("where").required("cond", SyntaxShape::RowCondition, "condition"); working_set.add_decl(sig.predeclare()); - working_set.add_decl(Box::new(If)); - - working_set.add_decl(Box::new(Let)); - - working_set.add_decl(Box::new(LetEnv)); - working_set.add_decl(Box::new(Alias)); working_set.add_decl(Box::new(Benchmark)); working_set.add_decl(Box::new(BuildString)); - - working_set.add_decl(Box::new(Def)); + working_set.add_decl(Box::new(Def)); working_set.add_decl(Box::new(Do)); working_set.add_decl(Box::new(Each)); working_set.add_decl(Box::new(ExportDef)); @@ -49,7 +43,7 @@ pub fn create_default_context() -> Rc> { working_set.add_decl(Box::new(Sys)); working_set.add_decl(Box::new(Table)); working_set.add_decl(Box::new(Use)); - working_set.add_decl(Box::new(Where)); + working_set.add_decl(Box::new(Where)); // This is a WIP proof of concept working_set.add_decl(Box::new(ListGitBranches)); From fb0f83e5742103a53a28f312b1d0f94f03a0004d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20=C5=BD=C3=A1dn=C3=ADk?= Date: Sat, 2 Oct 2021 00:12:30 +0300 Subject: [PATCH 12/17] Disallow hiding the same def twice; Add tests Tests got removed after rebase. --- crates/nu-protocol/src/engine/engine_state.rs | 13 +++- src/tests.rs | 64 +++++++++++++++++++ 2 files changed, 75 insertions(+), 2 deletions(-) diff --git a/crates/nu-protocol/src/engine/engine_state.rs b/crates/nu-protocol/src/engine/engine_state.rs index 0653ed73c9..ea75692ebd 100644 --- a/crates/nu-protocol/src/engine/engine_state.rs +++ b/crates/nu-protocol/src/engine/engine_state.rs @@ -337,8 +337,12 @@ impl<'a> StateWorkingSet<'a> { } pub fn hide_decl(&mut self, name: &[u8]) -> Option { + let mut hiding: HashSet = HashSet::new(); + // Since we can mutate scope frames in delta, remove the id directly for scope in self.delta.scope.iter_mut().rev() { + hiding.extend(&scope.hiding); + if let Some(decl_id) = scope.decls.remove(name) { return Some(decl_id); } @@ -352,9 +356,14 @@ impl<'a> StateWorkingSet<'a> { .expect("internal error: missing required scope frame"); for scope in self.permanent_state.scope.iter().rev() { + hiding.extend(&scope.hiding); + if let Some(decl_id) = scope.decls.get(name) { - last_scope_frame.hiding.insert(*decl_id); - return Some(*decl_id); + if !hiding.contains(decl_id) { + // Do not hide already hidden decl + last_scope_frame.hiding.insert(*decl_id); + return Some(*decl_id); + } } } diff --git a/src/tests.rs b/src/tests.rs index 77e888a893..c947d1a227 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -383,6 +383,70 @@ fn module_imports_5() -> TestResult { ) } +#[test] +fn module_import_uses_internal_command() -> TestResult { + run_test( + r#"module foo { def b [] { 2 }; export def a [] { b } }; use foo; foo.a"#, + "2", + ) +} + +#[test] +fn hides_def() -> TestResult { + fail_test( + r#"def foo [] { "foo" }; hide foo; foo"#, + "command not found", + ) +} + +#[test] +fn hides_def_then_redefines() -> TestResult { + fail_test( + r#"def foo [] { "foo" }; hide foo; def foo [] { "bar" }; foo"#, + "defined more than once", + ) +} + +#[test] +fn hides_def_in_scope_1() -> TestResult { + fail_test( + r#"def foo [] { "foo" }; do { hide foo; foo }"#, + "command not found", + ) +} + +#[test] +fn hides_def_in_scope_2() -> TestResult { + run_test( + r#"def foo [] { "foo" }; do { def foo [] { "bar" }; hide foo; foo }"#, + "foo", + ) +} + +#[test] +fn hides_def_in_scope_3() -> TestResult { + fail_test( + r#"def foo [] { "foo" }; do { hide foo; def foo [] { "bar" }; hide foo; foo }"#, + "command not found", + ) +} + +#[test] +fn hides_def_in_scope_4() -> TestResult { + fail_test( + r#"def foo [] { "foo" }; do { def foo [] { "bar" }; hide foo; hide foo; foo }"#, + "command not found", + ) +} + +#[test] +fn hide_twice_not_allowed() -> TestResult { + fail_test( + r#"def foo [] { "foo" }; hide foo; hide foo"#, + "unknown command", + ) +} + #[test] fn from_json_1() -> TestResult { run_test(r#"('{"name": "Fred"}' | from json).name"#, "Fred") From 2c1b074bdc72aec60f29fe962525fd24b895d92d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20=C5=BD=C3=A1dn=C3=ADk?= Date: Sat, 2 Oct 2021 00:17:02 +0300 Subject: [PATCH 13/17] Add test for double def --- src/tests.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/tests.rs b/src/tests.rs index c947d1a227..056cac1a93 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -447,6 +447,14 @@ fn hide_twice_not_allowed() -> TestResult { ) } +#[test] +fn def_twice_should_fail() -> TestResult { + fail_test( + r#"def foo [] { "foo" }; def foo [] { "bar" }"#, + "defined more than once", + ) +} + #[test] fn from_json_1() -> TestResult { run_test(r#"('{"name": "Fred"}' | from json).name"#, "Fred") From 6595c0659891235946ffa995e91cabe383a6caa6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20=C5=BD=C3=A1dn=C3=ADk?= Date: Sat, 2 Oct 2021 03:42:35 +0300 Subject: [PATCH 14/17] Relax panic into error Convert the panic when declaration cannot find predeclaration into an error. This error is already covered and reported in the predeclaration phase. --- crates/nu-parser/src/parse_keywords.rs | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/crates/nu-parser/src/parse_keywords.rs b/crates/nu-parser/src/parse_keywords.rs index 6eb40daad0..033a21e738 100644 --- a/crates/nu-parser/src/parse_keywords.rs +++ b/crates/nu-parser/src/parse_keywords.rs @@ -101,15 +101,20 @@ pub fn parse_def( if let (Some(name), Some(mut signature), Some(block_id)) = (&name, signature, block_id) { - let decl_id = working_set - .find_decl(name.as_bytes()) - .expect("internal error: predeclaration failed to add definition"); + if let Some(decl_id) = working_set.find_decl(name.as_bytes()) { + let declaration = working_set.get_decl_mut(decl_id); - let declaration = working_set.get_decl_mut(decl_id); + signature.name = name.clone(); - signature.name = name.clone(); - - *declaration = signature.into_block_command(block_id); + *declaration = signature.into_block_command(block_id); + } else { + error = error.or_else(|| { + Some(ParseError::UnknownState( + "Could not define hidden command".into(), + spans[1], + )) + }); + }; } } else { let err_span = Span { From 0cc121876b3b1a0e8f71941d74a6d50323cb5447 Mon Sep 17 00:00:00 2001 From: JT <547158+jntrnr@users.noreply.github.com> Date: Sun, 3 Oct 2021 06:12:05 +1300 Subject: [PATCH 15/17] Update tests.rs Update test errors to be more portable --- src/tests.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/tests.rs b/src/tests.rs index 844df53c35..5436f16b84 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -395,7 +395,7 @@ fn module_import_uses_internal_command() -> TestResult { fn hides_def() -> TestResult { fail_test( r#"def foo [] { "foo" }; hide foo; foo"#, - "command not found", + "not found", ) } @@ -411,7 +411,7 @@ fn hides_def_then_redefines() -> TestResult { fn hides_def_in_scope_1() -> TestResult { fail_test( r#"def foo [] { "foo" }; do { hide foo; foo }"#, - "command not found", + "not found", ) } @@ -427,7 +427,7 @@ fn hides_def_in_scope_2() -> TestResult { fn hides_def_in_scope_3() -> TestResult { fail_test( r#"def foo [] { "foo" }; do { hide foo; def foo [] { "bar" }; hide foo; foo }"#, - "command not found", + "not found", ) } @@ -435,7 +435,7 @@ fn hides_def_in_scope_3() -> TestResult { fn hides_def_in_scope_4() -> TestResult { fail_test( r#"def foo [] { "foo" }; do { def foo [] { "bar" }; hide foo; hide foo; foo }"#, - "command not found", + "not found", ) } From b5ec9e0360a9a971ac016b13d78f53a45a1fc7f0 Mon Sep 17 00:00:00 2001 From: JT <547158+jntrnr@users.noreply.github.com> Date: Sun, 3 Oct 2021 06:16:02 +1300 Subject: [PATCH 16/17] Update mod.rs --- crates/nu-command/src/core_commands/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/nu-command/src/core_commands/mod.rs b/crates/nu-command/src/core_commands/mod.rs index 384ed11e8c..3472f4d8b3 100644 --- a/crates/nu-command/src/core_commands/mod.rs +++ b/crates/nu-command/src/core_commands/mod.rs @@ -2,8 +2,8 @@ mod alias; mod def; mod do_; mod export_def; -mod hide; mod help; +mod hide; mod if_; mod let_; mod module; @@ -13,8 +13,8 @@ pub use alias::Alias; pub use def::Def; pub use do_::Do; pub use export_def::ExportDef; -pub use hide::Hide; pub use help::Help; +pub use hide::Hide; pub use if_::If; pub use let_::Let; pub use module::Module; From eba3484611707fcb0246622a392f63745ffca606 Mon Sep 17 00:00:00 2001 From: JT <547158+jntrnr@users.noreply.github.com> Date: Sun, 3 Oct 2021 06:17:51 +1300 Subject: [PATCH 17/17] Update tests.rs --- src/tests.rs | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/tests.rs b/src/tests.rs index 5436f16b84..7416aecb6f 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -393,10 +393,7 @@ fn module_import_uses_internal_command() -> TestResult { #[test] fn hides_def() -> TestResult { - fail_test( - r#"def foo [] { "foo" }; hide foo; foo"#, - "not found", - ) + fail_test(r#"def foo [] { "foo" }; hide foo; foo"#, "not found") } #[test] @@ -409,10 +406,7 @@ fn hides_def_then_redefines() -> TestResult { #[test] fn hides_def_in_scope_1() -> TestResult { - fail_test( - r#"def foo [] { "foo" }; do { hide foo; foo }"#, - "not found", - ) + fail_test(r#"def foo [] { "foo" }; do { hide foo; foo }"#, "not found") } #[test]