Skip to content

Commit b1f0209

Browse files
committed
optimize find_item to fetch Item only when needed
Signed-off-by: Miguel Guarniz <mi9uel9@gmail.com>
1 parent fb73ae2 commit b1f0209

File tree

3 files changed

+42
-29
lines changed

3 files changed

+42
-29
lines changed

compiler/rustc_passes/src/entry.rs

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use rustc_ast::entry::EntryPointType;
22
use rustc_errors::struct_span_err;
3+
use rustc_hir::def::DefKind;
34
use rustc_hir::def_id::{DefId, LocalDefId, CRATE_DEF_ID, LOCAL_CRATE};
4-
use rustc_hir::{Item, ItemKind, Node, CRATE_HIR_ID};
5+
use rustc_hir::{ItemId, Node, CRATE_HIR_ID};
56
use rustc_middle::ty::query::Providers;
67
use rustc_middle::ty::{DefIdTree, TyCtxt};
78
use rustc_session::config::{CrateType, EntryFnType};
@@ -40,30 +41,34 @@ fn entry_fn(tcx: TyCtxt<'_>, (): ()) -> Option<(DefId, EntryFnType)> {
4041
EntryContext { tcx, attr_main_fn: None, start_fn: None, non_main_fns: Vec::new() };
4142

4243
for id in tcx.hir().items() {
43-
let item = tcx.hir().item(id);
44-
find_item(item, &mut ctxt);
44+
find_item(id, &mut ctxt);
4545
}
4646

4747
configure_main(tcx, &ctxt)
4848
}
4949

5050
// Beware, this is duplicated in `librustc_builtin_macros/test_harness.rs`
5151
// (with `ast::Item`), so make sure to keep them in sync.
52-
fn entry_point_type(ctxt: &EntryContext<'_>, item: &Item<'_>, at_root: bool) -> EntryPointType {
53-
let attrs = ctxt.tcx.hir().attrs(item.hir_id());
52+
// A small optimization was added so that hir::Item is fetched only when needed.
53+
// An equivalent optimization was not applied to the duplicated code in test_harness.rs.
54+
fn entry_point_type(ctxt: &EntryContext<'_>, id: ItemId, at_root: bool) -> EntryPointType {
55+
let attrs = ctxt.tcx.hir().attrs(id.hir_id());
5456
if ctxt.tcx.sess.contains_name(attrs, sym::start) {
5557
EntryPointType::Start
5658
} else if ctxt.tcx.sess.contains_name(attrs, sym::rustc_main) {
5759
EntryPointType::MainAttr
58-
} else if item.ident.name == sym::main {
59-
if at_root {
60-
// This is a top-level function so can be `main`.
61-
EntryPointType::MainNamed
60+
} else {
61+
let item = ctxt.tcx.hir().item(id);
62+
if item.ident.name == sym::main {
63+
if at_root {
64+
// This is a top-level function so can be `main`.
65+
EntryPointType::MainNamed
66+
} else {
67+
EntryPointType::OtherMain
68+
}
6269
} else {
63-
EntryPointType::OtherMain
70+
EntryPointType::None
6471
}
65-
} else {
66-
EntryPointType::None
6772
}
6873
}
6974

@@ -72,13 +77,13 @@ fn throw_attr_err(sess: &Session, span: Span, attr: &str) {
7277
.emit();
7378
}
7479

75-
fn find_item(item: &Item<'_>, ctxt: &mut EntryContext<'_>) {
76-
let at_root = ctxt.tcx.opt_local_parent(item.def_id) == Some(CRATE_DEF_ID);
80+
fn find_item(id: ItemId, ctxt: &mut EntryContext<'_>) {
81+
let at_root = ctxt.tcx.opt_local_parent(id.def_id) == Some(CRATE_DEF_ID);
7782

78-
match entry_point_type(ctxt, item, at_root) {
83+
match entry_point_type(ctxt, id, at_root) {
7984
EntryPointType::None => (),
80-
_ if !matches!(item.kind, ItemKind::Fn(..)) => {
81-
let attrs = ctxt.tcx.hir().attrs(item.hir_id());
85+
_ if !matches!(ctxt.tcx.hir().def_kind(id.def_id), DefKind::Fn) => {
86+
let attrs = ctxt.tcx.hir().attrs(id.hir_id());
8287
if let Some(attr) = ctxt.tcx.sess.find_by_name(attrs, sym::start) {
8388
throw_attr_err(&ctxt.tcx.sess, attr.span, "start");
8489
}
@@ -88,31 +93,39 @@ fn find_item(item: &Item<'_>, ctxt: &mut EntryContext<'_>) {
8893
}
8994
EntryPointType::MainNamed => (),
9095
EntryPointType::OtherMain => {
91-
ctxt.non_main_fns.push(item.span);
96+
ctxt.non_main_fns.push(ctxt.tcx.def_span(id.def_id.to_def_id()));
9297
}
9398
EntryPointType::MainAttr => {
9499
if ctxt.attr_main_fn.is_none() {
95-
ctxt.attr_main_fn = Some((item.def_id, item.span));
100+
ctxt.attr_main_fn = Some((id.def_id, ctxt.tcx.def_span(id.def_id.to_def_id())));
96101
} else {
97102
struct_span_err!(
98103
ctxt.tcx.sess,
99-
item.span,
104+
ctxt.tcx.def_span(id.def_id.to_def_id()),
100105
E0137,
101106
"multiple functions with a `#[main]` attribute"
102107
)
103-
.span_label(item.span, "additional `#[main]` function")
108+
.span_label(
109+
ctxt.tcx.def_span(id.def_id.to_def_id()),
110+
"additional `#[main]` function",
111+
)
104112
.span_label(ctxt.attr_main_fn.unwrap().1, "first `#[main]` function")
105113
.emit();
106114
}
107115
}
108116
EntryPointType::Start => {
109117
if ctxt.start_fn.is_none() {
110-
ctxt.start_fn = Some((item.def_id, item.span));
118+
ctxt.start_fn = Some((id.def_id, ctxt.tcx.def_span(id.def_id.to_def_id())));
111119
} else {
112-
struct_span_err!(ctxt.tcx.sess, item.span, E0138, "multiple `start` functions")
113-
.span_label(ctxt.start_fn.unwrap().1, "previous `#[start]` function here")
114-
.span_label(item.span, "multiple `start` functions")
115-
.emit();
120+
struct_span_err!(
121+
ctxt.tcx.sess,
122+
ctxt.tcx.def_span(id.def_id.to_def_id()),
123+
E0138,
124+
"multiple `start` functions"
125+
)
126+
.span_label(ctxt.start_fn.unwrap().1, "previous `#[start]` function here")
127+
.span_label(ctxt.tcx.def_span(id.def_id.to_def_id()), "multiple `start` functions")
128+
.emit();
116129
}
117130
}
118131
}

src/test/ui/error-codes/E0138.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ error[E0138]: multiple `start` functions
22
--> $DIR/E0138.rs:7:1
33
|
44
LL | fn foo(argc: isize, argv: *const *const u8) -> isize { 0 }
5-
| ---------------------------------------------------------- previous `#[start]` function here
5+
| ---------------------------------------------------- previous `#[start]` function here
66
...
77
LL | fn f(argc: isize, argv: *const *const u8) -> isize { 0 }
8-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ multiple `start` functions
8+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ multiple `start` functions
99

1010
error: aborting due to previous error
1111

src/test/ui/main-wrong-location.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ note: here is a function named `main`
88
--> $DIR/main-wrong-location.rs:4:5
99
|
1010
LL | fn main() { }
11-
| ^^^^^^^^^^^^^
11+
| ^^^^^^^^^
1212
= note: you have one or more functions named `main` not defined at the crate level
1313
= help: consider moving the `main` function definitions
1414

0 commit comments

Comments
 (0)