From 8567160538a82110847b7a62a881ed222718881a Mon Sep 17 00:00:00 2001 From: Philip Herron Date: Tue, 1 Dec 2020 12:11:06 +0000 Subject: [PATCH] Ensure the array index is of integer type. The index expression must be validated to ensure it is an integer. Addresses: #27 #55 --- gcc/rust/analysis/rust-type-resolution.cc | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/gcc/rust/analysis/rust-type-resolution.cc b/gcc/rust/analysis/rust-type-resolution.cc index 387f7fcda209a..98ff9187059fb 100644 --- a/gcc/rust/analysis/rust-type-resolution.cc +++ b/gcc/rust/analysis/rust-type-resolution.cc @@ -496,7 +496,15 @@ TypeResolution::visit (AST::ArrayIndexExpr &expr) // check the index_type should be an i32 which should really be // more permissive - // TODO + AST::Type *i32 = nullptr; + scope.LookupType ("i32", &i32); + rust_assert (i32 != nullptr); + + if (!typesAreCompatible (array_index_type, i32, + expr.get_index_expr ()->get_locus_slow ())) + { + return; + } // the the element type from the array_expr_type and it _must_ be an array AST::ArrayType *resolved = ArrayTypeVisitor::Resolve (array_expr_type);