Skip to content

Commit 6457b29

Browse files
committed
move PanicInfo to mir module
1 parent 17a8cfd commit 6457b29

File tree

15 files changed

+78
-80
lines changed

15 files changed

+78
-80
lines changed

src/librustc/mir/interpret/error.rs

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use crate::ty::query::TyCtxtAt;
77
use crate::ty::{self, layout, Ty};
88

99
use backtrace::Backtrace;
10-
use hir::GeneratorKind;
1110
use rustc_errors::{struct_span_err, DiagnosticBuilder};
1211
use rustc_hir as hir;
1312
use rustc_macros::HashStable;
@@ -266,62 +265,6 @@ impl<'tcx> From<InterpError<'tcx>> for InterpErrorInfo<'tcx> {
266265
}
267266
}
268267

269-
/// Information about a panic.
270-
///
271-
/// FIXME: this is not actually an InterpError, and should probably be moved to another module.
272-
#[derive(Clone, RustcEncodable, RustcDecodable, HashStable, PartialEq)]
273-
pub enum PanicInfo<O> {
274-
BoundsCheck { len: O, index: O },
275-
Overflow(mir::BinOp),
276-
OverflowNeg,
277-
DivisionByZero,
278-
RemainderByZero,
279-
ResumedAfterReturn(GeneratorKind),
280-
ResumedAfterPanic(GeneratorKind),
281-
}
282-
283-
/// Type for MIR `Assert` terminator error messages.
284-
pub type AssertMessage<'tcx> = PanicInfo<mir::Operand<'tcx>>;
285-
286-
impl<O> PanicInfo<O> {
287-
/// Getting a description does not require `O` to be printable, and does not
288-
/// require allocation.
289-
/// The caller is expected to handle `BoundsCheck` separately.
290-
pub fn description(&self) -> &'static str {
291-
use PanicInfo::*;
292-
match self {
293-
Overflow(mir::BinOp::Add) => "attempt to add with overflow",
294-
Overflow(mir::BinOp::Sub) => "attempt to subtract with overflow",
295-
Overflow(mir::BinOp::Mul) => "attempt to multiply with overflow",
296-
Overflow(mir::BinOp::Div) => "attempt to divide with overflow",
297-
Overflow(mir::BinOp::Rem) => "attempt to calculate the remainder with overflow",
298-
OverflowNeg => "attempt to negate with overflow",
299-
Overflow(mir::BinOp::Shr) => "attempt to shift right with overflow",
300-
Overflow(mir::BinOp::Shl) => "attempt to shift left with overflow",
301-
Overflow(op) => bug!("{:?} cannot overflow", op),
302-
DivisionByZero => "attempt to divide by zero",
303-
RemainderByZero => "attempt to calculate the remainder with a divisor of zero",
304-
ResumedAfterReturn(GeneratorKind::Gen) => "generator resumed after completion",
305-
ResumedAfterReturn(GeneratorKind::Async(_)) => "`async fn` resumed after completion",
306-
ResumedAfterPanic(GeneratorKind::Gen) => "generator resumed after panicking",
307-
ResumedAfterPanic(GeneratorKind::Async(_)) => "`async fn` resumed after panicking",
308-
BoundsCheck { .. } => bug!("Unexpected PanicInfo"),
309-
}
310-
}
311-
}
312-
313-
impl<O: fmt::Debug> fmt::Debug for PanicInfo<O> {
314-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
315-
use PanicInfo::*;
316-
match self {
317-
BoundsCheck { ref len, ref index } => {
318-
write!(f, "index out of bounds: the len is {:?} but the index is {:?}", len, index)
319-
}
320-
_ => write!(f, "{}", self.description()),
321-
}
322-
}
323-
}
324-
325268
/// Error information for when the program we executed turned out not to actually be a valid
326269
/// program. This cannot happen in stand-alone Miri, but it can happen during CTFE/ConstProp
327270
/// where we work on generic code or execution does not have all information available.

src/librustc/mir/interpret/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@ mod queries;
9090
mod value;
9191

9292
pub use self::error::{
93-
struct_error, AssertMessage, ConstEvalErr, ConstEvalRawResult, ConstEvalResult, ErrorHandled,
94-
FrameInfo, InterpError, InterpErrorInfo, InterpResult, InvalidProgramInfo, PanicInfo,
95-
ResourceExhaustionInfo, UndefinedBehaviorInfo, UnsupportedOpInfo,
93+
struct_error, ConstEvalErr, ConstEvalRawResult, ConstEvalResult, ErrorHandled, FrameInfo,
94+
InterpError, InterpErrorInfo, InterpResult, InvalidProgramInfo, ResourceExhaustionInfo,
95+
UndefinedBehaviorInfo, UnsupportedOpInfo,
9696
};
9797

9898
pub use self::value::{get_slice_bytes, ConstValue, RawConst, Scalar, ScalarMaybeUndef};

src/librustc/mir/mod.rs

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//!
33
//! [rustc guide]: https://rust-lang.github.io/rustc-guide/mir/index.html
44
5-
use crate::mir::interpret::{GlobalAlloc, PanicInfo, Scalar};
5+
use crate::mir::interpret::{GlobalAlloc, Scalar};
66
use crate::mir::visit::MirVisitable;
77
use crate::ty::adjustment::PointerCast;
88
use crate::ty::fold::{TypeFoldable, TypeFolder, TypeVisitor};
@@ -36,7 +36,6 @@ pub use syntax::ast::Mutability;
3636
use syntax::ast::Name;
3737

3838
pub use self::cache::{BodyAndCache, ReadOnlyBodyAndCache};
39-
pub use self::interpret::AssertMessage;
4039
pub use self::query::*;
4140
pub use crate::read_only;
4241

@@ -1154,6 +1153,21 @@ pub enum TerminatorKind<'tcx> {
11541153
},
11551154
}
11561155

1156+
/// Information about an assertion failure.
1157+
#[derive(Clone, RustcEncodable, RustcDecodable, HashStable, PartialEq)]
1158+
pub enum PanicInfo<O> {
1159+
BoundsCheck { len: O, index: O },
1160+
Overflow(BinOp),
1161+
OverflowNeg,
1162+
DivisionByZero,
1163+
RemainderByZero,
1164+
ResumedAfterReturn(GeneratorKind),
1165+
ResumedAfterPanic(GeneratorKind),
1166+
}
1167+
1168+
/// Type for MIR `Assert` terminator error messages.
1169+
pub type AssertMessage<'tcx> = PanicInfo<Operand<'tcx>>;
1170+
11571171
pub type Successors<'a> =
11581172
iter::Chain<option::IntoIter<&'a BasicBlock>, slice::Iter<'a, BasicBlock>>;
11591173
pub type SuccessorsMut<'a> =
@@ -1383,6 +1397,45 @@ impl<'tcx> BasicBlockData<'tcx> {
13831397
}
13841398
}
13851399

1400+
impl<O> PanicInfo<O> {
1401+
/// Getting a description does not require `O` to be printable, and does not
1402+
/// require allocation.
1403+
/// The caller is expected to handle `BoundsCheck` separately.
1404+
pub fn description(&self) -> &'static str {
1405+
use PanicInfo::*;
1406+
match self {
1407+
Overflow(BinOp::Add) => "attempt to add with overflow",
1408+
Overflow(BinOp::Sub) => "attempt to subtract with overflow",
1409+
Overflow(BinOp::Mul) => "attempt to multiply with overflow",
1410+
Overflow(BinOp::Div) => "attempt to divide with overflow",
1411+
Overflow(BinOp::Rem) => "attempt to calculate the remainder with overflow",
1412+
OverflowNeg => "attempt to negate with overflow",
1413+
Overflow(BinOp::Shr) => "attempt to shift right with overflow",
1414+
Overflow(BinOp::Shl) => "attempt to shift left with overflow",
1415+
Overflow(op) => bug!("{:?} cannot overflow", op),
1416+
DivisionByZero => "attempt to divide by zero",
1417+
RemainderByZero => "attempt to calculate the remainder with a divisor of zero",
1418+
ResumedAfterReturn(GeneratorKind::Gen) => "generator resumed after completion",
1419+
ResumedAfterReturn(GeneratorKind::Async(_)) => "`async fn` resumed after completion",
1420+
ResumedAfterPanic(GeneratorKind::Gen) => "generator resumed after panicking",
1421+
ResumedAfterPanic(GeneratorKind::Async(_)) => "`async fn` resumed after panicking",
1422+
BoundsCheck { .. } => bug!("Unexpected PanicInfo"),
1423+
}
1424+
}
1425+
}
1426+
1427+
impl<O: fmt::Debug> fmt::Debug for PanicInfo<O> {
1428+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1429+
use PanicInfo::*;
1430+
match self {
1431+
BoundsCheck { ref len, ref index } => {
1432+
write!(f, "index out of bounds: the len is {:?} but the index is {:?}", len, index)
1433+
}
1434+
_ => write!(f, "{}", self.description()),
1435+
}
1436+
}
1437+
}
1438+
13861439
impl<'tcx> Debug for TerminatorKind<'tcx> {
13871440
fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
13881441
self.fmt_head(fmt)?;

src/librustc/mir/visit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ macro_rules! make_mir_visitor {
533533
fn super_assert_message(&mut self,
534534
msg: & $($mutability)? AssertMessage<'tcx>,
535535
location: Location) {
536-
use crate::mir::interpret::PanicInfo::*;
536+
use crate::mir::PanicInfo::*;
537537
match msg {
538538
BoundsCheck { len, index } => {
539539
self.visit_operand(len, location);

src/librustc_codegen_ssa/mir/block.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::MemFlags;
1111

1212
use rustc::middle::lang_items;
1313
use rustc::mir;
14-
use rustc::mir::interpret::PanicInfo;
14+
use rustc::mir::PanicInfo;
1515
use rustc::ty::layout::{self, FnAbiExt, HasTyCtxt, LayoutOf};
1616
use rustc::ty::{self, Instance, Ty, TypeFoldable};
1717
use rustc_index::vec::Idx;

src/librustc_mir/borrow_check/invalidation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ impl<'cx, 'tcx> Visitor<'tcx> for InvalidationGenerator<'cx, 'tcx> {
153153
}
154154
TerminatorKind::Assert { ref cond, expected: _, ref msg, target: _, cleanup: _ } => {
155155
self.consume_operand(location, cond);
156-
use rustc::mir::interpret::PanicInfo;
156+
use rustc::mir::PanicInfo;
157157
if let PanicInfo::BoundsCheck { ref len, ref index } = *msg {
158158
self.consume_operand(location, len);
159159
self.consume_operand(location, index);

src/librustc_mir/borrow_check/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,7 @@ impl<'cx, 'tcx> dataflow::generic::ResultsVisitor<'cx, 'tcx> for MirBorrowckCtxt
654654
}
655655
TerminatorKind::Assert { ref cond, expected: _, ref msg, target: _, cleanup: _ } => {
656656
self.consume_operand(loc, (cond, span), flow_state);
657-
use rustc::mir::interpret::PanicInfo;
657+
use rustc::mir::PanicInfo;
658658
if let PanicInfo::BoundsCheck { ref len, ref index } = *msg {
659659
self.consume_operand(loc, (len, span), flow_state);
660660
self.consume_operand(loc, (index, span), flow_state);

src/librustc_mir/borrow_check/type_check/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ use rustc::infer::canonical::QueryRegionConstraints;
99
use rustc::infer::outlives::env::RegionBoundPairs;
1010
use rustc::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
1111
use rustc::infer::{InferCtxt, InferOk, LateBoundRegionConversionTime, NLLRegionVariableOrigin};
12-
use rustc::mir::interpret::PanicInfo;
1312
use rustc::mir::tcx::PlaceTy;
1413
use rustc::mir::visit::{NonMutatingUseContext, PlaceContext, Visitor};
14+
use rustc::mir::PanicInfo;
1515
use rustc::mir::*;
1616
use rustc::traits::query::type_op;
1717
use rustc::traits::query::type_op::custom::CustomTypeOp;

src/librustc_mir/const_eval/error.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
use std::error::Error;
22
use std::fmt;
33

4+
use rustc::mir::PanicInfo;
45
use rustc_span::Symbol;
56

67
use super::InterpCx;
7-
use crate::interpret::{ConstEvalErr, InterpError, InterpErrorInfo, Machine, PanicInfo};
8+
use crate::interpret::{ConstEvalErr, InterpError, InterpErrorInfo, Machine};
89

910
/// The CTFE machine has some custom error kinds.
1011
#[derive(Clone, Debug)]

src/librustc_mir/const_eval/machine.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ use std::hash::Hash;
88

99
use rustc_data_structures::fx::FxHashMap;
1010

11+
use rustc::mir::AssertMessage;
1112
use rustc_span::source_map::Span;
1213
use rustc_span::symbol::Symbol;
1314

1415
use crate::interpret::{
15-
self, snapshot, AllocId, Allocation, AssertMessage, GlobalId, ImmTy, InterpCx, InterpResult,
16-
Memory, MemoryKind, OpTy, PlaceTy, Pointer, Scalar,
16+
self, snapshot, AllocId, Allocation, GlobalId, ImmTy, InterpCx, InterpResult, Memory,
17+
MemoryKind, OpTy, PlaceTy, Pointer, Scalar,
1718
};
1819

1920
use super::error::*;
@@ -280,7 +281,7 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
280281
msg: &AssertMessage<'tcx>,
281282
_unwind: Option<mir::BasicBlock>,
282283
) -> InterpResult<'tcx> {
283-
use rustc::mir::interpret::PanicInfo::*;
284+
use rustc::mir::PanicInfo::*;
284285
// Convert `PanicInfo<Operand>` to `PanicInfo<u64>`.
285286
let err = match msg {
286287
BoundsCheck { ref len, ref index } => {

src/librustc_mir/interpret/machine.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ use rustc_hir::def_id::DefId;
1111
use rustc_span::Span;
1212

1313
use super::{
14-
AllocId, Allocation, AllocationExtra, AssertMessage, Frame, ImmTy, InterpCx, InterpResult,
15-
Memory, MemoryKind, OpTy, Operand, PlaceTy, Pointer, Scalar,
14+
AllocId, Allocation, AllocationExtra, Frame, ImmTy, InterpCx, InterpResult, Memory, MemoryKind,
15+
OpTy, Operand, PlaceTy, Pointer, Scalar,
1616
};
1717

1818
/// Data returned by Machine::stack_pop,
@@ -171,7 +171,7 @@ pub trait Machine<'mir, 'tcx>: Sized {
171171
fn assert_panic(
172172
ecx: &mut InterpCx<'mir, 'tcx, Self>,
173173
span: Span,
174-
msg: &AssertMessage<'tcx>,
174+
msg: &mir::AssertMessage<'tcx>,
175175
unwind: Option<mir::BasicBlock>,
176176
) -> InterpResult<'tcx>;
177177

src/librustc_mir/transform/const_prop.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
use std::borrow::Cow;
55
use std::cell::Cell;
66

7-
use rustc::mir::interpret::{InterpError, InterpResult, PanicInfo, Scalar};
7+
use rustc::mir::interpret::{InterpError, InterpResult, Scalar};
88
use rustc::mir::visit::{
99
MutVisitor, MutatingUseContext, NonMutatingUseContext, PlaceContext, Visitor,
1010
};
1111
use rustc::mir::{
1212
read_only, AggregateKind, BasicBlock, BinOp, Body, BodyAndCache, ClearCrossCrate, Constant,
13-
Local, LocalDecl, LocalKind, Location, Operand, Place, ReadOnlyBodyAndCache, Rvalue,
13+
Local, LocalDecl, LocalKind, Location, Operand, PanicInfo, Place, ReadOnlyBodyAndCache, Rvalue,
1414
SourceInfo, SourceScope, SourceScopeData, Statement, StatementKind, Terminator, TerminatorKind,
1515
UnOp, RETURN_PLACE,
1616
};
@@ -198,7 +198,7 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for ConstPropMachine {
198198
fn assert_panic(
199199
_ecx: &mut InterpCx<'mir, 'tcx, Self>,
200200
_span: Span,
201-
_msg: &rustc::mir::interpret::AssertMessage<'tcx>,
201+
_msg: &rustc::mir::AssertMessage<'tcx>,
202202
_unwind: Option<rustc::mir::BasicBlock>,
203203
) -> InterpResult<'tcx> {
204204
bug!("panics terminators are not evaluated in ConstProp");

src/librustc_mir/transform/generator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1022,7 +1022,7 @@ fn create_generator_resume_function<'tcx>(
10221022

10231023
let mut cases = create_cases(body, &transform, Operation::Resume);
10241024

1025-
use rustc::mir::interpret::PanicInfo::{ResumedAfterPanic, ResumedAfterReturn};
1025+
use rustc::mir::PanicInfo::{ResumedAfterPanic, ResumedAfterReturn};
10261026

10271027
// Jump to the entry point on the unresumed
10281028
cases.insert(0, (UNRESUMED, BasicBlock::new(0)));

src/librustc_mir_build/build/expr/as_place.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::build::ForGuard::{OutsideGuard, RefWithinGuard};
55
use crate::build::{BlockAnd, BlockAndExtension, Builder};
66
use crate::hair::*;
77
use rustc::middle::region;
8-
use rustc::mir::interpret::PanicInfo::BoundsCheck;
8+
use rustc::mir::PanicInfo::BoundsCheck;
99
use rustc::mir::*;
1010
use rustc::ty::{self, CanonicalUserTypeAnnotation, Ty, TyCtxt, Variance};
1111
use rustc_span::Span;

src/librustc_mir_build/build/expr/as_rvalue.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::build::expr::category::{Category, RvalueFunc};
66
use crate::build::{BlockAnd, BlockAndExtension, Builder};
77
use crate::hair::*;
88
use rustc::middle::region;
9-
use rustc::mir::interpret::PanicInfo;
9+
use rustc::mir::PanicInfo;
1010
use rustc::mir::*;
1111
use rustc::ty::{self, Ty, UpvarSubsts};
1212
use rustc_span::Span;

0 commit comments

Comments
 (0)