Skip to content

Commit 8368590

Browse files
committed
Adjust computation of place types to detect more invalid places
1 parent 4f28344 commit 8368590

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

compiler/rustc_const_eval/src/transform/validate.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
use rustc_index::bit_set::BitSet;
44
use rustc_infer::infer::TyCtxtInferExt;
55
use rustc_middle::mir::interpret::Scalar;
6-
use rustc_middle::mir::traversal;
76
use rustc_middle::mir::visit::{PlaceContext, Visitor};
7+
use rustc_middle::mir::{traversal, Place};
88
use rustc_middle::mir::{
99
AggregateKind, BasicBlock, Body, BorrowKind, Local, Location, MirPass, MirPhase, Operand,
1010
PlaceElem, PlaceRef, ProjectionElem, Rvalue, SourceScope, Statement, StatementKind, Terminator,
@@ -240,6 +240,14 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
240240
self.super_projection_elem(local, proj_base, elem, context, location);
241241
}
242242

243+
fn visit_place(&mut self, place: &Place<'tcx>, _: PlaceContext, location: Location) {
244+
// Set off any `bug!`s in the type computation code
245+
let ty = place.ty(&self.body.local_decls, self.tcx);
246+
if ty.variant_index.is_some() {
247+
self.fail(location, "Top level places may not have their variant index set!");
248+
}
249+
}
250+
243251
fn visit_statement(&mut self, statement: &Statement<'tcx>, location: Location) {
244252
match &statement.kind {
245253
StatementKind::Assign(box (dest, rvalue)) => {

compiler/rustc_middle/src/mir/tcx.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ impl<'tcx> PlaceTy<'tcx> {
7676
V: ::std::fmt::Debug,
7777
T: ::std::fmt::Debug + Copy,
7878
{
79+
if self.variant_index.is_some() && !matches!(elem, ProjectionElem::Field(..)) {
80+
bug!("cannot use non field projection on downcasted place")
81+
}
7982
let answer = match *elem {
8083
ProjectionElem::Deref => {
8184
let ty = self

0 commit comments

Comments
 (0)