mirror of
https://github.com/ProvableHQ/leo.git
synced 2024-12-26 11:45:00 +03:00
Add compiler check for signed integer array access
This commit is contained in:
parent
3b737b2a34
commit
5210b1ec77
@ -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)
|
||||
}
|
||||
|
@ -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))?;
|
||||
|
@ -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) => {
|
||||
|
Loading…
Reference in New Issue
Block a user