rustc_mir_build/thir/cx/
mod.rs1use 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(
17 tcx: TyCtxt<'_>,
18 owner_def: LocalDefId,
19) -> Result<(&Steal<Thir<'_>>, 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::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
52struct 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::non_body_analysis(tcx, def),
105 typeck_results,
106 body_owner: def.to_def_id(),
107 apply_adjustments: !{
'done:
{
for i in tcx.hir_attrs(hir_id) {
#[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
}
}find_attr!(tcx.hir_attrs(hir_id), CustomMir(..) => ()).is_some(),
108 }
109 }
110
111 fn pattern_from_hir(&mut self, pat: &'tcx hir::Pat<'tcx>) -> Box<Pat<'tcx>> {
112 self.pattern_from_hir_with_annotation(pat, None)
113 }
114
115 fn pattern_from_hir_with_annotation(
116 &mut self,
117 pat: &'tcx hir::Pat<'tcx>,
118 let_stmt_type: Option<&hir::Ty<'tcx>>,
119 ) -> Box<Pat<'tcx>> {
120 crate::thir::pattern::pat_from_hir(
121 self.tcx,
122 self.typing_env,
123 self.typeck_results,
124 pat,
125 let_stmt_type,
126 )
127 }
128
129 fn closure_env_param(&self, owner_def: LocalDefId, expr_id: HirId) -> Option<Param<'tcx>> {
130 if self.tcx.def_kind(owner_def) != DefKind::Closure {
131 return None;
132 }
133
134 let closure_ty = self.typeck_results.node_type(expr_id);
135 Some(match *closure_ty.kind() {
136 ty::Coroutine(..) => {
137 Param { ty: closure_ty, pat: None, ty_span: None, self_kind: None, hir_id: None }
138 }
139 ty::Closure(_, args) => {
140 let closure_env_ty = self.tcx.closure_env_ty(
141 closure_ty,
142 args.as_closure().kind(),
143 self.tcx.lifetimes.re_erased,
144 );
145 Param {
146 ty: closure_env_ty,
147 pat: None,
148 ty_span: None,
149 self_kind: None,
150 hir_id: None,
151 }
152 }
153 ty::CoroutineClosure(_, args) => {
154 let closure_env_ty = self.tcx.closure_env_ty(
155 closure_ty,
156 args.as_coroutine_closure().kind(),
157 self.tcx.lifetimes.re_erased,
158 );
159 Param {
160 ty: closure_env_ty,
161 pat: None,
162 ty_span: None,
163 self_kind: None,
164 hir_id: None,
165 }
166 }
167 _ => ::rustc_middle::util::bug::bug_fmt(format_args!("unexpected closure type: {0}",
closure_ty))bug!("unexpected closure type: {closure_ty}"),
168 })
169 }
170
171 fn explicit_params(
172 &mut self,
173 owner_id: HirId,
174 fn_decl: &'tcx hir::FnDecl<'tcx>,
175 body: &'tcx hir::Body<'tcx>,
176 ) -> impl Iterator<Item = Param<'tcx>> {
177 let fn_sig = self.typeck_results.liberated_fn_sigs()[owner_id];
178
179 body.params.iter().enumerate().map(move |(index, param)| {
180 let ty_span = fn_decl
181 .inputs
182 .get(index)
183 .and_then(|ty| if param.pat.span != ty.span { Some(ty.span) } else { None });
185
186 let self_kind = if index == 0 && fn_decl.implicit_self.has_implicit_self() {
187 Some(fn_decl.implicit_self)
188 } else {
189 None
190 };
191
192 let ty = if fn_decl.c_variadic && index == fn_decl.inputs.len() {
195 let va_list_did = self.tcx.require_lang_item(LangItem::VaList, param.span);
196
197 self.tcx
198 .type_of(va_list_did)
199 .instantiate(self.tcx, &[self.tcx.lifetimes.re_erased.into()])
200 } else {
201 fn_sig.inputs()[index]
202 };
203
204 let pat = 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;