Skip to content

Commit 4731d4b

Browse files
committed
rustc_passes: De-duplicate more code with fn throw_attr_err()
So we can re-use the code for other attributes later.
1 parent 00ce472 commit 4731d4b

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

compiler/rustc_passes/src/entry.rs

+14-12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rustc_ast::entry::EntryPointType;
1+
use rustc_ast::{entry::EntryPointType, Attribute};
22
use rustc_errors::struct_span_err;
33
use rustc_hir::def::DefKind;
44
use rustc_hir::def_id::{DefId, LocalDefId, CRATE_DEF_ID, LOCAL_CRATE};
@@ -7,9 +7,8 @@ use rustc_middle::ty::query::Providers;
77
use rustc_middle::ty::{DefIdTree, TyCtxt};
88
use rustc_session::config::{CrateType, EntryFnType};
99
use rustc_session::parse::feature_err;
10-
use rustc_session::Session;
1110
use rustc_span::symbol::sym;
12-
use rustc_span::{Span, DUMMY_SP};
11+
use rustc_span::{Span, Symbol, DUMMY_SP};
1312

1413
struct EntryContext<'tcx> {
1514
tcx: TyCtxt<'tcx>,
@@ -72,9 +71,16 @@ fn entry_point_type(ctxt: &EntryContext<'_>, id: ItemId, at_root: bool) -> Entry
7271
}
7372
}
7473

75-
fn throw_attr_err(sess: &Session, span: Span, attr: &str) {
76-
sess.struct_span_err(span, &format!("`{}` attribute can only be used on functions", attr))
77-
.emit();
74+
fn err_if_attr_found(ctxt: &EntryContext<'_>, attrs: &[Attribute], sym: Symbol) {
75+
if let Some(attr) = ctxt.tcx.sess.find_by_name(attrs, sym) {
76+
ctxt.tcx
77+
.sess
78+
.struct_span_err(
79+
attr.span,
80+
&format!("`{}` attribute can only be used on functions", sym.as_str()),
81+
)
82+
.emit();
83+
}
7884
}
7985

8086
fn find_item(id: ItemId, ctxt: &mut EntryContext<'_>) {
@@ -84,12 +90,8 @@ fn find_item(id: ItemId, ctxt: &mut EntryContext<'_>) {
8490
EntryPointType::None => (),
8591
_ if !matches!(ctxt.tcx.def_kind(id.def_id), DefKind::Fn) => {
8692
let attrs = ctxt.tcx.hir().attrs(id.hir_id());
87-
if let Some(attr) = ctxt.tcx.sess.find_by_name(attrs, sym::start) {
88-
throw_attr_err(&ctxt.tcx.sess, attr.span, "start");
89-
}
90-
if let Some(attr) = ctxt.tcx.sess.find_by_name(attrs, sym::rustc_main) {
91-
throw_attr_err(&ctxt.tcx.sess, attr.span, "rustc_main");
92-
}
93+
err_if_attr_found(ctxt, attrs, sym::start);
94+
err_if_attr_found(ctxt, attrs, sym::rustc_main);
9395
}
9496
EntryPointType::MainNamed => (),
9597
EntryPointType::OtherMain => {

0 commit comments

Comments
 (0)