From 429c4332b1986765cffe874932ab9c023d333967 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20=C5=BD=C3=A1dn=C3=ADk?= Date: Sat, 20 May 2023 00:46:22 +0300 Subject: [PATCH] Make aliased call not look up predeclarations (#9244) --- crates/nu-command/tests/commands/alias.rs | 6 ++++++ crates/nu-command/tests/commands/use_.rs | 2 +- crates/nu-parser/src/parse_keywords.rs | 4 ++++ crates/nu-protocol/src/engine/engine_state.rs | 19 +++++++++++++------ 4 files changed, 24 insertions(+), 7 deletions(-) diff --git a/crates/nu-command/tests/commands/alias.rs b/crates/nu-command/tests/commands/alias.rs index 8c1ad7948..997a9ef94 100644 --- a/crates/nu-command/tests/commands/alias.rs +++ b/crates/nu-command/tests/commands/alias.rs @@ -149,3 +149,9 @@ fn alias_multiword_name() { let actual = nu!(r#"alias "foo bar" = echo 'test'; foo bar"#); assert_eq!(actual.out, "test"); } + +#[test] +fn alias_ordering() { + let actual = nu!(r#"alias bar = echo; def echo [] { 'dummy echo' }; bar 'foo'"#); + assert_eq!(actual.out, "foo"); +} diff --git a/crates/nu-command/tests/commands/use_.rs b/crates/nu-command/tests/commands/use_.rs index c3b35b338..e5a5644e7 100644 --- a/crates/nu-command/tests/commands/use_.rs +++ b/crates/nu-command/tests/commands/use_.rs @@ -171,9 +171,9 @@ fn use_export_env_combined() { sandbox.with_files(vec![FileWithContentToBeTrimmed( "spam.nu", r#" + def foo [] { 'foo' } alias bar = foo export-env { let-env FOO = (bar) } - def foo [] { 'foo' } "#, )]); diff --git a/crates/nu-parser/src/parse_keywords.rs b/crates/nu-parser/src/parse_keywords.rs index 361e7c211..a0527ccac 100644 --- a/crates/nu-parser/src/parse_keywords.rs +++ b/crates/nu-parser/src/parse_keywords.rs @@ -804,6 +804,8 @@ pub fn parse_alias( } let starting_error_count = working_set.parse_errors.len(); + working_set.search_predecls = false; + let expr = parse_call( working_set, replacement_spans, @@ -811,6 +813,8 @@ pub fn parse_alias( false, // TODO: Should this be set properly??? ); + working_set.search_predecls = true; + if starting_error_count != working_set.parse_errors.len() { if let Some(e) = working_set.parse_errors.get(starting_error_count) { if let ParseError::MissingPositional(..) = e { diff --git a/crates/nu-protocol/src/engine/engine_state.rs b/crates/nu-protocol/src/engine/engine_state.rs index 26e2c5ca7..f9dfb8198 100644 --- a/crates/nu-protocol/src/engine/engine_state.rs +++ b/crates/nu-protocol/src/engine/engine_state.rs @@ -952,6 +952,8 @@ pub struct StateWorkingSet<'a> { pub currently_parsed_cwd: Option, /// All previously parsed module files. Used to protect against circular imports. pub parsed_module_files: Vec, + /// Whether or not predeclarations are searched when looking up a command (used with aliases) + pub search_predecls: bool, pub parse_errors: Vec, } @@ -1130,6 +1132,7 @@ impl<'a> StateWorkingSet<'a> { type_scope: TypeScope::default(), currently_parsed_cwd: permanent_state.currently_parsed_cwd.clone(), parsed_module_files: vec![], + search_predecls: true, parse_errors: vec![], } } @@ -1456,9 +1459,11 @@ impl<'a> StateWorkingSet<'a> { let mut visibility: Visibility = Visibility::new(); for scope_frame in self.delta.scope.iter().rev() { - if let Some(decl_id) = scope_frame.predecls.get(name) { - if visibility.is_decl_id_visible(decl_id) { - return Some(*decl_id); + if self.search_predecls { + if let Some(decl_id) = scope_frame.predecls.get(name) { + if visibility.is_decl_id_visible(decl_id) { + return Some(*decl_id); + } } } @@ -1466,9 +1471,11 @@ impl<'a> StateWorkingSet<'a> { for overlay_frame in scope_frame.active_overlays(&mut removed_overlays).rev() { visibility.append(&overlay_frame.visibility); - if let Some(decl_id) = overlay_frame.predecls.get(name) { - if visibility.is_decl_id_visible(decl_id) { - return Some(*decl_id); + if self.search_predecls { + if let Some(decl_id) = overlay_frame.predecls.get(name) { + if visibility.is_decl_id_visible(decl_id) { + return Some(*decl_id); + } } }