Skip to content

Commit d3928b1

Browse files
committed
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.
2 parents 74a11c6 + 855fa13 commit d3928b1

File tree

1 file changed

+12
-18
lines changed
  • compiler/rustc_typeck/src/check

1 file changed

+12
-18
lines changed

compiler/rustc_typeck/src/check/mod.rs

+12-18
Original file line numberDiff line numberDiff line change
@@ -257,9 +257,7 @@ fn adt_destructor(tcx: TyCtxt<'_>, def_id: DefId) -> Option<ty::Destructor> {
257257
}
258258

259259
/// If this `DefId` is a "primary tables entry", returns
260-
/// `Some((body_id, header, decl))` with information about
261-
/// its body-id, fn-header and fn-decl (if any). Otherwise,
262-
/// returns `None`.
260+
/// `Some((body_id, body_ty, fn_sig))`. Otherwise, returns `None`.
263261
///
264262
/// If this function returns `Some`, then `typeck_results(def_id)` will
265263
/// 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> {
269267
fn primary_body_of(
270268
tcx: TyCtxt<'_>,
271269
id: hir::HirId,
272-
) -> Option<(hir::BodyId, Option<&hir::Ty<'_>>, Option<&hir::FnHeader>, Option<&hir::FnDecl<'_>>)> {
270+
) -> Option<(hir::BodyId, Option<&hir::Ty<'_>>, Option<&hir::FnSig<'_>>)> {
273271
match tcx.hir().get(id) {
274272
Node::Item(item) => match item.kind {
275273
hir::ItemKind::Const(ref ty, body) | hir::ItemKind::Static(ref ty, _, body) => {
276-
Some((body, Some(ty), None, None))
277-
}
278-
hir::ItemKind::Fn(ref sig, .., body) => {
279-
Some((body, None, Some(&sig.header), Some(&sig.decl)))
274+
Some((body, Some(ty), None))
280275
}
276+
hir::ItemKind::Fn(ref sig, .., body) => Some((body, None, Some(&sig))),
281277
_ => None,
282278
},
283279
Node::TraitItem(item) => match item.kind {
284-
hir::TraitItemKind::Const(ref ty, Some(body)) => Some((body, Some(ty), None, None)),
280+
hir::TraitItemKind::Const(ref ty, Some(body)) => Some((body, Some(ty), None)),
285281
hir::TraitItemKind::Fn(ref sig, hir::TraitFn::Provided(body)) => {
286-
Some((body, None, Some(&sig.header), Some(&sig.decl)))
282+
Some((body, None, Some(&sig)))
287283
}
288284
_ => None,
289285
},
290286
Node::ImplItem(item) => match item.kind {
291-
hir::ImplItemKind::Const(ref ty, body) => Some((body, Some(ty), None, None)),
292-
hir::ImplItemKind::Fn(ref sig, body) => {
293-
Some((body, None, Some(&sig.header), Some(&sig.decl)))
294-
}
287+
hir::ImplItemKind::Const(ref ty, body) => Some((body, Some(ty), None)),
288+
hir::ImplItemKind::Fn(ref sig, body) => Some((body, None, Some(&sig))),
295289
_ => None,
296290
},
297-
Node::AnonConst(constant) => Some((constant.body, None, None, None)),
291+
Node::AnonConst(constant) => Some((constant.body, None, None)),
298292
_ => None,
299293
}
300294
}
@@ -362,14 +356,14 @@ fn typeck_with_fallback<'tcx>(
362356
let span = tcx.hir().span(id);
363357

364358
// Figure out what primary body this item has.
365-
let (body_id, body_ty, fn_header, fn_decl) = primary_body_of(tcx, id).unwrap_or_else(|| {
359+
let (body_id, body_ty, fn_sig) = primary_body_of(tcx, id).unwrap_or_else(|| {
366360
span_bug!(span, "can't type-check body of {:?}", def_id);
367361
});
368362
let body = tcx.hir().body(body_id);
369363

370364
let typeck_results = Inherited::build(tcx, def_id).enter(|inh| {
371365
let param_env = tcx.param_env(def_id);
372-
let fcx = if let (Some(header), Some(decl)) = (fn_header, fn_decl) {
366+
let fcx = if let Some(hir::FnSig { header, decl, .. }) = fn_sig {
373367
let fn_sig = if crate::collect::get_infer_ret_ty(&decl.output).is_some() {
374368
let fcx = FnCtxt::new(&inh, param_env, body.value.hir_id);
375369
<dyn AstConv<'_>>::ty_of_fn(
@@ -513,7 +507,7 @@ fn typeck_with_fallback<'tcx>(
513507

514508
fcx.select_all_obligations_or_error();
515509

516-
if fn_decl.is_some() {
510+
if fn_sig.is_some() {
517511
fcx.regionck_fn(id, body);
518512
} else {
519513
fcx.regionck_expr(body);

0 commit comments

Comments
 (0)