Skip to content

Commit 3b3079e

Browse files
committed
Cleanup error handling on array index expression type resolution
1 parent 7d6579c commit 3b3079e

File tree

2 files changed

+18
-19
lines changed

2 files changed

+18
-19
lines changed

gcc/rust/typecheck/rust-hir-type-check-expr.cc

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -245,29 +245,18 @@ TypeCheckExpr::visit (HIR::RangeFromToInclExpr &expr)
245245
void
246246
TypeCheckExpr::visit (HIR::ArrayIndexExpr &expr)
247247
{
248-
TyTy::BaseType *size_ty;
249-
if (!context->lookup_builtin ("usize", &size_ty))
250-
{
251-
rust_error_at (
252-
expr.get_locus (),
253-
"Failure looking up size type for index in ArrayIndexExpr");
254-
return;
255-
}
256-
257-
auto resolved_index_expr
258-
= size_ty->unify (TypeCheckExpr::Resolve (expr.get_index_expr (), false));
259-
if (resolved_index_expr->get_kind () != TyTy::TypeKind::ERROR)
260-
{
261-
// allow the index expr to fail lets just continue on
262-
context->insert_type (expr.get_index_expr ()->get_mappings (),
263-
resolved_index_expr);
264-
}
265-
266248
auto array_expr_ty
267249
= TypeCheckExpr::Resolve (expr.get_array_expr (), inside_loop);
268250
if (array_expr_ty->get_kind () == TyTy::TypeKind::ERROR)
269251
return;
270-
else if (array_expr_ty->get_kind () == TyTy::TypeKind::REF)
252+
253+
auto index_expr_ty = TypeCheckExpr::Resolve (expr.get_index_expr (), false);
254+
if (index_expr_ty->get_kind () == TyTy::TypeKind::ERROR)
255+
return;
256+
257+
// is this a case of core::ops::index?
258+
259+
if (array_expr_ty->get_kind () == TyTy::TypeKind::REF)
271260
{
272261
// lets try and deref it since rust allows this
273262
auto ref = static_cast<TyTy::ReferenceType *> (array_expr_ty);
@@ -284,6 +273,15 @@ TypeCheckExpr::visit (HIR::ArrayIndexExpr &expr)
284273
return;
285274
}
286275

276+
TyTy::BaseType *size_ty;
277+
bool ok = context->lookup_builtin ("usize", &size_ty);
278+
rust_assert (ok);
279+
280+
auto resolved_index_expr
281+
= size_ty->unify (TypeCheckExpr::Resolve (expr.get_index_expr (), false));
282+
if (resolved_index_expr->get_kind () == TyTy::TypeKind::ERROR)
283+
return;
284+
287285
TyTy::ArrayType *array_type = static_cast<TyTy::ArrayType *> (array_expr_ty);
288286
infered = array_type->get_element_type ()->clone ();
289287
}

gcc/testsuite/rust/compile/usize1.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ fn main() {
22
let a = [1, 2, 3];
33
let b: u32 = 1;
44
let c = a[b]; // { dg-error "expected .usize. got .u32." }
5+
// { dg-error {failed to type resolve expression} "" { target *-*-* } .-1 }
56
}

0 commit comments

Comments
 (0)