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