about summary refs log tree commit diff
diff options
context:
space:
mode:
authorLeSeulArtichaut <leseulartichaut@gmail.com>2021-05-22 15:40:26 +0200
committerLeSeulArtichaut <leseulartichaut@gmail.com>2021-05-22 16:21:36 +0200
commit3f31044d908fed9d158cb8d24716447d8bc5f45e (patch)
treecb226f2e4b6e57dd32821122472adf701e1cb1b8
parent3797b0335ab1e119438b7aadb7c8bf80607b0b03 (diff)
downloadrust-3f31044d908fed9d158cb8d24716447d8bc5f45e.tar.gz
rust-3f31044d908fed9d158cb8d24716447d8bc5f45e.zip
Handle typeck errors properly
-rw-r--r--compiler/rustc_middle/src/query/mod.rs2
-rw-r--r--compiler/rustc_mir_build/src/check_unsafety.rs5
-rw-r--r--compiler/rustc_mir_build/src/thir/cx/mod.rs3
3 files changed, 9 insertions, 1 deletions
diff --git a/compiler/rustc_middle/src/query/mod.rs b/compiler/rustc_middle/src/query/mod.rs
index f940cb62d9e..9125be33c93 100644
--- a/compiler/rustc_middle/src/query/mod.rs
+++ b/compiler/rustc_middle/src/query/mod.rs
@@ -220,7 +220,7 @@ rustc_queries! {
         desc { "checking if the crate is_panic_runtime" }
     }
 
-    /// Fetch the THIR for a given body.
+    /// Fetch the THIR for a given body. If typeck for that body failed, returns an empty `Thir`.
     query thir_body(key: ty::WithOptConstParam<LocalDefId>) -> (&'tcx Steal<thir::Thir<'tcx>>, thir::ExprId) {
         desc { |tcx| "building THIR for `{}`", tcx.def_path_str(key.did.to_def_id()) }
     }
diff --git a/compiler/rustc_mir_build/src/check_unsafety.rs b/compiler/rustc_mir_build/src/check_unsafety.rs
index d0d376bd3e3..66fe5524ef4 100644
--- a/compiler/rustc_mir_build/src/check_unsafety.rs
+++ b/compiler/rustc_mir_build/src/check_unsafety.rs
@@ -331,6 +331,11 @@ impl UnsafeOpKind {
 pub fn check_unsafety<'tcx>(tcx: TyCtxt<'tcx>, def: ty::WithOptConstParam<LocalDefId>) {
     let (thir, expr) = tcx.thir_body(def);
     let thir = &thir.borrow();
+    // If `thir` is empty, a type error occured, skip this body.
+    if thir.exprs.is_empty() {
+        return;
+    }
+
     let hir_id = tcx.hir().local_def_id_to_hir_id(def.did);
     let body_unsafety = tcx.hir().fn_sig_by_hir_id(hir_id).map_or(BodyUnsafety::Safe, |fn_sig| {
         if fn_sig.header.unsafety == hir::Unsafety::Unsafe {
diff --git a/compiler/rustc_mir_build/src/thir/cx/mod.rs b/compiler/rustc_mir_build/src/thir/cx/mod.rs
index f7f092b2037..49ba71e3520 100644
--- a/compiler/rustc_mir_build/src/thir/cx/mod.rs
+++ b/compiler/rustc_mir_build/src/thir/cx/mod.rs
@@ -23,6 +23,9 @@ crate fn thir_body<'tcx>(
     let hir = tcx.hir();
     let body = hir.body(hir.body_owned_by(hir.local_def_id_to_hir_id(owner_def.did)));
     let mut cx = Cx::new(tcx, owner_def);
+    if cx.typeck_results.tainted_by_errors.is_some() {
+        return (tcx.alloc_steal_thir(Thir::new()), ExprId::from_u32(0));
+    }
     let expr = cx.mirror_expr(&body.value);
     (tcx.alloc_steal_thir(cx.thir), expr)
 }