Skip to content

Commit 0e1dd17

Browse files
committed
Auto merge of rust-lang#116259 - nnethercote:entry_point_type, r=cjgillot
Factor out duplicated `entry_point_type` functions A small but nice cleanup.
2 parents 16b7b39 + 7326cd9 commit 0e1dd17

File tree

3 files changed

+38
-44
lines changed

3 files changed

+38
-44
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

+4-18
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.
174-
fn entry_point_type(item: &ast::Item, depth: usize) -> EntryPointType {
172+
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 depth == 0 {
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> {
@@ -210,7 +196,7 @@ impl<'a> MutVisitor for EntryPointCleaner<'a> {
210196
// Remove any #[rustc_main] or #[start] from the AST so it doesn't
211197
// clash with the one we're going to add, but mark it as
212198
// #[allow(dead_code)] to avoid printing warnings.
213-
let item = match entry_point_type(&item, self.depth) {
199+
let item = match entry_point_type(&item, self.depth == 0) {
214200
EntryPointType::MainNamed | EntryPointType::RustcMainAttr | EntryPointType::Start => {
215201
item.map(|ast::Item { id, ident, attrs, kind, vis, span, tokens }| {
216202
let allow_dead_code = attr::mk_attr_nested_word(

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)