Skip to content

Commit 7326cd9

Browse files
committed
Factor out the two entry_point_type functions.
They are very similar, and each one has a comment about the importance of being kept in sync with the other. This commit removes the duplication.
1 parent 373cc21 commit 7326cd9

File tree

3 files changed

+36
-42
lines changed

3 files changed

+36
-42
lines changed

compiler/rustc_ast/src/entry.rs

+27
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
use crate::{attr, Attribute};
2+
use rustc_span::symbol::sym;
3+
use rustc_span::Symbol;
4+
15
#[derive(Debug)]
26
pub enum EntryPointType {
37
None,
@@ -6,3 +10,26 @@ pub enum EntryPointType {
610
Start,
711
OtherMain, // Not an entry point, but some other function named main
812
}
13+
14+
pub fn entry_point_type(
15+
attrs: &[Attribute],
16+
at_root: bool,
17+
name: Option<Symbol>,
18+
) -> EntryPointType {
19+
if attr::contains_name(attrs, sym::start) {
20+
EntryPointType::Start
21+
} else if attr::contains_name(attrs, sym::rustc_main) {
22+
EntryPointType::RustcMainAttr
23+
} else {
24+
if let Some(name) = name && name == sym::main {
25+
if at_root {
26+
// This is a top-level function so it can be `main`.
27+
EntryPointType::MainNamed
28+
} else {
29+
EntryPointType::OtherMain
30+
}
31+
} else {
32+
EntryPointType::None
33+
}
34+
}
35+
}

compiler/rustc_builtin_macros/src/test_harness.rs

+2-16
Original file line numberDiff line numberDiff line change
@@ -169,29 +169,15 @@ impl<'a> Visitor<'a> for InnerItemLinter<'_> {
169169
}
170170
}
171171

172-
// Beware, this is duplicated in librustc_passes/entry.rs (with
173-
// `rustc_hir::Item`), so make sure to keep them in sync.
174172
fn entry_point_type(item: &ast::Item, at_root: bool) -> EntryPointType {
175173
match item.kind {
176174
ast::ItemKind::Fn(..) => {
177-
if attr::contains_name(&item.attrs, sym::start) {
178-
EntryPointType::Start
179-
} else if attr::contains_name(&item.attrs, sym::rustc_main) {
180-
EntryPointType::RustcMainAttr
181-
} else if item.ident.name == sym::main {
182-
if at_root {
183-
// This is a top-level function so can be 'main'
184-
EntryPointType::MainNamed
185-
} else {
186-
EntryPointType::OtherMain
187-
}
188-
} else {
189-
EntryPointType::None
190-
}
175+
rustc_ast::entry::entry_point_type(&item.attrs, at_root, Some(item.ident.name))
191176
}
192177
_ => EntryPointType::None,
193178
}
194179
}
180+
195181
/// A folder used to remove any entry points (like fn main) because the harness
196182
/// generator will provide its own
197183
struct EntryPointCleaner<'a> {

compiler/rustc_passes/src/entry.rs

+7-26
Original file line numberDiff line numberDiff line change
@@ -52,31 +52,6 @@ fn entry_fn(tcx: TyCtxt<'_>, (): ()) -> Option<(DefId, EntryFnType)> {
5252
configure_main(tcx, &ctxt)
5353
}
5454

55-
// Beware, this is duplicated in `librustc_builtin_macros/test_harness.rs`
56-
// (with `ast::Item`), so make sure to keep them in sync.
57-
// A small optimization was added so that hir::Item is fetched only when needed.
58-
// An equivalent optimization was not applied to the duplicated code in test_harness.rs.
59-
fn entry_point_type(ctxt: &EntryContext<'_>, id: ItemId, at_root: bool) -> EntryPointType {
60-
let attrs = ctxt.tcx.hir().attrs(id.hir_id());
61-
if attr::contains_name(attrs, sym::start) {
62-
EntryPointType::Start
63-
} else if attr::contains_name(attrs, sym::rustc_main) {
64-
EntryPointType::RustcMainAttr
65-
} else {
66-
if let Some(name) = ctxt.tcx.opt_item_name(id.owner_id.to_def_id())
67-
&& name == sym::main {
68-
if at_root {
69-
// This is a top-level function so can be `main`.
70-
EntryPointType::MainNamed
71-
} else {
72-
EntryPointType::OtherMain
73-
}
74-
} else {
75-
EntryPointType::None
76-
}
77-
}
78-
}
79-
8055
fn attr_span_by_symbol(ctxt: &EntryContext<'_>, id: ItemId, sym: Symbol) -> Option<Span> {
8156
let attrs = ctxt.tcx.hir().attrs(id.hir_id());
8257
attr::find_by_name(attrs, sym).map(|attr| attr.span)
@@ -85,7 +60,13 @@ fn attr_span_by_symbol(ctxt: &EntryContext<'_>, id: ItemId, sym: Symbol) -> Opti
8560
fn find_item(id: ItemId, ctxt: &mut EntryContext<'_>) {
8661
let at_root = ctxt.tcx.opt_local_parent(id.owner_id.def_id) == Some(CRATE_DEF_ID);
8762

88-
match entry_point_type(ctxt, id, at_root) {
63+
let attrs = ctxt.tcx.hir().attrs(id.hir_id());
64+
let entry_point_type = rustc_ast::entry::entry_point_type(
65+
attrs,
66+
at_root,
67+
ctxt.tcx.opt_item_name(id.owner_id.to_def_id()),
68+
);
69+
match entry_point_type {
8970
EntryPointType::None => {
9071
if let Some(span) = attr_span_by_symbol(ctxt, id, sym::unix_sigpipe) {
9172
ctxt.tcx.sess.emit_err(AttrOnlyOnMain { span, attr: sym::unix_sigpipe });

0 commit comments

Comments
 (0)