Skip to content

Commit c97d899

Browse files
committed
spanview debug output caused ICE when a function had no body
1 parent f12795f commit c97d899

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

compiler/rustc_mir/src/util/spanview.rs

+10-9
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,11 @@ where
9999
W: Write,
100100
{
101101
let def_id = body.source.def_id();
102-
let body_span = hir_body(tcx, def_id).value.span;
102+
let hir_body = hir_body(tcx, def_id);
103+
if hir_body.is_none() {
104+
return Ok(());
105+
}
106+
let body_span = hir_body.unwrap().value.span;
103107
let mut span_viewables = Vec::new();
104108
for (bb, data) in body.basic_blocks().iter_enumerated() {
105109
match spanview {
@@ -664,19 +668,16 @@ fn fn_span<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> Span {
664668
let hir_id =
665669
tcx.hir().local_def_id_to_hir_id(def_id.as_local().expect("expected DefId is local"));
666670
let fn_decl_span = tcx.hir().span(hir_id);
667-
let body_span = hir_body(tcx, def_id).value.span;
668-
if fn_decl_span.ctxt() == body_span.ctxt() {
669-
fn_decl_span.to(body_span)
671+
if let Some(body_span) = hir_body(tcx, def_id).map(|hir_body| hir_body.value.span) {
672+
if fn_decl_span.ctxt() == body_span.ctxt() { fn_decl_span.to(body_span) } else { body_span }
670673
} else {
671-
// This probably occurs for functions defined via macros
672-
body_span
674+
fn_decl_span
673675
}
674676
}
675677

676-
fn hir_body<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> &'tcx rustc_hir::Body<'tcx> {
678+
fn hir_body<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> Option<&'tcx rustc_hir::Body<'tcx>> {
677679
let hir_node = tcx.hir().get_if_local(def_id).expect("expected DefId is local");
678-
let fn_body_id = hir::map::associated_body(hir_node).expect("HIR node is a function with body");
679-
tcx.hir().body(fn_body_id)
680+
hir::map::associated_body(hir_node).map(|fn_body_id| tcx.hir().body(fn_body_id))
680681
}
681682

682683
fn escape_html(s: &str) -> String {

0 commit comments

Comments
 (0)