Skip to content

Commit

Permalink
Add compile time array bounds checking
Browse files Browse the repository at this point in the history
  • Loading branch information
VonTum committed Jan 3, 2024
1 parent f05e8ab commit 8249501
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
2 changes: 1 addition & 1 deletion multiply_add.sus
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ module generative : int in -> int o, int o2 {


o = a[in];
o2 = a[a[1]];
o2 = a[a[0]];
}


Expand Down
9 changes: 7 additions & 2 deletions src/instantiation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ impl<'fl, 'l> InstantiationContext<'fl, 'l> {
match IntT::try_from(val) {
Ok(val) => Some(val),
Err(e) => {
self.errors.error_basic(span, format!("Generative integer too large: {val}"));
self.errors.error_basic(span, format!("Generative integer does not fit in {}: {val}", std::any::type_name::<IntT>()));
None
}
}
Expand Down Expand Up @@ -302,7 +302,12 @@ impl<'fl, 'l> InstantiationContext<'fl, 'l> {
let Value::Array(arr_val) = self.get_generation_value(arr)? else {return None};
let arr_idx_val = self.get_generation_value(arr_idx)?;
let idx : usize = self.extract_integer_from_value(arr_idx_val, arr_idx.1)?;
arr_val[idx].clone()
if let Some(item) = arr_val.get(idx) {
item.clone()
} else {
self.errors.error_basic(arr_idx.1, format!("Compile-Time Array index is out of range: idx: {idx}, array size: {}", arr_val.len()));
return None
}
}
WireSource::Constant{value} => value.clone()
})
Expand Down

0 comments on commit 8249501

Please sign in to comment.