feat(overlay): expose constants with overlay use (#15081)

# Description

`overlay use` now imports constants exported from modules, just like
`use`.

```nushell
# foo.nu
export const a = 1
export const b = 2
```

- `overlay use foo.nu` being equivalent to `use foo.nu *` and exposing
constants `$a = 1` and `$b = 2`
- `overlay use foo.nu -p` being equivalent to `use foo.nu` and exposing
the constant `$foo = {a: 1, b: 2}`

# User-Facing Changes

`overlay use` now imports constants just like `use`.

# Tests + Formatting

- 🟢 toolkit fmt
- 🟢 toolkit clippy
- 🟢 toolkit test
- 🟢 toolkit test stdlib

# After Submitting
N/A
This commit is contained in:
Bahex 2025-02-13 13:55:03 +03:00 committed by GitHub
parent 62e56d3581
commit 4ac4f71a37
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 33 additions and 13 deletions

View File

@ -2977,8 +2977,7 @@ pub fn parse_overlay_new(working_set: &mut StateWorkingSet, call: Box<Call>) ->
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<Call>) ->
working_set.add_overlay(
final_overlay_name.as_bytes().to_vec(),
origin_module_id,
definitions.decls,
definitions.modules,
definitions,
has_prefix,
);
} else {

View File

@ -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<u8>,
origin: ModuleId,
decls: Vec<(Vec<u8>, DeclId)>,
modules: Vec<(Vec<u8>, 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) {

View File

@ -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"));