diff --git a/ncc/src/types.rs b/ncc/src/types.rs index c89d4a9..17931e1 100644 --- a/ncc/src/types.rs +++ b/ncc/src/types.rs @@ -179,17 +179,27 @@ impl Expr } else { - let first_type = exprs[0].eval_type()?; + let mut elem_type = exprs[0].eval_type()?; for expr in &exprs[1..] { let expr_type = expr.eval_type()?; - if !first_type.eq(&expr_type) { - return ParseError::msg_only("array element types do not match"); + + match (&elem_type, &expr_type) { + (Int(m), Int(n)) => { + elem_type = Type::Int(max(*m, *n)) + } + + _ => { + if !elem_type.eq(&expr_type) { + return ParseError::msg_only("array element types do not match"); + } + } } + } Ok(Array { - elem_type: Box::new(first_type), + elem_type: Box::new(elem_type), size_expr: Box::new(Expr::Int(exprs.len() as i128)) }) } diff --git a/ncc/tests/arrays.c b/ncc/tests/arrays.c index 764466b..830ef23 100644 --- a/ncc/tests/arrays.c +++ b/ncc/tests/arrays.c @@ -7,6 +7,9 @@ uint8_t bytes2d[2][2] = { {0, 1}, {2, 3} }; uint8_t array2d[600][800]; +// Integer literals of type long and int in the same array literal +u64 arr_int_long[2] = { 0x7fff8beb, 0x8000e82a }; + int main() { assert(int_array[0] == 7);