Skip to main content

rustc_hir_analysis/check/
entry.rs

1use std::ops::Not;
2
3use rustc_abi::ExternAbi;
4use rustc_hir as hir;
5use rustc_hir::{Node, find_attr};
6use rustc_infer::infer::TyCtxtInferExt;
7use rustc_middle::span_bug;
8use rustc_middle::ty::{self, TyCtxt, TypingMode};
9use rustc_session::config::EntryFnType;
10use rustc_span::def_id::{CRATE_DEF_ID, DefId, LocalDefId};
11use rustc_span::{ErrorGuaranteed, Span};
12use rustc_trait_selection::error_reporting::InferCtxtErrorExt;
13use rustc_trait_selection::regions::InferCtxtRegionExt;
14use rustc_trait_selection::traits::{self, ObligationCause, ObligationCauseCode};
15
16use super::check_function_signature;
17use crate::errors;
18
19pub(crate) fn check_for_entry_fn(tcx: TyCtxt<'_>) -> Result<(), ErrorGuaranteed> {
20    match tcx.entry_fn(()) {
21        Some((def_id, EntryFnType::Main { .. })) => check_main_fn_ty(tcx, def_id),
22        _ => Ok(()),
23    }
24}
25
26fn check_main_fn_ty(tcx: TyCtxt<'_>, main_def_id: DefId) -> Result<(), ErrorGuaranteed> {
27    let main_fnsig = tcx.fn_sig(main_def_id).instantiate_identity();
28    let main_span = tcx.def_span(main_def_id);
29
30    fn main_fn_diagnostics_def_id(tcx: TyCtxt<'_>, def_id: DefId, sp: Span) -> LocalDefId {
31        if let Some(local_def_id) = def_id.as_local() {
32            let hir_type = tcx.type_of(local_def_id).instantiate_identity();
33            if !#[allow(non_exhaustive_omitted_patterns)] match hir_type.kind() {
    ty::FnDef(..) => true,
    _ => false,
}matches!(hir_type.kind(), ty::FnDef(..)) {
34                ::rustc_middle::util::bug::span_bug_fmt(sp,
    format_args!("main has a non-function type: found `{0}`", hir_type));span_bug!(sp, "main has a non-function type: found `{}`", hir_type);
35            }
36            local_def_id
37        } else {
38            CRATE_DEF_ID
39        }
40    }
41
42    fn main_fn_generics_params_span(tcx: TyCtxt<'_>, def_id: DefId) -> Option<Span> {
43        if !def_id.is_local() {
44            return None;
45        }
46        match tcx.hir_node_by_def_id(def_id.expect_local()) {
47            Node::Item(hir::Item { kind: hir::ItemKind::Fn { generics, .. }, .. }) => {
48                generics.params.is_empty().not().then_some(generics.span)
49            }
50            _ => {
51                ::rustc_middle::util::bug::span_bug_fmt(tcx.def_span(def_id),
    format_args!("main has a non-function type"));span_bug!(tcx.def_span(def_id), "main has a non-function type");
52            }
53        }
54    }
55
56    fn main_fn_where_clauses_span(tcx: TyCtxt<'_>, def_id: DefId) -> Option<Span> {
57        if !def_id.is_local() {
58            return None;
59        }
60        match tcx.hir_node_by_def_id(def_id.expect_local()) {
61            Node::Item(hir::Item { kind: hir::ItemKind::Fn { generics, .. }, .. }) => {
62                Some(generics.where_clause_span)
63            }
64            _ => {
65                ::rustc_middle::util::bug::span_bug_fmt(tcx.def_span(def_id),
    format_args!("main has a non-function type"));span_bug!(tcx.def_span(def_id), "main has a non-function type");
66            }
67        }
68    }
69
70    fn main_fn_asyncness_span(tcx: TyCtxt<'_>, def_id: DefId) -> Option<Span> {
71        if !def_id.is_local() {
72            return None;
73        }
74        Some(tcx.def_span(def_id))
75    }
76
77    fn main_fn_return_type_span(tcx: TyCtxt<'_>, def_id: DefId) -> Option<Span> {
78        if !def_id.is_local() {
79            return None;
80        }
81        match tcx.hir_node_by_def_id(def_id.expect_local()) {
82            Node::Item(hir::Item { kind: hir::ItemKind::Fn { sig: fn_sig, .. }, .. }) => {
83                Some(fn_sig.decl.output.span())
84            }
85            _ => {
86                ::rustc_middle::util::bug::span_bug_fmt(tcx.def_span(def_id),
    format_args!("main has a non-function type"));span_bug!(tcx.def_span(def_id), "main has a non-function type");
87            }
88        }
89    }
90
91    let main_diagnostics_def_id = main_fn_diagnostics_def_id(tcx, main_def_id, main_span);
92
93    let main_asyncness = tcx.asyncness(main_def_id);
94    if main_asyncness.is_async() {
95        let asyncness_span = main_fn_asyncness_span(tcx, main_def_id);
96        return Err(tcx
97            .dcx()
98            .emit_err(errors::MainFunctionAsync { span: main_span, asyncness: asyncness_span }));
99    }
100
101    if let Some(attr_span) = {
    {
        'done:
            {
            for i in
                ::rustc_hir::attrs::HasAttrs::get_attrs(main_def_id, &tcx) {
                #[allow(unused_imports)]
                use rustc_hir::attrs::AttributeKind::*;
                let i: &rustc_hir::Attribute = i;
                match i {
                    rustc_hir::Attribute::Parsed(TrackCaller(span)) => {
                        break 'done Some(*span);
                    }
                    rustc_hir::Attribute::Unparsed(..) =>
                        {}
                        #[deny(unreachable_patterns)]
                        _ => {}
                }
            }
            None
        }
    }
}find_attr!(tcx, main_def_id, TrackCaller(span) => *span) {
102        return Err(tcx
103            .dcx()
104            .emit_err(errors::TrackCallerOnMain { span: attr_span, annotated: main_span }));
105    }
106
107    if !tcx.codegen_fn_attrs(main_def_id).target_features.is_empty()
108        // Calling functions with `#[target_feature]` is not unsafe on WASM, see #84988
109        && !tcx.sess.target.is_like_wasm
110        && !tcx.sess.opts.actually_rustdoc
111    {
112        return Err(tcx.dcx().emit_err(errors::TargetFeatureOnMain { main: main_span }));
113    }
114
115    // Main should have no WC, so empty param env is OK here.
116    let param_env = ty::ParamEnv::empty();
117    let expected_return_type;
118    if let Some(term_did) = tcx.lang_items().termination() {
119        let return_ty = main_fnsig.output();
120        let return_ty_span = main_fn_return_type_span(tcx, main_def_id).unwrap_or(main_span);
121        let Some(return_ty) = return_ty.no_bound_vars() else {
122            return Err(tcx
123                .dcx()
124                .emit_err(errors::MainFunctionReturnTypeGeneric { span: return_ty_span }));
125        };
126        let infcx = tcx.infer_ctxt().build(TypingMode::non_body_analysis());
127        let cause = traits::ObligationCause::new(
128            return_ty_span,
129            main_diagnostics_def_id,
130            ObligationCauseCode::MainFunctionType,
131        );
132        let ocx = traits::ObligationCtxt::new_with_diagnostics(&infcx);
133        let norm_return_ty = ocx.normalize(&cause, param_env, return_ty);
134        ocx.register_bound(cause, param_env, norm_return_ty, term_did);
135        let errors = ocx.evaluate_obligations_error_on_ambiguity();
136        if !errors.is_empty() {
137            return Err(infcx.err_ctxt().report_fulfillment_errors(errors));
138        }
139
140        let region_errors =
141            infcx.resolve_regions(main_diagnostics_def_id, param_env, ty::List::empty());
142
143        if !region_errors.is_empty() {
144            return Err(infcx
145                .err_ctxt()
146                .report_region_errors(main_diagnostics_def_id, &region_errors));
147        }
148        // now we can take the return type of the given main function
149        expected_return_type = norm_return_ty;
150    } else {
151        // standard () main return type
152        expected_return_type = tcx.types.unit;
153    }
154
155    let expected_sig = ty::Binder::dummy(tcx.mk_fn_sig(
156        [],
157        expected_return_type,
158        false,
159        hir::Safety::Safe,
160        ExternAbi::Rust,
161    ));
162
163    check_function_signature(
164        tcx,
165        ObligationCause::new(
166            main_span,
167            main_diagnostics_def_id,
168            ObligationCauseCode::MainFunctionType,
169        ),
170        main_def_id,
171        expected_sig,
172    )?;
173
174    let main_fn_generics = tcx.generics_of(main_def_id);
175    let main_fn_predicates = tcx.predicates_of(main_def_id);
176    if main_fn_generics.count() != 0 || !main_fnsig.bound_vars().is_empty() {
177        let generics_param_span = main_fn_generics_params_span(tcx, main_def_id);
178        return Err(tcx.dcx().emit_err(errors::MainFunctionGenericParameters {
179            span: generics_param_span.unwrap_or(main_span),
180            label_span: generics_param_span,
181        }));
182    } else if !main_fn_predicates.predicates.is_empty() {
183        // generics may bring in implicit predicates, so we skip this check if generics is present.
184        let generics_where_clauses_span = main_fn_where_clauses_span(tcx, main_def_id);
185        return Err(tcx.dcx().emit_err(errors::WhereClauseOnMain {
186            span: generics_where_clauses_span.unwrap_or(main_span),
187            generics_span: generics_where_clauses_span,
188        }));
189    }
190
191    Ok(())
192}