Recursively export constants from modules (#10049)

<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx

you can also mention related issues, PRs or discussions!
-->

# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.

Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->

https://github.com/nushell/nushell/pull/9773 introduced constants to
modules and allowed to export them, but only within one level. This PR:
* allows recursive exporting of constants from all submodules
* fixes submodule imports in a list import pattern
* makes sure exported constants are actual constants

Should unblock https://github.com/nushell/nushell/pull/9678

### Example:
```nushell
module spam {
    export module eggs {
        export module bacon {
            export const viking = 'eats'
        }
    }
}

use spam 
print $spam.eggs.bacon.viking  # prints 'eats'

use spam [eggs]
print $eggs.bacon.viking  # prints 'eats'

use spam eggs bacon viking
print $viking  # prints 'eats'
```

### Limitation 1:

Considering the above `spam` module, attempting to get `eggs bacon` from
`spam` module doesn't work directly:
```nushell
use spam [ eggs bacon ]  # attempts to load `eggs`, then `bacon`
use spam [ "eggs bacon" ]  # obviously wrong name for a constant, but doesn't work also for commands
```

Workaround (for example):
```nushell
use spam eggs
use eggs [ bacon ]

print $bacon.viking  # prints 'eats'
```

I'm thinking I'll just leave it in, as you can easily work around this.
It is also a limitation of the import pattern in general, not just
constants.

### Limitation 2:

`overlay use` successfully imports the constants, but `overlay hide`
does not hide them, even though it seems to hide normal variables
successfully. This needs more investigation.

# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->

Allows recursive constant exports from submodules.

# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.

Make sure you've run and fixed any issues with these commands:

- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect -A clippy::result_large_err` to check that
you're using the standard code style
- `cargo test --workspace` to check that all tests pass
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library

> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->

# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
This commit is contained in:
Jakub Žádník
2023-08-20 15:51:35 +03:00
committed by GitHub
parent e6ce8a89be
commit 3148acd3a4
16 changed files with 351 additions and 178 deletions

View File

@ -1,6 +1,6 @@
use crate::{
ast::ImportPatternMember, engine::StateWorkingSet, BlockId, DeclId, ModuleId, ParseError, Span,
VarId,
Value, VarId,
};
use indexmap::IndexMap;
@ -8,19 +8,19 @@ use indexmap::IndexMap;
pub struct ResolvedImportPattern {
pub decls: Vec<(Vec<u8>, DeclId)>,
pub modules: Vec<(Vec<u8>, ModuleId)>,
pub variables: Vec<(Vec<u8>, VarId)>,
pub constants: Vec<(Vec<u8>, Value)>,
}
impl ResolvedImportPattern {
pub fn new(
decls: Vec<(Vec<u8>, DeclId)>,
modules: Vec<(Vec<u8>, ModuleId)>,
variables: Vec<(Vec<u8>, VarId)>,
constants: Vec<(Vec<u8>, Value)>,
) -> Self {
ResolvedImportPattern {
decls,
modules,
variables,
constants,
}
}
}
@ -31,7 +31,7 @@ pub struct Module {
pub name: Vec<u8>,
pub decls: IndexMap<Vec<u8>, DeclId>,
pub submodules: IndexMap<Vec<u8>, ModuleId>,
pub variables: IndexMap<Vec<u8>, VarId>,
pub constants: IndexMap<Vec<u8>, VarId>,
pub env_block: Option<BlockId>, // `export-env { ... }` block
pub main: Option<DeclId>, // `export def main`
pub span: Option<Span>,
@ -43,7 +43,7 @@ impl Module {
name,
decls: IndexMap::new(),
submodules: IndexMap::new(),
variables: IndexMap::new(),
constants: IndexMap::new(),
env_block: None,
main: None,
span: None,
@ -55,7 +55,7 @@ impl Module {
name,
decls: IndexMap::new(),
submodules: IndexMap::new(),
variables: IndexMap::new(),
constants: IndexMap::new(),
env_block: None,
main: None,
span: Some(span),
@ -75,7 +75,7 @@ impl Module {
}
pub fn add_variable(&mut self, name: Vec<u8>, var_id: VarId) -> Option<VarId> {
self.variables.insert(name, var_id)
self.constants.insert(name, var_id)
}
pub fn add_env_block(&mut self, block_id: BlockId) {
@ -95,8 +95,8 @@ impl Module {
working_set: &StateWorkingSet,
self_id: ModuleId,
members: &[ImportPatternMember],
name_override: Option<&[u8]>, // name under the module was stored (doesn't have to be the
// same as self.name)
name_override: Option<&[u8]>, // name under the module was stored (doesn't have to be the same as self.name)
backup_span: Span,
) -> (ResolvedImportPattern, Vec<ParseError>) {
let final_name = name_override.unwrap_or(&self.name).to_vec();
@ -105,13 +105,15 @@ impl Module {
} else {
// Import pattern was just name without any members
let mut decls = vec![];
let mut vars = vec![];
let mut const_rows = vec![];
let mut errors = vec![];
for (_, id) in &self.submodules {
let submodule = working_set.get_module(*id);
let span = submodule.span.or(self.span).unwrap_or(backup_span);
let (sub_results, sub_errors) =
submodule.resolve_import_pattern(working_set, *id, &[], None);
submodule.resolve_import_pattern(working_set, *id, &[], None, span);
errors.extend(sub_errors);
for (sub_name, sub_decl_id) in sub_results.decls {
@ -122,16 +124,35 @@ impl Module {
decls.push((new_name, sub_decl_id));
}
for (sub_name, sub_var_id) in sub_results.variables {
vars.push((sub_name, sub_var_id));
}
const_rows.extend(sub_results.constants);
}
decls.extend(self.decls_with_head(&final_name));
vars.extend(self.vars());
for (name, var_id) in self.consts() {
match working_set.get_constant(var_id) {
Ok(const_val) => const_rows.push((name, const_val.clone())),
Err(err) => errors.push(err),
}
}
let mut const_cols = vec![];
let mut const_vals = vec![];
for (name, val) in const_rows {
const_cols.push(String::from_utf8_lossy(&name).to_string());
const_vals.push(val);
}
let span = self.span.unwrap_or(backup_span);
let const_record = Value::record(const_cols, const_vals, span);
return (
ResolvedImportPattern::new(decls, vec![(final_name, self_id)], vars),
ResolvedImportPattern::new(
decls,
vec![(final_name.clone(), self_id)],
vec![(final_name, const_record)],
),
errors,
);
};
@ -159,14 +180,30 @@ impl Module {
ResolvedImportPattern::new(vec![(name.clone(), *decl_id)], vec![], vec![]),
vec![],
)
} else if let Some(var_id) = self.variables.get(name) {
(
ResolvedImportPattern::new(vec![], vec![], vec![(name.clone(), *var_id)]),
vec![],
)
} else if let Some(var_id) = self.constants.get(name) {
match working_set.get_constant(*var_id) {
Ok(const_val) => (
ResolvedImportPattern::new(
vec![],
vec![],
vec![(name.clone(), const_val.clone())],
),
vec![],
),
Err(err) => (
ResolvedImportPattern::new(vec![], vec![], vec![]),
vec![err],
),
}
} else if let Some(submodule_id) = self.submodules.get(name) {
let submodule = working_set.get_module(*submodule_id);
submodule.resolve_import_pattern(working_set, *submodule_id, rest, None)
submodule.resolve_import_pattern(
working_set,
*submodule_id,
rest,
None,
self.span.unwrap_or(backup_span),
)
} else {
(
ResolvedImportPattern::new(vec![], vec![], vec![]),
@ -177,33 +214,46 @@ impl Module {
ImportPatternMember::Glob { .. } => {
let mut decls = vec![];
let mut submodules = vec![];
let mut variables = vec![];
let mut constants = vec![];
let mut errors = vec![];
for (_, id) in &self.submodules {
let submodule = working_set.get_module(*id);
let (sub_results, sub_errors) =
submodule.resolve_import_pattern(working_set, *id, &[], None);
let (sub_results, sub_errors) = submodule.resolve_import_pattern(
working_set,
*id,
&[],
None,
self.span.unwrap_or(backup_span),
);
decls.extend(sub_results.decls);
submodules.extend(sub_results.modules);
variables.extend(sub_results.variables);
constants.extend(sub_results.constants);
errors.extend(sub_errors);
}
decls.extend(self.decls());
variables.extend(self.variables.clone());
constants.extend(self.constants.iter().filter_map(|(name, var_id)| {
match working_set.get_constant(*var_id) {
Ok(const_val) => Some((name.clone(), const_val.clone())),
Err(err) => {
errors.push(err);
None
}
}
}));
submodules.extend(self.submodules());
(
ResolvedImportPattern::new(decls, submodules, variables),
ResolvedImportPattern::new(decls, submodules, constants),
errors,
)
}
ImportPatternMember::List { names } => {
let mut decls = vec![];
let mut submodules = vec![];
let mut variables = vec![];
let mut modules = vec![];
let mut constants = vec![];
let mut errors = vec![];
for (name, span) in names {
@ -215,17 +265,32 @@ impl Module {
}
} else if let Some(decl_id) = self.decls.get(name) {
decls.push((name.clone(), *decl_id));
} else if let Some(var_id) = self.variables.get(name) {
variables.push((name.clone(), *var_id));
} else if let Some(var_id) = self.constants.get(name) {
match working_set.get_constant(*var_id) {
Ok(const_val) => constants.push((name.clone(), const_val.clone())),
Err(err) => errors.push(err),
}
} else if let Some(submodule_id) = self.submodules.get(name) {
submodules.push((name.clone(), *submodule_id));
let submodule = working_set.get_module(*submodule_id);
let (sub_results, sub_errors) = submodule.resolve_import_pattern(
working_set,
*submodule_id,
rest,
None,
self.span.unwrap_or(backup_span),
);
decls.extend(sub_results.decls);
modules.extend(sub_results.modules);
constants.extend(sub_results.constants);
errors.extend(sub_errors);
} else {
errors.push(ParseError::ExportNotFound(*span));
}
}
(
ResolvedImportPattern::new(decls, submodules, variables),
ResolvedImportPattern::new(decls, modules, constants),
errors,
)
}
@ -262,8 +327,8 @@ impl Module {
result
}
pub fn vars(&self) -> Vec<(Vec<u8>, VarId)> {
self.variables
pub fn consts(&self) -> Vec<(Vec<u8>, VarId)> {
self.constants
.iter()
.map(|(name, id)| (name.to_vec(), *id))
.collect()