diff --git a/crates/nu-parser/src/parse_keywords.rs b/crates/nu-parser/src/parse_keywords.rs index 7396b4ea93..dbe8c7292c 100644 --- a/crates/nu-parser/src/parse_keywords.rs +++ b/crates/nu-parser/src/parse_keywords.rs @@ -2977,8 +2977,7 @@ pub fn parse_overlay_new(working_set: &mut StateWorkingSet, call: Box) -> working_set.add_overlay( overlay_name.as_bytes().to_vec(), module_id, - vec![], - vec![], + ResolvedImportPattern::new(vec![], vec![], vec![], vec![]), false, ); @@ -3192,8 +3191,7 @@ pub fn parse_overlay_use(working_set: &mut StateWorkingSet, call: Box) -> working_set.add_overlay( final_overlay_name.as_bytes().to_vec(), origin_module_id, - definitions.decls, - definitions.modules, + definitions, has_prefix, ); } else { diff --git a/crates/nu-protocol/src/engine/state_working_set.rs b/crates/nu-protocol/src/engine/state_working_set.rs index c7643b501b..11be80efb9 100644 --- a/crates/nu-protocol/src/engine/state_working_set.rs +++ b/crates/nu-protocol/src/engine/state_working_set.rs @@ -5,7 +5,8 @@ use crate::{ StateDelta, Variable, VirtualPath, Visibility, }, BlockId, Category, CompileError, Config, DeclId, FileId, GetSpan, Module, ModuleId, OverlayId, - ParseError, ParseWarning, Signature, Span, SpanId, Type, Value, VarId, VirtualPathId, + ParseError, ParseWarning, ResolvedImportPattern, Signature, Span, SpanId, Type, Value, VarId, + VirtualPathId, }; use core::panic; use std::{ @@ -915,7 +916,12 @@ impl<'a> StateWorkingSet<'a> { let name = self.last_overlay_name().to_vec(); let origin = overlay_frame.origin; let prefixed = overlay_frame.prefixed; - self.add_overlay(name, origin, vec![], vec![], prefixed); + self.add_overlay( + name, + origin, + ResolvedImportPattern::new(vec![], vec![], vec![], vec![]), + prefixed, + ); } self.delta @@ -952,8 +958,7 @@ impl<'a> StateWorkingSet<'a> { &mut self, name: Vec, origin: ModuleId, - decls: Vec<(Vec, DeclId)>, - modules: Vec<(Vec, ModuleId)>, + definitions: ResolvedImportPattern, prefixed: bool, ) { let last_scope_frame = self.delta.last_scope_frame_mut(); @@ -980,8 +985,22 @@ impl<'a> StateWorkingSet<'a> { self.move_predecls_to_overlay(); - self.use_decls(decls); - self.use_modules(modules); + self.use_decls(definitions.decls); + self.use_modules(definitions.modules); + + let mut constants = vec![]; + + for (name, const_vid) in definitions.constants { + constants.push((name, const_vid)); + } + + for (name, const_val) in definitions.constant_values { + let const_var_id = + self.add_variable(name.clone(), Span::unknown(), const_val.get_type(), false); + self.set_variable_const_val(const_var_id, const_val); + constants.push((name, const_var_id)); + } + self.use_variables(constants); } pub fn remove_overlay(&mut self, name: &[u8], keep_custom: bool) { diff --git a/tests/const_/mod.rs b/tests/const_/mod.rs index dabf3892f8..b969c2c6f1 100644 --- a/tests/const_/mod.rs +++ b/tests/const_/mod.rs @@ -347,9 +347,8 @@ fn const_captures_in_closures_work() { assert_eq!(actual.out, "hello world"); } -#[ignore = "TODO: Need to fix `overlay hide` to hide the constants brought by `overlay use`"] #[test] -fn complex_const_overlay_use_hide() { +fn complex_const_overlay_use() { let inp = &[MODULE_SETUP, "overlay use spam", "$X"]; let actual = nu!(&inp.join("; ")); assert_eq!(actual.out, "x"); @@ -365,11 +364,15 @@ fn complex_const_overlay_use_hide() { let inp = &[ MODULE_SETUP, "overlay use spam", - "($eggs.bacon.none | is-empty)", + "($eggs.bacon not-has 'none')", ]; let actual = nu!(&inp.join("; ")); assert_eq!(actual.out, "true"); +} +#[ignore = "TODO: `overlay hide` should be possible to use after `overlay use` in the same source unit."] +#[test] +fn overlay_use_hide_in_single_source_unit() { let inp = &[MODULE_SETUP, "overlay use spam", "overlay hide", "$eggs"]; let actual = nu!(&inp.join("; ")); assert!(actual.err.contains("nu::parser::variable_not_found"));