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 hir = tcx.hir();
26 let body = hir.body_owned_by(owner_def);
27 let mut cx = ThirBuildCx::new(tcx, owner_def);
28 if let Some(reported) = cx.typeck_results.tainted_by_errors {
29 return Err(reported);
30 }
31 let expr = cx.mirror_expr(body.value);
32
33 let owner_id = tcx.local_def_id_to_hir_id(owner_def);
34 if let Some(fn_decl) = hir.fn_decl_by_hir_id(owner_id) {
35 let closure_env_param = cx.closure_env_param(owner_def, owner_id);
36 let explicit_params = cx.explicit_params(owner_id, fn_decl, &body);
37 cx.thir.params = closure_env_param.into_iter().chain(explicit_params).collect();
38
39 if tcx.is_coroutine(owner_def.to_def_id()) && body.params.is_empty() {
42 cx.thir.params.push(Param {
43 ty: tcx.types.unit,
44 pat: None,
45 ty_span: None,
46 self_kind: None,
47 hir_id: None,
48 });
49 }
50 }
51
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 = tcx.hir();
78 let hir_id = tcx.local_def_id_to_hir_id(def);
79
80 let body_type = if hir.body_owner_kind(def).is_fn_or_closure() {
81 BodyTy::Fn(typeck_results.liberated_fn_sigs()[hir_id])
84 } else {
85 BodyTy::Const(typeck_results.node_type(hir_id))
97 };
98
99 Self {
100 tcx,
101 thir: Thir::new(body_type),
102 typing_env: ty::TypingEnv::non_body_analysis(tcx, def),
105 region_scope_tree: tcx.region_scope_tree(def),
106 typeck_results,
107 rvalue_scopes: &typeck_results.rvalue_scopes,
108 body_owner: def.to_def_id(),
109 apply_adjustments: hir
110 .attrs(hir_id)
111 .iter()
112 .all(|attr| attr.name_or_empty() != rustc_span::sym::custom_mir),
113 }
114 }
115
116 #[instrument(level = "debug", skip(self))]
117 fn pattern_from_hir(&mut self, p: &'tcx hir::Pat<'tcx>) -> Box<Pat<'tcx>> {
118 pat_from_hir(self.tcx, self.typing_env, self.typeck_results, p)
119 }
120
121 fn closure_env_param(&self, owner_def: LocalDefId, expr_id: HirId) -> Option<Param<'tcx>> {
122 if self.tcx.def_kind(owner_def) != DefKind::Closure {
123 return None;
124 }
125
126 let closure_ty = self.typeck_results.node_type(expr_id);
127 Some(match *closure_ty.kind() {
128 ty::Coroutine(..) => {
129 Param { ty: closure_ty, pat: None, ty_span: None, self_kind: None, hir_id: None }
130 }
131 ty::Closure(_, args) => {
132 let closure_env_ty = self.tcx.closure_env_ty(
133 closure_ty,
134 args.as_closure().kind(),
135 self.tcx.lifetimes.re_erased,
136 );
137 Param {
138 ty: closure_env_ty,
139 pat: None,
140 ty_span: None,
141 self_kind: None,
142 hir_id: None,
143 }
144 }
145 ty::CoroutineClosure(_, args) => {
146 let closure_env_ty = self.tcx.closure_env_ty(
147 closure_ty,
148 args.as_coroutine_closure().kind(),
149 self.tcx.lifetimes.re_erased,
150 );
151 Param {
152 ty: closure_env_ty,
153 pat: None,
154 ty_span: None,
155 self_kind: None,
156 hir_id: None,
157 }
158 }
159 _ => bug!("unexpected closure type: {closure_ty}"),
160 })
161 }
162
163 fn explicit_params<'a>(
164 &'a mut self,
165 owner_id: HirId,
166 fn_decl: &'tcx hir::FnDecl<'tcx>,
167 body: &'tcx hir::Body<'tcx>,
168 ) -> impl Iterator<Item = Param<'tcx>> + 'a {
169 let fn_sig = self.typeck_results.liberated_fn_sigs()[owner_id];
170
171 body.params.iter().enumerate().map(move |(index, param)| {
172 let ty_span = fn_decl
173 .inputs
174 .get(index)
175 .and_then(|ty| if param.pat.span != ty.span { Some(ty.span) } else { None });
177
178 let self_kind = if index == 0 && fn_decl.implicit_self.has_implicit_self() {
179 Some(fn_decl.implicit_self)
180 } else {
181 None
182 };
183
184 let ty = if fn_decl.c_variadic && index == fn_decl.inputs.len() {
187 let va_list_did = self.tcx.require_lang_item(LangItem::VaList, Some(param.span));
188
189 self.tcx
190 .type_of(va_list_did)
191 .instantiate(self.tcx, &[self.tcx.lifetimes.re_erased.into()])
192 } else {
193 fn_sig.inputs()[index]
194 };
195
196 let pat = self.pattern_from_hir(param.pat);
197 Param { pat: Some(pat), ty, ty_span, self_kind, hir_id: Some(param.hir_id) }
198 })
199 }
200
201 fn user_args_applied_to_ty_of_hir_id(
202 &self,
203 hir_id: HirId,
204 ) -> Option<ty::CanonicalUserType<'tcx>> {
205 crate::thir::util::user_args_applied_to_ty_of_hir_id(self.typeck_results, hir_id)
206 }
207}
208
209mod block;
210mod expr;