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
36 changed files with 317 additions and 185 deletions

View File

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

View File

@ -1,16 +1,16 @@
use nu_protocol::{
ast::Expr,
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};
pub struct ScopeData<'e, 's> {
engine_state: &'e EngineState,
stack: &'s Stack,
vars_map: HashMap<&'e Vec<u8>, &'e usize>,
decls_map: HashMap<&'e Vec<u8>, &'e usize>,
modules_map: HashMap<&'e Vec<u8>, &'e usize>,
vars_map: HashMap<&'e Vec<u8>, &'e VarId>,
decls_map: HashMap<&'e Vec<u8>, &'e DeclId>,
modules_map: HashMap<&'e Vec<u8>, &'e ModuleId>,
visibility: Visibility,
}
@ -62,7 +62,7 @@ impl<'e, 's> ScopeData<'e, 's> {
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(
record! {
@ -116,7 +116,7 @@ impl<'e, 's> ScopeData<'e, 's> {
"creates_scope" => Value::bool(signature.creates_scope, span),
"extra_description" => Value::string(decl.extra_description(), 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))
@ -333,7 +333,7 @@ impl<'e, 's> ScopeData<'e, 's> {
let record = record! {
"name" => Value::string(String::from_utf8_lossy(command_name), 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))
@ -353,7 +353,7 @@ impl<'e, 's> ScopeData<'e, 's> {
if let Some(alias) = decl.as_alias() {
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 {
Value::nothing(span)
};
@ -367,7 +367,7 @@ impl<'e, 's> ScopeData<'e, 's> {
"name" => Value::string(String::from_utf8_lossy(&decl_name), span),
"expansion" => Value::string(expansion, 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,
},
span,
@ -395,7 +395,7 @@ impl<'e, 's> ScopeData<'e, 's> {
Some(Value::record(
record! {
"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,
))
@ -414,7 +414,7 @@ impl<'e, 's> ScopeData<'e, 's> {
Some(Value::record(
record! {
"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,
))
@ -433,7 +433,7 @@ impl<'e, 's> ScopeData<'e, 's> {
Some(Value::record(
record! {
"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,
))
@ -457,7 +457,7 @@ impl<'e, 's> ScopeData<'e, 's> {
record! {
"name" => Value::string(String::from_utf8_lossy(name_bytes), 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,
)
@ -486,7 +486,7 @@ impl<'e, 's> ScopeData<'e, 's> {
"has_env_block" => Value::bool(module.env_block.is_some(), span),
"description" => Value::string(module_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,
)