rustc_hir_typeck/
check.rs

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/// Helper used for fns and closures. Does the grungy work of checking a function
22/// body and returns the function context used for that purpose, since in the case of a fn item
23/// there is still a bit more to do.
24///
25/// * ...
26/// * inherited: other fields inherited from the enclosing fn (if any)
27#[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
39    let tcx = fcx.tcx;
40    let hir = tcx.hir();
41
42    let declared_ret_ty = fn_sig.output();
43
44    let ret_ty =
45        fcx.register_infer_ok_obligations(fcx.infcx.replace_opaque_types_with_inference_vars(
46            declared_ret_ty,
47            fn_def_id,
48            decl.output.span(),
49            fcx.param_env,
50        ));
51
52    fcx.coroutine_types = coroutine_types;
53    fcx.ret_coercion = Some(RefCell::new(CoerceMany::new(ret_ty)));
54
55    let span = body.value.span;
56
57    forbid_intrinsic_abi(tcx, span, fn_sig.abi);
58
59    GatherLocalsVisitor::new(fcx).visit_body(body);
60
61    // C-variadic fns also have a `VaList` input that's not listed in `fn_sig`
62    // (as it's created inside the body itself, not passed in from outside).
63    let maybe_va_list = fn_sig.c_variadic.then(|| {
64        let span = body.params.last().unwrap().span;
65        let va_list_did = tcx.require_lang_item(LangItem::VaList, Some(span));
66        let region = fcx.next_region_var(RegionVariableOrigin::MiscVariable(span));
67
68        tcx.type_of(va_list_did).instantiate(tcx, &[region.into()])
69    });
70
71    // Add formal parameters.
72    let inputs_hir = hir.fn_decl_by_hir_id(fn_id).map(|decl| &decl.inputs);
73    let inputs_fn = fn_sig.inputs().iter().copied();
74    for (idx, (param_ty, param)) in inputs_fn.chain(maybe_va_list).zip(body.params).enumerate() {
75        // We checked the root's signature during wfcheck, but not the child.
76        if fcx.tcx.is_typeck_child(fn_def_id.to_def_id()) {
77            fcx.register_wf_obligation(
78                param_ty.into(),
79                param.span,
80                ObligationCauseCode::WellFormed(Some(WellFormedLoc::Param {
81                    function: fn_def_id,
82                    param_idx: idx,
83                })),
84            );
85        }
86
87        // Check the pattern.
88        let ty: Option<&hir::Ty<'_>> = inputs_hir.and_then(|h| h.get(idx));
89        let ty_span = ty.map(|ty| ty.span);
90        fcx.check_pat_top(param.pat, param_ty, ty_span, None, None);
91        if param.pat.is_never_pattern() {
92            fcx.function_diverges_because_of_empty_arguments.set(Diverges::Always {
93                span: param.pat.span,
94                custom_note: Some("any code following a never pattern is unreachable"),
95            });
96        }
97
98        // Check that argument is Sized.
99        if !params_can_be_unsized {
100            fcx.require_type_is_sized(
101                param_ty,
102                param.ty_span,
103                // ty.span == binding_span iff this is a closure parameter with no type ascription,
104                // or if it's an implicit `self` parameter
105                ObligationCauseCode::SizedArgumentType(
106                    if ty_span == Some(param.span) && tcx.is_closure_like(fn_def_id.into()) {
107                        None
108                    } else {
109                        ty.map(|ty| ty.hir_id)
110                    },
111                ),
112            );
113        }
114
115        fcx.write_ty(param.hir_id, param_ty);
116    }
117
118    fcx.typeck_results.borrow_mut().liberated_fn_sigs_mut().insert(fn_id, fn_sig);
119
120    let return_or_body_span = match decl.output {
121        hir::FnRetTy::DefaultReturn(_) => body.value.span,
122        hir::FnRetTy::Return(ty) => ty.span,
123    };
124
125    fcx.require_type_is_sized(
126        declared_ret_ty,
127        return_or_body_span,
128        ObligationCauseCode::SizedReturnType,
129    );
130    // We checked the root's signature during wfcheck, but not the child.
131    if fcx.tcx.is_typeck_child(fn_def_id.to_def_id()) {
132        fcx.require_type_is_sized(
133            declared_ret_ty,
134            return_or_body_span,
135            ObligationCauseCode::WellFormed(None),
136        );
137    }
138
139    fcx.is_whole_body.set(true);
140    fcx.check_return_or_body_tail(body.value, false);
141
142    // Finalize the return check by taking the LUB of the return types
143    // we saw and assigning it to the expected return type. This isn't
144    // really expected to fail, since the coercions would have failed
145    // earlier when trying to find a LUB.
146    let coercion = fcx.ret_coercion.take().unwrap().into_inner();
147    let mut actual_return_ty = coercion.complete(fcx);
148    debug!("actual_return_ty = {:?}", actual_return_ty);
149    if let ty::Dynamic(..) = declared_ret_ty.kind() {
150        // We have special-cased the case where the function is declared
151        // `-> dyn Foo` and we don't actually relate it to the
152        // `fcx.ret_coercion`, so just instantiate a type variable.
153        actual_return_ty = fcx.next_ty_var(span);
154        debug!("actual_return_ty replaced with {:?}", actual_return_ty);
155    }
156
157    // HACK(oli-obk, compiler-errors): We should be comparing this against
158    // `declared_ret_ty`, but then anything uninferred would be inferred to
159    // the opaque type itself. That again would cause writeback to assume
160    // we have a recursive call site and do the sadly stabilized fallback to `()`.
161    fcx.demand_suptype(span, ret_ty, actual_return_ty);
162
163    // Check that a function marked as `#[panic_handler]` has signature `fn(&PanicInfo) -> !`
164    if tcx.is_lang_item(fn_def_id.to_def_id(), LangItem::PanicImpl) {
165        check_panic_info_fn(tcx, fn_def_id, fn_sig);
166    }
167
168    if tcx.is_lang_item(fn_def_id.to_def_id(), LangItem::Start) {
169        check_lang_start_fn(tcx, fn_sig, fn_def_id);
170    }
171
172    fcx.coroutine_types
173}
174
175fn check_panic_info_fn(tcx: TyCtxt<'_>, fn_id: LocalDefId, fn_sig: ty::FnSig<'_>) {
176    let span = tcx.def_span(fn_id);
177
178    let DefKind::Fn = tcx.def_kind(fn_id) else {
179        tcx.dcx().span_err(span, "should be a function");
180        return;
181    };
182
183    let generic_counts = tcx.generics_of(fn_id).own_counts();
184    if generic_counts.types != 0 {
185        tcx.dcx().span_err(span, "should have no type parameters");
186    }
187    if generic_counts.consts != 0 {
188        tcx.dcx().span_err(span, "should have no const parameters");
189    }
190
191    let panic_info_did = tcx.require_lang_item(hir::LangItem::PanicInfo, Some(span));
192
193    // build type `for<'a, 'b> fn(&'a PanicInfo<'b>) -> !`
194    let panic_info_ty = tcx.type_of(panic_info_did).instantiate(
195        tcx,
196        &[ty::GenericArg::from(ty::Region::new_bound(
197            tcx,
198            ty::INNERMOST,
199            ty::BoundRegion { var: ty::BoundVar::from_u32(1), kind: ty::BoundRegionKind::Anon },
200        ))],
201    );
202    let panic_info_ref_ty = Ty::new_imm_ref(
203        tcx,
204        ty::Region::new_bound(
205            tcx,
206            ty::INNERMOST,
207            ty::BoundRegion { var: ty::BoundVar::ZERO, kind: ty::BoundRegionKind::Anon },
208        ),
209        panic_info_ty,
210    );
211
212    let bounds = tcx.mk_bound_variable_kinds(&[
213        ty::BoundVariableKind::Region(ty::BoundRegionKind::Anon),
214        ty::BoundVariableKind::Region(ty::BoundRegionKind::Anon),
215    ]);
216    let expected_sig = ty::Binder::bind_with_vars(
217        tcx.mk_fn_sig([panic_info_ref_ty], tcx.types.never, false, fn_sig.safety, ExternAbi::Rust),
218        bounds,
219    );
220
221    let _ = check_function_signature(
222        tcx,
223        ObligationCause::new(span, fn_id, ObligationCauseCode::LangFunctionType(sym::panic_impl)),
224        fn_id.into(),
225        expected_sig,
226    );
227}
228
229fn check_lang_start_fn<'tcx>(tcx: TyCtxt<'tcx>, fn_sig: ty::FnSig<'tcx>, def_id: LocalDefId) {
230    // build type `fn(main: fn() -> T, argc: isize, argv: *const *const u8, sigpipe: u8)`
231
232    // make a Ty for the generic on the fn for diagnostics
233    // FIXME: make the lang item generic checks check for the right generic *kind*
234    // for example `start`'s generic should be a type parameter
235    let generics = tcx.generics_of(def_id);
236    let fn_generic = generics.param_at(0, tcx);
237    let generic_ty = Ty::new_param(tcx, fn_generic.index, fn_generic.name);
238    let main_fn_ty = Ty::new_fn_ptr(
239        tcx,
240        Binder::dummy(tcx.mk_fn_sig([], generic_ty, false, hir::Safety::Safe, ExternAbi::Rust)),
241    );
242
243    let expected_sig = ty::Binder::dummy(tcx.mk_fn_sig(
244        [
245            main_fn_ty,
246            tcx.types.isize,
247            Ty::new_imm_ptr(tcx, Ty::new_imm_ptr(tcx, tcx.types.u8)),
248            tcx.types.u8,
249        ],
250        tcx.types.isize,
251        false,
252        fn_sig.safety,
253        ExternAbi::Rust,
254    ));
255
256    let _ = check_function_signature(
257        tcx,
258        ObligationCause::new(
259            tcx.def_span(def_id),
260            def_id,
261            ObligationCauseCode::LangFunctionType(sym::start),
262        ),
263        def_id.into(),
264        expected_sig,
265    );
266}