aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorYizhe <yizhe@pku.edu.cn>2021-03-17 06:53:58 +0000
committerYizhePKU <yizhe@pku.edu.cn>2021-04-02 17:10:45 +0000
commitf12d8b770b236142b64a4dd542d3c157e8cc4293 (patch)
tree46c541ff09a933ff16bc652b97c53e585bc6d322 /gcc
parent2ba543eb04c4a724d7e40f48a14088629127821a (diff)
downloadgcc-f12d8b770b236142b64a4dd542d3c157e8cc4293.zip
gcc-f12d8b770b236142b64a4dd542d3c157e8cc4293.tar.gz
gcc-f12d8b770b236142b64a4dd542d3c157e8cc4293.tar.bz2
Fix out-of-bound-array-access in type checking
Diffstat (limited to 'gcc')
-rw-r--r--gcc/rust/typecheck/rust-hir-type-check-expr.h23
1 files changed, 18 insertions, 5 deletions
diff --git a/gcc/rust/typecheck/rust-hir-type-check-expr.h b/gcc/rust/typecheck/rust-hir-type-check-expr.h
index 836328f..a734dcf 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-expr.h
+++ b/gcc/rust/typecheck/rust-hir-type-check-expr.h
@@ -35,6 +35,9 @@ class TypeCheckExpr : public TypeCheckBase
using Rust::Resolver::TypeCheckBase::visit;
public:
+ /* Perform type checking on expr. Also runs type unification algorithm.
+ Returns the unified type of expr.
+ `inside_loop` acts as a context for BreakExpr, etc. May change later. */
static TyTy::BaseType *Resolve (HIR::Expr *expr, bool inside_loop)
{
TypeCheckExpr resolver (inside_loop);
@@ -686,6 +689,7 @@ public:
HIR::ArrayElems *elements = expr.get_internal_elements ();
size_t num_elems = elements->get_num_elements ();
+ // Check the type of array elements
elements->accept_vis (*this);
rust_assert (infered_array_elems != nullptr);
@@ -702,14 +706,19 @@ public:
return true;
});
- infered_array_elems = types[0];
- for (size_t i = 1; i < types.size (); i++)
+ // Create an infer type and register its hir_id
+ infered_array_elems
+ = new TyTy::InferType (mappings->get_next_hir_id (),
+ TyTy::InferType::InferTypeKind::GENERAL);
+
+ for (auto &type : types)
{
- infered_array_elems = infered_array_elems->unify (types.at (i));
+ infered_array_elems = infered_array_elems->unify (type);
}
-
for (auto &elem : types)
- infered_array_elems->append_reference (elem->get_ref ());
+ {
+ infered_array_elems->append_reference (elem->get_ref ());
+ }
}
void visit (HIR::ArrayElemsCopied &elems) override
@@ -995,7 +1004,11 @@ private:
gcc_unreachable ();
}
+ /* The return value of TypeCheckExpr::Resolve */
TyTy::BaseType *infered;
+
+ /* The return value of visit(ArrayElemsValues&) and visit(ArrayElemsCopied&)
+ Stores the type of array elements, if `expr` is ArrayExpr. */
TyTy::BaseType *infered_array_elems;
bool inside_loop;