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