aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2022-08-05 13:38:37 +0000
committerGitHub <noreply@github.com>2022-08-05 13:38:37 +0000
commit7beea479c5a1e7e415223f3fbd4e16c20c3214ec (patch)
treed0ad3824e02135310865254787603c99b27224f7 /gcc/rust
parent8725e324aad3eac0df074e5f0a494b5e3f332a31 (diff)
parentc86ac620c2bdb852b8078f37b898ab40d96ec0b0 (diff)
downloadgcc-7beea479c5a1e7e415223f3fbd4e16c20c3214ec.zip
gcc-7beea479c5a1e7e415223f3fbd4e16c20c3214ec.tar.gz
gcc-7beea479c5a1e7e415223f3fbd4e16c20c3214ec.tar.bz2
Merge #1437
1437: Array index access does not need to unsize to a slice for access r=philberty a=philberty When we define the core code for SliceIndex access its possible for an array to be fully coerced into a slice DST and follow the normal slice index access which removes support for GCC -Warray-index checks and generates unnessecary code for array access. Fixes #1436 Co-authored-by: Philip Herron <philip.herron@embecosm.com>
Diffstat (limited to 'gcc/rust')
-rw-r--r--gcc/rust/typecheck/rust-hir-type-check-expr.cc63
1 files changed, 36 insertions, 27 deletions
diff --git a/gcc/rust/typecheck/rust-hir-type-check-expr.cc b/gcc/rust/typecheck/rust-hir-type-check-expr.cc
index 9c31284..ff1165d 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-expr.cc
+++ b/gcc/rust/typecheck/rust-hir-type-check-expr.cc
@@ -250,6 +250,35 @@ TypeCheckExpr::visit (HIR::ArrayIndexExpr &expr)
if (index_expr_ty->get_kind () == TyTy::TypeKind::ERROR)
return;
+ // first attempt to use direct array index logic
+ auto direct_array_expr_ty = array_expr_ty;
+ if (direct_array_expr_ty->get_kind () == TyTy::TypeKind::REF)
+ {
+ // lets try and deref it since rust allows this
+ auto ref = static_cast<TyTy::ReferenceType *> (direct_array_expr_ty);
+ auto base = ref->get_base ();
+ if (base->get_kind () == TyTy::TypeKind::ARRAY)
+ direct_array_expr_ty = base;
+ }
+
+ TyTy::BaseType *size_ty;
+ bool ok = context->lookup_builtin ("usize", &size_ty);
+ rust_assert (ok);
+
+ bool maybe_simple_array_access = index_expr_ty->can_eq (size_ty, false);
+ if (maybe_simple_array_access
+ && direct_array_expr_ty->get_kind () == TyTy::TypeKind::ARRAY)
+ {
+ auto resolved_index_expr = size_ty->unify (index_expr_ty);
+ if (resolved_index_expr->get_kind () == TyTy::TypeKind::ERROR)
+ return;
+
+ TyTy::ArrayType *array_type
+ = static_cast<TyTy::ArrayType *> (direct_array_expr_ty);
+ infered = array_type->get_element_type ()->clone ();
+ return;
+ }
+
// is this a case of core::ops::index?
auto lang_item_type = Analysis::RustLangItem::ItemType::INDEX;
bool operator_overloaded
@@ -266,33 +295,13 @@ TypeCheckExpr::visit (HIR::ArrayIndexExpr &expr)
return;
}
- if (array_expr_ty->get_kind () == TyTy::TypeKind::REF)
- {
- // lets try and deref it since rust allows this
- auto ref = static_cast<TyTy::ReferenceType *> (array_expr_ty);
- auto base = ref->get_base ();
- if (base->get_kind () == TyTy::TypeKind::ARRAY)
- array_expr_ty = base;
- }
-
- if (array_expr_ty->get_kind () != TyTy::TypeKind::ARRAY)
- {
- rust_error_at (expr.get_index_expr ()->get_locus (),
- "expected an ArrayType got [%s]",
- array_expr_ty->as_string ().c_str ());
- return;
- }
-
- TyTy::BaseType *size_ty;
- bool ok = context->lookup_builtin ("usize", &size_ty);
- rust_assert (ok);
-
- auto resolved_index_expr = size_ty->unify (index_expr_ty);
- if (resolved_index_expr->get_kind () == TyTy::TypeKind::ERROR)
- return;
-
- TyTy::ArrayType *array_type = static_cast<TyTy::ArrayType *> (array_expr_ty);
- infered = array_type->get_element_type ()->clone ();
+ // error[E0277]: the type `[{integer}]` cannot be indexed by `u32`
+ RichLocation r (expr.get_locus ());
+ r.add_range (expr.get_array_expr ()->get_locus ());
+ r.add_range (expr.get_index_expr ()->get_locus ());
+ rust_error_at (r, "the type %<%s%> cannot be indexed by %<%s%>",
+ array_expr_ty->get_name ().c_str (),
+ index_expr_ty->get_name ().c_str ());
}
void