Merge pull request #330 from AleoHQ/feature/unsigned-array-access-only

Add compiler check for signed integer array access
This commit is contained in:
Howard Wu 2020-09-02 13:44:11 -07:00 committed by GitHub
commit afc8c6b931
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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) => {