Skip to content

Commit 8b318be

Browse files
committed
rustc: Typecheck vector index expressions for interior vectors
1 parent ac20606 commit 8b318be

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

src/comp/middle/typeck.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2216,23 +2216,24 @@ fn check_expr(&@fn_ctxt fcx, &@ast::expr expr) {
22162216

22172217
check_expr(fcx, idx);
22182218
auto idx_t = expr_ty(fcx.ccx.tcx, idx);
2219+
if (!type_is_integral(fcx, idx.span, idx_t)) {
2220+
fcx.ccx.tcx.sess.span_err(idx.span,
2221+
"mismatched types: expected integer but found " +
2222+
ty_to_str(fcx.ccx.tcx, idx_t));
2223+
}
2224+
22192225
alt (structure_of(fcx, expr.span, base_t)) {
22202226
case (ty::ty_vec(?mt)) {
2221-
if (! type_is_integral(fcx, idx.span, idx_t)) {
2222-
fcx.ccx.tcx.sess.span_err
2223-
(idx.span,
2224-
"non-integral type of vec index: "
2225-
+ ty_to_str(fcx.ccx.tcx, idx_t));
2226-
}
2227+
write::ty_only_fixup(fcx, a.id, mt.ty);
2228+
}
2229+
case (ty::ty_ivec(?mt)) {
22272230
write::ty_only_fixup(fcx, a.id, mt.ty);
22282231
}
22292232
case (ty::ty_str) {
2230-
if (! type_is_integral(fcx, idx.span, idx_t)) {
2231-
fcx.ccx.tcx.sess.span_err
2232-
(idx.span,
2233-
"non-integral type of str index: "
2234-
+ ty_to_str(fcx.ccx.tcx, idx_t));
2235-
}
2233+
auto typ = ty::mk_mach(fcx.ccx.tcx, common::ty_u8);
2234+
write::ty_only_fixup(fcx, a.id, typ);
2235+
}
2236+
case (ty::ty_istr) {
22362237
auto typ = ty::mk_mach(fcx.ccx.tcx, common::ty_u8);
22372238
write::ty_only_fixup(fcx, a.id, typ);
22382239
}

0 commit comments

Comments
 (0)