1
Fork 0

Auto merge of #87465 - audunhalland:refactor_typeck_primary_body_of, r=eddyb

Simplify typeck/primary_body_of, fix comment to match return signature

Hi, new contributor here! I'm carefully reading through the various modules just to learn. I noticed this function, `primary_body_of`, which has gone through a couple of refactors over time, adding new `Option`s to its returned tuple. Observations:

1. the `fn`'s documentation was not all up to date with the the current return signature.
2. `FnHeader` and `FnDecl` are always both `Some` or `None`. So I figured it might just return a reference to the full `hir::FnSig`, for simplicity and more precise typing. It's a pure refactor.

I'm learning better by working with code than just reading it, so here goes! If you want to avoid pure refactor PRs that don't really fix anything, I can revert the code change to only update the comment instead.
This commit is contained in:
bors 2021-08-09 01:08:52 +00:00
commit d3928b183d

View file

@ -257,9 +257,7 @@ fn adt_destructor(tcx: TyCtxt<'_>, def_id: DefId) -> Option<ty::Destructor> {
} }
/// If this `DefId` is a "primary tables entry", returns /// If this `DefId` is a "primary tables entry", returns
/// `Some((body_id, header, decl))` with information about /// `Some((body_id, body_ty, fn_sig))`. Otherwise, returns `None`.
/// its body-id, fn-header and fn-decl (if any). Otherwise,
/// returns `None`.
/// ///
/// If this function returns `Some`, then `typeck_results(def_id)` will /// If this function returns `Some`, then `typeck_results(def_id)` will
/// succeed; if it returns `None`, then `typeck_results(def_id)` may or /// succeed; if it returns `None`, then `typeck_results(def_id)` may or
@ -269,32 +267,28 @@ fn adt_destructor(tcx: TyCtxt<'_>, def_id: DefId) -> Option<ty::Destructor> {
fn primary_body_of( fn primary_body_of(
tcx: TyCtxt<'_>, tcx: TyCtxt<'_>,
id: hir::HirId, id: hir::HirId,
) -> Option<(hir::BodyId, Option<&hir::Ty<'_>>, Option<&hir::FnHeader>, Option<&hir::FnDecl<'_>>)> { ) -> Option<(hir::BodyId, Option<&hir::Ty<'_>>, Option<&hir::FnSig<'_>>)> {
match tcx.hir().get(id) { match tcx.hir().get(id) {
Node::Item(item) => match item.kind { Node::Item(item) => match item.kind {
hir::ItemKind::Const(ref ty, body) | hir::ItemKind::Static(ref ty, _, body) => { hir::ItemKind::Const(ref ty, body) | hir::ItemKind::Static(ref ty, _, body) => {
Some((body, Some(ty), None, None)) Some((body, Some(ty), None))
}
hir::ItemKind::Fn(ref sig, .., body) => {
Some((body, None, Some(&sig.header), Some(&sig.decl)))
} }
hir::ItemKind::Fn(ref sig, .., body) => Some((body, None, Some(&sig))),
_ => None, _ => None,
}, },
Node::TraitItem(item) => match item.kind { Node::TraitItem(item) => match item.kind {
hir::TraitItemKind::Const(ref ty, Some(body)) => Some((body, Some(ty), None, None)), hir::TraitItemKind::Const(ref ty, Some(body)) => Some((body, Some(ty), None)),
hir::TraitItemKind::Fn(ref sig, hir::TraitFn::Provided(body)) => { hir::TraitItemKind::Fn(ref sig, hir::TraitFn::Provided(body)) => {
Some((body, None, Some(&sig.header), Some(&sig.decl))) Some((body, None, Some(&sig)))
} }
_ => None, _ => None,
}, },
Node::ImplItem(item) => match item.kind { Node::ImplItem(item) => match item.kind {
hir::ImplItemKind::Const(ref ty, body) => Some((body, Some(ty), None, None)), hir::ImplItemKind::Const(ref ty, body) => Some((body, Some(ty), None)),
hir::ImplItemKind::Fn(ref sig, body) => { hir::ImplItemKind::Fn(ref sig, body) => Some((body, None, Some(&sig))),
Some((body, None, Some(&sig.header), Some(&sig.decl)))
}
_ => None, _ => None,
}, },
Node::AnonConst(constant) => Some((constant.body, None, None, None)), Node::AnonConst(constant) => Some((constant.body, None, None)),
_ => None, _ => None,
} }
} }
@ -362,14 +356,14 @@ fn typeck_with_fallback<'tcx>(
let span = tcx.hir().span(id); let span = tcx.hir().span(id);
// Figure out what primary body this item has. // Figure out what primary body this item has.
let (body_id, body_ty, fn_header, fn_decl) = primary_body_of(tcx, id).unwrap_or_else(|| { let (body_id, body_ty, fn_sig) = primary_body_of(tcx, id).unwrap_or_else(|| {
span_bug!(span, "can't type-check body of {:?}", def_id); span_bug!(span, "can't type-check body of {:?}", def_id);
}); });
let body = tcx.hir().body(body_id); let body = tcx.hir().body(body_id);
let typeck_results = Inherited::build(tcx, def_id).enter(|inh| { let typeck_results = Inherited::build(tcx, def_id).enter(|inh| {
let param_env = tcx.param_env(def_id); let param_env = tcx.param_env(def_id);
let fcx = if let (Some(header), Some(decl)) = (fn_header, fn_decl) { let fcx = if let Some(hir::FnSig { header, decl, .. }) = fn_sig {
let fn_sig = if crate::collect::get_infer_ret_ty(&decl.output).is_some() { let fn_sig = if crate::collect::get_infer_ret_ty(&decl.output).is_some() {
let fcx = FnCtxt::new(&inh, param_env, body.value.hir_id); let fcx = FnCtxt::new(&inh, param_env, body.value.hir_id);
<dyn AstConv<'_>>::ty_of_fn( <dyn AstConv<'_>>::ty_of_fn(
@ -513,7 +507,7 @@ fn typeck_with_fallback<'tcx>(
fcx.select_all_obligations_or_error(); fcx.select_all_obligations_or_error();
if fn_decl.is_some() { if fn_sig.is_some() {
fcx.regionck_fn(id, body); fcx.regionck_fn(id, body);
} else { } else {
fcx.regionck_expr(body); fcx.regionck_expr(body);