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

@ -11,7 +11,7 @@ use itertools::Itertools;
use log::trace;
use nu_engine::DIR_VAR_PARSER_INFO;
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,
IN_VARIABLE_ID,
};
@ -921,9 +921,9 @@ pub fn parse_internal_call(
working_set: &mut StateWorkingSet,
command_span: Span,
spans: &[Span],
decl_id: usize,
decl_id: DeclId,
) -> 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);
call.decl_id = decl_id;
@ -6583,6 +6583,7 @@ pub fn parse(
let mut errors = vec![];
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 = BlockId::new(block_id);
if !seen_blocks.contains_key(&block_id) {
let mut captures = vec![];
@ -6625,7 +6626,7 @@ pub fn parse(
// already saved in permanent state
if !captures.is_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);
block.captures = captures.into_iter().map(|(var_id, _)| var_id).collect();