From 3f31044d908fed9d158cb8d24716447d8bc5f45e Mon Sep 17 00:00:00 2001 From: LeSeulArtichaut Date: Sat, 22 May 2021 15:40:26 +0200 Subject: [PATCH] Handle typeck errors properly --- compiler/rustc_middle/src/query/mod.rs | 2 +- compiler/rustc_mir_build/src/check_unsafety.rs | 5 +++++ compiler/rustc_mir_build/src/thir/cx/mod.rs | 3 +++ 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_middle/src/query/mod.rs b/compiler/rustc_middle/src/query/mod.rs index f940cb62d9e2..9125be33c93d 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) -> (&'tcx Steal>, 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 d0d376bd3e3d..66fe5524ef4b 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) { 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 f7f092b2037a..49ba71e3520d 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) }