1use std::cell::RefCell;
2
3use rustc_abi::ExternAbi;
4use rustc_hir as hir;
5use rustc_hir::def::DefKind;
6use rustc_hir::intravisit::Visitor;
7use rustc_hir::lang_items::LangItem;
8use rustc_hir_analysis::check::{check_function_signature, forbid_intrinsic_abi};
9use rustc_infer::infer::RegionVariableOrigin;
10use rustc_infer::traits::WellFormedLoc;
11use rustc_middle::ty::{self, Binder, Ty, TyCtxt};
12use rustc_span::def_id::LocalDefId;
13use rustc_span::sym;
14use rustc_trait_selection::traits::{ObligationCause, ObligationCauseCode};
15use tracing::{debug, instrument};
16
17use crate::coercion::CoerceMany;
18use crate::gather_locals::GatherLocalsVisitor;
19use crate::{CoroutineTypes, Diverges, FnCtxt};
20
21#[instrument(skip(fcx, body), level = "debug")]
28pub(super) fn check_fn<'a, 'tcx>(
29 fcx: &mut FnCtxt<'a, 'tcx>,
30 fn_sig: ty::FnSig<'tcx>,
31 coroutine_types: Option<CoroutineTypes<'tcx>>,
32 decl: &'tcx hir::FnDecl<'tcx>,
33 fn_def_id: LocalDefId,
34 body: &'tcx hir::Body<'tcx>,
35 params_can_be_unsized: bool,
36) -> Option<CoroutineTypes<'tcx>> {
37 let fn_id = fcx.tcx.local_def_id_to_hir_id(fn_def_id);
38 let tcx = fcx.tcx;
39 let declared_ret_ty = fn_sig.output();
40 let ret_ty =
41 fcx.register_infer_ok_obligations(fcx.infcx.replace_opaque_types_with_inference_vars(
42 declared_ret_ty,
43 fn_def_id,
44 decl.output.span(),
45 fcx.param_env,
46 ));
47
48 fcx.coroutine_types = coroutine_types;
49 fcx.ret_coercion = Some(RefCell::new(CoerceMany::new(ret_ty)));
50
51 let span = body.value.span;
52
53 forbid_intrinsic_abi(tcx, span, fn_sig.abi);
54
55 GatherLocalsVisitor::new(fcx).visit_body(body);
56
57 let maybe_va_list = fn_sig.c_variadic.then(|| {
60 let span = body.params.last().unwrap().span;
61 let va_list_did = tcx.require_lang_item(LangItem::VaList, Some(span));
62 let region = fcx.next_region_var(RegionVariableOrigin::MiscVariable(span));
63
64 tcx.type_of(va_list_did).instantiate(tcx, &[region.into()])
65 });
66
67 let inputs_hir = tcx.hir_fn_decl_by_hir_id(fn_id).map(|decl| &decl.inputs);
69 let inputs_fn = fn_sig.inputs().iter().copied();
70 for (idx, (param_ty, param)) in inputs_fn.chain(maybe_va_list).zip(body.params).enumerate() {
71 if fcx.tcx.is_typeck_child(fn_def_id.to_def_id()) {
73 fcx.register_wf_obligation(
74 param_ty.into(),
75 param.span,
76 ObligationCauseCode::WellFormed(Some(WellFormedLoc::Param {
77 function: fn_def_id,
78 param_idx: idx,
79 })),
80 );
81 }
82
83 let ty: Option<&hir::Ty<'_>> = inputs_hir.and_then(|h| h.get(idx));
85 let ty_span = ty.map(|ty| ty.span);
86 fcx.check_pat_top(param.pat, param_ty, ty_span, None, None);
87 if param.pat.is_never_pattern() {
88 fcx.function_diverges_because_of_empty_arguments.set(Diverges::Always {
89 span: param.pat.span,
90 custom_note: Some("any code following a never pattern is unreachable"),
91 });
92 }
93
94 if !params_can_be_unsized {
96 fcx.require_type_is_sized(
97 param_ty,
98 param.ty_span,
99 ObligationCauseCode::SizedArgumentType(
102 if ty_span == Some(param.span) && tcx.is_closure_like(fn_def_id.into()) {
103 None
104 } else {
105 ty.map(|ty| ty.hir_id)
106 },
107 ),
108 );
109 }
110
111 fcx.write_ty(param.hir_id, param_ty);
112 }
113
114 fcx.typeck_results.borrow_mut().liberated_fn_sigs_mut().insert(fn_id, fn_sig);
115
116 if fcx.tcx.is_typeck_child(fn_def_id.to_def_id()) {
118 let return_or_body_span = match decl.output {
119 hir::FnRetTy::DefaultReturn(_) => body.value.span,
120 hir::FnRetTy::Return(ty) => ty.span,
121 };
122
123 fcx.require_type_is_sized(
124 declared_ret_ty,
125 return_or_body_span,
126 ObligationCauseCode::SizedReturnType,
127 );
128 }
129
130 fcx.is_whole_body.set(true);
131 fcx.check_return_or_body_tail(body.value, false);
132
133 let coercion = fcx.ret_coercion.take().unwrap().into_inner();
138 let mut actual_return_ty = coercion.complete(fcx);
139 debug!("actual_return_ty = {:?}", actual_return_ty);
140 if let ty::Dynamic(..) = declared_ret_ty.kind() {
141 actual_return_ty = fcx.next_ty_var(span);
145 debug!("actual_return_ty replaced with {:?}", actual_return_ty);
146 }
147
148 fcx.demand_suptype(span, ret_ty, actual_return_ty);
153
154 if tcx.is_lang_item(fn_def_id.to_def_id(), LangItem::PanicImpl) {
156 check_panic_info_fn(tcx, fn_def_id, fn_sig);
157 }
158
159 if tcx.is_lang_item(fn_def_id.to_def_id(), LangItem::Start) {
160 check_lang_start_fn(tcx, fn_sig, fn_def_id);
161 }
162
163 fcx.coroutine_types
164}
165
166fn check_panic_info_fn(tcx: TyCtxt<'_>, fn_id: LocalDefId, fn_sig: ty::FnSig<'_>) {
167 let span = tcx.def_span(fn_id);
168
169 let DefKind::Fn = tcx.def_kind(fn_id) else {
170 tcx.dcx().span_err(span, "should be a function");
171 return;
172 };
173
174 let generic_counts = tcx.generics_of(fn_id).own_counts();
175 if generic_counts.types != 0 {
176 tcx.dcx().span_err(span, "should have no type parameters");
177 }
178 if generic_counts.consts != 0 {
179 tcx.dcx().span_err(span, "should have no const parameters");
180 }
181
182 let panic_info_did = tcx.require_lang_item(hir::LangItem::PanicInfo, Some(span));
183
184 let panic_info_ty = tcx.type_of(panic_info_did).instantiate(
186 tcx,
187 &[ty::GenericArg::from(ty::Region::new_bound(
188 tcx,
189 ty::INNERMOST,
190 ty::BoundRegion { var: ty::BoundVar::from_u32(1), kind: ty::BoundRegionKind::Anon },
191 ))],
192 );
193 let panic_info_ref_ty = Ty::new_imm_ref(
194 tcx,
195 ty::Region::new_bound(
196 tcx,
197 ty::INNERMOST,
198 ty::BoundRegion { var: ty::BoundVar::ZERO, kind: ty::BoundRegionKind::Anon },
199 ),
200 panic_info_ty,
201 );
202
203 let bounds = tcx.mk_bound_variable_kinds(&[
204 ty::BoundVariableKind::Region(ty::BoundRegionKind::Anon),
205 ty::BoundVariableKind::Region(ty::BoundRegionKind::Anon),
206 ]);
207 let expected_sig = ty::Binder::bind_with_vars(
208 tcx.mk_fn_sig([panic_info_ref_ty], tcx.types.never, false, fn_sig.safety, ExternAbi::Rust),
209 bounds,
210 );
211
212 let _ = check_function_signature(
213 tcx,
214 ObligationCause::new(span, fn_id, ObligationCauseCode::LangFunctionType(sym::panic_impl)),
215 fn_id.into(),
216 expected_sig,
217 );
218}
219
220fn check_lang_start_fn<'tcx>(tcx: TyCtxt<'tcx>, fn_sig: ty::FnSig<'tcx>, def_id: LocalDefId) {
221 let generics = tcx.generics_of(def_id);
227 let fn_generic = generics.param_at(0, tcx);
228 let generic_ty = Ty::new_param(tcx, fn_generic.index, fn_generic.name);
229 let main_fn_ty = Ty::new_fn_ptr(
230 tcx,
231 Binder::dummy(tcx.mk_fn_sig([], generic_ty, false, hir::Safety::Safe, ExternAbi::Rust)),
232 );
233
234 let expected_sig = ty::Binder::dummy(tcx.mk_fn_sig(
235 [
236 main_fn_ty,
237 tcx.types.isize,
238 Ty::new_imm_ptr(tcx, Ty::new_imm_ptr(tcx, tcx.types.u8)),
239 tcx.types.u8,
240 ],
241 tcx.types.isize,
242 false,
243 fn_sig.safety,
244 ExternAbi::Rust,
245 ));
246
247 let _ = check_function_signature(
248 tcx,
249 ObligationCause::new(
250 tcx.def_span(def_id),
251 def_id,
252 ObligationCauseCode::LangFunctionType(sym::start),
253 ),
254 def_id.into(),
255 expected_sig,
256 );
257}