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    // We checked the root's ret ty during wfcheck, but not the child.
121    if fcx.tcx.is_typeck_child(fn_def_id.to_def_id()) {
122        let return_or_body_span = match decl.output {
123            hir::FnRetTy::DefaultReturn(_) => body.value.span,
124            hir::FnRetTy::Return(ty) => ty.span,
125        };
126
127        fcx.require_type_is_sized(
128            declared_ret_ty,
129            return_or_body_span,
130            ObligationCauseCode::SizedReturnType,
131        );
132    }
133
134    fcx.is_whole_body.set(true);
135    fcx.check_return_or_body_tail(body.value, false);
136
137    // Finalize the return check by taking the LUB of the return types
138    // we saw and assigning it to the expected return type. This isn't
139    // really expected to fail, since the coercions would have failed
140    // earlier when trying to find a LUB.
141    let coercion = fcx.ret_coercion.take().unwrap().into_inner();
142    let mut actual_return_ty = coercion.complete(fcx);
143    debug!("actual_return_ty = {:?}", actual_return_ty);
144    if let ty::Dynamic(..) = declared_ret_ty.kind() {
145        // We have special-cased the case where the function is declared
146        // `-> dyn Foo` and we don't actually relate it to the
147        // `fcx.ret_coercion`, so just instantiate a type variable.
148        actual_return_ty = fcx.next_ty_var(span);
149        debug!("actual_return_ty replaced with {:?}", actual_return_ty);
150    }
151
152    // HACK(oli-obk, compiler-errors): We should be comparing this against
153    // `declared_ret_ty`, but then anything uninferred would be inferred to
154    // the opaque type itself. That again would cause writeback to assume
155    // we have a recursive call site and do the sadly stabilized fallback to `()`.
156    fcx.demand_suptype(span, ret_ty, actual_return_ty);
157
158    // Check that a function marked as `#[panic_handler]` has signature `fn(&PanicInfo) -> !`
159    if tcx.is_lang_item(fn_def_id.to_def_id(), LangItem::PanicImpl) {
160        check_panic_info_fn(tcx, fn_def_id, fn_sig);
161    }
162
163    if tcx.is_lang_item(fn_def_id.to_def_id(), LangItem::Start) {
164        check_lang_start_fn(tcx, fn_sig, fn_def_id);
165    }
166
167    fcx.coroutine_types
168}
169
170fn check_panic_info_fn(tcx: TyCtxt<'_>, fn_id: LocalDefId, fn_sig: ty::FnSig<'_>) {
171    let span = tcx.def_span(fn_id);
172
173    let DefKind::Fn = tcx.def_kind(fn_id) else {
174        tcx.dcx().span_err(span, "should be a function");
175        return;
176    };
177
178    let generic_counts = tcx.generics_of(fn_id).own_counts();
179    if generic_counts.types != 0 {
180        tcx.dcx().span_err(span, "should have no type parameters");
181    }
182    if generic_counts.consts != 0 {
183        tcx.dcx().span_err(span, "should have no const parameters");
184    }
185
186    let panic_info_did = tcx.require_lang_item(hir::LangItem::PanicInfo, Some(span));
187
188    // build type `for<'a, 'b> fn(&'a PanicInfo<'b>) -> !`
189    let panic_info_ty = tcx.type_of(panic_info_did).instantiate(
190        tcx,
191        &[ty::GenericArg::from(ty::Region::new_bound(
192            tcx,
193            ty::INNERMOST,
194            ty::BoundRegion { var: ty::BoundVar::from_u32(1), kind: ty::BoundRegionKind::Anon },
195        ))],
196    );
197    let panic_info_ref_ty = Ty::new_imm_ref(
198        tcx,
199        ty::Region::new_bound(
200            tcx,
201            ty::INNERMOST,
202            ty::BoundRegion { var: ty::BoundVar::ZERO, kind: ty::BoundRegionKind::Anon },
203        ),
204        panic_info_ty,
205    );
206
207    let bounds = tcx.mk_bound_variable_kinds(&[
208        ty::BoundVariableKind::Region(ty::BoundRegionKind::Anon),
209        ty::BoundVariableKind::Region(ty::BoundRegionKind::Anon),
210    ]);
211    let expected_sig = ty::Binder::bind_with_vars(
212        tcx.mk_fn_sig([panic_info_ref_ty], tcx.types.never, false, fn_sig.safety, ExternAbi::Rust),
213        bounds,
214    );
215
216    let _ = check_function_signature(
217        tcx,
218        ObligationCause::new(span, fn_id, ObligationCauseCode::LangFunctionType(sym::panic_impl)),
219        fn_id.into(),
220        expected_sig,
221    );
222}
223
224fn check_lang_start_fn<'tcx>(tcx: TyCtxt<'tcx>, fn_sig: ty::FnSig<'tcx>, def_id: LocalDefId) {
225    // build type `fn(main: fn() -> T, argc: isize, argv: *const *const u8, sigpipe: u8)`
226
227    // make a Ty for the generic on the fn for diagnostics
228    // FIXME: make the lang item generic checks check for the right generic *kind*
229    // for example `start`'s generic should be a type parameter
230    let generics = tcx.generics_of(def_id);
231    let fn_generic = generics.param_at(0, tcx);
232    let generic_ty = Ty::new_param(tcx, fn_generic.index, fn_generic.name);
233    let main_fn_ty = Ty::new_fn_ptr(
234        tcx,
235        Binder::dummy(tcx.mk_fn_sig([], generic_ty, false, hir::Safety::Safe, ExternAbi::Rust)),
236    );
237
238    let expected_sig = ty::Binder::dummy(tcx.mk_fn_sig(
239        [
240            main_fn_ty,
241            tcx.types.isize,
242            Ty::new_imm_ptr(tcx, Ty::new_imm_ptr(tcx, tcx.types.u8)),
243            tcx.types.u8,
244        ],
245        tcx.types.isize,
246        false,
247        fn_sig.safety,
248        ExternAbi::Rust,
249    ));
250
251    let _ = check_function_signature(
252        tcx,
253        ObligationCause::new(
254            tcx.def_span(def_id),
255            def_id,
256            ObligationCauseCode::LangFunctionType(sym::start),
257        ),
258        def_id.into(),
259        expected_sig,
260    );
261}