impl implicit u32 array indices

This commit is contained in:
collin 2021-01-07 13:34:34 -05:00
parent 7220fdb84f
commit 60d243fd43
4 changed files with 43 additions and 3 deletions

View File

@ -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);
}

View File

@ -0,0 +1,5 @@
function main() {
let a = [0u32; 4];
let b = a[0]; // This should not cause a type inference error
}

View File

@ -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();
}

View File

@ -0,0 +1,5 @@
function main() {
let a = [0u32; 4];
let b = a[0..2]; // This should not cause a type inference error
}