Replace raw usize IDs with new types (#13832)

# Description

In this PR I replaced most of the raw usize IDs with
[newtypes](https://doc.rust-lang.org/rust-by-example/generics/new_types.html).
Some other IDs already started using new types and in this PR I did not
want to touch them. To make the implementation less repetitive, I made
use of a generic `Id<T>` with marker structs. If this lands I would try
to move make other IDs also in this pattern.

Also at some places I needed to use `cast`, I'm not sure if the type was
incorrect and therefore casting not needed or if actually different ID
types intermingle sometimes.

# User-Facing Changes

Probably few, if you got a `DeclId` via a function and placed it later
again it will still work.
This commit is contained in:
Piepmatz 2024-09-30 13:20:15 +02:00 committed by GitHub
parent fc61416c79
commit f0c83a4459
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
36 changed files with 317 additions and 185 deletions

View file

@ -7,7 +7,7 @@ use nu_protocol::{
ast::{Argument, Call, Expr, Expression}, ast::{Argument, Call, Expr, Expression},
debugger::WithoutDebug, debugger::WithoutDebug,
engine::{Stack, StateWorkingSet}, engine::{Stack, StateWorkingSet},
CompletionSort, PipelineData, Span, Type, Value, CompletionSort, DeclId, PipelineData, Span, Type, Value,
}; };
use nu_utils::IgnoreCaseExt; use nu_utils::IgnoreCaseExt;
use std::collections::HashMap; use std::collections::HashMap;
@ -16,12 +16,12 @@ use super::completion_common::sort_suggestions;
pub struct CustomCompletion { pub struct CustomCompletion {
stack: Stack, stack: Stack,
decl_id: usize, decl_id: DeclId,
line: String, line: String,
} }
impl CustomCompletion { impl CustomCompletion {
pub fn new(stack: Stack, decl_id: usize, line: String) -> Self { pub fn new(stack: Stack, decl_id: DeclId, line: String) -> Self {
Self { Self {
stack, stack,
decl_id, decl_id,

View file

@ -2,7 +2,7 @@ use nu_engine::eval_block;
use nu_protocol::{ use nu_protocol::{
debugger::WithoutDebug, debugger::WithoutDebug,
engine::{EngineState, Stack}, engine::{EngineState, Stack},
IntoPipelineData, Span, Value, BlockId, IntoPipelineData, Span, Value,
}; };
use reedline::{menu_functions::parse_selection_char, Completer, Suggestion}; use reedline::{menu_functions::parse_selection_char, Completer, Suggestion};
use std::sync::Arc; use std::sync::Arc;
@ -10,7 +10,7 @@ use std::sync::Arc;
const SELECTION_CHAR: char = '!'; const SELECTION_CHAR: char = '!';
pub struct NuMenuCompleter { pub struct NuMenuCompleter {
block_id: usize, block_id: BlockId,
span: Span, span: Span,
stack: Stack, stack: Stack,
engine_state: Arc<EngineState>, engine_state: Arc<EngineState>,
@ -19,7 +19,7 @@ pub struct NuMenuCompleter {
impl NuMenuCompleter { impl NuMenuCompleter {
pub fn new( pub fn new(
block_id: usize, block_id: BlockId,
span: Span, span: Span,
stack: Stack, stack: Stack,
engine_state: Arc<EngineState>, engine_state: Arc<EngineState>,

View file

@ -2,7 +2,7 @@ use nu_engine::{
command_prelude::*, find_in_dirs_env, get_dirs_var_from_call, get_eval_block, redirect_env, command_prelude::*, find_in_dirs_env, get_dirs_var_from_call, get_eval_block, redirect_env,
}; };
use nu_parser::trim_quotes_str; use nu_parser::trim_quotes_str;
use nu_protocol::{ast::Expr, engine::CommandType}; use nu_protocol::{ast::Expr, engine::CommandType, ModuleId};
use std::path::Path; use std::path::Path;
@ -64,7 +64,7 @@ impl Command for OverlayUse {
let mut name_arg: Spanned<String> = call.req(engine_state, caller_stack, 0)?; let mut name_arg: Spanned<String> = call.req(engine_state, caller_stack, 0)?;
name_arg.item = trim_quotes_str(&name_arg.item).to_string(); name_arg.item = trim_quotes_str(&name_arg.item).to_string();
let maybe_origin_module_id = let maybe_origin_module_id: Option<ModuleId> =
if let Some(overlay_expr) = call.get_parser_info(caller_stack, "overlay_expr") { if let Some(overlay_expr) = call.get_parser_info(caller_stack, "overlay_expr") {
if let Expr::Overlay(module_id) = &overlay_expr.expr { if let Expr::Overlay(module_id) = &overlay_expr.expr {
*module_id *module_id

View file

@ -183,6 +183,7 @@ mod test {
use nu_protocol::{ use nu_protocol::{
ast::{CellPath, PathMember}, ast::{CellPath, PathMember},
engine::Closure, engine::Closure,
BlockId,
}; };
use std::collections::HashSet; use std::collections::HashSet;
@ -244,7 +245,7 @@ mod test {
Value::list(vec![Value::bool(true, span)], span), Value::list(vec![Value::bool(true, span)], span),
Value::closure( Value::closure(
Closure { Closure {
block_id: 0, block_id: BlockId::new(0),
captures: Vec::new(), captures: Vec::new(),
}, },
span, span,

View file

@ -271,7 +271,7 @@ pub fn debug_string_without_formatting(value: &Value) -> String {
.join(" ") .join(" ")
), ),
//TODO: It would be good to drill deeper into closures. //TODO: It would be good to drill deeper into closures.
Value::Closure { val, .. } => format!("<Closure {}>", val.block_id), Value::Closure { val, .. } => format!("<Closure {}>", val.block_id.get()),
Value::Nothing { .. } => String::new(), Value::Nothing { .. } => String::new(),
Value::Error { error, .. } => format!("{error:?}"), Value::Error { error, .. } => format!("{error:?}"),
Value::Binary { val, .. } => format!("{val:?}"), Value::Binary { val, .. } => format!("{val:?}"),

View file

@ -1,4 +1,5 @@
use nu_engine::command_prelude::*; use nu_engine::command_prelude::*;
use nu_protocol::{BlockId, DeclId};
#[derive(Clone)] #[derive(Clone)]
pub struct ViewIr; pub struct ViewIr;
@ -86,7 +87,8 @@ the declaration may not be in scope.
let decl_id = val let decl_id = val
.try_into() .try_into()
.ok() .ok()
.filter(|id| *id < engine_state.num_decls()) .map(DeclId::new)
.filter(|id| id.get() < engine_state.num_decls())
.ok_or_else(|| ShellError::IncorrectValue { .ok_or_else(|| ShellError::IncorrectValue {
msg: "not a valid decl id".into(), msg: "not a valid decl id".into(),
val_span: target.span(), val_span: target.span(),
@ -102,11 +104,15 @@ the declaration may not be in scope.
})? })?
} }
// Block by ID - often shows up in IR // Block by ID - often shows up in IR
Value::Int { val, .. } => val.try_into().map_err(|_| ShellError::IncorrectValue { Value::Int { val, .. } => {
msg: "not a valid block id".into(), val.try_into()
val_span: target.span(), .map(BlockId::new)
call_span: call.head, .map_err(|_| ShellError::IncorrectValue {
})?, msg: "not a valid block id".into(),
val_span: target.span(),
call_span: call.head,
})?
}
// Pass through errors // Pass through errors
Value::Error { error, .. } => return Err(*error), Value::Error { error, .. } => return Err(*error),
_ => { _ => {
@ -119,7 +125,7 @@ the declaration may not be in scope.
let Some(block) = engine_state.try_get_block(block_id) else { let Some(block) = engine_state.try_get_block(block_id) else {
return Err(ShellError::GenericError { return Err(ShellError::GenericError {
error: format!("Unknown block ID: {block_id}"), error: format!("Unknown block ID: {}", block_id.get()),
msg: "ensure the block ID is correct and try again".into(), msg: "ensure the block ID is correct and try again".into(),
span: Some(target.span()), span: Some(target.span()),
help: None, help: None,

View file

@ -2,7 +2,7 @@ use nu_engine::{
command_prelude::*, find_in_dirs_env, get_dirs_var_from_call, get_eval_block_with_early_return, command_prelude::*, find_in_dirs_env, get_dirs_var_from_call, get_eval_block_with_early_return,
redirect_env, redirect_env,
}; };
use nu_protocol::engine::CommandType; use nu_protocol::{engine::CommandType, BlockId};
use std::path::PathBuf; use std::path::PathBuf;
/// Source a file for environment variables. /// Source a file for environment variables.
@ -50,6 +50,7 @@ impl Command for SourceEnv {
// Note: this hidden positional is the block_id that corresponded to the 0th position // Note: this hidden positional is the block_id that corresponded to the 0th position
// it is put here by the parser // it is put here by the parser
let block_id: i64 = call.req_parser_info(engine_state, caller_stack, "block_id")?; let block_id: i64 = call.req_parser_info(engine_state, caller_stack, "block_id")?;
let block_id = BlockId::new(block_id as usize);
// Set the currently evaluated directory (file-relative PWD) // Set the currently evaluated directory (file-relative PWD)
let file_path = if let Some(path) = find_in_dirs_env( let file_path = if let Some(path) = find_in_dirs_env(
@ -78,7 +79,7 @@ impl Command for SourceEnv {
); );
// Evaluate the block // Evaluate the block
let block = engine_state.get_block(block_id as usize).clone(); let block = engine_state.get_block(block_id).clone();
let mut callee_stack = caller_stack let mut callee_stack = caller_stack
.gather_captures(engine_state, &block.captures) .gather_captures(engine_state, &block.captures)
.reset_pipes(); .reset_pipes();

View file

@ -118,7 +118,7 @@ fn local_into_string(value: Value, separator: &str, config: &Config) -> String {
.map(|(x, y)| format!("{}: {}", x, local_into_string(y, ", ", config))) .map(|(x, y)| format!("{}: {}", x, local_into_string(y, ", ", config)))
.collect::<Vec<_>>() .collect::<Vec<_>>()
.join(separator), .join(separator),
Value::Closure { val, .. } => format!("<Closure {}>", val.block_id), Value::Closure { val, .. } => format!("<Closure {}>", val.block_id.get()),
Value::Nothing { .. } => String::new(), Value::Nothing { .. } => String::new(),
Value::Error { error, .. } => format!("{error:?}"), Value::Error { error, .. } => format!("{error:?}"),
Value::Binary { val, .. } => format!("{val:?}"), Value::Binary { val, .. } => format!("{val:?}"),

View file

@ -1,5 +1,5 @@
use nu_engine::{command_prelude::*, get_eval_block_with_early_return}; use nu_engine::{command_prelude::*, get_eval_block_with_early_return};
use nu_protocol::engine::CommandType; use nu_protocol::{engine::CommandType, BlockId};
/// Source a file for environment variables. /// Source a file for environment variables.
#[derive(Clone)] #[derive(Clone)]
@ -44,7 +44,8 @@ impl Command for Source {
// Note: this hidden positional is the block_id that corresponded to the 0th position // Note: this hidden positional is the block_id that corresponded to the 0th position
// it is put here by the parser // it is put here by the parser
let block_id: i64 = call.req_parser_info(engine_state, stack, "block_id")?; let block_id: i64 = call.req_parser_info(engine_state, stack, "block_id")?;
let block = engine_state.get_block(block_id as usize).clone(); let block_id = BlockId::new(block_id as usize);
let block = engine_state.get_block(block_id).clone();
let eval_block_with_early_return = get_eval_block_with_early_return(engine_state); let eval_block_with_early_return = get_eval_block_with_early_return(engine_state);

View file

@ -10,8 +10,8 @@ use nu_protocol::{
debugger::DebugContext, debugger::DebugContext,
engine::{Closure, EngineState, Redirection, Stack, StateWorkingSet}, engine::{Closure, EngineState, Redirection, Stack, StateWorkingSet},
eval_base::Eval, eval_base::Eval,
ByteStreamSource, Config, DataSource, FromValue, IntoPipelineData, OutDest, PipelineData, BlockId, ByteStreamSource, Config, DataSource, FromValue, IntoPipelineData, OutDest,
PipelineMetadata, ShellError, Span, Spanned, Type, Value, VarId, ENV_VARIABLE_ID, PipelineData, PipelineMetadata, ShellError, Span, Spanned, Type, Value, VarId, ENV_VARIABLE_ID,
}; };
use nu_utils::IgnoreCaseExt; use nu_utils::IgnoreCaseExt;
use std::{fs::OpenOptions, path::PathBuf, sync::Arc}; use std::{fs::OpenOptions, path::PathBuf, sync::Arc};
@ -752,7 +752,7 @@ impl Eval for EvalRuntime {
fn eval_subexpression<D: DebugContext>( fn eval_subexpression<D: DebugContext>(
engine_state: &EngineState, engine_state: &EngineState,
stack: &mut Stack, stack: &mut Stack,
block_id: usize, block_id: BlockId,
span: Span, span: Span,
) -> Result<Value, ShellError> { ) -> Result<Value, ShellError> {
let block = engine_state.get_block(block_id); let block = engine_state.get_block(block_id);
@ -899,7 +899,7 @@ impl Eval for EvalRuntime {
fn eval_row_condition_or_closure( fn eval_row_condition_or_closure(
engine_state: &EngineState, engine_state: &EngineState,
stack: &mut Stack, stack: &mut Stack,
block_id: usize, block_id: BlockId,
span: Span, span: Span,
) -> Result<Value, ShellError> { ) -> Result<Value, ShellError> {
let captures = engine_state let captures = engine_state

View file

@ -1,16 +1,16 @@
use nu_protocol::{ use nu_protocol::{
ast::Expr, ast::Expr,
engine::{Command, EngineState, Stack, Visibility}, engine::{Command, EngineState, Stack, Visibility},
record, ModuleId, Signature, Span, SyntaxShape, Type, Value, record, DeclId, ModuleId, Signature, Span, SyntaxShape, Type, Value, VarId,
}; };
use std::{cmp::Ordering, collections::HashMap}; use std::{cmp::Ordering, collections::HashMap};
pub struct ScopeData<'e, 's> { pub struct ScopeData<'e, 's> {
engine_state: &'e EngineState, engine_state: &'e EngineState,
stack: &'s Stack, stack: &'s Stack,
vars_map: HashMap<&'e Vec<u8>, &'e usize>, vars_map: HashMap<&'e Vec<u8>, &'e VarId>,
decls_map: HashMap<&'e Vec<u8>, &'e usize>, decls_map: HashMap<&'e Vec<u8>, &'e DeclId>,
modules_map: HashMap<&'e Vec<u8>, &'e usize>, modules_map: HashMap<&'e Vec<u8>, &'e ModuleId>,
visibility: Visibility, visibility: Visibility,
} }
@ -62,7 +62,7 @@ impl<'e, 's> ScopeData<'e, 's> {
Value::nothing(span) Value::nothing(span)
}; };
let var_id_val = Value::int(**var_id as i64, span); let var_id_val = Value::int(var_id.get() as i64, span);
vars.push(Value::record( vars.push(Value::record(
record! { record! {
@ -116,7 +116,7 @@ impl<'e, 's> ScopeData<'e, 's> {
"creates_scope" => Value::bool(signature.creates_scope, span), "creates_scope" => Value::bool(signature.creates_scope, span),
"extra_description" => Value::string(decl.extra_description(), span), "extra_description" => Value::string(decl.extra_description(), span),
"search_terms" => Value::string(decl.search_terms().join(", "), span), "search_terms" => Value::string(decl.search_terms().join(", "), span),
"decl_id" => Value::int(**decl_id as i64, span), "decl_id" => Value::int(decl_id.get() as i64, span),
}; };
commands.push(Value::record(record, span)) commands.push(Value::record(record, span))
@ -333,7 +333,7 @@ impl<'e, 's> ScopeData<'e, 's> {
let record = record! { let record = record! {
"name" => Value::string(String::from_utf8_lossy(command_name), span), "name" => Value::string(String::from_utf8_lossy(command_name), span),
"description" => Value::string(decl.description(), span), "description" => Value::string(decl.description(), span),
"decl_id" => Value::int(**decl_id as i64, span), "decl_id" => Value::int(decl_id.get() as i64, span),
}; };
externals.push(Value::record(record, span)) externals.push(Value::record(record, span))
@ -353,7 +353,7 @@ impl<'e, 's> ScopeData<'e, 's> {
if let Some(alias) = decl.as_alias() { if let Some(alias) = decl.as_alias() {
let aliased_decl_id = if let Expr::Call(wrapped_call) = &alias.wrapped_call.expr let aliased_decl_id = if let Expr::Call(wrapped_call) = &alias.wrapped_call.expr
{ {
Value::int(wrapped_call.decl_id as i64, span) Value::int(wrapped_call.decl_id.get() as i64, span)
} else { } else {
Value::nothing(span) Value::nothing(span)
}; };
@ -367,7 +367,7 @@ impl<'e, 's> ScopeData<'e, 's> {
"name" => Value::string(String::from_utf8_lossy(&decl_name), span), "name" => Value::string(String::from_utf8_lossy(&decl_name), span),
"expansion" => Value::string(expansion, span), "expansion" => Value::string(expansion, span),
"description" => Value::string(alias.description(), span), "description" => Value::string(alias.description(), span),
"decl_id" => Value::int(decl_id as i64, span), "decl_id" => Value::int(decl_id.get() as i64, span),
"aliased_decl_id" => aliased_decl_id, "aliased_decl_id" => aliased_decl_id,
}, },
span, span,
@ -395,7 +395,7 @@ impl<'e, 's> ScopeData<'e, 's> {
Some(Value::record( Some(Value::record(
record! { record! {
"name" => Value::string(String::from_utf8_lossy(name_bytes), span), "name" => Value::string(String::from_utf8_lossy(name_bytes), span),
"decl_id" => Value::int(*decl_id as i64, span), "decl_id" => Value::int(decl_id.get() as i64, span),
}, },
span, span,
)) ))
@ -414,7 +414,7 @@ impl<'e, 's> ScopeData<'e, 's> {
Some(Value::record( Some(Value::record(
record! { record! {
"name" => Value::string(String::from_utf8_lossy(name_bytes), span), "name" => Value::string(String::from_utf8_lossy(name_bytes), span),
"decl_id" => Value::int(*decl_id as i64, span), "decl_id" => Value::int(decl_id.get() as i64, span),
}, },
span, span,
)) ))
@ -433,7 +433,7 @@ impl<'e, 's> ScopeData<'e, 's> {
Some(Value::record( Some(Value::record(
record! { record! {
"name" => Value::string(String::from_utf8_lossy(name_bytes), span), "name" => Value::string(String::from_utf8_lossy(name_bytes), span),
"decl_id" => Value::int(*decl_id as i64, span), "decl_id" => Value::int(decl_id.get() as i64, span),
}, },
span, span,
)) ))
@ -457,7 +457,7 @@ impl<'e, 's> ScopeData<'e, 's> {
record! { record! {
"name" => Value::string(String::from_utf8_lossy(name_bytes), span), "name" => Value::string(String::from_utf8_lossy(name_bytes), span),
"type" => Value::string(self.engine_state.get_var(*var_id).ty.to_string(), span), "type" => Value::string(self.engine_state.get_var(*var_id).ty.to_string(), span),
"var_id" => Value::int(*var_id as i64, span), "var_id" => Value::int(var_id.get() as i64, span),
}, },
span, span,
) )
@ -486,7 +486,7 @@ impl<'e, 's> ScopeData<'e, 's> {
"has_env_block" => Value::bool(module.env_block.is_some(), span), "has_env_block" => Value::bool(module.env_block.is_some(), span),
"description" => Value::string(module_desc, span), "description" => Value::string(module_desc, span),
"extra_description" => Value::string(module_extra_desc, span), "extra_description" => Value::string(module_extra_desc, span),
"module_id" => Value::int(*module_id as i64, span), "module_id" => Value::int(module_id.get() as i64, span),
}, },
span, span,
) )

View file

@ -3528,7 +3528,7 @@ pub fn parse_source(working_set: &mut StateWorkingSet, lite_command: &LiteComman
"block_id".to_string(), "block_id".to_string(),
Expression::new( Expression::new(
working_set, working_set,
Expr::Int(block_id as i64), Expr::Int(block_id.get() as i64),
spans[1], spans[1],
Type::Any, Type::Any,
), ),

View file

@ -11,7 +11,7 @@ use itertools::Itertools;
use log::trace; use log::trace;
use nu_engine::DIR_VAR_PARSER_INFO; use nu_engine::DIR_VAR_PARSER_INFO;
use nu_protocol::{ use nu_protocol::{
ast::*, engine::StateWorkingSet, eval_const::eval_constant, BlockId, DidYouMean, Flag, ast::*, engine::StateWorkingSet, eval_const::eval_constant, BlockId, DeclId, DidYouMean, Flag,
ParseError, PositionalArg, Signature, Span, Spanned, SyntaxShape, Type, VarId, ENV_VARIABLE_ID, ParseError, PositionalArg, Signature, Span, Spanned, SyntaxShape, Type, VarId, ENV_VARIABLE_ID,
IN_VARIABLE_ID, IN_VARIABLE_ID,
}; };
@ -921,9 +921,9 @@ pub fn parse_internal_call(
working_set: &mut StateWorkingSet, working_set: &mut StateWorkingSet,
command_span: Span, command_span: Span,
spans: &[Span], spans: &[Span],
decl_id: usize, decl_id: DeclId,
) -> ParsedInternalCall { ) -> ParsedInternalCall {
trace!("parsing: internal call (decl id: {})", decl_id); trace!("parsing: internal call (decl id: {})", decl_id.get());
let mut call = Call::new(command_span); let mut call = Call::new(command_span);
call.decl_id = decl_id; call.decl_id = decl_id;
@ -6583,6 +6583,7 @@ pub fn parse(
let mut errors = vec![]; let mut errors = vec![];
for (block_idx, block) in working_set.delta.blocks.iter().enumerate() { for (block_idx, block) in working_set.delta.blocks.iter().enumerate() {
let block_id = block_idx + working_set.permanent_state.num_blocks(); let block_id = block_idx + working_set.permanent_state.num_blocks();
let block_id = BlockId::new(block_id);
if !seen_blocks.contains_key(&block_id) { if !seen_blocks.contains_key(&block_id) {
let mut captures = vec![]; let mut captures = vec![];
@ -6625,7 +6626,7 @@ pub fn parse(
// already saved in permanent state // already saved in permanent state
if !captures.is_empty() if !captures.is_empty()
&& block_captures_empty && block_captures_empty
&& block_id >= working_set.permanent_state.num_blocks() && block_id.get() >= working_set.permanent_state.num_blocks()
{ {
let block = working_set.get_block_mut(block_id); let block = working_set.get_block_mut(block_id);
block.captures = captures.into_iter().map(|(var_id, _)| var_id).collect(); block.captures = captures.into_iter().map(|(var_id, _)| var_id).collect();

View file

@ -2,7 +2,7 @@ use nu_parser::*;
use nu_protocol::{ use nu_protocol::{
ast::{Argument, Expr, Expression, ExternalArgument, PathMember, Range}, ast::{Argument, Expr, Expression, ExternalArgument, PathMember, Range},
engine::{Call, Command, EngineState, Stack, StateWorkingSet}, engine::{Call, Command, EngineState, Stack, StateWorkingSet},
Category, ParseError, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Category, DeclId, ParseError, PipelineData, ShellError, Signature, Span, SyntaxShape, Type,
}; };
use rstest::rstest; use rstest::rstest;
@ -607,7 +607,7 @@ pub fn parse_call() {
assert!(element.redirection.is_none()); assert!(element.redirection.is_none());
if let Expr::Call(call) = &element.expr.expr { if let Expr::Call(call) = &element.expr.expr {
assert_eq!(call.decl_id, 0); assert_eq!(call.decl_id, DeclId::new(0));
} }
} }
@ -661,7 +661,7 @@ pub fn parse_call_short_flag_batch_arg_allowed() {
assert!(element.redirection.is_none()); assert!(element.redirection.is_none());
if let Expr::Call(call) = &element.expr.expr { if let Expr::Call(call) = &element.expr.expr {
assert_eq!(call.decl_id, 0); assert_eq!(call.decl_id, DeclId::new(0));
assert_eq!(call.arguments.len(), 2); assert_eq!(call.arguments.len(), 2);
matches!(call.arguments[0], Argument::Named((_, None, None))); matches!(call.arguments[0], Argument::Named((_, None, None)));
matches!(call.arguments[1], Argument::Named((_, None, Some(_)))); matches!(call.arguments[1], Argument::Named((_, None, Some(_))));

View file

@ -1,5 +1,8 @@
use nu_protocol::{CustomValue, IntoSpanned, ShellError, Spanned, Value}; use nu_protocol::{CustomValue, IntoSpanned, ShellError, Spanned, Value};
#[allow(unused_imports)] // both are definitely used
use nu_protocol::{BlockId, VarId};
/// Do something with all [`CustomValue`]s recursively within a `Value`. This is not limited to /// Do something with all [`CustomValue`]s recursively within a `Value`. This is not limited to
/// plugin custom values. /// plugin custom values.
pub fn with_custom_values_in<E>( pub fn with_custom_values_in<E>(
@ -36,8 +39,8 @@ fn find_custom_values() {
]), ]),
"closure" => Value::test_closure( "closure" => Value::test_closure(
Closure { Closure {
block_id: 0, block_id: BlockId::new(0),
captures: vec![(0, cv.clone()), (1, Value::test_string("foo"))] captures: vec![(VarId::new(0), cv.clone()), (VarId::new(1), Value::test_string("foo"))]
} }
), ),
}); });

View file

@ -177,7 +177,7 @@ impl<'a> PluginExecutionContext for PluginExecutionCommandContext<'a> {
error: "Plugin misbehaving".into(), error: "Plugin misbehaving".into(),
msg: format!( msg: format!(
"Tried to evaluate unknown block id: {}", "Tried to evaluate unknown block id: {}",
closure.item.block_id closure.item.block_id.get()
), ),
span: Some(closure.span), span: Some(closure.span),
help: None, help: None,
@ -226,10 +226,10 @@ impl<'a> PluginExecutionContext for PluginExecutionCommandContext<'a> {
redirect_stdout: bool, redirect_stdout: bool,
redirect_stderr: bool, redirect_stderr: bool,
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
if decl_id >= self.engine_state.num_decls() { if decl_id.get() >= self.engine_state.num_decls() {
return Err(ShellError::GenericError { return Err(ShellError::GenericError {
error: "Plugin misbehaving".into(), error: "Plugin misbehaving".into(),
msg: format!("Tried to call unknown decl id: {}", decl_id), msg: format!("Tried to call unknown decl id: {}", decl_id.get()),
span: Some(call.head), span: Some(call.head),
help: None, help: None,
inner: vec![], inner: vec![],

View file

@ -17,7 +17,7 @@ use nu_plugin_protocol::{
use nu_protocol::{ use nu_protocol::{
ast::{Math, Operator}, ast::{Math, Operator},
engine::Closure, engine::Closure,
ByteStreamType, CustomValue, DataSource, IntoInterruptiblePipelineData, IntoSpanned, BlockId, ByteStreamType, CustomValue, DataSource, IntoInterruptiblePipelineData, IntoSpanned,
PipelineData, PipelineMetadata, PluginMetadata, PluginSignature, ShellError, Signals, Span, PipelineData, PipelineMetadata, PluginMetadata, PluginSignature, ShellError, Signals, Span,
Spanned, Value, Spanned, Value,
}; };
@ -431,7 +431,7 @@ fn manager_consume_engine_call_forwards_to_subscriber_with_pipeline_data() -> Re
call: EngineCall::EvalClosure { call: EngineCall::EvalClosure {
closure: Spanned { closure: Spanned {
item: Closure { item: Closure {
block_id: 0, block_id: BlockId::new(0),
captures: vec![], captures: vec![],
}, },
span: Span::test_data(), span: Span::test_data(),

View file

@ -1,7 +1,9 @@
use std::sync::Arc; use std::sync::Arc;
use nu_plugin_protocol::test_util::{test_plugin_custom_value, TestCustomValue}; use nu_plugin_protocol::test_util::{test_plugin_custom_value, TestCustomValue};
use nu_protocol::{engine::Closure, record, CustomValue, IntoSpanned, ShellError, Span, Value}; use nu_protocol::{
engine::Closure, record, BlockId, CustomValue, IntoSpanned, ShellError, Span, Value, VarId,
};
use crate::{ use crate::{
test_util::test_plugin_custom_value_with_source, PluginCustomValueWithSource, PluginSource, test_util::test_plugin_custom_value_with_source, PluginCustomValueWithSource, PluginSource,
@ -132,8 +134,11 @@ fn check_closure_custom_values(
fn add_source_in_nested_closure() -> Result<(), ShellError> { fn add_source_in_nested_closure() -> Result<(), ShellError> {
let orig_custom_val = Value::test_custom_value(Box::new(test_plugin_custom_value())); let orig_custom_val = Value::test_custom_value(Box::new(test_plugin_custom_value()));
let mut val = Value::test_closure(Closure { let mut val = Value::test_closure(Closure {
block_id: 0, block_id: BlockId::new(0),
captures: vec![(0, orig_custom_val.clone()), (1, orig_custom_val.clone())], captures: vec![
(VarId::new(0), orig_custom_val.clone()),
(VarId::new(1), orig_custom_val.clone()),
],
}); });
let source = Arc::new(PluginSource::new_fake("foo")); let source = Arc::new(PluginSource::new_fake("foo"));
PluginCustomValueWithSource::add_source_in(&mut val, &source)?; PluginCustomValueWithSource::add_source_in(&mut val, &source)?;

View file

@ -627,7 +627,7 @@ pub enum EngineCallResponse<D> {
PipelineData(D), PipelineData(D),
Config(SharedCow<Config>), Config(SharedCow<Config>),
ValueMap(HashMap<String, Value>), ValueMap(HashMap<String, Value>),
Identifier(usize), Identifier(DeclId),
} }
impl<D> EngineCallResponse<D> { impl<D> EngineCallResponse<D> {

View file

@ -1,7 +1,7 @@
use crate::test_util::{expected_test_custom_value, test_plugin_custom_value, TestCustomValue}; use crate::test_util::{expected_test_custom_value, test_plugin_custom_value, TestCustomValue};
use super::PluginCustomValue; use super::PluginCustomValue;
use nu_protocol::{engine::Closure, record, CustomValue, ShellError, Span, Value}; use nu_protocol::{engine::Closure, record, BlockId, CustomValue, ShellError, Span, Value, VarId};
fn check_record_custom_values( fn check_record_custom_values(
val: &Value, val: &Value,
@ -156,8 +156,11 @@ fn serialize_in_list() -> Result<(), ShellError> {
fn serialize_in_closure() -> Result<(), ShellError> { fn serialize_in_closure() -> Result<(), ShellError> {
let orig_custom_val = Value::test_custom_value(Box::new(TestCustomValue(24))); let orig_custom_val = Value::test_custom_value(Box::new(TestCustomValue(24)));
let mut val = Value::test_closure(Closure { let mut val = Value::test_closure(Closure {
block_id: 0, block_id: BlockId::new(0),
captures: vec![(0, orig_custom_val.clone()), (1, orig_custom_val.clone())], captures: vec![
(VarId::new(0), orig_custom_val.clone()),
(VarId::new(1), orig_custom_val.clone()),
],
}); });
PluginCustomValue::serialize_custom_values_in(&mut val)?; PluginCustomValue::serialize_custom_values_in(&mut val)?;
@ -239,8 +242,11 @@ fn deserialize_in_list() -> Result<(), ShellError> {
fn deserialize_in_closure() -> Result<(), ShellError> { fn deserialize_in_closure() -> Result<(), ShellError> {
let orig_custom_val = Value::test_custom_value(Box::new(test_plugin_custom_value())); let orig_custom_val = Value::test_custom_value(Box::new(test_plugin_custom_value()));
let mut val = Value::test_closure(Closure { let mut val = Value::test_closure(Closure {
block_id: 0, block_id: BlockId::new(0),
captures: vec![(0, orig_custom_val.clone()), (1, orig_custom_val.clone())], captures: vec![
(VarId::new(0), orig_custom_val.clone()),
(VarId::new(1), orig_custom_val.clone()),
],
}); });
PluginCustomValue::deserialize_custom_values_in(&mut val)?; PluginCustomValue::deserialize_custom_values_in(&mut val)?;

View file

@ -9,8 +9,8 @@ use nu_plugin_protocol::{
PluginCustomValue, PluginInput, PluginOutput, Protocol, ProtocolInfo, StreamData, PluginCustomValue, PluginInput, PluginOutput, Protocol, ProtocolInfo, StreamData,
}; };
use nu_protocol::{ use nu_protocol::{
engine::Closure, ByteStreamType, Config, CustomValue, IntoInterruptiblePipelineData, engine::Closure, BlockId, ByteStreamType, Config, CustomValue, IntoInterruptiblePipelineData,
LabeledError, PipelineData, PluginSignature, ShellError, Signals, Span, Spanned, Value, LabeledError, PipelineData, PluginSignature, ShellError, Signals, Span, Spanned, Value, VarId,
}; };
use std::{ use std::{
collections::HashMap, collections::HashMap,
@ -1040,8 +1040,8 @@ fn interface_eval_closure_with_stream() -> Result<(), ShellError> {
.eval_closure_with_stream( .eval_closure_with_stream(
&Spanned { &Spanned {
item: Closure { item: Closure {
block_id: 42, block_id: BlockId::new(42),
captures: vec![(0, Value::test_int(5))], captures: vec![(VarId::new(0), Value::test_int(5))],
}, },
span: Span::test_data(), span: Span::test_data(),
}, },
@ -1064,10 +1064,14 @@ fn interface_eval_closure_with_stream() -> Result<(), ShellError> {
redirect_stdout, redirect_stdout,
redirect_stderr, redirect_stderr,
} => { } => {
assert_eq!(42, closure.item.block_id, "closure.item.block_id"); assert_eq!(
BlockId::new(42),
closure.item.block_id,
"closure.item.block_id"
);
assert_eq!(1, closure.item.captures.len(), "closure.item.captures.len"); assert_eq!(1, closure.item.captures.len(), "closure.item.captures.len");
assert_eq!( assert_eq!(
(0, Value::test_int(5)), (VarId::new(0), Value::test_int(5)),
closure.item.captures[0], closure.item.captures[0],
"closure.item.captures[0]" "closure.item.captures[0]"
); );

View file

@ -96,7 +96,7 @@ pub struct Call {
impl Call { impl Call {
pub fn new(head: Span) -> Call { pub fn new(head: Span) -> Call {
Self { Self {
decl_id: 0, decl_id: DeclId::new(0),
head, head,
arguments: vec![], arguments: vec![],
parser_info: HashMap::new(), parser_info: HashMap::new(),

View file

@ -6,7 +6,7 @@ use super::{
Range, Table, ValueWithUnit, Range, Table, ValueWithUnit,
}; };
use crate::{ use crate::{
ast::ImportPattern, engine::StateWorkingSet, BlockId, OutDest, Signature, Span, VarId, ast::ImportPattern, engine::StateWorkingSet, BlockId, ModuleId, OutDest, Signature, Span, VarId,
}; };
/// An [`Expression`] AST node /// An [`Expression`] AST node
@ -47,7 +47,7 @@ pub enum Expr {
CellPath(CellPath), CellPath(CellPath),
FullCellPath(Box<FullCellPath>), FullCellPath(Box<FullCellPath>),
ImportPattern(Box<ImportPattern>), ImportPattern(Box<ImportPattern>),
Overlay(Option<BlockId>), // block ID of the overlay's origin module Overlay(Option<ModuleId>),
Signature(Box<Signature>), Signature(Box<Signature>),
StringInterpolation(Vec<Expression>), StringInterpolation(Vec<Expression>),
/// The boolean is `true` if the string is quoted. /// The boolean is `true` if the string is quoted.

View file

@ -31,7 +31,7 @@ impl Call<'_> {
// anyway. // anyway.
Call { Call {
head: span, head: span,
decl_id: 0, decl_id: DeclId::new(0),
inner: CallImpl::AstBox(Box::new(ast::Call::new(span))), inner: CallImpl::AstBox(Box::new(ast::Call::new(span))),
} }
} }

View file

@ -93,7 +93,7 @@ pub struct EngineState {
pub config: Arc<Config>, pub config: Arc<Config>,
pub pipeline_externals_state: Arc<(AtomicU32, AtomicU32)>, pub pipeline_externals_state: Arc<(AtomicU32, AtomicU32)>,
pub repl_state: Arc<Mutex<ReplState>>, pub repl_state: Arc<Mutex<ReplState>>,
pub table_decl_id: Option<usize>, pub table_decl_id: Option<DeclId>,
#[cfg(feature = "plugin")] #[cfg(feature = "plugin")]
pub plugin_path: Option<PathBuf>, pub plugin_path: Option<PathBuf>,
#[cfg(feature = "plugin")] #[cfg(feature = "plugin")]
@ -114,9 +114,9 @@ pub struct EngineState {
// The max number of compiled regexes to keep around in a LRU cache, arbitrarily chosen // The max number of compiled regexes to keep around in a LRU cache, arbitrarily chosen
const REGEX_CACHE_SIZE: usize = 100; // must be nonzero, otherwise will panic const REGEX_CACHE_SIZE: usize = 100; // must be nonzero, otherwise will panic
pub const NU_VARIABLE_ID: usize = 0; pub const NU_VARIABLE_ID: VarId = VarId::new(0);
pub const IN_VARIABLE_ID: usize = 1; pub const IN_VARIABLE_ID: VarId = VarId::new(1);
pub const ENV_VARIABLE_ID: usize = 2; pub const ENV_VARIABLE_ID: VarId = VarId::new(2);
// NOTE: If you add more to this list, make sure to update the > checks based on the last in the list // NOTE: If you add more to this list, make sure to update the > checks based on the last in the list
// The first span is unknown span // The first span is unknown span
@ -144,7 +144,7 @@ impl EngineState {
// make sure we have some default overlay: // make sure we have some default overlay:
scope: ScopeFrame::with_empty_overlay( scope: ScopeFrame::with_empty_overlay(
DEFAULT_OVERLAY_NAME.as_bytes().to_vec(), DEFAULT_OVERLAY_NAME.as_bytes().to_vec(),
0, ModuleId::new(0),
false, false,
), ),
signal_handlers: None, signal_handlers: None,
@ -380,7 +380,7 @@ impl EngineState {
let other_names = other.active_overlays.iter().map(|other_id| { let other_names = other.active_overlays.iter().map(|other_id| {
&other &other
.overlays .overlays
.get(*other_id) .get(other_id.get())
.expect("internal error: missing overlay") .expect("internal error: missing overlay")
.0 .0
}); });
@ -410,7 +410,7 @@ impl EngineState {
&self &self
.scope .scope
.overlays .overlays
.get(overlay_id) .get(overlay_id.get())
.expect("internal error: missing overlay") .expect("internal error: missing overlay")
.0 .0
} }
@ -419,7 +419,7 @@ impl EngineState {
&self &self
.scope .scope
.overlays .overlays
.get(overlay_id) .get(overlay_id.get())
.expect("internal error: missing overlay") .expect("internal error: missing overlay")
.1 .1
} }
@ -763,7 +763,7 @@ impl EngineState {
pub fn get_var(&self, var_id: VarId) -> &Variable { pub fn get_var(&self, var_id: VarId) -> &Variable {
self.vars self.vars
.get(var_id) .get(var_id.get())
.expect("internal error: missing variable") .expect("internal error: missing variable")
} }
@ -773,12 +773,12 @@ impl EngineState {
} }
pub fn generate_nu_constant(&mut self) { pub fn generate_nu_constant(&mut self) {
self.vars[NU_VARIABLE_ID].const_val = Some(create_nu_constant(self, Span::unknown())); self.vars[NU_VARIABLE_ID.get()].const_val = Some(create_nu_constant(self, Span::unknown()));
} }
pub fn get_decl(&self, decl_id: DeclId) -> &dyn Command { pub fn get_decl(&self, decl_id: DeclId) -> &dyn Command {
self.decls self.decls
.get(decl_id) .get(decl_id.get())
.expect("internal error: missing declaration") .expect("internal error: missing declaration")
.as_ref() .as_ref()
} }
@ -810,7 +810,7 @@ impl EngineState {
pub fn get_signature(&self, decl: &dyn Command) -> Signature { pub fn get_signature(&self, decl: &dyn Command) -> Signature {
if let Some(block_id) = decl.block_id() { if let Some(block_id) = decl.block_id() {
*self.blocks[block_id].signature.clone() *self.blocks[block_id.get()].signature.clone()
} else { } else {
decl.signature() decl.signature()
} }
@ -830,7 +830,7 @@ impl EngineState {
pub fn get_block(&self, block_id: BlockId) -> &Arc<Block> { pub fn get_block(&self, block_id: BlockId) -> &Arc<Block> {
self.blocks self.blocks
.get(block_id) .get(block_id.get())
.expect("internal error: missing block") .expect("internal error: missing block")
} }
@ -840,18 +840,18 @@ impl EngineState {
/// are normally a compiler error. This only exists to stop plugins from crashing the engine if /// are normally a compiler error. This only exists to stop plugins from crashing the engine if
/// they send us something invalid. /// they send us something invalid.
pub fn try_get_block(&self, block_id: BlockId) -> Option<&Arc<Block>> { pub fn try_get_block(&self, block_id: BlockId) -> Option<&Arc<Block>> {
self.blocks.get(block_id) self.blocks.get(block_id.get())
} }
pub fn get_module(&self, module_id: ModuleId) -> &Module { pub fn get_module(&self, module_id: ModuleId) -> &Module {
self.modules self.modules
.get(module_id) .get(module_id.get())
.expect("internal error: missing module") .expect("internal error: missing module")
} }
pub fn get_virtual_path(&self, virtual_path_id: VirtualPathId) -> &(String, VirtualPath) { pub fn get_virtual_path(&self, virtual_path_id: VirtualPathId) -> &(String, VirtualPath) {
self.virtual_paths self.virtual_paths
.get(virtual_path_id) .get(virtual_path_id.get())
.expect("internal error: missing virtual path") .expect("internal error: missing virtual path")
} }
@ -879,7 +879,7 @@ impl EngineState {
covered_span, covered_span,
}); });
self.num_files() - 1 FileId::new(self.num_files() - 1)
} }
pub fn set_config_path(&mut self, key: &str, val: PathBuf) { pub fn set_config_path(&mut self, key: &str, val: PathBuf) {
@ -1065,7 +1065,7 @@ mod engine_state_tests {
let mut engine_state = StateWorkingSet::new(&engine_state); let mut engine_state = StateWorkingSet::new(&engine_state);
let id = engine_state.add_file("test.nu".into(), &[]); let id = engine_state.add_file("test.nu".into(), &[]);
assert_eq!(id, 0); assert_eq!(id, FileId::new(0));
} }
#[test] #[test]
@ -1076,8 +1076,8 @@ mod engine_state_tests {
let mut working_set = StateWorkingSet::new(&engine_state); let mut working_set = StateWorkingSet::new(&engine_state);
let working_set_id = working_set.add_file("child.nu".into(), &[]); let working_set_id = working_set.add_file("child.nu".into(), &[]);
assert_eq!(parent_id, 0); assert_eq!(parent_id, FileId::new(0));
assert_eq!(working_set_id, 1); assert_eq!(working_set_id, FileId::new(1));
} }
#[test] #[test]

View file

@ -76,7 +76,7 @@ impl ScopeFrame {
pub fn with_empty_overlay(name: Vec<u8>, origin: ModuleId, prefixed: bool) -> Self { pub fn with_empty_overlay(name: Vec<u8>, origin: ModuleId, prefixed: bool) -> Self {
Self { Self {
overlays: vec![(name, OverlayFrame::from_origin(origin, prefixed))], overlays: vec![(name, OverlayFrame::from_origin(origin, prefixed))],
active_overlays: vec![0], active_overlays: vec![OverlayId::new(0)],
removed_overlays: vec![], removed_overlays: vec![],
predecls: HashMap::new(), predecls: HashMap::new(),
} }
@ -86,7 +86,7 @@ impl ScopeFrame {
for overlay_id in self.active_overlays.iter().rev() { for overlay_id in self.active_overlays.iter().rev() {
if let Some(var_id) = self if let Some(var_id) = self
.overlays .overlays
.get(*overlay_id) .get(overlay_id.get())
.expect("internal error: missing overlay") .expect("internal error: missing overlay")
.1 .1
.vars .vars
@ -139,7 +139,7 @@ impl ScopeFrame {
pub fn get_overlay_name(&self, overlay_id: OverlayId) -> &[u8] { pub fn get_overlay_name(&self, overlay_id: OverlayId) -> &[u8] {
&self &self
.overlays .overlays
.get(overlay_id) .get(overlay_id.get())
.expect("internal error: missing overlay") .expect("internal error: missing overlay")
.0 .0
} }
@ -147,7 +147,7 @@ impl ScopeFrame {
pub fn get_overlay(&self, overlay_id: OverlayId) -> &OverlayFrame { pub fn get_overlay(&self, overlay_id: OverlayId) -> &OverlayFrame {
&self &self
.overlays .overlays
.get(overlay_id) .get(overlay_id.get())
.expect("internal error: missing overlay") .expect("internal error: missing overlay")
.1 .1
} }
@ -155,19 +155,23 @@ impl ScopeFrame {
pub fn get_overlay_mut(&mut self, overlay_id: OverlayId) -> &mut OverlayFrame { pub fn get_overlay_mut(&mut self, overlay_id: OverlayId) -> &mut OverlayFrame {
&mut self &mut self
.overlays .overlays
.get_mut(overlay_id) .get_mut(overlay_id.get())
.expect("internal error: missing overlay") .expect("internal error: missing overlay")
.1 .1
} }
pub fn find_overlay(&self, name: &[u8]) -> Option<OverlayId> { pub fn find_overlay(&self, name: &[u8]) -> Option<OverlayId> {
self.overlays.iter().position(|(n, _)| n == name) self.overlays
.iter()
.position(|(n, _)| n == name)
.map(OverlayId::new)
} }
pub fn find_active_overlay(&self, name: &[u8]) -> Option<OverlayId> { pub fn find_active_overlay(&self, name: &[u8]) -> Option<OverlayId> {
self.overlays self.overlays
.iter() .iter()
.position(|(n, _)| n == name) .position(|(n, _)| n == name)
.map(OverlayId::new)
.filter(|id| self.active_overlays.contains(id)) .filter(|id| self.active_overlays.contains(id))
} }
} }

View file

@ -734,7 +734,7 @@ impl Stack {
mod test { mod test {
use std::sync::Arc; use std::sync::Arc;
use crate::{engine::EngineState, Span, Value}; use crate::{engine::EngineState, Span, Value, VarId};
use super::Stack; use super::Stack;
@ -749,22 +749,25 @@ mod test {
#[test] #[test]
fn test_children_see_inner_values() { fn test_children_see_inner_values() {
let mut original = Stack::new(); let mut original = Stack::new();
original.add_var(0, string_value("hello")); original.add_var(VarId::new(0), string_value("hello"));
let cloned = Stack::with_parent(Arc::new(original)); let cloned = Stack::with_parent(Arc::new(original));
assert_eq!(cloned.get_var(0, ZERO_SPAN), Ok(string_value("hello"))); assert_eq!(
cloned.get_var(VarId::new(0), ZERO_SPAN),
Ok(string_value("hello"))
);
} }
#[test] #[test]
fn test_children_dont_see_deleted_values() { fn test_children_dont_see_deleted_values() {
let mut original = Stack::new(); let mut original = Stack::new();
original.add_var(0, string_value("hello")); original.add_var(VarId::new(0), string_value("hello"));
let mut cloned = Stack::with_parent(Arc::new(original)); let mut cloned = Stack::with_parent(Arc::new(original));
cloned.remove_var(0); cloned.remove_var(VarId::new(0));
assert_eq!( assert_eq!(
cloned.get_var(0, ZERO_SPAN), cloned.get_var(VarId::new(0), ZERO_SPAN),
Err(crate::ShellError::VariableNotFoundAtRuntime { span: ZERO_SPAN }) Err(crate::ShellError::VariableNotFoundAtRuntime { span: ZERO_SPAN })
); );
} }
@ -772,60 +775,69 @@ mod test {
#[test] #[test]
fn test_children_changes_override_parent() { fn test_children_changes_override_parent() {
let mut original = Stack::new(); let mut original = Stack::new();
original.add_var(0, string_value("hello")); original.add_var(VarId::new(0), string_value("hello"));
let mut cloned = Stack::with_parent(Arc::new(original)); let mut cloned = Stack::with_parent(Arc::new(original));
cloned.add_var(0, string_value("there")); cloned.add_var(VarId::new(0), string_value("there"));
assert_eq!(cloned.get_var(0, ZERO_SPAN), Ok(string_value("there"))); assert_eq!(
cloned.get_var(VarId::new(0), ZERO_SPAN),
Ok(string_value("there"))
);
cloned.remove_var(0); cloned.remove_var(VarId::new(0));
// the underlying value shouldn't magically re-appear // the underlying value shouldn't magically re-appear
assert_eq!( assert_eq!(
cloned.get_var(0, ZERO_SPAN), cloned.get_var(VarId::new(0), ZERO_SPAN),
Err(crate::ShellError::VariableNotFoundAtRuntime { span: ZERO_SPAN }) Err(crate::ShellError::VariableNotFoundAtRuntime { span: ZERO_SPAN })
); );
} }
#[test] #[test]
fn test_children_changes_persist_in_offspring() { fn test_children_changes_persist_in_offspring() {
let mut original = Stack::new(); let mut original = Stack::new();
original.add_var(0, string_value("hello")); original.add_var(VarId::new(0), string_value("hello"));
let mut cloned = Stack::with_parent(Arc::new(original)); let mut cloned = Stack::with_parent(Arc::new(original));
cloned.add_var(1, string_value("there")); cloned.add_var(VarId::new(1), string_value("there"));
cloned.remove_var(0); cloned.remove_var(VarId::new(0));
let cloned = Stack::with_parent(Arc::new(cloned)); let cloned = Stack::with_parent(Arc::new(cloned));
assert_eq!( assert_eq!(
cloned.get_var(0, ZERO_SPAN), cloned.get_var(VarId::new(0), ZERO_SPAN),
Err(crate::ShellError::VariableNotFoundAtRuntime { span: ZERO_SPAN }) Err(crate::ShellError::VariableNotFoundAtRuntime { span: ZERO_SPAN })
); );
assert_eq!(cloned.get_var(1, ZERO_SPAN), Ok(string_value("there"))); assert_eq!(
cloned.get_var(VarId::new(1), ZERO_SPAN),
Ok(string_value("there"))
);
} }
#[test] #[test]
fn test_merging_children_back_to_parent() { fn test_merging_children_back_to_parent() {
let mut original = Stack::new(); let mut original = Stack::new();
let engine_state = EngineState::new(); let engine_state = EngineState::new();
original.add_var(0, string_value("hello")); original.add_var(VarId::new(0), string_value("hello"));
let original_arc = Arc::new(original); let original_arc = Arc::new(original);
let mut cloned = Stack::with_parent(original_arc.clone()); let mut cloned = Stack::with_parent(original_arc.clone());
cloned.add_var(1, string_value("there")); cloned.add_var(VarId::new(1), string_value("there"));
cloned.remove_var(0); cloned.remove_var(VarId::new(0));
cloned.add_env_var("ADDED_IN_CHILD".to_string(), string_value("New Env Var")); cloned.add_env_var("ADDED_IN_CHILD".to_string(), string_value("New Env Var"));
let original = Stack::with_changes_from_child(original_arc, cloned); let original = Stack::with_changes_from_child(original_arc, cloned);
assert_eq!( assert_eq!(
original.get_var(0, ZERO_SPAN), original.get_var(VarId::new(0), ZERO_SPAN),
Err(crate::ShellError::VariableNotFoundAtRuntime { span: ZERO_SPAN }) Err(crate::ShellError::VariableNotFoundAtRuntime { span: ZERO_SPAN })
); );
assert_eq!(original.get_var(1, ZERO_SPAN), Ok(string_value("there"))); assert_eq!(
original.get_var(VarId::new(1), ZERO_SPAN),
Ok(string_value("there"))
);
assert_eq!( assert_eq!(
original.get_env_var(&engine_state, "ADDED_IN_CHILD"), original.get_env_var(&engine_state, "ADDED_IN_CHILD"),

View file

@ -98,7 +98,7 @@ impl StateDelta {
Some( Some(
&mut last_scope &mut last_scope
.overlays .overlays
.get_mut(*last_overlay_id) .get_mut(last_overlay_id.get())
.expect("internal error: missing required overlay") .expect("internal error: missing required overlay")
.1, .1,
) )
@ -117,7 +117,7 @@ impl StateDelta {
Some( Some(
&last_scope &last_scope
.overlays .overlays
.get(*last_overlay_id) .get(last_overlay_id.get())
.expect("internal error: missing required overlay") .expect("internal error: missing required overlay")
.1, .1,
) )

View file

@ -4,8 +4,8 @@ use crate::{
description::build_desc, CachedFile, Command, CommandType, EngineState, OverlayFrame, description::build_desc, CachedFile, Command, CommandType, EngineState, OverlayFrame,
StateDelta, Variable, VirtualPath, Visibility, StateDelta, Variable, VirtualPath, Visibility,
}, },
BlockId, Category, CompileError, Config, DeclId, FileId, GetSpan, Module, ModuleId, ParseError, BlockId, Category, CompileError, Config, DeclId, FileId, GetSpan, Module, ModuleId, OverlayId,
ParseWarning, Signature, Span, SpanId, Type, Value, VarId, VirtualPathId, ParseError, ParseWarning, Signature, Span, SpanId, Type, Value, VarId, VirtualPathId,
}; };
use core::panic; use core::panic;
use std::{ use std::{
@ -92,7 +92,7 @@ impl<'a> StateWorkingSet<'a> {
for overlay_id in scope_frame.active_overlays.iter().rev() { for overlay_id in scope_frame.active_overlays.iter().rev() {
let (overlay_name, _) = scope_frame let (overlay_name, _) = scope_frame
.overlays .overlays
.get(*overlay_id) .get(overlay_id.get())
.expect("internal error: missing overlay"); .expect("internal error: missing overlay");
names.insert(overlay_name); names.insert(overlay_name);
@ -112,6 +112,7 @@ impl<'a> StateWorkingSet<'a> {
self.delta.decls.push(decl); self.delta.decls.push(decl);
let decl_id = self.num_decls() - 1; let decl_id = self.num_decls() - 1;
let decl_id = DeclId::new(decl_id);
self.last_overlay_mut().insert_decl(name, decl_id); self.last_overlay_mut().insert_decl(name, decl_id);
@ -152,6 +153,7 @@ impl<'a> StateWorkingSet<'a> {
self.delta.decls.push(decl); self.delta.decls.push(decl);
let decl_id = self.num_decls() - 1; let decl_id = self.num_decls() - 1;
let decl_id = DeclId::new(decl_id);
self.delta self.delta
.last_scope_frame_mut() .last_scope_frame_mut()
@ -268,7 +270,7 @@ impl<'a> StateWorkingSet<'a> {
self.delta.blocks.push(block); self.delta.blocks.push(block);
self.num_blocks() - 1 BlockId::new(self.num_blocks() - 1)
} }
pub fn add_module(&mut self, name: &str, module: Module, comments: Vec<Span>) -> ModuleId { pub fn add_module(&mut self, name: &str, module: Module, comments: Vec<Span>) -> ModuleId {
@ -276,6 +278,7 @@ impl<'a> StateWorkingSet<'a> {
self.delta.modules.push(Arc::new(module)); self.delta.modules.push(Arc::new(module));
let module_id = self.num_modules() - 1; let module_id = self.num_modules() - 1;
let module_id = ModuleId::new(module_id);
if !comments.is_empty() { if !comments.is_empty() {
self.delta self.delta
@ -314,7 +317,7 @@ impl<'a> StateWorkingSet<'a> {
} }
pub fn get_contents_of_file(&self, file_id: FileId) -> Option<&[u8]> { pub fn get_contents_of_file(&self, file_id: FileId) -> Option<&[u8]> {
if let Some(cached_file) = self.permanent_state.get_file_contents().get(file_id) { if let Some(cached_file) = self.permanent_state.get_file_contents().get(file_id.get()) {
return Some(&cached_file.content); return Some(&cached_file.content);
} }
// The index subtraction will not underflow, if we hit the permanent state first. // The index subtraction will not underflow, if we hit the permanent state first.
@ -322,7 +325,7 @@ impl<'a> StateWorkingSet<'a> {
if let Some(cached_file) = self if let Some(cached_file) = self
.delta .delta
.get_file_contents() .get_file_contents()
.get(file_id - self.permanent_state.num_files()) .get(file_id.get() - self.permanent_state.num_files())
{ {
return Some(&cached_file.content); return Some(&cached_file.content);
} }
@ -335,7 +338,7 @@ impl<'a> StateWorkingSet<'a> {
// First, look for the file to see if we already have it // First, look for the file to see if we already have it
for (idx, cached_file) in self.files().enumerate() { for (idx, cached_file) in self.files().enumerate() {
if *cached_file.name == filename && &*cached_file.content == contents { if *cached_file.name == filename && &*cached_file.content == contents {
return idx; return FileId::new(idx);
} }
} }
@ -350,18 +353,19 @@ impl<'a> StateWorkingSet<'a> {
covered_span, covered_span,
}); });
self.num_files() - 1 FileId::new(self.num_files() - 1)
} }
#[must_use] #[must_use]
pub fn add_virtual_path(&mut self, name: String, virtual_path: VirtualPath) -> VirtualPathId { pub fn add_virtual_path(&mut self, name: String, virtual_path: VirtualPath) -> VirtualPathId {
self.delta.virtual_paths.push((name, virtual_path)); self.delta.virtual_paths.push((name, virtual_path));
self.num_virtual_paths() - 1 VirtualPathId::new(self.num_virtual_paths() - 1)
} }
pub fn get_span_for_filename(&self, filename: &str) -> Option<Span> { pub fn get_span_for_filename(&self, filename: &str) -> Option<Span> {
let file_id = self.files().position(|file| &*file.name == filename)?; let file_id = self.files().position(|file| &*file.name == filename)?;
let file_id = FileId::new(file_id);
Some(self.get_span_for_file(file_id)) Some(self.get_span_for_file(file_id))
} }
@ -373,7 +377,7 @@ impl<'a> StateWorkingSet<'a> {
pub fn get_span_for_file(&self, file_id: FileId) -> Span { pub fn get_span_for_file(&self, file_id: FileId) -> Span {
let result = self let result = self
.files() .files()
.nth(file_id) .nth(file_id.get())
.expect("internal error: could not find source for previously parsed file"); .expect("internal error: could not find source for previously parsed file");
result.covered_span result.covered_span
@ -526,7 +530,7 @@ impl<'a> StateWorkingSet<'a> {
pub fn next_var_id(&self) -> VarId { pub fn next_var_id(&self) -> VarId {
let num_permanent_vars = self.permanent_state.num_vars(); let num_permanent_vars = self.permanent_state.num_vars();
num_permanent_vars + self.delta.vars.len() VarId::new(num_permanent_vars + self.delta.vars.len())
} }
pub fn list_variables(&self) -> Vec<&[u8]> { pub fn list_variables(&self) -> Vec<&[u8]> {
@ -635,40 +639,40 @@ impl<'a> StateWorkingSet<'a> {
pub fn set_variable_type(&mut self, var_id: VarId, ty: Type) { pub fn set_variable_type(&mut self, var_id: VarId, ty: Type) {
let num_permanent_vars = self.permanent_state.num_vars(); let num_permanent_vars = self.permanent_state.num_vars();
if var_id < num_permanent_vars { if var_id.get() < num_permanent_vars {
panic!("Internal error: attempted to set into permanent state from working set") panic!("Internal error: attempted to set into permanent state from working set")
} else { } else {
self.delta.vars[var_id - num_permanent_vars].ty = ty; self.delta.vars[var_id.get() - num_permanent_vars].ty = ty;
} }
} }
pub fn set_variable_const_val(&mut self, var_id: VarId, val: Value) { pub fn set_variable_const_val(&mut self, var_id: VarId, val: Value) {
let num_permanent_vars = self.permanent_state.num_vars(); let num_permanent_vars = self.permanent_state.num_vars();
if var_id < num_permanent_vars { if var_id.get() < num_permanent_vars {
panic!("Internal error: attempted to set into permanent state from working set") panic!("Internal error: attempted to set into permanent state from working set")
} else { } else {
self.delta.vars[var_id - num_permanent_vars].const_val = Some(val); self.delta.vars[var_id.get() - num_permanent_vars].const_val = Some(val);
} }
} }
pub fn get_variable(&self, var_id: VarId) -> &Variable { pub fn get_variable(&self, var_id: VarId) -> &Variable {
let num_permanent_vars = self.permanent_state.num_vars(); let num_permanent_vars = self.permanent_state.num_vars();
if var_id < num_permanent_vars { if var_id.get() < num_permanent_vars {
self.permanent_state.get_var(var_id) self.permanent_state.get_var(var_id)
} else { } else {
self.delta self.delta
.vars .vars
.get(var_id - num_permanent_vars) .get(var_id.get() - num_permanent_vars)
.expect("internal error: missing variable") .expect("internal error: missing variable")
} }
} }
pub fn get_variable_if_possible(&self, var_id: VarId) -> Option<&Variable> { pub fn get_variable_if_possible(&self, var_id: VarId) -> Option<&Variable> {
let num_permanent_vars = self.permanent_state.num_vars(); let num_permanent_vars = self.permanent_state.num_vars();
if var_id < num_permanent_vars { if var_id.get() < num_permanent_vars {
Some(self.permanent_state.get_var(var_id)) Some(self.permanent_state.get_var(var_id))
} else { } else {
self.delta.vars.get(var_id - num_permanent_vars) self.delta.vars.get(var_id.get() - num_permanent_vars)
} }
} }
@ -687,12 +691,12 @@ impl<'a> StateWorkingSet<'a> {
pub fn get_decl(&self, decl_id: DeclId) -> &dyn Command { pub fn get_decl(&self, decl_id: DeclId) -> &dyn Command {
let num_permanent_decls = self.permanent_state.num_decls(); let num_permanent_decls = self.permanent_state.num_decls();
if decl_id < num_permanent_decls { if decl_id.get() < num_permanent_decls {
self.permanent_state.get_decl(decl_id) self.permanent_state.get_decl(decl_id)
} else { } else {
self.delta self.delta
.decls .decls
.get(decl_id - num_permanent_decls) .get(decl_id.get() - num_permanent_decls)
.expect("internal error: missing declaration") .expect("internal error: missing declaration")
.as_ref() .as_ref()
} }
@ -700,12 +704,12 @@ impl<'a> StateWorkingSet<'a> {
pub fn get_decl_mut(&mut self, decl_id: DeclId) -> &mut Box<dyn Command> { pub fn get_decl_mut(&mut self, decl_id: DeclId) -> &mut Box<dyn Command> {
let num_permanent_decls = self.permanent_state.num_decls(); let num_permanent_decls = self.permanent_state.num_decls();
if decl_id < num_permanent_decls { if decl_id.get() < num_permanent_decls {
panic!("internal error: can only mutate declarations in working set") panic!("internal error: can only mutate declarations in working set")
} else { } else {
self.delta self.delta
.decls .decls
.get_mut(decl_id - num_permanent_decls) .get_mut(decl_id.get() - num_permanent_decls)
.expect("internal error: missing declaration") .expect("internal error: missing declaration")
} }
} }
@ -756,36 +760,36 @@ impl<'a> StateWorkingSet<'a> {
pub fn get_block(&self, block_id: BlockId) -> &Arc<Block> { pub fn get_block(&self, block_id: BlockId) -> &Arc<Block> {
let num_permanent_blocks = self.permanent_state.num_blocks(); let num_permanent_blocks = self.permanent_state.num_blocks();
if block_id < num_permanent_blocks { if block_id.get() < num_permanent_blocks {
self.permanent_state.get_block(block_id) self.permanent_state.get_block(block_id)
} else { } else {
self.delta self.delta
.blocks .blocks
.get(block_id - num_permanent_blocks) .get(block_id.get() - num_permanent_blocks)
.expect("internal error: missing block") .expect("internal error: missing block")
} }
} }
pub fn get_module(&self, module_id: ModuleId) -> &Module { pub fn get_module(&self, module_id: ModuleId) -> &Module {
let num_permanent_modules = self.permanent_state.num_modules(); let num_permanent_modules = self.permanent_state.num_modules();
if module_id < num_permanent_modules { if module_id.get() < num_permanent_modules {
self.permanent_state.get_module(module_id) self.permanent_state.get_module(module_id)
} else { } else {
self.delta self.delta
.modules .modules
.get(module_id - num_permanent_modules) .get(module_id.get() - num_permanent_modules)
.expect("internal error: missing module") .expect("internal error: missing module")
} }
} }
pub fn get_block_mut(&mut self, block_id: BlockId) -> &mut Block { pub fn get_block_mut(&mut self, block_id: BlockId) -> &mut Block {
let num_permanent_blocks = self.permanent_state.num_blocks(); let num_permanent_blocks = self.permanent_state.num_blocks();
if block_id < num_permanent_blocks { if block_id.get() < num_permanent_blocks {
panic!("Attempt to mutate a block that is in the permanent (immutable) state") panic!("Attempt to mutate a block that is in the permanent (immutable) state")
} else { } else {
self.delta self.delta
.blocks .blocks
.get_mut(block_id - num_permanent_blocks) .get_mut(block_id.get() - num_permanent_blocks)
.map(Arc::make_mut) .map(Arc::make_mut)
.expect("internal error: missing block") .expect("internal error: missing block")
} }
@ -912,7 +916,7 @@ impl<'a> StateWorkingSet<'a> {
last_scope_frame last_scope_frame
.overlays .overlays
.push((name, OverlayFrame::from_origin(origin, prefixed))); .push((name, OverlayFrame::from_origin(origin, prefixed)));
last_scope_frame.overlays.len() - 1 OverlayId::new(last_scope_frame.overlays.len() - 1)
}; };
last_scope_frame last_scope_frame
@ -989,13 +993,13 @@ impl<'a> StateWorkingSet<'a> {
pub fn find_module_by_span(&self, span: Span) -> Option<ModuleId> { pub fn find_module_by_span(&self, span: Span) -> Option<ModuleId> {
for (id, module) in self.delta.modules.iter().enumerate() { for (id, module) in self.delta.modules.iter().enumerate() {
if Some(span) == module.span { if Some(span) == module.span {
return Some(self.permanent_state.num_modules() + id); return Some(ModuleId::new(self.permanent_state.num_modules() + id));
} }
} }
for (module_id, module) in self.permanent_state.modules.iter().enumerate() { for (module_id, module) in self.permanent_state.modules.iter().enumerate() {
if Some(span) == module.span { if Some(span) == module.span {
return Some(module_id); return Some(ModuleId::new(module_id));
} }
} }
@ -1020,12 +1024,12 @@ impl<'a> StateWorkingSet<'a> {
pub fn get_virtual_path(&self, virtual_path_id: VirtualPathId) -> &(String, VirtualPath) { pub fn get_virtual_path(&self, virtual_path_id: VirtualPathId) -> &(String, VirtualPath) {
let num_permanent_virtual_paths = self.permanent_state.num_virtual_paths(); let num_permanent_virtual_paths = self.permanent_state.num_virtual_paths();
if virtual_path_id < num_permanent_virtual_paths { if virtual_path_id.get() < num_permanent_virtual_paths {
self.permanent_state.get_virtual_path(virtual_path_id) self.permanent_state.get_virtual_path(virtual_path_id)
} else { } else {
self.delta self.delta
.virtual_paths .virtual_paths
.get(virtual_path_id - num_permanent_virtual_paths) .get(virtual_path_id.get() - num_permanent_virtual_paths)
.expect("internal error: missing virtual path") .expect("internal error: missing virtual path")
} }
} }

View file

@ -5,7 +5,7 @@ use crate::{
ExternalArgument, ListItem, Math, Operator, RecordItem, ExternalArgument, ListItem, Math, Operator, RecordItem,
}, },
debugger::DebugContext, debugger::DebugContext,
Config, GetSpan, Range, Record, ShellError, Span, Value, VarId, ENV_VARIABLE_ID, BlockId, Config, GetSpan, Range, Record, ShellError, Span, Value, VarId, ENV_VARIABLE_ID,
}; };
use std::{collections::HashMap, sync::Arc}; use std::{collections::HashMap, sync::Arc};
@ -369,7 +369,7 @@ pub trait Eval {
fn eval_subexpression<D: DebugContext>( fn eval_subexpression<D: DebugContext>(
state: Self::State<'_>, state: Self::State<'_>,
mut_state: &mut Self::MutState, mut_state: &mut Self::MutState,
block_id: usize, block_id: BlockId,
span: Span, span: Span,
) -> Result<Value, ShellError>; ) -> Result<Value, ShellError>;
@ -396,7 +396,7 @@ pub trait Eval {
fn eval_row_condition_or_closure( fn eval_row_condition_or_closure(
state: Self::State<'_>, state: Self::State<'_>,
mut_state: &mut Self::MutState, mut_state: &mut Self::MutState,
block_id: usize, block_id: BlockId,
span: Span, span: Span,
) -> Result<Value, ShellError>; ) -> Result<Value, ShellError>;

View file

@ -7,7 +7,8 @@ use crate::{
debugger::{DebugContext, WithoutDebug}, debugger::{DebugContext, WithoutDebug},
engine::{EngineState, StateWorkingSet}, engine::{EngineState, StateWorkingSet},
eval_base::Eval, eval_base::Eval,
record, Config, HistoryFileFormat, PipelineData, Record, ShellError, Span, Value, VarId, record, BlockId, Config, HistoryFileFormat, PipelineData, Record, ShellError, Span, Value,
VarId,
}; };
use nu_system::os_info::{get_kernel_version, get_os_arch, get_os_family, get_os_name}; use nu_system::os_info::{get_kernel_version, get_os_arch, get_os_family, get_os_name};
use std::{ use std::{
@ -481,7 +482,7 @@ impl Eval for EvalConst {
fn eval_subexpression<D: DebugContext>( fn eval_subexpression<D: DebugContext>(
working_set: &StateWorkingSet, working_set: &StateWorkingSet,
_: &mut (), _: &mut (),
block_id: usize, block_id: BlockId,
span: Span, span: Span,
) -> Result<Value, ShellError> { ) -> Result<Value, ShellError> {
// TODO: Allow debugging const eval // TODO: Allow debugging const eval
@ -516,7 +517,7 @@ impl Eval for EvalConst {
fn eval_row_condition_or_closure( fn eval_row_condition_or_closure(
_: &StateWorkingSet, _: &StateWorkingSet,
_: &mut (), _: &mut (),
_: usize, _: BlockId,
span: Span, span: Span,
) -> Result<Value, ShellError> { ) -> Result<Value, ShellError> {
Err(ShellError::NotAConstant { span }) Err(ShellError::NotAConstant { span })

View file

@ -1,12 +1,90 @@
use std::any;
use std::fmt::{Debug, Error, Formatter};
use std::marker::PhantomData;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
pub type VarId = usize; #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub type DeclId = usize; pub struct Id<T> {
pub type BlockId = usize; inner: usize,
pub type ModuleId = usize; _phantom: PhantomData<T>,
pub type OverlayId = usize; }
pub type FileId = usize;
pub type VirtualPathId = usize; impl<T> Id<T> {
/// Creates a new `Id`.
///
/// Using a distinct type like `Id` instead of `usize` helps us avoid mixing plain integers
/// with identifiers.
#[inline]
pub const fn new(inner: usize) -> Self {
Self {
inner,
_phantom: PhantomData,
}
}
/// Returns the inner `usize` value.
///
/// This requires an explicit call, ensuring we only use the raw value when intended.
#[inline]
pub const fn get(self) -> usize {
self.inner
}
}
impl<T> Debug for Id<T> {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
let marker = any::type_name::<T>().split("::").last().expect("not empty");
write!(f, "{marker}Id({})", self.inner)
}
}
impl<T> Serialize for Id<T> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
self.inner.serialize(serializer)
}
}
impl<'de, T> Deserialize<'de> for Id<T> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let inner = usize::deserialize(deserializer)?;
Ok(Self {
inner,
_phantom: PhantomData,
})
}
}
pub mod marker {
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Var;
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Decl;
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Block;
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Module;
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Overlay;
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct File;
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct VirtualPath;
}
pub type VarId = Id<marker::Var>;
pub type DeclId = Id<marker::Decl>;
pub type BlockId = Id<marker::Block>;
pub type ModuleId = Id<marker::Module>;
pub type OverlayId = Id<marker::Overlay>;
pub type FileId = Id<marker::File>;
pub type VirtualPathId = Id<marker::VirtualPath>;
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)] #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct SpanId(pub usize); // more robust ID style used in the new parser pub struct SpanId(pub usize); // more robust ID style used in the new parser

View file

@ -276,11 +276,11 @@ impl<'a> FmtDecl<'a> {
impl fmt::Display for FmtDecl<'_> { impl fmt::Display for FmtDecl<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "decl {} {:?}", self.0, self.1) write!(f, "decl {} {:?}", self.0.get(), self.1)
} }
} }
struct FmtVar<'a>(DeclId, Option<&'a str>); struct FmtVar<'a>(VarId, Option<&'a str>);
impl<'a> FmtVar<'a> { impl<'a> FmtVar<'a> {
fn new(engine_state: &'a EngineState, var_id: VarId) -> Self { fn new(engine_state: &'a EngineState, var_id: VarId) -> Self {
@ -297,9 +297,9 @@ impl<'a> FmtVar<'a> {
impl fmt::Display for FmtVar<'_> { impl fmt::Display for FmtVar<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(name) = self.1 { if let Some(name) = self.1 {
write!(f, "var {} {:?}", self.0, name) write!(f, "var {} {:?}", self.0.get(), name)
} else { } else {
write!(f, "var {}", self.0) write!(f, "var {}", self.0.get())
} }
} }
} }
@ -346,9 +346,9 @@ impl<'a> fmt::Display for FmtLiteral<'a> {
Literal::Filesize(q) => write!(f, "filesize({q}b)"), Literal::Filesize(q) => write!(f, "filesize({q}b)"),
Literal::Duration(q) => write!(f, "duration({q}ns)"), Literal::Duration(q) => write!(f, "duration({q}ns)"),
Literal::Binary(b) => write!(f, "binary({})", FmtData(self.data, *b)), Literal::Binary(b) => write!(f, "binary({})", FmtData(self.data, *b)),
Literal::Block(id) => write!(f, "block({id})"), Literal::Block(id) => write!(f, "block({})", id.get()),
Literal::Closure(id) => write!(f, "closure({id})"), Literal::Closure(id) => write!(f, "closure({})", id.get()),
Literal::RowCondition(id) => write!(f, "row_condition({id})"), Literal::RowCondition(id) => write!(f, "row_condition({})", id.get()),
Literal::Range { Literal::Range {
start, start,
step, step,

View file

@ -1,4 +1,7 @@
use crate::engine::{StateWorkingSet, VirtualPath}; use crate::{
engine::{StateWorkingSet, VirtualPath},
FileId,
};
use std::{ use std::{
ffi::OsStr, ffi::OsStr,
path::{Path, PathBuf}, path::{Path, PathBuf},
@ -112,7 +115,7 @@ impl ParserPath {
std::fs::File::open(p).map(|f| Box::new(f) as Box<dyn std::io::Read>) std::fs::File::open(p).map(|f| Box::new(f) as Box<dyn std::io::Read>)
} }
ParserPath::VirtualFile(_, file_id) => working_set ParserPath::VirtualFile(_, file_id) => working_set
.get_contents_of_file(*file_id) .get_contents_of_file(FileId::new(*file_id))
.map(|bytes| Box::new(bytes) as Box<dyn std::io::Read>) .map(|bytes| Box::new(bytes) as Box<dyn std::io::Read>)
.ok_or(std::io::ErrorKind::NotFound.into()), .ok_or(std::io::ErrorKind::NotFound.into()),
@ -136,7 +139,9 @@ impl ParserPath {
virtual_path: &VirtualPath, virtual_path: &VirtualPath,
) -> Self { ) -> Self {
match virtual_path { match virtual_path {
VirtualPath::File(file_id) => ParserPath::VirtualFile(PathBuf::from(name), *file_id), VirtualPath::File(file_id) => {
ParserPath::VirtualFile(PathBuf::from(name), file_id.get())
}
VirtualPath::Dir(entries) => ParserPath::VirtualDir( VirtualPath::Dir(entries) => ParserPath::VirtualDir(
PathBuf::from(name), PathBuf::from(name),
entries entries

View file

@ -23,7 +23,7 @@ use crate::{
ast::{Bits, Boolean, CellPath, Comparison, Math, Operator, PathMember}, ast::{Bits, Boolean, CellPath, Comparison, Math, Operator, PathMember},
did_you_mean, did_you_mean,
engine::{Closure, EngineState}, engine::{Closure, EngineState},
Config, ShellError, Signals, Span, Type, BlockId, Config, ShellError, Signals, Span, Type,
}; };
use chrono::{DateTime, Datelike, FixedOffset, Locale, TimeZone}; use chrono::{DateTime, Datelike, FixedOffset, Locale, TimeZone};
use chrono_humanize::HumanTime; use chrono_humanize::HumanTime;
@ -863,7 +863,7 @@ impl Value {
.collect::<Vec<_>>() .collect::<Vec<_>>()
.join(separator) .join(separator)
), ),
Value::Closure { val, .. } => format!("<Closure {}>", val.block_id), Value::Closure { val, .. } => format!("<Closure {}>", val.block_id.get()),
Value::Nothing { .. } => String::new(), Value::Nothing { .. } => String::new(),
Value::Error { error, .. } => format!("{error:?}"), Value::Error { error, .. } => format!("{error:?}"),
Value::Binary { val, .. } => format!("{val:?}"), Value::Binary { val, .. } => format!("{val:?}"),
@ -2029,7 +2029,7 @@ impl Value {
Value::test_record(Record::new()), Value::test_record(Record::new()),
Value::test_list(Vec::new()), Value::test_list(Vec::new()),
Value::test_closure(Closure { Value::test_closure(Closure {
block_id: 0, block_id: BlockId::new(0),
captures: Vec::new(), captures: Vec::new(),
}), }),
Value::test_nothing(), Value::test_nothing(),

View file

@ -12,7 +12,7 @@ mod tests {
use nu_protocol::{ use nu_protocol::{
ast::{CellPath, PathMember, RangeInclusion}, ast::{CellPath, PathMember, RangeInclusion},
engine::Closure, engine::Closure,
record, IntRange, Range, Span, Value, record, BlockId, IntRange, Range, Span, Value,
}; };
use crate::{from_nuon, to_nuon, ToStyle}; use crate::{from_nuon, to_nuon, ToStyle};
@ -175,7 +175,7 @@ mod tests {
fn to_nuon_errs_on_closure() { fn to_nuon_errs_on_closure() {
assert!(to_nuon( assert!(to_nuon(
&Value::test_closure(Closure { &Value::test_closure(Closure {
block_id: 0, block_id: BlockId::new(0),
captures: vec![] captures: vec![]
}), }),
ToStyle::Raw, ToStyle::Raw,