Skip to content

Commit da85cce

Browse files
authored
Rollup merge of #85935 - tmiasko:validate-indexing, r=jonas-schievink
Validate type of locals used as indices
2 parents 2390917 + 11478bd commit da85cce

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

compiler/rustc_mir/src/transform/validate.rs

+20-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ use rustc_middle::mir::interpret::Scalar;
1111
use rustc_middle::mir::traversal;
1212
use rustc_middle::mir::visit::{PlaceContext, Visitor};
1313
use rustc_middle::mir::{
14-
AggregateKind, BasicBlock, Body, BorrowKind, Local, Location, MirPhase, Operand, PlaceRef,
15-
Rvalue, SourceScope, Statement, StatementKind, Terminator, TerminatorKind,
14+
AggregateKind, BasicBlock, Body, BorrowKind, Local, Location, MirPhase, Operand, PlaceElem,
15+
PlaceRef, ProjectionElem, Rvalue, SourceScope, Statement, StatementKind, Terminator,
16+
TerminatorKind,
1617
};
1718
use rustc_middle::ty::fold::BottomUpFolder;
1819
use rustc_middle::ty::{self, ParamEnv, Ty, TyCtxt, TypeFoldable};
@@ -217,6 +218,23 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
217218
self.super_operand(operand, location);
218219
}
219220

221+
fn visit_projection_elem(
222+
&mut self,
223+
local: Local,
224+
proj_base: &[PlaceElem<'tcx>],
225+
elem: PlaceElem<'tcx>,
226+
context: PlaceContext,
227+
location: Location,
228+
) {
229+
if let ProjectionElem::Index(index) = elem {
230+
let index_ty = self.body.local_decls[index].ty;
231+
if index_ty != self.tcx.types.usize {
232+
self.fail(location, format!("bad index ({:?} != usize)", index_ty))
233+
}
234+
}
235+
self.super_projection_elem(local, proj_base, elem, context, location);
236+
}
237+
220238
fn visit_statement(&mut self, statement: &Statement<'tcx>, location: Location) {
221239
match &statement.kind {
222240
StatementKind::Assign(box (dest, rvalue)) => {

0 commit comments

Comments
 (0)