1
Fork 0

Auto merge of #135031 - RalfJung:intrinsics-without-body, r=oli-obk

rustc_intrinsic: support functions without body

We synthesize a HIR body `loop {}` but such bodyless intrinsics.

Most of the diff is due to turning `ItemKind::Fn` into a brace (named-field) enum variant, because it carries a `bool`-typed field now. This is to remember whether the function has a body. MIR building panics to avoid ever translating the fake `loop {}` body, and the intrinsic logic uses the lack of a body to implicitly mark that intrinsic as must-be-overridden.

I first tried actually having no body rather than generating the fake body, but there's a *lot* of code that assumes that all function items have HIR and MIR, so this didn't work very well. Then I noticed that even `rustc_intrinsic_must_be_overridden` intrinsics have MIR generated (they are filled with an `Unreachable` terminator) so I guess I am not the first to discover this. ;)

r? `@oli-obk`
This commit is contained in:
bors 2025-01-04 12:50:38 +00:00
commit fd127a3a84
81 changed files with 272 additions and 175 deletions

View file

@ -1005,11 +1005,25 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
if let Some(source_scope) = scope {
self.source_scope = source_scope;
}
if self.tcx.intrinsic(self.def_id).is_some_and(|i| i.must_be_overridden) {
let source_info = self.source_info(rustc_span::DUMMY_SP);
self.cfg.terminate(block, source_info, TerminatorKind::Unreachable);
self.cfg.start_new_block().unit()
} else {
// Ensure we don't silently codegen functions with fake bodies.
match self.tcx.hir_node(self.hir_id) {
hir::Node::Item(hir::Item {
kind: hir::ItemKind::Fn { has_body: false, .. },
..
}) => {
self.tcx.dcx().span_delayed_bug(
expr_span,
format!("fn item without body has reached MIR building: {:?}", self.def_id),
);
}
_ => {}
}
self.expr_into_dest(Place::return_place(), block, expr_id)
}
}