Add compiler check for signed integer array access

This commit is contained in:
collin 2020-09-02 13:11:42 -07:00
parent 3b737b2a34
commit 5210b1ec77
3 changed files with 22 additions and 3 deletions

View File

@ -83,8 +83,9 @@ impl IntegerError {
}
pub fn invalid_index(span: Span) -> Self {
let message =
format!("index must be a constant value integer. allocated indices produce a circuit of unknown size");
let message = format!(
"index must be a constant value unsigned integer. allocated indices produce a circuit of unknown size"
);
Self::new_from_span(message, span)
}

View File

@ -154,7 +154,10 @@ impl Integer {
}
pub fn to_usize(&self, span: Span) -> Result<usize, IntegerError> {
let value = self.get_value().ok_or(IntegerError::invalid_index(span.clone()))?;
let unsigned_integer = self;
let value_option: Option<String> = match_unsigned_integer!(unsigned_integer => unsigned_integer.get_value());
let value = value_option.ok_or(IntegerError::invalid_index(span.clone()))?;
let value_usize = value
.parse::<usize>()
.map_err(|_| IntegerError::invalid_integer(value, span))?;

View File

@ -65,6 +65,21 @@ macro_rules! match_integer {
};
}
#[macro_export]
macro_rules! match_unsigned_integer {
($integer: ident => $expression: expr) => {
match $integer {
Integer::U8($integer) => $expression,
Integer::U16($integer) => $expression,
Integer::U32($integer) => $expression,
Integer::U64($integer) => $expression,
Integer::U128($integer) => $expression,
_ => None,
}
};
}
#[macro_export]
macro_rules! match_signed_integer {
($integer: ident, $span: ident => $expression: expr) => {