]>
Commit | Line | Data |
---|---|---|
74b04a01 | 1 | use rustc_ast::entry::EntryPointType; |
dfeec247 | 2 | use rustc_errors::struct_span_err; |
c295e0f8 | 3 | use rustc_hir::def_id::{DefId, CRATE_DEF_ID, CRATE_DEF_INDEX, LOCAL_CRATE}; |
dfeec247 | 4 | use rustc_hir::itemlikevisit::ItemLikeVisitor; |
136023e0 | 5 | use rustc_hir::{ForeignItem, HirId, ImplItem, Item, ItemKind, Node, TraitItem, CRATE_HIR_ID}; |
ba9703b0 XL |
6 | use rustc_middle::hir::map::Map; |
7 | use rustc_middle::ty::query::Providers; | |
8 | use rustc_middle::ty::TyCtxt; | |
f9f354fc | 9 | use rustc_session::config::{CrateType, EntryFnType}; |
cdc7bbd5 | 10 | use rustc_session::parse::feature_err; |
f9f354fc | 11 | use rustc_session::Session; |
dfeec247 XL |
12 | use rustc_span::symbol::sym; |
13 | use rustc_span::{Span, DUMMY_SP}; | |
60c5eb7d | 14 | |
dc9dc135 | 15 | struct EntryContext<'a, 'tcx> { |
1a4d82fc | 16 | session: &'a Session, |
970d7e83 | 17 | |
ba9703b0 | 18 | map: Map<'tcx>, |
970d7e83 | 19 | |
e1599b0c | 20 | /// The function that has attribute named `main`. |
532ac7d7 | 21 | attr_main_fn: Option<(HirId, Span)>, |
970d7e83 | 22 | |
e1599b0c | 23 | /// The function that has the attribute 'start' on it. |
532ac7d7 | 24 | start_fn: Option<(HirId, Span)>, |
970d7e83 | 25 | |
e1599b0c XL |
26 | /// The functions that one might think are `main` but aren't, e.g. |
27 | /// main functions not defined at the top level. For diagnostics. | |
dfeec247 | 28 | non_main_fns: Vec<(HirId, Span)>, |
970d7e83 LB |
29 | } |
30 | ||
476ff2be | 31 | impl<'a, 'tcx> ItemLikeVisitor<'tcx> for EntryContext<'a, 'tcx> { |
dfeec247 | 32 | fn visit_item(&mut self, item: &'tcx Item<'tcx>) { |
6a06907d | 33 | let def_key = self.map.def_key(item.def_id); |
92a42be0 SL |
34 | let at_root = def_key.parent == Some(CRATE_DEF_INDEX); |
35 | find_item(item, self, at_root); | |
1a4d82fc | 36 | } |
476ff2be | 37 | |
dfeec247 | 38 | fn visit_trait_item(&mut self, _trait_item: &'tcx TraitItem<'tcx>) { |
e1599b0c | 39 | // Entry fn is never a trait item. |
32a655c1 | 40 | } |
476ff2be | 41 | |
dfeec247 | 42 | fn visit_impl_item(&mut self, _impl_item: &'tcx ImplItem<'tcx>) { |
e1599b0c | 43 | // Entry fn is never a trait item. |
476ff2be | 44 | } |
fc512014 XL |
45 | |
46 | fn visit_foreign_item(&mut self, _: &'tcx ForeignItem<'tcx>) { | |
47 | // Entry fn is never a foreign item. | |
48 | } | |
1a4d82fc | 49 | } |
970d7e83 | 50 | |
17df50a5 | 51 | fn entry_fn(tcx: TyCtxt<'_>, (): ()) -> Option<(DefId, EntryFnType)> { |
f9f354fc | 52 | let any_exe = tcx.sess.crate_types().iter().any(|ty| *ty == CrateType::Executable); |
1a4d82fc | 53 | if !any_exe { |
e1599b0c | 54 | // No need to find a main function. |
9fa01778 | 55 | return None; |
970d7e83 LB |
56 | } |
57 | ||
1a4d82fc | 58 | // If the user wants no main function at all, then stop here. |
6a06907d | 59 | if tcx.sess.contains_name(&tcx.hir().attrs(CRATE_HIR_ID), sym::no_main) { |
9fa01778 | 60 | return None; |
1a4d82fc JJ |
61 | } |
62 | ||
63 | let mut ctxt = EntryContext { | |
9fa01778 XL |
64 | session: tcx.sess, |
65 | map: tcx.hir(), | |
970d7e83 LB |
66 | attr_main_fn: None, |
67 | start_fn: None, | |
1a4d82fc | 68 | non_main_fns: Vec::new(), |
970d7e83 LB |
69 | }; |
70 | ||
c295e0f8 | 71 | tcx.hir().visit_all_item_likes(&mut ctxt); |
970d7e83 | 72 | |
9fa01778 | 73 | configure_main(tcx, &ctxt) |
970d7e83 LB |
74 | } |
75 | ||
3dfed10e XL |
76 | // Beware, this is duplicated in `librustc_builtin_macros/test_harness.rs` |
77 | // (with `ast::Item`), so make sure to keep them in sync. | |
6a06907d XL |
78 | fn entry_point_type(ctxt: &EntryContext<'_, '_>, item: &Item<'_>, at_root: bool) -> EntryPointType { |
79 | let attrs = ctxt.map.attrs(item.hir_id()); | |
80 | if ctxt.session.contains_name(attrs, sym::start) { | |
29967ef6 | 81 | EntryPointType::Start |
cdc7bbd5 | 82 | } else if ctxt.session.contains_name(attrs, sym::rustc_main) { |
29967ef6 XL |
83 | EntryPointType::MainAttr |
84 | } else if item.ident.name == sym::main { | |
85 | if at_root { | |
86 | // This is a top-level function so can be `main`. | |
87 | EntryPointType::MainNamed | |
88 | } else { | |
89 | EntryPointType::OtherMain | |
970d7e83 | 90 | } |
29967ef6 XL |
91 | } else { |
92 | EntryPointType::None | |
e9174d1e SL |
93 | } |
94 | } | |
95 | ||
29967ef6 XL |
96 | fn throw_attr_err(sess: &Session, span: Span, attr: &str) { |
97 | sess.struct_span_err(span, &format!("`{}` attribute can only be used on functions", attr)) | |
98 | .emit(); | |
99 | } | |
100 | ||
dfeec247 | 101 | fn find_item(item: &Item<'_>, ctxt: &mut EntryContext<'_, '_>, at_root: bool) { |
6a06907d | 102 | match entry_point_type(ctxt, item, at_root) { |
29967ef6 XL |
103 | EntryPointType::None => (), |
104 | _ if !matches!(item.kind, ItemKind::Fn(..)) => { | |
6a06907d XL |
105 | let attrs = ctxt.map.attrs(item.hir_id()); |
106 | if let Some(attr) = ctxt.session.find_by_name(attrs, sym::start) { | |
29967ef6 XL |
107 | throw_attr_err(&ctxt.session, attr.span, "start"); |
108 | } | |
cdc7bbd5 XL |
109 | if let Some(attr) = ctxt.session.find_by_name(attrs, sym::rustc_main) { |
110 | throw_attr_err(&ctxt.session, attr.span, "rustc_main"); | |
e9174d1e | 111 | } |
dfeec247 | 112 | } |
cdc7bbd5 | 113 | EntryPointType::MainNamed => (), |
e9174d1e | 114 | EntryPointType::OtherMain => { |
6a06907d | 115 | ctxt.non_main_fns.push((item.hir_id(), item.span)); |
dfeec247 | 116 | } |
e9174d1e SL |
117 | EntryPointType::MainAttr => { |
118 | if ctxt.attr_main_fn.is_none() { | |
6a06907d | 119 | ctxt.attr_main_fn = Some((item.hir_id(), item.span)); |
e9174d1e | 120 | } else { |
dfeec247 XL |
121 | struct_span_err!( |
122 | ctxt.session, | |
123 | item.span, | |
124 | E0137, | |
125 | "multiple functions with a `#[main]` attribute" | |
126 | ) | |
416331ca XL |
127 | .span_label(item.span, "additional `#[main]` function") |
128 | .span_label(ctxt.attr_main_fn.unwrap().1, "first `#[main]` function") | |
5bcae85e | 129 | .emit(); |
e9174d1e | 130 | } |
dfeec247 | 131 | } |
e9174d1e SL |
132 | EntryPointType::Start => { |
133 | if ctxt.start_fn.is_none() { | |
6a06907d | 134 | ctxt.start_fn = Some((item.hir_id(), item.span)); |
e9174d1e | 135 | } else { |
e1599b0c | 136 | struct_span_err!(ctxt.session, item.span, E0138, "multiple `start` functions") |
dfeec247 | 137 | .span_label(ctxt.start_fn.unwrap().1, "previous `#[start]` function here") |
7cac9316 | 138 | .span_label(item.span, "multiple `start` functions") |
5bcae85e | 139 | .emit(); |
e9174d1e | 140 | } |
9fa01778 | 141 | } |
970d7e83 | 142 | } |
970d7e83 LB |
143 | } |
144 | ||
cdc7bbd5 | 145 | fn configure_main(tcx: TyCtxt<'_>, visitor: &EntryContext<'_, '_>) -> Option<(DefId, EntryFnType)> { |
532ac7d7 | 146 | if let Some((hir_id, _)) = visitor.start_fn { |
cdc7bbd5 | 147 | Some((tcx.hir().local_def_id(hir_id).to_def_id(), EntryFnType::Start)) |
532ac7d7 | 148 | } else if let Some((hir_id, _)) = visitor.attr_main_fn { |
cdc7bbd5 | 149 | Some((tcx.hir().local_def_id(hir_id).to_def_id(), EntryFnType::Main)) |
970d7e83 | 150 | } else { |
136023e0 XL |
151 | if let Some(main_def) = tcx.resolutions(()).main_def { |
152 | if let Some(def_id) = main_def.opt_fn_def_id() { | |
153 | // non-local main imports are handled below | |
154 | if def_id.is_local() { | |
155 | let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local()); | |
156 | if matches!(tcx.hir().find(hir_id), Some(Node::ForeignItem(_))) { | |
157 | tcx.sess | |
158 | .struct_span_err( | |
159 | tcx.hir().span(hir_id), | |
160 | "the `main` function cannot be declared in an `extern` block", | |
161 | ) | |
162 | .emit(); | |
163 | return None; | |
164 | } | |
165 | } | |
166 | ||
167 | if main_def.is_import && !tcx.features().imported_main { | |
168 | let span = main_def.span; | |
169 | feature_err( | |
170 | &tcx.sess.parse_sess, | |
171 | sym::imported_main, | |
172 | span, | |
173 | "using an imported function as entry point `main` is experimental", | |
174 | ) | |
175 | .emit(); | |
176 | } | |
177 | return Some((def_id, EntryFnType::Main)); | |
178 | } | |
179 | } | |
e1599b0c | 180 | no_main_err(tcx, visitor); |
9fa01778 | 181 | None |
970d7e83 LB |
182 | } |
183 | } | |
9fa01778 | 184 | |
e1599b0c | 185 | fn no_main_err(tcx: TyCtxt<'_>, visitor: &EntryContext<'_, '_>) { |
c295e0f8 | 186 | let sp = tcx.def_span(CRATE_DEF_ID); |
e74abb32 XL |
187 | if *tcx.sess.parse_sess.reached_eof.borrow() { |
188 | // There's an unclosed brace that made the parser reach `Eof`, we shouldn't complain about | |
189 | // the missing `fn main()` then as it might have been hidden inside an unclosed block. | |
190 | tcx.sess.delay_span_bug(sp, "`main` not found, but expected unclosed brace error"); | |
191 | return; | |
192 | } | |
193 | ||
e1599b0c | 194 | // There is no main function. |
dfeec247 XL |
195 | let mut err = struct_span_err!( |
196 | tcx.sess, | |
197 | DUMMY_SP, | |
198 | E0601, | |
199 | "`main` function not found in crate `{}`", | |
200 | tcx.crate_name(LOCAL_CRATE) | |
201 | ); | |
e1599b0c XL |
202 | let filename = &tcx.sess.local_crate_source_file; |
203 | let note = if !visitor.non_main_fns.is_empty() { | |
204 | for &(_, span) in &visitor.non_main_fns { | |
205 | err.span_note(span, "here is a function named `main`"); | |
206 | } | |
207 | err.note("you have one or more functions named `main` not defined at the crate level"); | |
cdc7bbd5 | 208 | err.help("consider moving the `main` function definitions"); |
e1599b0c | 209 | // There were some functions named `main` though. Try to give the user a hint. |
dfeec247 XL |
210 | format!( |
211 | "the main function must be defined at the crate level{}", | |
212 | filename.as_ref().map(|f| format!(" (in `{}`)", f.display())).unwrap_or_default() | |
213 | ) | |
e1599b0c XL |
214 | } else if let Some(filename) = filename { |
215 | format!("consider adding a `main` function to `{}`", filename.display()) | |
216 | } else { | |
217 | String::from("consider adding a `main` function at the crate level") | |
218 | }; | |
e1599b0c XL |
219 | // The file may be empty, which leads to the diagnostic machinery not emitting this |
220 | // note. This is a relatively simple way to detect that case and emit a span-less | |
221 | // note instead. | |
74b04a01 | 222 | if tcx.sess.source_map().lookup_line(sp.lo()).is_ok() { |
e1599b0c XL |
223 | err.set_span(sp); |
224 | err.span_label(sp, ¬e); | |
225 | } else { | |
226 | err.note(¬e); | |
227 | } | |
cdc7bbd5 | 228 | |
136023e0 | 229 | if let Some(main_def) = tcx.resolutions(()).main_def { |
cdc7bbd5 XL |
230 | if main_def.opt_fn_def_id().is_none() { |
231 | // There is something at `crate::main`, but it is not a function definition. | |
94222f64 | 232 | err.span_label(main_def.span, "non-function item at `crate::main` is found"); |
cdc7bbd5 XL |
233 | } |
234 | } | |
235 | ||
e1599b0c | 236 | if tcx.sess.teach(&err.get_code().unwrap()) { |
dfeec247 XL |
237 | err.note( |
238 | "If you don't know the basics of Rust, you can go look to the Rust Book \ | |
239 | to get started: https://doc.rust-lang.org/book/", | |
240 | ); | |
e1599b0c XL |
241 | } |
242 | err.emit(); | |
243 | } | |
244 | ||
f035d41b | 245 | pub fn provide(providers: &mut Providers) { |
dfeec247 | 246 | *providers = Providers { entry_fn, ..*providers }; |
9fa01778 | 247 | } |