about summary refs log tree commit diff
diff options
context:
space:
mode:
authorDavid Haig <david@ninjasource.com>2019-11-26 00:30:07 +0000
committerDavid Haig <david@ninjasource.com>2019-11-28 07:43:53 +0000
commit6ef625fb41772b8a694588b7497687b8f7ee1c72 (patch)
tree85f1816723e530d55cf4a58ac7191a309b2aa5cf
parent88821ed54d2eacc35ccbf6ab056b6677f23c3e1a (diff)
downloadrust-6ef625fb41772b8a694588b7497687b8f7ee1c72.tar.gz
rust-6ef625fb41772b8a694588b7497687b8f7ee1c72.zip
Remove duplication using single variant for error
-rw-r--r--src/librustc/mir/interpret/error.rs22
-rw-r--r--src/librustc/mir/mod.rs6
-rw-r--r--src/librustc/mir/visit.rs3
-rw-r--r--src/librustc_mir/interpret/terminator.rs6
-rw-r--r--src/librustc_mir/transform/generator.rs23
5 files changed, 22 insertions, 38 deletions
diff --git a/src/librustc/mir/interpret/error.rs b/src/librustc/mir/interpret/error.rs
index 93543bc3616..6ff1eee593b 100644
--- a/src/librustc/mir/interpret/error.rs
+++ b/src/librustc/mir/interpret/error.rs
@@ -13,7 +13,7 @@ use rustc_macros::HashStable;
 use rustc_target::spec::abi::Abi;
 use syntax_pos::{Pos, Span};
 use syntax::symbol::Symbol;
-
+use hir::GeneratorKind;
 use std::{fmt, env};
 
 use rustc_error_codes::*;
@@ -264,10 +264,8 @@ pub enum PanicInfo<O> {
     OverflowNeg,
     DivisionByZero,
     RemainderByZero,
-    GeneratorResumedAfterReturn,
-    GeneratorResumedAfterPanic,
-    AsyncResumedAfterReturn,
-    AsyncResumedAfterPanic,
+    ResumedAfterReturn(GeneratorKind),
+    ResumedAfterPanic(GeneratorKind),
 }
 
 /// Type for MIR `Assert` terminator error messages.
@@ -302,14 +300,16 @@ impl<O> PanicInfo<O> {
                 "attempt to divide by zero",
             RemainderByZero =>
                 "attempt to calculate the remainder with a divisor of zero",
-            GeneratorResumedAfterReturn =>
+            ResumedAfterReturn(GeneratorKind::Gen) =>
                 "generator resumed after completion",
-            GeneratorResumedAfterPanic =>
-                "generator resumed after panicking",
-            AsyncResumedAfterReturn =>
+            // FIXME: Do we want a separate message for each Async variant (Block, Closure, Fn)?
+            ResumedAfterReturn(GeneratorKind::Async(_)) =>
                 "`async fn` resumed after completion",
-            AsyncResumedAfterPanic =>
-                "`async fn` resumed after panic",
+            ResumedAfterPanic(GeneratorKind::Gen) =>
+                "generator resumed after panicking",
+            // FIXME: Do we want a separate message for each Async variant (Block, Closure, Fn)?
+            ResumedAfterPanic(GeneratorKind::Async(_)) =>
+                "`async fn` resumed after panicking",
             Panic { .. } | BoundsCheck { .. } =>
                 bug!("Unexpected PanicInfo"),
         }
diff --git a/src/librustc/mir/mod.rs b/src/librustc/mir/mod.rs
index 67dc1da8a40..8c1690a177b 100644
--- a/src/librustc/mir/mod.rs
+++ b/src/librustc/mir/mod.rs
@@ -2981,8 +2981,7 @@ impl<'tcx> TypeFoldable<'tcx> for Terminator<'tcx> {
                             index: index.fold_with(folder),
                         },
                     Panic { .. } | Overflow(_) | OverflowNeg | DivisionByZero | RemainderByZero |
-                    GeneratorResumedAfterReturn | GeneratorResumedAfterPanic |
-                    AsyncResumedAfterReturn | AsyncResumedAfterPanic =>
+                    ResumedAfterReturn(_) | ResumedAfterPanic(_)  =>
                         msg.clone(),
                 };
                 Assert { cond: cond.fold_with(folder), expected, msg, target, cleanup }
@@ -3028,8 +3027,7 @@ impl<'tcx> TypeFoldable<'tcx> for Terminator<'tcx> {
                             len.visit_with(visitor) || index.visit_with(visitor),
                         Panic { .. } | Overflow(_) | OverflowNeg |
                         DivisionByZero | RemainderByZero |
-                        GeneratorResumedAfterReturn | GeneratorResumedAfterPanic |
-                        AsyncResumedAfterReturn | AsyncResumedAfterPanic =>
+                        ResumedAfterReturn(_) | ResumedAfterPanic(_) =>
                             false
                     }
                 } else {
diff --git a/src/librustc/mir/visit.rs b/src/librustc/mir/visit.rs
index cce1ef9eb8b..58c12ef2501 100644
--- a/src/librustc/mir/visit.rs
+++ b/src/librustc/mir/visit.rs
@@ -517,8 +517,7 @@ macro_rules! make_mir_visitor {
                         self.visit_operand(index, location);
                     }
                     Panic { .. } | Overflow(_) | OverflowNeg | DivisionByZero | RemainderByZero |
-                    GeneratorResumedAfterReturn | GeneratorResumedAfterPanic |
-                    AsyncResumedAfterReturn | AsyncResumedAfterPanic => {
+                    ResumedAfterReturn(_) | ResumedAfterPanic(_) => {
                         // Nothing to visit
                     }
                 }
diff --git a/src/librustc_mir/interpret/terminator.rs b/src/librustc_mir/interpret/terminator.rs
index 59b721b73b5..e5a62fc6c86 100644
--- a/src/librustc_mir/interpret/terminator.rs
+++ b/src/librustc_mir/interpret/terminator.rs
@@ -142,10 +142,8 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
                         OverflowNeg => err_panic!(OverflowNeg),
                         DivisionByZero => err_panic!(DivisionByZero),
                         RemainderByZero => err_panic!(RemainderByZero),
-                        GeneratorResumedAfterReturn => err_panic!(GeneratorResumedAfterReturn),
-                        GeneratorResumedAfterPanic => err_panic!(GeneratorResumedAfterPanic),
-                        AsyncResumedAfterReturn => err_panic!(AsyncResumedAfterReturn),
-                        AsyncResumedAfterPanic => err_panic!(AsyncResumedAfterPanic),
+                        ResumedAfterReturn(generator_kind) => err_panic!(ResumedAfterReturn(*generator_kind)),
+                        ResumedAfterPanic(generator_kind) => err_panic!(ResumedAfterPanic(*generator_kind)),
                         Panic { .. } => bug!("`Panic` variant cannot occur in MIR"),
                     }
                     .into());
diff --git a/src/librustc_mir/transform/generator.rs b/src/librustc_mir/transform/generator.rs
index c6fe877ec59..799d5d3d055 100644
--- a/src/librustc_mir/transform/generator.rs
+++ b/src/librustc_mir/transform/generator.rs
@@ -50,7 +50,7 @@
 //! Otherwise it drops all the values in scope at the last suspension point.
 
 use rustc::hir;
-use rustc::hir::{def_id::DefId, GeneratorKind};
+use rustc::hir::def_id::DefId;
 use rustc::mir::*;
 use rustc::mir::visit::{PlaceContext, Visitor, MutVisitor};
 use rustc::ty::{self, TyCtxt, AdtDef, Ty};
@@ -1056,28 +1056,17 @@ fn create_generator_resume_function<'tcx>(
     let mut cases = create_cases(body, &transform, |point| Some(point.resume));
 
     use rustc::mir::interpret::PanicInfo::{
-        GeneratorResumedAfterPanic,
-        GeneratorResumedAfterReturn,
-        AsyncResumedAfterReturn,
-        AsyncResumedAfterPanic,
+        ResumedAfterPanic,
+        ResumedAfterReturn,
     };
 
     // Jump to the entry point on the unresumed
     cases.insert(0, (UNRESUMED, BasicBlock::new(0)));
 
     // Panic when resumed on the returned or poisoned state
-    match body.generator_kind {
-        Some(GeneratorKind::Async(_)) => {
-            cases.insert(1, (RETURNED, insert_panic_block(tcx, body, AsyncResumedAfterReturn)));
-            cases.insert(2, (POISONED, insert_panic_block(tcx, body, AsyncResumedAfterPanic)));
-        },
-        Some(GeneratorKind::Gen) => {
-            cases.insert(1, (RETURNED, insert_panic_block(tcx, body, GeneratorResumedAfterReturn)));
-            cases.insert(2, (POISONED, insert_panic_block(tcx, body, GeneratorResumedAfterPanic)));
-        },
-        None => {
-            // N/A because we would never create a resume function if there was no generator_kind
-        }
+    if let Some(generator_kind) = body.generator_kind {
+        cases.insert(1, (RETURNED, insert_panic_block(tcx, body, ResumedAfterReturn(generator_kind))));
+        cases.insert(2, (POISONED, insert_panic_block(tcx, body, ResumedAfterPanic(generator_kind))));
     };
 
     insert_switch(body, cases, &transform, TerminatorKind::Unreachable);