Skip to content

Commit 46abacf

Browse files
committed
rustc: Require that vector indices are uints
This commit tightens up the restriction on types used to index slices to require exactly `uint` indices. Previously any integral type was accepted, but this leads to a few subtle problems: * 64-bit indices don't make much sense on 32-bit systems * Signed indices for slices used as negative indexing isn't implemented This was discussed at the recent work week, and also has some discussion on issue #10453. Closes #10453
1 parent 3786b55 commit 46abacf

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

src/librustc/middle/ty.rs

+7
Original file line numberDiff line numberDiff line change
@@ -2602,6 +2602,13 @@ pub fn type_is_integral(ty: t) -> bool {
26022602
}
26032603
}
26042604

2605+
pub fn type_is_uint(ty: t) -> bool {
2606+
match get(ty).sty {
2607+
ty_infer(IntVar(_)) | ty_uint(ast::TyU) => true,
2608+
_ => false
2609+
}
2610+
}
2611+
26052612
pub fn type_is_char(ty: t) -> bool {
26062613
match get(ty).sty {
26072614
ty_char => true,

src/librustc/middle/typeck/check/mod.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -3151,7 +3151,7 @@ fn check_expr_with_unifier(fcx: &FnCtxt,
31513151
lvalue_pref, |base_t, _| ty::index(base_t));
31523152
match field_ty {
31533153
Some(mt) => {
3154-
require_integral(fcx, idx.span, idx_t);
3154+
check_expr_has_type(fcx, idx, ty::mk_uint());
31553155
fcx.write_ty(id, mt.ty);
31563156
fcx.write_autoderef_adjustment(base.id, autoderefs);
31573157
}
@@ -3195,6 +3195,15 @@ fn check_expr_with_unifier(fcx: &FnCtxt,
31953195
unifier();
31963196
}
31973197
3198+
pub fn require_uint(fcx: &FnCtxt, sp: Span, t: ty::t) {
3199+
if !type_is_uint(fcx, sp, t) {
3200+
fcx.type_error_message(sp, |actual| {
3201+
format!("mismatched types: expected `uint` type but found `{}`",
3202+
actual)
3203+
}, t, None);
3204+
}
3205+
}
3206+
31983207
pub fn require_integral(fcx: &FnCtxt, sp: Span, t: ty::t) {
31993208
if !type_is_integral(fcx, sp, t) {
32003209
fcx.type_error_message(sp, |actual| {
@@ -3854,6 +3863,11 @@ pub fn type_is_integral(fcx: &FnCtxt, sp: Span, typ: ty::t) -> bool {
38543863
return ty::type_is_integral(typ_s);
38553864
}
38563865

3866+
pub fn type_is_uint(fcx: &FnCtxt, sp: Span, typ: ty::t) -> bool {
3867+
let typ_s = structurally_resolved_type(fcx, sp, typ);
3868+
return ty::type_is_uint(typ_s);
3869+
}
3870+
38573871
pub fn type_is_scalar(fcx: &FnCtxt, sp: Span, typ: ty::t) -> bool {
38583872
let typ_s = structurally_resolved_type(fcx, sp, typ);
38593873
return ty::type_is_scalar(typ_s);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Make sure that indexing an array is only valid with a `uint`, not any other
12+
// integral type.
13+
14+
fn main() {
15+
fn bar<T>(_: T) {}
16+
[0][0u8]; //~ ERROR: mismatched types
17+
18+
[0][0]; // should infer to be a uint
19+
20+
let i = 0; // i is an IntVar
21+
[0][i]; // i should be locked to uint
22+
bar::<int>(i); // i should not be re-coerced back to an int
23+
//~^ ERROR: mismatched types
24+
}
25+

0 commit comments

Comments
 (0)