mirror of
https://github.com/ProvableHQ/leo.git
synced 2024-11-24 16:08:55 +03:00
impl implicit u32 array indices
This commit is contained in:
parent
7220fdb84f
commit
60d243fd43
@ -798,6 +798,18 @@ impl Frame {
|
||||
}
|
||||
}
|
||||
|
||||
///
|
||||
/// Returns `Type::U32` if the given index has `Type::TypeVariable`.
|
||||
/// Hard codes all implicitly typed indices to u32.
|
||||
/// Ex: `arr[0]` => `arr[0u32]`
|
||||
///
|
||||
fn parse_index(&mut self, index: &Expression) -> Result<Type, FrameError> {
|
||||
Ok(match self.parse_expression(index)? {
|
||||
Type::TypeVariable(_) => Type::IntegerType(IntegerType::U32),
|
||||
type_ => type_,
|
||||
})
|
||||
}
|
||||
|
||||
///
|
||||
/// Returns the type of the accessed array element.
|
||||
///
|
||||
@ -809,7 +821,7 @@ impl Frame {
|
||||
};
|
||||
|
||||
// Parse the expression type.
|
||||
let type_ = self.parse_expression(index)?;
|
||||
let type_ = self.parse_index(index)?;
|
||||
|
||||
// Assert the type is an index.
|
||||
self.assert_index(&type_, span);
|
||||
@ -836,14 +848,14 @@ impl Frame {
|
||||
|
||||
if let Some(expression) = left {
|
||||
// Parse the expression type.
|
||||
let type_ = self.parse_expression(expression)?;
|
||||
let type_ = self.parse_index(expression)?;
|
||||
|
||||
self.assert_index(&type_, span);
|
||||
}
|
||||
|
||||
if let Some(expression) = right {
|
||||
// Parse the expression type.
|
||||
let type_ = self.parse_expression(expression)?;
|
||||
let type_ = self.parse_index(expression)?;
|
||||
|
||||
self.assert_index(&type_, span);
|
||||
}
|
||||
|
5
type-inference/tests/arrays/index_implicit.leo
Normal file
5
type-inference/tests/arrays/index_implicit.leo
Normal file
@ -0,0 +1,5 @@
|
||||
function main() {
|
||||
let a = [0u32; 4];
|
||||
|
||||
let b = a[0]; // This should not cause a type inference error
|
||||
}
|
@ -42,3 +42,21 @@ fn test_invalid_spread() {
|
||||
|
||||
check.expect_error();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_index_implicit() {
|
||||
let program_string = include_str!("index_implicit.leo");
|
||||
|
||||
let check = TestTypeInference::new(program_string);
|
||||
|
||||
check.check()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_slice_implicit() {
|
||||
let program_string = include_str!("slice_implicit.leo");
|
||||
|
||||
let check = TestTypeInference::new(program_string);
|
||||
|
||||
check.check();
|
||||
}
|
||||
|
5
type-inference/tests/arrays/slice_implicit.leo
Normal file
5
type-inference/tests/arrays/slice_implicit.leo
Normal file
@ -0,0 +1,5 @@
|
||||
function main() {
|
||||
let a = [0u32; 4];
|
||||
|
||||
let b = a[0..2]; // This should not cause a type inference error
|
||||
}
|
Loading…
Reference in New Issue
Block a user