Skip to content

Commit 3f31044

Browse files
Handle typeck errors properly
1 parent 3797b03 commit 3f31044

File tree

3 files changed

+9
-1
lines changed

3 files changed

+9
-1
lines changed

compiler/rustc_middle/src/query/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ rustc_queries! {
220220
desc { "checking if the crate is_panic_runtime" }
221221
}
222222

223-
/// Fetch the THIR for a given body.
223+
/// Fetch the THIR for a given body. If typeck for that body failed, returns an empty `Thir`.
224224
query thir_body(key: ty::WithOptConstParam<LocalDefId>) -> (&'tcx Steal<thir::Thir<'tcx>>, thir::ExprId) {
225225
desc { |tcx| "building THIR for `{}`", tcx.def_path_str(key.did.to_def_id()) }
226226
}

compiler/rustc_mir_build/src/check_unsafety.rs

+5
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,11 @@ impl UnsafeOpKind {
331331
pub fn check_unsafety<'tcx>(tcx: TyCtxt<'tcx>, def: ty::WithOptConstParam<LocalDefId>) {
332332
let (thir, expr) = tcx.thir_body(def);
333333
let thir = &thir.borrow();
334+
// If `thir` is empty, a type error occured, skip this body.
335+
if thir.exprs.is_empty() {
336+
return;
337+
}
338+
334339
let hir_id = tcx.hir().local_def_id_to_hir_id(def.did);
335340
let body_unsafety = tcx.hir().fn_sig_by_hir_id(hir_id).map_or(BodyUnsafety::Safe, |fn_sig| {
336341
if fn_sig.header.unsafety == hir::Unsafety::Unsafe {

compiler/rustc_mir_build/src/thir/cx/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ crate fn thir_body<'tcx>(
2323
let hir = tcx.hir();
2424
let body = hir.body(hir.body_owned_by(hir.local_def_id_to_hir_id(owner_def.did)));
2525
let mut cx = Cx::new(tcx, owner_def);
26+
if cx.typeck_results.tainted_by_errors.is_some() {
27+
return (tcx.alloc_steal_thir(Thir::new()), ExprId::from_u32(0));
28+
}
2629
let expr = cx.mirror_expr(&body.value);
2730
(tcx.alloc_steal_thir(cx.thir), expr)
2831
}

0 commit comments

Comments
 (0)