]> git.lizzy.rs Git - rust.git/commitdiff
Remove duplication using single variant for error
authorDavid Haig <david@ninjasource.com>
Tue, 26 Nov 2019 00:30:07 +0000 (00:30 +0000)
committerDavid Haig <david@ninjasource.com>
Thu, 28 Nov 2019 07:43:53 +0000 (07:43 +0000)
src/librustc/mir/interpret/error.rs
src/librustc/mir/mod.rs
src/librustc/mir/visit.rs
src/librustc_mir/interpret/terminator.rs
src/librustc_mir/transform/generator.rs

index 93543bc361643587f39cace61ac8bdbc79354a6b..6ff1eee593b59e2e6271fb402e120c5d4d550ba0 100644 (file)
@@ -13,7 +13,7 @@
 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 @@ pub fn description(&self) -> &'static str {
                 "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"),
         }
index 67dc1da8a40e47e4b6d69ea2f5a4bbc75d77395b..8c1690a177bde150b5bec4ea5845159e1be66b75 100644 (file)
@@ -2981,8 +2981,7 @@ fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
                             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 @@ fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
                             len.visit_with(visitor) || index.visit_with(visitor),
                         Panic { .. } | Overflow(_) | OverflowNeg |
                         DivisionByZero | RemainderByZero |
-                        GeneratorResumedAfterReturn | GeneratorResumedAfterPanic |
-                        AsyncResumedAfterReturn | AsyncResumedAfterPanic =>
+                        ResumedAfterReturn(_) | ResumedAfterPanic(_) =>
                             false
                     }
                 } else {
index cce1ef9eb8be88f5b9e1fdfbcc23972ac32b00ba..58c12ef2501559ed514b44a90fd5d6547c75aab3 100644 (file)
@@ -517,8 +517,7 @@ fn super_assert_message(&mut self,
                         self.visit_operand(index, location);
                     }
                     Panic { .. } | Overflow(_) | OverflowNeg | DivisionByZero | RemainderByZero |
-                    GeneratorResumedAfterReturn | GeneratorResumedAfterPanic |
-                    AsyncResumedAfterReturn | AsyncResumedAfterPanic => {
+                    ResumedAfterReturn(_) | ResumedAfterPanic(_) => {
                         // Nothing to visit
                     }
                 }
index 59b721b73b5f22f9119d37911127b3de019d91ec..e5a62fc6c86c37a1d8ffd5f203e7d41440b70770 100644 (file)
@@ -142,10 +142,8 @@ pub(super) fn eval_terminator(
                         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());
index c6fe877ec59e7a2a9eac26ac00924aa7f6912989..799d5d3d05517619a7cd31080d734430592e6cfa 100644 (file)
@@ -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);