From 24b68971a889edcbb95d7a827296db0f91e45ede Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sun, 16 Jan 2022 22:54:28 +0800 Subject: [PATCH] Fix offset calc. --- src/eval/chaining.rs | 16 ++++++++-------- src/eval/target.rs | 12 +++++++----- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/eval/chaining.rs b/src/eval/chaining.rs index 6ac788fd..d2589f0b 100644 --- a/src/eval/chaining.rs +++ b/src/eval/chaining.rs @@ -1,7 +1,7 @@ //! Types to support chaining operations (i.e. indexing and dotting). #![cfg(any(not(feature = "no_index"), not(feature = "no_object")))] -use super::{calc_index, EvalState, GlobalRuntimeState, Target}; +use super::{EvalState, GlobalRuntimeState, Target}; use crate::ast::{Expr, OpAssignment}; use crate::types::dynamic::Union; use crate::{Dynamic, Engine, Module, Position, RhaiResult, RhaiResultOf, Scope, StaticVec, ERR}; @@ -790,7 +790,7 @@ impl Engine { .as_int() .map_err(|typ| self.make_type_mismatch_err::(typ, pos))?; let len = arr.len(); - let arr_idx = calc_index(len, index, true, || { + let arr_idx = super::calc_index(len, index, true, || { ERR::ErrorArrayBounds(len, index, pos).into() })?; @@ -804,7 +804,7 @@ impl Engine { .as_int() .map_err(|typ| self.make_type_mismatch_err::(typ, pos))?; let len = arr.len(); - let arr_idx = calc_index(len, index, true, || { + let arr_idx = super::calc_index(len, index, true, || { ERR::ErrorArrayBounds(len, index, pos).into() })?; @@ -845,10 +845,10 @@ impl Engine { let start = range.start; let end = range.end; - let start = calc_index(BITS, start, false, || { + let start = super::calc_index(BITS, start, false, || { ERR::ErrorBitFieldBounds(BITS, start, pos).into() })?; - let end = calc_index(BITS, end, false, || { + let end = super::calc_index(BITS, end, false, || { ERR::ErrorBitFieldBounds(BITS, end, pos).into() })?; @@ -870,10 +870,10 @@ impl Engine { let start = *range.start(); let end = *range.end(); - let start = calc_index(BITS, start, false, || { + let start = super::calc_index(BITS, start, false, || { ERR::ErrorBitFieldBounds(BITS, start, pos).into() })?; - let end = calc_index(BITS, end, false, || { + let end = super::calc_index(BITS, end, false, || { ERR::ErrorBitFieldBounds(BITS, end, pos).into() })?; @@ -914,7 +914,7 @@ impl Engine { const BITS: usize = std::mem::size_of::() * 8; - let bit = calc_index(BITS, index, true, || { + let bit = super::calc_index(BITS, index, true, || { ERR::ErrorBitFieldBounds(BITS, index, pos).into() })?; diff --git a/src/eval/target.rs b/src/eval/target.rs index 4209c3fa..f31647f9 100644 --- a/src/eval/target.rs +++ b/src/eval/target.rs @@ -1,7 +1,7 @@ //! Type to hold a mutable reference to the target of an evaluation. use crate::types::dynamic::Variant; -use crate::{Dynamic, RhaiResultOf, INT}; +use crate::{Dynamic, RhaiResultOf}; use std::ops::{Deref, DerefMut}; #[cfg(feature = "no_std")] use std::prelude::v1::*; @@ -11,8 +11,9 @@ use std::prelude::v1::*; // Negative starting positions count from the end. // // Values going over bounds are limited to the actual length. -#[inline(always)] -pub fn calc_offset_len(length: usize, start: INT, len: INT) -> (usize, usize) { +#[inline] +#[allow(dead_code)] +pub fn calc_offset_len(length: usize, start: crate::INT, len: crate::INT) -> (usize, usize) { let start = if start < 0 { start.checked_abs().map_or(0, |positive_start| { length - usize::min(positive_start as usize, length) @@ -39,10 +40,11 @@ pub fn calc_offset_len(length: usize, start: INT, len: INT) -> (usize, usize) { // Negative starting positions count from the end. // // Values going over bounds call the provided closure to return a default value or an error. -#[inline(always)] +#[inline] +#[allow(dead_code)] pub fn calc_index( length: usize, - start: INT, + start: crate::INT, negative_count_from_end: bool, err: impl Fn() -> Result, ) -> Result {