mirror of
https://github.com/nushell/nushell.git
synced 2025-05-21 18:30:46 +02:00
# Description This PR does a few things to help improve type hovers and, in the process, fixes a few outstanding issues in the type system. Here's a list of the changes: * `for` now will try to infer the type of the iteration variable based on the expression it's given. This fixes things like `for x in [1, 2, 3] { }` where `x` now properly gets the int type. * Removed old input/output type fields from the signature, focuses on the vec of signatures. Updated a bunch of dataframe commands that hadn't moved over. This helps tie things together a bit better * Fixed inference of types from subexpressions to use the last expression in the block * Fixed handling of explicit types in `let` and `mut` calls, so we now respect that as the authoritative type I also tried to add `def` input/output type inference, but unfortunately we only know the predecl types universally, which means we won't have enough information to properly know what the types of the custom commands are. # User-Facing Changes Script typechecking will get tighter in some cases Hovers should be more accurate in some cases that previously resorted to any. # 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 -- crates/nu-std/tests/run.nu` 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. --> --------- Co-authored-by: Darren Schroeder <343840+fdncred@users.noreply.github.com>
295 lines
8.1 KiB
Rust
295 lines
8.1 KiB
Rust
use crate::{DeclId, ModuleId, OverlayId, Type, Value, VarId};
|
|
use std::borrow::Borrow;
|
|
use std::collections::HashMap;
|
|
use std::hash::{Hash, Hasher};
|
|
|
|
pub static DEFAULT_OVERLAY_NAME: &str = "zero";
|
|
|
|
/// Tells whether a decl is visible or not
|
|
#[derive(Debug, Clone)]
|
|
pub struct Visibility {
|
|
decl_ids: HashMap<DeclId, bool>,
|
|
}
|
|
|
|
impl Visibility {
|
|
pub fn new() -> Self {
|
|
Visibility {
|
|
decl_ids: HashMap::new(),
|
|
}
|
|
}
|
|
|
|
pub fn is_decl_id_visible(&self, decl_id: &DeclId) -> bool {
|
|
*self.decl_ids.get(decl_id).unwrap_or(&true) // by default it's visible
|
|
}
|
|
|
|
pub fn hide_decl_id(&mut self, decl_id: &DeclId) {
|
|
self.decl_ids.insert(*decl_id, false);
|
|
}
|
|
|
|
pub fn use_decl_id(&mut self, decl_id: &DeclId) {
|
|
self.decl_ids.insert(*decl_id, true);
|
|
}
|
|
|
|
/// Overwrite own values with the other
|
|
pub fn merge_with(&mut self, other: Visibility) {
|
|
self.decl_ids.extend(other.decl_ids);
|
|
}
|
|
|
|
/// Take new values from the other but keep own values
|
|
pub fn append(&mut self, other: &Visibility) {
|
|
for (decl_id, visible) in other.decl_ids.iter() {
|
|
if !self.decl_ids.contains_key(decl_id) {
|
|
self.decl_ids.insert(*decl_id, *visible);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub struct ScopeFrame {
|
|
/// List of both active and inactive overlays in this ScopeFrame.
|
|
///
|
|
/// The order does not have any meaning. Indexed locally (within this ScopeFrame) by
|
|
/// OverlayIds in active_overlays.
|
|
pub overlays: Vec<(Vec<u8>, OverlayFrame)>,
|
|
|
|
/// List of currently active overlays.
|
|
///
|
|
/// Order is significant: The last item points at the last activated overlay.
|
|
pub active_overlays: Vec<OverlayId>,
|
|
|
|
/// Removed overlays from previous scope frames / permanent state
|
|
pub removed_overlays: Vec<Vec<u8>>,
|
|
|
|
/// temporary storage for predeclarations
|
|
pub predecls: HashMap<Vec<u8>, DeclId>,
|
|
}
|
|
|
|
impl ScopeFrame {
|
|
pub fn new() -> Self {
|
|
Self {
|
|
overlays: vec![],
|
|
active_overlays: vec![],
|
|
removed_overlays: vec![],
|
|
predecls: HashMap::new(),
|
|
}
|
|
}
|
|
|
|
pub fn with_empty_overlay(name: Vec<u8>, origin: ModuleId, prefixed: bool) -> Self {
|
|
Self {
|
|
overlays: vec![(name, OverlayFrame::from_origin(origin, prefixed))],
|
|
active_overlays: vec![0],
|
|
removed_overlays: vec![],
|
|
predecls: HashMap::new(),
|
|
}
|
|
}
|
|
|
|
pub fn get_var(&self, var_name: &[u8]) -> Option<&VarId> {
|
|
for overlay_id in self.active_overlays.iter().rev() {
|
|
if let Some(var_id) = self
|
|
.overlays
|
|
.get(*overlay_id)
|
|
.expect("internal error: missing overlay")
|
|
.1
|
|
.vars
|
|
.get(var_name)
|
|
{
|
|
return Some(var_id);
|
|
}
|
|
}
|
|
|
|
None
|
|
}
|
|
|
|
pub fn active_overlay_ids(&self, removed_overlays: &mut Vec<Vec<u8>>) -> Vec<OverlayId> {
|
|
for name in &self.removed_overlays {
|
|
if !removed_overlays.contains(name) {
|
|
removed_overlays.push(name.clone());
|
|
}
|
|
}
|
|
|
|
self.active_overlays
|
|
.iter()
|
|
.filter(|id| !removed_overlays.contains(self.get_overlay_name(**id)))
|
|
.copied()
|
|
.collect()
|
|
}
|
|
|
|
pub fn active_overlays<'a, 'b>(
|
|
&'b self,
|
|
removed_overlays: &'a mut Vec<Vec<u8>>,
|
|
) -> impl DoubleEndedIterator<Item = &OverlayFrame> + 'a
|
|
where
|
|
'b: 'a,
|
|
{
|
|
self.active_overlay_ids(removed_overlays)
|
|
.into_iter()
|
|
.map(|id| self.get_overlay(id))
|
|
}
|
|
|
|
pub fn active_overlay_names(&self, removed_overlays: &mut Vec<Vec<u8>>) -> Vec<&Vec<u8>> {
|
|
self.active_overlay_ids(removed_overlays)
|
|
.iter()
|
|
.map(|id| self.get_overlay_name(*id))
|
|
.collect()
|
|
}
|
|
|
|
pub fn get_overlay_name(&self, overlay_id: OverlayId) -> &Vec<u8> {
|
|
&self
|
|
.overlays
|
|
.get(overlay_id)
|
|
.expect("internal error: missing overlay")
|
|
.0
|
|
}
|
|
|
|
pub fn get_overlay(&self, overlay_id: OverlayId) -> &OverlayFrame {
|
|
&self
|
|
.overlays
|
|
.get(overlay_id)
|
|
.expect("internal error: missing overlay")
|
|
.1
|
|
}
|
|
|
|
pub fn get_overlay_mut(&mut self, overlay_id: OverlayId) -> &mut OverlayFrame {
|
|
&mut self
|
|
.overlays
|
|
.get_mut(overlay_id)
|
|
.expect("internal error: missing overlay")
|
|
.1
|
|
}
|
|
|
|
pub fn find_overlay(&self, name: &[u8]) -> Option<OverlayId> {
|
|
self.overlays.iter().position(|(n, _)| n == name)
|
|
}
|
|
|
|
pub fn find_active_overlay(&self, name: &[u8]) -> Option<OverlayId> {
|
|
self.overlays
|
|
.iter()
|
|
.position(|(n, _)| n == name)
|
|
.and_then(|id| {
|
|
if self.active_overlays.contains(&id) {
|
|
Some(id)
|
|
} else {
|
|
None
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub struct OverlayFrame {
|
|
pub vars: HashMap<Vec<u8>, VarId>,
|
|
pub constants: HashMap<VarId, Value>,
|
|
pub predecls: HashMap<Vec<u8>, DeclId>, // temporary storage for predeclarations
|
|
pub decls: HashMap<(Vec<u8>, Type), DeclId>,
|
|
pub modules: HashMap<Vec<u8>, ModuleId>,
|
|
pub visibility: Visibility,
|
|
pub origin: ModuleId, // The original module the overlay was created from
|
|
pub prefixed: bool, // Whether the overlay has definitions prefixed with its name
|
|
}
|
|
|
|
impl OverlayFrame {
|
|
pub fn from_origin(origin: ModuleId, prefixed: bool) -> Self {
|
|
Self {
|
|
vars: HashMap::new(),
|
|
constants: HashMap::new(),
|
|
predecls: HashMap::new(),
|
|
decls: HashMap::new(),
|
|
modules: HashMap::new(),
|
|
visibility: Visibility::new(),
|
|
origin,
|
|
prefixed,
|
|
}
|
|
}
|
|
|
|
pub fn insert_decl(&mut self, name: Vec<u8>, input: Type, decl_id: DeclId) -> Option<DeclId> {
|
|
self.decls.insert((name, input), decl_id)
|
|
}
|
|
|
|
pub fn insert_module(&mut self, name: Vec<u8>, module_id: ModuleId) -> Option<ModuleId> {
|
|
self.modules.insert(name, module_id)
|
|
}
|
|
|
|
pub fn get_decl(&self, name: &[u8], input: &Type) -> Option<DeclId> {
|
|
if let Some(decl) = self.decls.get(&(name, input) as &dyn DeclKey) {
|
|
Some(*decl)
|
|
} else {
|
|
// then fallback to not using the input type
|
|
for decl_key in self.decls.keys() {
|
|
if decl_key.0 == name {
|
|
// FIXME: this fallback may give bad type information
|
|
// in the case where no matching type is found. But, at
|
|
// least we treat it as a found internal command rather
|
|
// than an external command, which would cause further issues
|
|
return Some(
|
|
*self
|
|
.decls
|
|
.get(decl_key)
|
|
.expect("internal error: found decl not actually found"),
|
|
);
|
|
}
|
|
}
|
|
|
|
None
|
|
}
|
|
}
|
|
}
|
|
|
|
trait DeclKey {
|
|
fn name(&self) -> &[u8];
|
|
fn input(&self) -> &Type;
|
|
}
|
|
|
|
impl Hash for dyn DeclKey + '_ {
|
|
fn hash<H: Hasher>(&self, state: &mut H) {
|
|
self.name().hash(state);
|
|
self.input().hash(state);
|
|
}
|
|
}
|
|
|
|
impl PartialEq for dyn DeclKey + '_ {
|
|
fn eq(&self, other: &Self) -> bool {
|
|
self.name() == other.name() && self.input() == other.input()
|
|
}
|
|
}
|
|
|
|
impl Eq for dyn DeclKey + '_ {}
|
|
|
|
impl<'a> DeclKey for (&'a [u8], &Type) {
|
|
fn name(&self) -> &[u8] {
|
|
self.0
|
|
}
|
|
|
|
fn input(&self) -> &Type {
|
|
self.1
|
|
}
|
|
}
|
|
|
|
impl DeclKey for (Vec<u8>, Type) {
|
|
fn name(&self) -> &[u8] {
|
|
&self.0
|
|
}
|
|
|
|
fn input(&self) -> &Type {
|
|
&self.1
|
|
}
|
|
}
|
|
|
|
impl<'a> Borrow<dyn DeclKey + 'a> for (Vec<u8>, Type) {
|
|
fn borrow(&self) -> &(dyn DeclKey + 'a) {
|
|
self
|
|
}
|
|
}
|
|
|
|
impl Default for Visibility {
|
|
fn default() -> Self {
|
|
Self::new()
|
|
}
|
|
}
|
|
|
|
impl Default for ScopeFrame {
|
|
fn default() -> Self {
|
|
Self::new()
|
|
}
|
|
}
|