Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify typeck/primary_body_of, fix comment to match return signature #87465

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 12 additions & 18 deletions compiler/rustc_typeck/src/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,9 +257,7 @@ fn adt_destructor(tcx: TyCtxt<'_>, def_id: DefId) -> Option<ty::Destructor> {
}

/// If this `DefId` is a "primary tables entry", returns
/// `Some((body_id, header, decl))` with information about
/// its body-id, fn-header and fn-decl (if any). Otherwise,
/// returns `None`.
/// `Some((body_id, body_ty, fn_sig))`. Otherwise, returns `None`.
///
/// If this function returns `Some`, then `typeck_results(def_id)` will
/// succeed; if it returns `None`, then `typeck_results(def_id)` may or
Expand All @@ -269,32 +267,28 @@ fn adt_destructor(tcx: TyCtxt<'_>, def_id: DefId) -> Option<ty::Destructor> {
fn primary_body_of(
tcx: TyCtxt<'_>,
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) {
Node::Item(item) => match item.kind {
hir::ItemKind::Const(ref ty, body) | hir::ItemKind::Static(ref ty, _, body) => {
Some((body, Some(ty), None, None))
}
hir::ItemKind::Fn(ref sig, .., body) => {
Some((body, None, Some(&sig.header), Some(&sig.decl)))
Some((body, Some(ty), None))
}
hir::ItemKind::Fn(ref sig, .., body) => Some((body, None, Some(&sig))),
_ => None,
},
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)) => {
Some((body, None, Some(&sig.header), Some(&sig.decl)))
Some((body, None, Some(&sig)))
}
_ => None,
},
Node::ImplItem(item) => match item.kind {
hir::ImplItemKind::Const(ref ty, body) => Some((body, Some(ty), None, None)),
hir::ImplItemKind::Fn(ref sig, body) => {
Some((body, None, Some(&sig.header), Some(&sig.decl)))
}
hir::ImplItemKind::Const(ref ty, body) => Some((body, Some(ty), None)),
hir::ImplItemKind::Fn(ref sig, body) => Some((body, None, Some(&sig))),
Comment on lines 286 to +288
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is probably why the two halves were returned separately, I seem to recall a MethodSig existing that was different from FnSig.

Aha, found it! 30d7279 followed by 27511b2 resulted in an unified hir::FnSig, whereas before there was only hir::MethodSig, without a hir::FnSig.

_ => None,
},
Node::AnonConst(constant) => Some((constant.body, None, None, None)),
Node::AnonConst(constant) => Some((constant.body, None, None)),
_ => None,
}
}
Expand Down Expand Up @@ -362,14 +356,14 @@ fn typeck_with_fallback<'tcx>(
let span = tcx.hir().span(id);

// 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);
});
let body = tcx.hir().body(body_id);

let typeck_results = Inherited::build(tcx, def_id).enter(|inh| {
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 fcx = FnCtxt::new(&inh, param_env, body.value.hir_id);
<dyn AstConv<'_>>::ty_of_fn(
Expand Down Expand Up @@ -513,7 +507,7 @@ fn typeck_with_fallback<'tcx>(

fcx.select_all_obligations_or_error();

if fn_decl.is_some() {
if fn_sig.is_some() {
fcx.regionck_fn(id, body);
} else {
fcx.regionck_expr(body);
Expand Down