rustc_mir_build/thir/cx/
mod.rs
1use rustc_data_structures::steal::Steal;
6use rustc_errors::ErrorGuaranteed;
7use rustc_hir as hir;
8use rustc_hir::HirId;
9use rustc_hir::def::DefKind;
10use rustc_hir::def_id::{DefId, LocalDefId};
11use rustc_hir::lang_items::LangItem;
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 let expr = cx.mirror_expr(body.value);
31
32 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 Ok((tcx.alloc_steal_thir(cx.thir), expr))
52}
53
54struct ThirBuildCx<'tcx> {
56 tcx: TyCtxt<'tcx>,
57 thir: Thir<'tcx>,
59
60 typing_env: ty::TypingEnv<'tcx>,
61
62 region_scope_tree: &'tcx region::ScopeTree,
63 typeck_results: &'tcx ty::TypeckResults<'tcx>,
64 rvalue_scopes: &'tcx RvalueScopes,
65
66 apply_adjustments: bool,
68
69 body_owner: DefId,
71}
72
73impl<'tcx> ThirBuildCx<'tcx> {
74 fn new(tcx: TyCtxt<'tcx>, def: LocalDefId) -> Self {
75 let typeck_results = tcx.typeck(def);
76 let hir_id = tcx.local_def_id_to_hir_id(def);
77
78 let body_type = match tcx.hir_body_owner_kind(def) {
79 rustc_hir::BodyOwnerKind::Fn | rustc_hir::BodyOwnerKind::Closure => {
80 BodyTy::Fn(typeck_results.liberated_fn_sigs()[hir_id])
83 }
84 rustc_hir::BodyOwnerKind::Const { .. } | rustc_hir::BodyOwnerKind::Static(_) => {
85 BodyTy::Const(typeck_results.node_type(hir_id))
97 }
98 rustc_hir::BodyOwnerKind::GlobalAsm => {
99 BodyTy::GlobalAsm(typeck_results.node_type(hir_id))
100 }
101 };
102
103 Self {
104 tcx,
105 thir: Thir::new(body_type),
106 typing_env: ty::TypingEnv::non_body_analysis(tcx, def),
109 region_scope_tree: tcx.region_scope_tree(def),
110 typeck_results,
111 rvalue_scopes: &typeck_results.rvalue_scopes,
112 body_owner: def.to_def_id(),
113 apply_adjustments: tcx
114 .hir_attrs(hir_id)
115 .iter()
116 .all(|attr| attr.name_or_empty() != rustc_span::sym::custom_mir),
117 }
118 }
119
120 #[instrument(level = "debug", skip(self))]
121 fn pattern_from_hir(&mut self, p: &'tcx hir::Pat<'tcx>) -> Box<Pat<'tcx>> {
122 pat_from_hir(self.tcx, self.typing_env, self.typeck_results, p)
123 }
124
125 fn closure_env_param(&self, owner_def: LocalDefId, expr_id: HirId) -> Option<Param<'tcx>> {
126 if self.tcx.def_kind(owner_def) != DefKind::Closure {
127 return None;
128 }
129
130 let closure_ty = self.typeck_results.node_type(expr_id);
131 Some(match *closure_ty.kind() {
132 ty::Coroutine(..) => {
133 Param { ty: closure_ty, pat: None, ty_span: None, self_kind: None, hir_id: None }
134 }
135 ty::Closure(_, args) => {
136 let closure_env_ty = self.tcx.closure_env_ty(
137 closure_ty,
138 args.as_closure().kind(),
139 self.tcx.lifetimes.re_erased,
140 );
141 Param {
142 ty: closure_env_ty,
143 pat: None,
144 ty_span: None,
145 self_kind: None,
146 hir_id: None,
147 }
148 }
149 ty::CoroutineClosure(_, args) => {
150 let closure_env_ty = self.tcx.closure_env_ty(
151 closure_ty,
152 args.as_coroutine_closure().kind(),
153 self.tcx.lifetimes.re_erased,
154 );
155 Param {
156 ty: closure_env_ty,
157 pat: None,
158 ty_span: None,
159 self_kind: None,
160 hir_id: None,
161 }
162 }
163 _ => bug!("unexpected closure type: {closure_ty}"),
164 })
165 }
166
167 fn explicit_params(
168 &mut self,
169 owner_id: HirId,
170 fn_decl: &'tcx hir::FnDecl<'tcx>,
171 body: &'tcx hir::Body<'tcx>,
172 ) -> impl Iterator<Item = Param<'tcx>> {
173 let fn_sig = self.typeck_results.liberated_fn_sigs()[owner_id];
174
175 body.params.iter().enumerate().map(move |(index, param)| {
176 let ty_span = fn_decl
177 .inputs
178 .get(index)
179 .and_then(|ty| if param.pat.span != ty.span { Some(ty.span) } else { None });
181
182 let self_kind = if index == 0 && fn_decl.implicit_self.has_implicit_self() {
183 Some(fn_decl.implicit_self)
184 } else {
185 None
186 };
187
188 let ty = if fn_decl.c_variadic && index == fn_decl.inputs.len() {
191 let va_list_did = self.tcx.require_lang_item(LangItem::VaList, Some(param.span));
192
193 self.tcx
194 .type_of(va_list_did)
195 .instantiate(self.tcx, &[self.tcx.lifetimes.re_erased.into()])
196 } else {
197 fn_sig.inputs()[index]
198 };
199
200 let pat = self.pattern_from_hir(param.pat);
201 Param { pat: Some(pat), ty, ty_span, self_kind, hir_id: Some(param.hir_id) }
202 })
203 }
204
205 fn user_args_applied_to_ty_of_hir_id(
206 &self,
207 hir_id: HirId,
208 ) -> Option<ty::CanonicalUserType<'tcx>> {
209 crate::thir::util::user_args_applied_to_ty_of_hir_id(self.tcx, self.typeck_results, hir_id)
210 }
211}
212
213mod block;
214mod expr;