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