Skip to content

Commit 6ef625f

Browse files
author
David Haig
committed
Remove duplication using single variant for error
1 parent 88821ed commit 6ef625f

File tree

5 files changed

+22
-38
lines changed

5 files changed

+22
-38
lines changed

src/librustc/mir/interpret/error.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc_macros::HashStable;
1313
use rustc_target::spec::abi::Abi;
1414
use syntax_pos::{Pos, Span};
1515
use syntax::symbol::Symbol;
16-
16+
use hir::GeneratorKind;
1717
use std::{fmt, env};
1818

1919
use rustc_error_codes::*;
@@ -264,10 +264,8 @@ pub enum PanicInfo<O> {
264264
OverflowNeg,
265265
DivisionByZero,
266266
RemainderByZero,
267-
GeneratorResumedAfterReturn,
268-
GeneratorResumedAfterPanic,
269-
AsyncResumedAfterReturn,
270-
AsyncResumedAfterPanic,
267+
ResumedAfterReturn(GeneratorKind),
268+
ResumedAfterPanic(GeneratorKind),
271269
}
272270

273271
/// Type for MIR `Assert` terminator error messages.
@@ -302,14 +300,16 @@ impl<O> PanicInfo<O> {
302300
"attempt to divide by zero",
303301
RemainderByZero =>
304302
"attempt to calculate the remainder with a divisor of zero",
305-
GeneratorResumedAfterReturn =>
303+
ResumedAfterReturn(GeneratorKind::Gen) =>
306304
"generator resumed after completion",
307-
GeneratorResumedAfterPanic =>
308-
"generator resumed after panicking",
309-
AsyncResumedAfterReturn =>
305+
// FIXME: Do we want a separate message for each Async variant (Block, Closure, Fn)?
306+
ResumedAfterReturn(GeneratorKind::Async(_)) =>
310307
"`async fn` resumed after completion",
311-
AsyncResumedAfterPanic =>
312-
"`async fn` resumed after panic",
308+
ResumedAfterPanic(GeneratorKind::Gen) =>
309+
"generator resumed after panicking",
310+
// FIXME: Do we want a separate message for each Async variant (Block, Closure, Fn)?
311+
ResumedAfterPanic(GeneratorKind::Async(_)) =>
312+
"`async fn` resumed after panicking",
313313
Panic { .. } | BoundsCheck { .. } =>
314314
bug!("Unexpected PanicInfo"),
315315
}

src/librustc/mir/mod.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2981,8 +2981,7 @@ impl<'tcx> TypeFoldable<'tcx> for Terminator<'tcx> {
29812981
index: index.fold_with(folder),
29822982
},
29832983
Panic { .. } | Overflow(_) | OverflowNeg | DivisionByZero | RemainderByZero |
2984-
GeneratorResumedAfterReturn | GeneratorResumedAfterPanic |
2985-
AsyncResumedAfterReturn | AsyncResumedAfterPanic =>
2984+
ResumedAfterReturn(_) | ResumedAfterPanic(_) =>
29862985
msg.clone(),
29872986
};
29882987
Assert { cond: cond.fold_with(folder), expected, msg, target, cleanup }
@@ -3028,8 +3027,7 @@ impl<'tcx> TypeFoldable<'tcx> for Terminator<'tcx> {
30283027
len.visit_with(visitor) || index.visit_with(visitor),
30293028
Panic { .. } | Overflow(_) | OverflowNeg |
30303029
DivisionByZero | RemainderByZero |
3031-
GeneratorResumedAfterReturn | GeneratorResumedAfterPanic |
3032-
AsyncResumedAfterReturn | AsyncResumedAfterPanic =>
3030+
ResumedAfterReturn(_) | ResumedAfterPanic(_) =>
30333031
false
30343032
}
30353033
} else {

src/librustc/mir/visit.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -517,8 +517,7 @@ macro_rules! make_mir_visitor {
517517
self.visit_operand(index, location);
518518
}
519519
Panic { .. } | Overflow(_) | OverflowNeg | DivisionByZero | RemainderByZero |
520-
GeneratorResumedAfterReturn | GeneratorResumedAfterPanic |
521-
AsyncResumedAfterReturn | AsyncResumedAfterPanic => {
520+
ResumedAfterReturn(_) | ResumedAfterPanic(_) => {
522521
// Nothing to visit
523522
}
524523
}

src/librustc_mir/interpret/terminator.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,8 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
142142
OverflowNeg => err_panic!(OverflowNeg),
143143
DivisionByZero => err_panic!(DivisionByZero),
144144
RemainderByZero => err_panic!(RemainderByZero),
145-
GeneratorResumedAfterReturn => err_panic!(GeneratorResumedAfterReturn),
146-
GeneratorResumedAfterPanic => err_panic!(GeneratorResumedAfterPanic),
147-
AsyncResumedAfterReturn => err_panic!(AsyncResumedAfterReturn),
148-
AsyncResumedAfterPanic => err_panic!(AsyncResumedAfterPanic),
145+
ResumedAfterReturn(generator_kind) => err_panic!(ResumedAfterReturn(*generator_kind)),
146+
ResumedAfterPanic(generator_kind) => err_panic!(ResumedAfterPanic(*generator_kind)),
149147
Panic { .. } => bug!("`Panic` variant cannot occur in MIR"),
150148
}
151149
.into());

src/librustc_mir/transform/generator.rs

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
//! Otherwise it drops all the values in scope at the last suspension point.
5151
5252
use rustc::hir;
53-
use rustc::hir::{def_id::DefId, GeneratorKind};
53+
use rustc::hir::def_id::DefId;
5454
use rustc::mir::*;
5555
use rustc::mir::visit::{PlaceContext, Visitor, MutVisitor};
5656
use rustc::ty::{self, TyCtxt, AdtDef, Ty};
@@ -1056,28 +1056,17 @@ fn create_generator_resume_function<'tcx>(
10561056
let mut cases = create_cases(body, &transform, |point| Some(point.resume));
10571057

10581058
use rustc::mir::interpret::PanicInfo::{
1059-
GeneratorResumedAfterPanic,
1060-
GeneratorResumedAfterReturn,
1061-
AsyncResumedAfterReturn,
1062-
AsyncResumedAfterPanic,
1059+
ResumedAfterPanic,
1060+
ResumedAfterReturn,
10631061
};
10641062

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

10681066
// Panic when resumed on the returned or poisoned state
1069-
match body.generator_kind {
1070-
Some(GeneratorKind::Async(_)) => {
1071-
cases.insert(1, (RETURNED, insert_panic_block(tcx, body, AsyncResumedAfterReturn)));
1072-
cases.insert(2, (POISONED, insert_panic_block(tcx, body, AsyncResumedAfterPanic)));
1073-
},
1074-
Some(GeneratorKind::Gen) => {
1075-
cases.insert(1, (RETURNED, insert_panic_block(tcx, body, GeneratorResumedAfterReturn)));
1076-
cases.insert(2, (POISONED, insert_panic_block(tcx, body, GeneratorResumedAfterPanic)));
1077-
},
1078-
None => {
1079-
// N/A because we would never create a resume function if there was no generator_kind
1080-
}
1067+
if let Some(generator_kind) = body.generator_kind {
1068+
cases.insert(1, (RETURNED, insert_panic_block(tcx, body, ResumedAfterReturn(generator_kind))));
1069+
cases.insert(2, (POISONED, insert_panic_block(tcx, body, ResumedAfterPanic(generator_kind))));
10811070
};
10821071

10831072
insert_switch(body, cases, &transform, TerminatorKind::Unreachable);

0 commit comments

Comments
 (0)