]>
Commit | Line | Data |
---|---|---|
f2b60f7d | 1 | use rustc_ast::entry::EntryPointType; |
2b03887a | 2 | use rustc_errors::error_code; |
923072b8 | 3 | use rustc_hir::def::DefKind; |
04454e1e | 4 | use rustc_hir::def_id::{DefId, LocalDefId, CRATE_DEF_ID, LOCAL_CRATE}; |
923072b8 | 5 | use rustc_hir::{ItemId, Node, CRATE_HIR_ID}; |
ba9703b0 | 6 | use rustc_middle::ty::query::Providers; |
04454e1e | 7 | use rustc_middle::ty::{DefIdTree, TyCtxt}; |
f2b60f7d | 8 | use rustc_session::config::{sigpipe, CrateType, EntryFnType}; |
cdc7bbd5 | 9 | use rustc_session::parse::feature_err; |
dfeec247 | 10 | use rustc_span::symbol::sym; |
2b03887a FG |
11 | use rustc_span::{Span, Symbol}; |
12 | ||
13 | use crate::errors::{ | |
14 | AttrOnlyInFunctions, AttrOnlyOnMain, AttrOnlyOnRootMain, ExternMain, MultipleRustcMain, | |
15 | MultipleStartFunctions, NoMainErr, UnixSigpipeValues, | |
16 | }; | |
60c5eb7d | 17 | |
04454e1e FG |
18 | struct EntryContext<'tcx> { |
19 | tcx: TyCtxt<'tcx>, | |
970d7e83 | 20 | |
e1599b0c | 21 | /// The function that has attribute named `main`. |
5099ac24 | 22 | attr_main_fn: Option<(LocalDefId, Span)>, |
970d7e83 | 23 | |
e1599b0c | 24 | /// The function that has the attribute 'start' on it. |
5099ac24 | 25 | start_fn: Option<(LocalDefId, Span)>, |
970d7e83 | 26 | |
e1599b0c XL |
27 | /// The functions that one might think are `main` but aren't, e.g. |
28 | /// main functions not defined at the top level. For diagnostics. | |
5099ac24 | 29 | non_main_fns: Vec<Span>, |
970d7e83 LB |
30 | } |
31 | ||
17df50a5 | 32 | fn entry_fn(tcx: TyCtxt<'_>, (): ()) -> Option<(DefId, EntryFnType)> { |
f9f354fc | 33 | let any_exe = tcx.sess.crate_types().iter().any(|ty| *ty == CrateType::Executable); |
1a4d82fc | 34 | if !any_exe { |
e1599b0c | 35 | // No need to find a main function. |
9fa01778 | 36 | return None; |
970d7e83 LB |
37 | } |
38 | ||
1a4d82fc | 39 | // If the user wants no main function at all, then stop here. |
6a06907d | 40 | if tcx.sess.contains_name(&tcx.hir().attrs(CRATE_HIR_ID), sym::no_main) { |
9fa01778 | 41 | return None; |
1a4d82fc JJ |
42 | } |
43 | ||
04454e1e FG |
44 | let mut ctxt = |
45 | EntryContext { tcx, attr_main_fn: None, start_fn: None, non_main_fns: Vec::new() }; | |
970d7e83 | 46 | |
923072b8 FG |
47 | for id in tcx.hir().items() { |
48 | find_item(id, &mut ctxt); | |
49 | } | |
970d7e83 | 50 | |
9fa01778 | 51 | configure_main(tcx, &ctxt) |
970d7e83 LB |
52 | } |
53 | ||
3dfed10e XL |
54 | // Beware, this is duplicated in `librustc_builtin_macros/test_harness.rs` |
55 | // (with `ast::Item`), so make sure to keep them in sync. | |
923072b8 FG |
56 | // A small optimization was added so that hir::Item is fetched only when needed. |
57 | // An equivalent optimization was not applied to the duplicated code in test_harness.rs. | |
58 | fn entry_point_type(ctxt: &EntryContext<'_>, id: ItemId, at_root: bool) -> EntryPointType { | |
59 | let attrs = ctxt.tcx.hir().attrs(id.hir_id()); | |
04454e1e | 60 | if ctxt.tcx.sess.contains_name(attrs, sym::start) { |
29967ef6 | 61 | EntryPointType::Start |
04454e1e | 62 | } else if ctxt.tcx.sess.contains_name(attrs, sym::rustc_main) { |
923072b8 FG |
63 | EntryPointType::RustcMainAttr |
64 | } else { | |
2b03887a | 65 | if let Some(name) = ctxt.tcx.opt_item_name(id.owner_id.to_def_id()) |
923072b8 FG |
66 | && name == sym::main { |
67 | if at_root { | |
68 | // This is a top-level function so can be `main`. | |
69 | EntryPointType::MainNamed | |
70 | } else { | |
71 | EntryPointType::OtherMain | |
72 | } | |
29967ef6 | 73 | } else { |
923072b8 | 74 | EntryPointType::None |
970d7e83 | 75 | } |
e9174d1e SL |
76 | } |
77 | } | |
78 | ||
2b03887a | 79 | fn attr_span_by_symbol(ctxt: &EntryContext<'_>, id: ItemId, sym: Symbol) -> Option<Span> { |
f2b60f7d | 80 | let attrs = ctxt.tcx.hir().attrs(id.hir_id()); |
2b03887a | 81 | ctxt.tcx.sess.find_by_name(attrs, sym).map(|attr| attr.span) |
29967ef6 XL |
82 | } |
83 | ||
923072b8 | 84 | fn find_item(id: ItemId, ctxt: &mut EntryContext<'_>) { |
2b03887a | 85 | let at_root = ctxt.tcx.opt_local_parent(id.owner_id.def_id) == Some(CRATE_DEF_ID); |
923072b8 FG |
86 | |
87 | match entry_point_type(ctxt, id, at_root) { | |
f2b60f7d | 88 | EntryPointType::None => { |
2b03887a FG |
89 | if let Some(span) = attr_span_by_symbol(ctxt, id, sym::unix_sigpipe) { |
90 | ctxt.tcx.sess.emit_err(AttrOnlyOnMain { span, attr: sym::unix_sigpipe }); | |
91 | } | |
f2b60f7d | 92 | } |
2b03887a FG |
93 | _ if !matches!(ctxt.tcx.def_kind(id.owner_id), DefKind::Fn) => { |
94 | for attr in [sym::start, sym::rustc_main] { | |
95 | if let Some(span) = attr_span_by_symbol(ctxt, id, attr) { | |
96 | ctxt.tcx.sess.emit_err(AttrOnlyInFunctions { span, attr }); | |
97 | } | |
98 | } | |
dfeec247 | 99 | } |
cdc7bbd5 | 100 | EntryPointType::MainNamed => (), |
e9174d1e | 101 | EntryPointType::OtherMain => { |
2b03887a FG |
102 | if let Some(span) = attr_span_by_symbol(ctxt, id, sym::unix_sigpipe) { |
103 | ctxt.tcx.sess.emit_err(AttrOnlyOnRootMain { span, attr: sym::unix_sigpipe }); | |
104 | } | |
105 | ctxt.non_main_fns.push(ctxt.tcx.def_span(id.owner_id)); | |
dfeec247 | 106 | } |
923072b8 | 107 | EntryPointType::RustcMainAttr => { |
e9174d1e | 108 | if ctxt.attr_main_fn.is_none() { |
2b03887a | 109 | ctxt.attr_main_fn = Some((id.owner_id.def_id, ctxt.tcx.def_span(id.owner_id))); |
e9174d1e | 110 | } else { |
2b03887a FG |
111 | ctxt.tcx.sess.emit_err(MultipleRustcMain { |
112 | span: ctxt.tcx.def_span(id.owner_id.to_def_id()), | |
113 | first: ctxt.attr_main_fn.unwrap().1, | |
114 | additional: ctxt.tcx.def_span(id.owner_id.to_def_id()), | |
115 | }); | |
e9174d1e | 116 | } |
dfeec247 | 117 | } |
e9174d1e | 118 | EntryPointType::Start => { |
2b03887a FG |
119 | if let Some(span) = attr_span_by_symbol(ctxt, id, sym::unix_sigpipe) { |
120 | ctxt.tcx.sess.emit_err(AttrOnlyOnMain { span, attr: sym::unix_sigpipe }); | |
121 | } | |
e9174d1e | 122 | if ctxt.start_fn.is_none() { |
2b03887a | 123 | ctxt.start_fn = Some((id.owner_id.def_id, ctxt.tcx.def_span(id.owner_id))); |
e9174d1e | 124 | } else { |
2b03887a FG |
125 | ctxt.tcx.sess.emit_err(MultipleStartFunctions { |
126 | span: ctxt.tcx.def_span(id.owner_id), | |
127 | labeled: ctxt.tcx.def_span(id.owner_id.to_def_id()), | |
128 | previous: ctxt.start_fn.unwrap().1, | |
129 | }); | |
e9174d1e | 130 | } |
9fa01778 | 131 | } |
970d7e83 | 132 | } |
970d7e83 LB |
133 | } |
134 | ||
04454e1e | 135 | fn configure_main(tcx: TyCtxt<'_>, visitor: &EntryContext<'_>) -> Option<(DefId, EntryFnType)> { |
5099ac24 FG |
136 | if let Some((def_id, _)) = visitor.start_fn { |
137 | Some((def_id.to_def_id(), EntryFnType::Start)) | |
f2b60f7d FG |
138 | } else if let Some((local_def_id, _)) = visitor.attr_main_fn { |
139 | let def_id = local_def_id.to_def_id(); | |
140 | Some((def_id, EntryFnType::Main { sigpipe: sigpipe(tcx, def_id) })) | |
970d7e83 | 141 | } else { |
5e7ed085 FG |
142 | if let Some(main_def) = tcx.resolutions(()).main_def && let Some(def_id) = main_def.opt_fn_def_id() { |
143 | // non-local main imports are handled below | |
144 | if let Some(def_id) = def_id.as_local() && matches!(tcx.hir().find_by_def_id(def_id), Some(Node::ForeignItem(_))) { | |
2b03887a | 145 | tcx.sess.emit_err(ExternMain { span: tcx.def_span(def_id) }); |
5e7ed085 | 146 | return None; |
136023e0 | 147 | } |
5e7ed085 FG |
148 | |
149 | if main_def.is_import && !tcx.features().imported_main { | |
150 | let span = main_def.span; | |
151 | feature_err( | |
152 | &tcx.sess.parse_sess, | |
153 | sym::imported_main, | |
154 | span, | |
155 | "using an imported function as entry point `main` is experimental", | |
156 | ) | |
157 | .emit(); | |
158 | } | |
f2b60f7d | 159 | return Some((def_id, EntryFnType::Main { sigpipe: sigpipe(tcx, def_id) })); |
136023e0 | 160 | } |
e1599b0c | 161 | no_main_err(tcx, visitor); |
9fa01778 | 162 | None |
970d7e83 LB |
163 | } |
164 | } | |
9fa01778 | 165 | |
f2b60f7d FG |
166 | fn sigpipe(tcx: TyCtxt<'_>, def_id: DefId) -> u8 { |
167 | if let Some(attr) = tcx.get_attr(def_id, sym::unix_sigpipe) { | |
168 | match (attr.value_str(), attr.meta_item_list()) { | |
169 | (Some(sym::inherit), None) => sigpipe::INHERIT, | |
170 | (Some(sym::sig_ign), None) => sigpipe::SIG_IGN, | |
171 | (Some(sym::sig_dfl), None) => sigpipe::SIG_DFL, | |
172 | (_, Some(_)) => { | |
173 | // Keep going so that `fn emit_malformed_attribute()` can print | |
174 | // an excellent error message | |
175 | sigpipe::DEFAULT | |
176 | } | |
177 | _ => { | |
2b03887a | 178 | tcx.sess.emit_err(UnixSigpipeValues { span: attr.span }); |
f2b60f7d FG |
179 | sigpipe::DEFAULT |
180 | } | |
181 | } | |
182 | } else { | |
183 | sigpipe::DEFAULT | |
184 | } | |
185 | } | |
186 | ||
04454e1e | 187 | fn no_main_err(tcx: TyCtxt<'_>, visitor: &EntryContext<'_>) { |
c295e0f8 | 188 | let sp = tcx.def_span(CRATE_DEF_ID); |
e74abb32 XL |
189 | if *tcx.sess.parse_sess.reached_eof.borrow() { |
190 | // There's an unclosed brace that made the parser reach `Eof`, we shouldn't complain about | |
191 | // the missing `fn main()` then as it might have been hidden inside an unclosed block. | |
192 | tcx.sess.delay_span_bug(sp, "`main` not found, but expected unclosed brace error"); | |
193 | return; | |
194 | } | |
195 | ||
e1599b0c | 196 | // There is no main function. |
2b03887a | 197 | let mut has_filename = true; |
6522a427 | 198 | let filename = tcx.sess.local_crate_source_file().unwrap_or_else(|| { |
2b03887a FG |
199 | has_filename = false; |
200 | Default::default() | |
201 | }); | |
202 | let main_def_opt = tcx.resolutions(()).main_def; | |
203 | let diagnostic_id = error_code!(E0601); | |
204 | let add_teach_note = tcx.sess.teach(&diagnostic_id); | |
e1599b0c XL |
205 | // The file may be empty, which leads to the diagnostic machinery not emitting this |
206 | // note. This is a relatively simple way to detect that case and emit a span-less | |
207 | // note instead. | |
2b03887a FG |
208 | let file_empty = !tcx.sess.source_map().lookup_line(sp.hi()).is_ok(); |
209 | ||
210 | tcx.sess.emit_err(NoMainErr { | |
211 | sp, | |
212 | crate_name: tcx.crate_name(LOCAL_CRATE), | |
213 | has_filename, | |
214 | filename, | |
215 | file_empty, | |
216 | non_main_fns: visitor.non_main_fns.clone(), | |
217 | main_def_opt, | |
218 | add_teach_note, | |
219 | }); | |
e1599b0c XL |
220 | } |
221 | ||
f035d41b | 222 | pub fn provide(providers: &mut Providers) { |
dfeec247 | 223 | *providers = Providers { entry_fn, ..*providers }; |
9fa01778 | 224 | } |