rustc_mir_build/thir/cx/
mod.rs1use rustc_data_structures::steal::Steal;
6use rustc_errors::ErrorGuaranteed;
7use rustc_hir::attrs::AttributeKind;
8use rustc_hir::def::DefKind;
9use rustc_hir::def_id::{DefId, LocalDefId};
10use rustc_hir::lang_items::LangItem;
11use rustc_hir::{self as hir, HirId, find_attr};
12use rustc_middle::bug;
13use rustc_middle::middle::region;
14use rustc_middle::thir::*;
15use rustc_middle::ty::{self, RvalueScopes, TyCtxt};
16use tracing::instrument;
17
18use crate::thir::pattern::pat_from_hir;
19
20pub(crate) fn thir_body(
22 tcx: TyCtxt<'_>,
23 owner_def: LocalDefId,
24) -> Result<(&Steal<Thir<'_>>, ExprId), ErrorGuaranteed> {
25 let body = tcx.hir_body_owned_by(owner_def);
26 let mut cx = ThirBuildCx::new(tcx, owner_def);
27 if let Some(reported) = cx.typeck_results.tainted_by_errors {
28 return Err(reported);
29 }
30
31 let owner_id = tcx.local_def_id_to_hir_id(owner_def);
33 if let Some(fn_decl) = tcx.hir_fn_decl_by_hir_id(owner_id) {
34 let closure_env_param = cx.closure_env_param(owner_def, owner_id);
35 let explicit_params = cx.explicit_params(owner_id, fn_decl, &body);
36 cx.thir.params = closure_env_param.into_iter().chain(explicit_params).collect();
37
38 if tcx.is_coroutine(owner_def.to_def_id()) && body.params.is_empty() {
41 cx.thir.params.push(Param {
42 ty: tcx.types.unit,
43 pat: None,
44 ty_span: None,
45 self_kind: None,
46 hir_id: None,
47 });
48 }
49 }
50
51 let expr = cx.mirror_expr(body.value);
52 Ok((tcx.alloc_steal_thir(cx.thir), expr))
53}
54
55struct ThirBuildCx<'tcx> {
57 tcx: TyCtxt<'tcx>,
58 thir: Thir<'tcx>,
60
61 typing_env: ty::TypingEnv<'tcx>,
62
63 region_scope_tree: &'tcx region::ScopeTree,
64 typeck_results: &'tcx ty::TypeckResults<'tcx>,
65 rvalue_scopes: &'tcx RvalueScopes,
66
67 apply_adjustments: bool,
69
70 body_owner: DefId,
72}
73
74impl<'tcx> ThirBuildCx<'tcx> {
75 fn new(tcx: TyCtxt<'tcx>, def: LocalDefId) -> Self {
76 let typeck_results = tcx.typeck(def);
77 let hir_id = tcx.local_def_id_to_hir_id(def);
78
79 let body_type = match tcx.hir_body_owner_kind(def) {
80 rustc_hir::BodyOwnerKind::Fn | rustc_hir::BodyOwnerKind::Closure => {
81 BodyTy::Fn(typeck_results.liberated_fn_sigs()[hir_id])
84 }
85 rustc_hir::BodyOwnerKind::Const { .. } | rustc_hir::BodyOwnerKind::Static(_) => {
86 BodyTy::Const(typeck_results.node_type(hir_id))
98 }
99 rustc_hir::BodyOwnerKind::GlobalAsm => {
100 BodyTy::GlobalAsm(typeck_results.node_type(hir_id))
101 }
102 };
103
104 Self {
105 tcx,
106 thir: Thir::new(body_type),
107 typing_env: ty::TypingEnv::non_body_analysis(tcx, def),
110 region_scope_tree: tcx.region_scope_tree(def),
111 typeck_results,
112 rvalue_scopes: &typeck_results.rvalue_scopes,
113 body_owner: def.to_def_id(),
114 apply_adjustments:
115 !find_attr!(tcx.hir_attrs(hir_id), AttributeKind::CustomMir(..) => ()).is_some(),
116 }
117 }
118
119 #[instrument(level = "debug", skip(self))]
120 fn pattern_from_hir(&mut self, p: &'tcx hir::Pat<'tcx>) -> Box<Pat<'tcx>> {
121 pat_from_hir(self.tcx, self.typing_env, self.typeck_results, p)
122 }
123
124 fn closure_env_param(&self, owner_def: LocalDefId, expr_id: HirId) -> Option<Param<'tcx>> {
125 if self.tcx.def_kind(owner_def) != DefKind::Closure {
126 return None;
127 }
128
129 let closure_ty = self.typeck_results.node_type(expr_id);
130 Some(match *closure_ty.kind() {
131 ty::Coroutine(..) => {
132 Param { ty: closure_ty, pat: None, ty_span: None, self_kind: None, hir_id: None }
133 }
134 ty::Closure(_, args) => {
135 let closure_env_ty = self.tcx.closure_env_ty(
136 closure_ty,
137 args.as_closure().kind(),
138 self.tcx.lifetimes.re_erased,
139 );
140 Param {
141 ty: closure_env_ty,
142 pat: None,
143 ty_span: None,
144 self_kind: None,
145 hir_id: None,
146 }
147 }
148 ty::CoroutineClosure(_, args) => {
149 let closure_env_ty = self.tcx.closure_env_ty(
150 closure_ty,
151 args.as_coroutine_closure().kind(),
152 self.tcx.lifetimes.re_erased,
153 );
154 Param {
155 ty: closure_env_ty,
156 pat: None,
157 ty_span: None,
158 self_kind: None,
159 hir_id: None,
160 }
161 }
162 _ => bug!("unexpected closure type: {closure_ty}"),
163 })
164 }
165
166 fn explicit_params(
167 &mut self,
168 owner_id: HirId,
169 fn_decl: &'tcx hir::FnDecl<'tcx>,
170 body: &'tcx hir::Body<'tcx>,
171 ) -> impl Iterator<Item = Param<'tcx>> {
172 let fn_sig = self.typeck_results.liberated_fn_sigs()[owner_id];
173
174 body.params.iter().enumerate().map(move |(index, param)| {
175 let ty_span = fn_decl
176 .inputs
177 .get(index)
178 .and_then(|ty| if param.pat.span != ty.span { Some(ty.span) } else { None });
180
181 let self_kind = if index == 0 && fn_decl.implicit_self.has_implicit_self() {
182 Some(fn_decl.implicit_self)
183 } else {
184 None
185 };
186
187 let ty = if fn_decl.c_variadic && index == fn_decl.inputs.len() {
190 let va_list_did = self.tcx.require_lang_item(LangItem::VaList, param.span);
191
192 self.tcx
193 .type_of(va_list_did)
194 .instantiate(self.tcx, &[self.tcx.lifetimes.re_erased.into()])
195 } else {
196 fn_sig.inputs()[index]
197 };
198
199 let pat = self.pattern_from_hir(param.pat);
200 Param { pat: Some(pat), ty, ty_span, self_kind, hir_id: Some(param.hir_id) }
201 })
202 }
203
204 fn user_args_applied_to_ty_of_hir_id(
205 &self,
206 hir_id: HirId,
207 ) -> Option<ty::CanonicalUserType<'tcx>> {
208 crate::thir::util::user_args_applied_to_ty_of_hir_id(self.tcx, self.typeck_results, hir_id)
209 }
210}
211
212mod block;
213mod expr;