2020-07-06 23:49:53 +02:00
|
|
|
//! This module contains the functionality to convert from the wacky tcx data
|
2020-07-21 09:09:27 +00:00
|
|
|
//! structures into the THIR. The `builder` is generally ignorant of the tcx,
|
2019-02-08 14:53:55 +01:00
|
|
|
//! etc., and instead goes through the `Cx` for most of its work.
|
2015-10-05 12:31:48 -04:00
|
|
|
|
2021-04-04 18:42:17 +02:00
|
|
|
use rustc_data_structures::steal::Steal;
|
2022-01-22 18:49:12 -06:00
|
|
|
use rustc_errors::ErrorGuaranteed;
|
2020-01-05 02:37:57 +01:00
|
|
|
use rustc_hir as hir;
|
2024-08-17 22:18:37 +10:00
|
|
|
use rustc_hir::HirId;
|
2022-08-22 22:29:25 +02:00
|
|
|
use rustc_hir::def::DefKind;
|
2020-07-06 23:49:53 +02:00
|
|
|
use rustc_hir::def_id::{DefId, LocalDefId};
|
2022-08-27 12:21:02 +02:00
|
|
|
use rustc_hir::lang_items::LangItem;
|
2024-05-29 10:03:40 +00:00
|
|
|
use rustc_middle::bug;
|
2020-03-29 17:19:48 +02:00
|
|
|
use rustc_middle::middle::region;
|
2021-04-04 02:24:02 +02:00
|
|
|
use rustc_middle::thir::*;
|
2024-05-02 17:49:23 +02:00
|
|
|
use rustc_middle::ty::{self, RvalueScopes, TyCtxt};
|
2024-05-22 14:35:22 +10:00
|
|
|
use tracing::instrument;
|
2015-08-18 17:59:21 -04:00
|
|
|
|
2021-04-04 02:24:02 +02:00
|
|
|
use crate::thir::pattern::pat_from_hir;
|
2024-07-29 08:13:50 +10:00
|
|
|
|
2025-02-04 15:43:02 +11:00
|
|
|
/// Query implementation for [`TyCtxt::thir_body`].
|
2022-12-20 22:10:40 +01:00
|
|
|
pub(crate) fn thir_body(
|
|
|
|
tcx: TyCtxt<'_>,
|
2022-05-08 15:53:19 +02:00
|
|
|
owner_def: LocalDefId,
|
2022-12-20 22:10:40 +01:00
|
|
|
) -> Result<(&Steal<Thir<'_>>, ExprId), ErrorGuaranteed> {
|
2025-02-17 14:17:57 +11:00
|
|
|
let body = tcx.hir_body_owned_by(owner_def);
|
2025-02-04 15:43:02 +11:00
|
|
|
let mut cx = ThirBuildCx::new(tcx, owner_def);
|
2022-01-22 18:49:12 -06:00
|
|
|
if let Some(reported) = cx.typeck_results.tainted_by_errors {
|
|
|
|
return Err(reported);
|
2021-05-22 15:40:26 +02:00
|
|
|
}
|
2023-11-21 20:07:32 +01:00
|
|
|
let expr = cx.mirror_expr(body.value);
|
2022-08-27 12:21:02 +02:00
|
|
|
|
2023-11-24 19:28:19 +03:00
|
|
|
let owner_id = tcx.local_def_id_to_hir_id(owner_def);
|
2025-02-17 14:17:57 +11:00
|
|
|
if let Some(fn_decl) = tcx.hir_fn_decl_by_hir_id(owner_id) {
|
2022-05-08 15:53:19 +02:00
|
|
|
let closure_env_param = cx.closure_env_param(owner_def, owner_id);
|
2024-05-29 10:03:40 +00:00
|
|
|
let explicit_params = cx.explicit_params(owner_id, fn_decl, &body);
|
2022-08-22 22:29:25 +02:00
|
|
|
cx.thir.params = closure_env_param.into_iter().chain(explicit_params).collect();
|
|
|
|
|
|
|
|
// The resume argument may be missing, in that case we need to provide it here.
|
|
|
|
// It will always be `()` in this case.
|
2023-11-26 21:05:08 +08:00
|
|
|
if tcx.is_coroutine(owner_def.to_def_id()) && body.params.is_empty() {
|
2022-08-22 22:29:25 +02:00
|
|
|
cx.thir.params.push(Param {
|
2024-05-02 17:49:23 +02:00
|
|
|
ty: tcx.types.unit,
|
2022-08-22 22:29:25 +02:00
|
|
|
pat: None,
|
|
|
|
ty_span: None,
|
|
|
|
self_kind: None,
|
|
|
|
hir_id: None,
|
|
|
|
});
|
|
|
|
}
|
2022-08-27 12:21:02 +02:00
|
|
|
}
|
|
|
|
|
2022-01-22 18:49:12 -06:00
|
|
|
Ok((tcx.alloc_steal_thir(cx.thir), expr))
|
2021-03-05 21:56:02 +01:00
|
|
|
}
|
|
|
|
|
2025-02-04 15:43:02 +11:00
|
|
|
/// Context for lowering HIR to THIR for a single function body (or other kind of body).
|
|
|
|
struct ThirBuildCx<'tcx> {
|
2021-03-05 21:56:02 +01:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2025-02-04 15:43:02 +11:00
|
|
|
/// The THIR data that this context is building.
|
2021-04-03 19:58:46 +02:00
|
|
|
thir: Thir<'tcx>,
|
2017-07-26 15:54:44 +03:00
|
|
|
|
2024-11-22 12:17:50 +01:00
|
|
|
typing_env: ty::TypingEnv<'tcx>,
|
2017-07-26 15:54:44 +03:00
|
|
|
|
2022-08-30 00:10:40 +02:00
|
|
|
region_scope_tree: &'tcx region::ScopeTree,
|
|
|
|
typeck_results: &'tcx ty::TypeckResults<'tcx>,
|
|
|
|
rvalue_scopes: &'tcx RvalueScopes,
|
2016-05-26 15:42:29 +03:00
|
|
|
|
2022-08-03 04:30:13 -07:00
|
|
|
/// False to indicate that adjustments should not be applied. Only used for `custom_mir`
|
|
|
|
apply_adjustments: bool,
|
|
|
|
|
2019-05-07 04:40:36 +03:00
|
|
|
/// The `DefId` of the owner of this body.
|
|
|
|
body_owner: DefId,
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
|
|
|
|
2025-02-04 15:43:02 +11:00
|
|
|
impl<'tcx> ThirBuildCx<'tcx> {
|
|
|
|
fn new(tcx: TyCtxt<'tcx>, def: LocalDefId) -> Self {
|
2022-05-08 15:53:19 +02:00
|
|
|
let typeck_results = tcx.typeck(def);
|
2023-11-24 19:28:19 +03:00
|
|
|
let hir_id = tcx.local_def_id_to_hir_id(def);
|
2023-01-22 14:37:16 +00:00
|
|
|
|
2025-02-17 16:09:46 +00:00
|
|
|
let body_type = match tcx.hir_body_owner_kind(def) {
|
|
|
|
rustc_hir::BodyOwnerKind::Fn | rustc_hir::BodyOwnerKind::Closure => {
|
|
|
|
// fetch the fully liberated fn signature (that is, all bound
|
|
|
|
// types/lifetimes replaced)
|
|
|
|
BodyTy::Fn(typeck_results.liberated_fn_sigs()[hir_id])
|
|
|
|
}
|
|
|
|
rustc_hir::BodyOwnerKind::Const { .. } | rustc_hir::BodyOwnerKind::Static(_) => {
|
|
|
|
// Get the revealed type of this const. This is *not* the adjusted
|
|
|
|
// type of its body, which may be a subtype of this type. For
|
|
|
|
// example:
|
|
|
|
//
|
|
|
|
// fn foo(_: &()) {}
|
|
|
|
// static X: fn(&'static ()) = foo;
|
|
|
|
//
|
|
|
|
// The adjusted type of the body of X is `for<'a> fn(&'a ())` which
|
|
|
|
// is not the same as the type of X. We need the type of the return
|
|
|
|
// place to be the type of the constant because NLL typeck will
|
|
|
|
// equate them.
|
|
|
|
BodyTy::Const(typeck_results.node_type(hir_id))
|
|
|
|
}
|
|
|
|
rustc_hir::BodyOwnerKind::GlobalAsm => {
|
|
|
|
BodyTy::GlobalAsm(typeck_results.node_type(hir_id))
|
|
|
|
}
|
2023-01-22 14:37:16 +00:00
|
|
|
};
|
|
|
|
|
2025-02-04 15:43:02 +11:00
|
|
|
Self {
|
2017-07-26 15:54:44 +03:00
|
|
|
tcx,
|
2023-01-22 14:37:16 +00:00
|
|
|
thir: Thir::new(body_type),
|
2024-11-22 12:17:50 +01:00
|
|
|
// FIXME(#132279): We're in a body, we should use a typing
|
|
|
|
// mode which reveals the opaque types defined by that body.
|
|
|
|
typing_env: ty::TypingEnv::non_body_analysis(tcx, def),
|
2022-05-08 15:53:19 +02:00
|
|
|
region_scope_tree: tcx.region_scope_tree(def),
|
2020-07-17 08:47:04 +00:00
|
|
|
typeck_results,
|
2022-04-01 21:12:18 +08:00
|
|
|
rvalue_scopes: &typeck_results.rvalue_scopes,
|
2022-05-08 15:53:19 +02:00
|
|
|
body_owner: def.to_def_id(),
|
2025-02-21 18:33:05 +11:00
|
|
|
apply_adjustments: tcx
|
|
|
|
.hir_attrs(hir_id)
|
2022-08-03 04:30:13 -07:00
|
|
|
.iter()
|
2025-04-10 14:33:59 +10:00
|
|
|
.all(|attr| !attr.has_name(rustc_span::sym::custom_mir)),
|
2017-07-26 15:54:44 +03:00
|
|
|
}
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
|
|
|
|
2022-08-31 13:01:10 +00:00
|
|
|
#[instrument(level = "debug", skip(self))]
|
2024-08-17 22:18:37 +10:00
|
|
|
fn pattern_from_hir(&mut self, p: &'tcx hir::Pat<'tcx>) -> Box<Pat<'tcx>> {
|
2025-02-04 15:59:58 +11:00
|
|
|
pat_from_hir(self.tcx, self.typing_env, self.typeck_results, p)
|
2017-08-04 00:41:44 +03:00
|
|
|
}
|
2022-08-27 12:21:02 +02:00
|
|
|
|
2024-01-14 18:29:01 +00:00
|
|
|
fn closure_env_param(&self, owner_def: LocalDefId, expr_id: HirId) -> Option<Param<'tcx>> {
|
|
|
|
if self.tcx.def_kind(owner_def) != DefKind::Closure {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
let closure_ty = self.typeck_results.node_type(expr_id);
|
|
|
|
Some(match *closure_ty.kind() {
|
|
|
|
ty::Coroutine(..) => {
|
|
|
|
Param { ty: closure_ty, pat: None, ty_span: None, self_kind: None, hir_id: None }
|
2023-11-26 21:05:08 +08:00
|
|
|
}
|
2024-01-24 22:27:25 +00:00
|
|
|
ty::Closure(_, args) => {
|
2024-01-14 18:29:01 +00:00
|
|
|
let closure_env_ty = self.tcx.closure_env_ty(
|
|
|
|
closure_ty,
|
2024-01-24 22:27:25 +00:00
|
|
|
args.as_closure().kind(),
|
|
|
|
self.tcx.lifetimes.re_erased,
|
|
|
|
);
|
|
|
|
Param {
|
|
|
|
ty: closure_env_ty,
|
|
|
|
pat: None,
|
|
|
|
ty_span: None,
|
|
|
|
self_kind: None,
|
|
|
|
hir_id: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ty::CoroutineClosure(_, args) => {
|
|
|
|
let closure_env_ty = self.tcx.closure_env_ty(
|
|
|
|
closure_ty,
|
|
|
|
args.as_coroutine_closure().kind(),
|
2024-01-14 18:29:01 +00:00
|
|
|
self.tcx.lifetimes.re_erased,
|
2022-08-22 22:29:25 +02:00
|
|
|
);
|
2024-01-14 18:29:01 +00:00
|
|
|
Param {
|
|
|
|
ty: closure_env_ty,
|
2022-08-22 22:29:25 +02:00
|
|
|
pat: None,
|
|
|
|
ty_span: None,
|
|
|
|
self_kind: None,
|
|
|
|
hir_id: None,
|
2024-01-14 18:29:01 +00:00
|
|
|
}
|
2022-08-22 22:29:25 +02:00
|
|
|
}
|
2024-01-14 18:29:01 +00:00
|
|
|
_ => bug!("unexpected closure type: {closure_ty}"),
|
|
|
|
})
|
2022-08-22 22:29:25 +02:00
|
|
|
}
|
|
|
|
|
2025-02-20 18:58:46 +00:00
|
|
|
fn explicit_params(
|
|
|
|
&mut self,
|
2022-08-27 12:21:02 +02:00
|
|
|
owner_id: HirId,
|
|
|
|
fn_decl: &'tcx hir::FnDecl<'tcx>,
|
2024-06-03 09:11:58 +00:00
|
|
|
body: &'tcx hir::Body<'tcx>,
|
2025-02-20 18:58:46 +00:00
|
|
|
) -> impl Iterator<Item = Param<'tcx>> {
|
2022-08-27 12:21:02 +02:00
|
|
|
let fn_sig = self.typeck_results.liberated_fn_sigs()[owner_id];
|
|
|
|
|
|
|
|
body.params.iter().enumerate().map(move |(index, param)| {
|
|
|
|
let ty_span = fn_decl
|
|
|
|
.inputs
|
|
|
|
.get(index)
|
|
|
|
// Make sure that inferred closure args have no type span
|
|
|
|
.and_then(|ty| if param.pat.span != ty.span { Some(ty.span) } else { None });
|
|
|
|
|
|
|
|
let self_kind = if index == 0 && fn_decl.implicit_self.has_implicit_self() {
|
|
|
|
Some(fn_decl.implicit_self)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
// C-variadic fns also have a `VaList` input that's not listed in `fn_sig`
|
|
|
|
// (as it's created inside the body itself, not passed in from outside).
|
|
|
|
let ty = if fn_decl.c_variadic && index == fn_decl.inputs.len() {
|
|
|
|
let va_list_did = self.tcx.require_lang_item(LangItem::VaList, Some(param.span));
|
|
|
|
|
2023-07-11 22:35:29 +01:00
|
|
|
self.tcx
|
|
|
|
.type_of(va_list_did)
|
|
|
|
.instantiate(self.tcx, &[self.tcx.lifetimes.re_erased.into()])
|
2022-08-27 12:21:02 +02:00
|
|
|
} else {
|
|
|
|
fn_sig.inputs()[index]
|
|
|
|
};
|
|
|
|
|
|
|
|
let pat = self.pattern_from_hir(param.pat);
|
2022-08-22 22:29:25 +02:00
|
|
|
Param { pat: Some(pat), ty, ty_span, self_kind, hir_id: Some(param.hir_id) }
|
2022-08-27 12:21:02 +02:00
|
|
|
})
|
|
|
|
}
|
2018-10-02 11:00:57 -04:00
|
|
|
|
2025-02-04 15:59:58 +11:00
|
|
|
fn user_args_applied_to_ty_of_hir_id(
|
|
|
|
&self,
|
|
|
|
hir_id: HirId,
|
|
|
|
) -> Option<ty::CanonicalUserType<'tcx>> {
|
2025-03-09 19:04:37 +00:00
|
|
|
crate::thir::util::user_args_applied_to_ty_of_hir_id(self.tcx, self.typeck_results, hir_id)
|
2018-10-02 11:00:57 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-18 17:59:21 -04:00
|
|
|
mod block;
|
|
|
|
mod expr;
|