Skip to content

Commit 79ef4ef

Browse files
authored
Unrolled build for #147682
Rollup merge of #147682 - jdonszelmann:convert-rustc-main, r=JonathanBrouwer convert `rustc_main` to the new attribute parsing infrastructure r? ``@JonathanBrouwer``
2 parents 844264a + 047c37c commit 79ef4ef

File tree

12 files changed

+74
-97
lines changed

12 files changed

+74
-97
lines changed

compiler/rustc_ast/src/entry.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
use rustc_span::{Symbol, sym};
22

3-
use crate::attr::{self, AttributeExt};
4-
53
#[derive(Debug)]
64
pub enum EntryPointType {
75
/// This function is not an entrypoint.
@@ -30,11 +28,11 @@ pub enum EntryPointType {
3028
}
3129

3230
pub fn entry_point_type(
33-
attrs: &[impl AttributeExt],
31+
has_rustc_main: bool,
3432
at_root: bool,
3533
name: Option<Symbol>,
3634
) -> EntryPointType {
37-
if attr::contains_name(attrs, sym::rustc_main) {
35+
if has_rustc_main {
3836
EntryPointType::RustcMainAttr
3937
} else if let Some(name) = name
4038
&& name == sym::main

compiler/rustc_attr_parsing/src/attributes/rustc_internal.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
use super::prelude::*;
22
use super::util::parse_single_integer;
33

4-
pub(crate) struct RustcLayoutScalarValidRangeStart;
4+
pub(crate) struct RustcMainParser;
55

6-
impl<S: Stage> SingleAttributeParser<S> for RustcLayoutScalarValidRangeStart {
6+
impl<S: Stage> NoArgsAttributeParser<S> for RustcMainParser {
7+
const PATH: &'static [Symbol] = &[sym::rustc_main];
8+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
9+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Fn)]);
10+
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcMain;
11+
}
12+
13+
pub(crate) struct RustcLayoutScalarValidRangeStartParser;
14+
15+
impl<S: Stage> SingleAttributeParser<S> for RustcLayoutScalarValidRangeStartParser {
716
const PATH: &'static [Symbol] = &[sym::rustc_layout_scalar_valid_range_start];
817
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepInnermost;
918
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
@@ -16,9 +25,9 @@ impl<S: Stage> SingleAttributeParser<S> for RustcLayoutScalarValidRangeStart {
1625
}
1726
}
1827

19-
pub(crate) struct RustcLayoutScalarValidRangeEnd;
28+
pub(crate) struct RustcLayoutScalarValidRangeEndParser;
2029

21-
impl<S: Stage> SingleAttributeParser<S> for RustcLayoutScalarValidRangeEnd {
30+
impl<S: Stage> SingleAttributeParser<S> for RustcLayoutScalarValidRangeEndParser {
2231
const PATH: &'static [Symbol] = &[sym::rustc_layout_scalar_valid_range_end];
2332
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepInnermost;
2433
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ use crate::attributes::proc_macro_attrs::{
5353
use crate::attributes::prototype::CustomMirParser;
5454
use crate::attributes::repr::{AlignParser, AlignStaticParser, ReprParser};
5555
use crate::attributes::rustc_internal::{
56-
RustcLayoutScalarValidRangeEnd, RustcLayoutScalarValidRangeStart,
56+
RustcLayoutScalarValidRangeEndParser, RustcLayoutScalarValidRangeStartParser, RustcMainParser,
5757
RustcObjectLifetimeDefaultParser, RustcSimdMonomorphizeLaneLimitParser,
5858
};
5959
use crate::attributes::semantics::MayDangleParser;
@@ -197,8 +197,8 @@ attribute_parsers!(
197197
Single<RecursionLimitParser>,
198198
Single<RustcBuiltinMacroParser>,
199199
Single<RustcForceInlineParser>,
200-
Single<RustcLayoutScalarValidRangeEnd>,
201-
Single<RustcLayoutScalarValidRangeStart>,
200+
Single<RustcLayoutScalarValidRangeEndParser>,
201+
Single<RustcLayoutScalarValidRangeStartParser>,
202202
Single<RustcObjectLifetimeDefaultParser>,
203203
Single<RustcSimdMonomorphizeLaneLimitParser>,
204204
Single<SanitizeParser>,
@@ -238,6 +238,7 @@ attribute_parsers!(
238238
Single<WithoutArgs<ProcMacroParser>>,
239239
Single<WithoutArgs<PubTransparentParser>>,
240240
Single<WithoutArgs<RustcCoherenceIsCoreParser>>,
241+
Single<WithoutArgs<RustcMainParser>>,
241242
Single<WithoutArgs<SpecializationTraitParser>>,
242243
Single<WithoutArgs<StdInternalSymbolParser>>,
243244
Single<WithoutArgs<TrackCallerParser>>,

compiler/rustc_builtin_macros/src/test_harness.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use std::mem;
44

55
use rustc_ast as ast;
6+
use rustc_ast::attr::contains_name;
67
use rustc_ast::entry::EntryPointType;
78
use rustc_ast::mut_visit::*;
89
use rustc_ast::visit::Visitor;
@@ -172,9 +173,11 @@ impl<'a> Visitor<'a> for InnerItemLinter<'_> {
172173

173174
fn entry_point_type(item: &ast::Item, at_root: bool) -> EntryPointType {
174175
match &item.kind {
175-
ast::ItemKind::Fn(fn_) => {
176-
rustc_ast::entry::entry_point_type(&item.attrs, at_root, Some(fn_.ident.name))
177-
}
176+
ast::ItemKind::Fn(fn_) => rustc_ast::entry::entry_point_type(
177+
contains_name(&item.attrs, sym::rustc_main),
178+
at_root,
179+
Some(fn_.ident.name),
180+
),
178181
_ => EntryPointType::None,
179182
}
180183
}

compiler/rustc_hir/src/attrs/data_structures.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,9 @@ pub enum AttributeKind {
670670
/// Represents `#[rustc_layout_scalar_valid_range_start]`.
671671
RustcLayoutScalarValidRangeStart(Box<u128>, Span),
672672

673+
/// Represents `#[rustc_main]`.
674+
RustcMain,
675+
673676
/// Represents `#[rustc_object_lifetime_default]`.
674677
RustcObjectLifetimeDefault,
675678

compiler/rustc_hir/src/attrs/encode_cross_crate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ impl AttributeKind {
8888
RustcCoherenceIsCore(..) => No,
8989
RustcLayoutScalarValidRangeEnd(..) => Yes,
9090
RustcLayoutScalarValidRangeStart(..) => Yes,
91+
RustcMain => No,
9192
RustcObjectLifetimeDefault => No,
9293
RustcSimdMonomorphizeLaneLimit(..) => Yes, // Affects layout computation, which needs to work cross-crate
9394
Sanitize { .. } => No,

compiler/rustc_passes/messages.ftl

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@ passes_attr_crate_level =
3434
.suggestion = to apply to the crate, use an inner attribute
3535
.note = read <https://doc.rust-lang.org/nightly/rustdoc/the-doc-attribute.html#at-the-crate-level> for more information
3636
37-
passes_attr_only_in_functions =
38-
`{$attr}` attribute can only be used on functions
39-
4037
passes_autodiff_attr =
4138
`#[autodiff]` should be applied to a function
4239
.label = not a function

compiler/rustc_passes/src/check_attr.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
283283
| AttributeKind::ObjcSelector { .. }
284284
| AttributeKind::RustcCoherenceIsCore(..)
285285
| AttributeKind::DebuggerVisualizer(..)
286+
| AttributeKind::RustcMain,
286287
) => { /* do nothing */ }
287288
Attribute::Unparsed(attr_item) => {
288289
style = Some(attr_item.style);
@@ -2397,14 +2398,8 @@ fn check_invalid_crate_level_attr(tcx: TyCtxt<'_>, attrs: &[Attribute]) {
23972398
// Check for builtin attributes at the crate level
23982399
// which were unsuccessfully resolved due to cannot determine
23992400
// resolution for the attribute macro error.
2400-
const ATTRS_TO_CHECK: &[Symbol] = &[
2401-
sym::rustc_main,
2402-
sym::derive,
2403-
sym::test,
2404-
sym::test_case,
2405-
sym::global_allocator,
2406-
sym::bench,
2407-
];
2401+
const ATTRS_TO_CHECK: &[Symbol] =
2402+
&[sym::derive, sym::test, sym::test_case, sym::global_allocator, sym::bench];
24082403

24092404
for attr in attrs {
24102405
// FIXME(jdonszelmann): all attrs should be combined here cleaning this up some day.

compiler/rustc_passes/src/entry.rs

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
use rustc_ast::attr;
22
use rustc_ast::entry::EntryPointType;
33
use rustc_errors::codes::*;
4-
use rustc_hir::def::DefKind;
4+
use rustc_hir::attrs::AttributeKind;
55
use rustc_hir::def_id::{CRATE_DEF_ID, DefId, LOCAL_CRATE, LocalDefId};
6-
use rustc_hir::{CRATE_HIR_ID, ItemId, Node};
6+
use rustc_hir::{CRATE_HIR_ID, ItemId, Node, find_attr};
77
use rustc_middle::query::Providers;
88
use rustc_middle::ty::TyCtxt;
99
use rustc_session::RemapFileNameExt;
1010
use rustc_session::config::{CrateType, EntryFnType, RemapPathScopeComponents, sigpipe};
11-
use rustc_span::{Span, Symbol, sym};
11+
use rustc_span::{Span, sym};
1212

13-
use crate::errors::{AttrOnlyInFunctions, ExternMain, MultipleRustcMain, NoMainErr};
13+
use crate::errors::{ExternMain, MultipleRustcMain, NoMainErr};
1414

1515
struct EntryContext<'tcx> {
1616
tcx: TyCtxt<'tcx>,
@@ -44,26 +44,12 @@ fn entry_fn(tcx: TyCtxt<'_>, (): ()) -> Option<(DefId, EntryFnType)> {
4444
configure_main(tcx, &ctxt)
4545
}
4646

47-
fn attr_span_by_symbol(ctxt: &EntryContext<'_>, id: ItemId, sym: Symbol) -> Option<Span> {
48-
let attrs = ctxt.tcx.hir_attrs(id.hir_id());
49-
attr::find_by_name(attrs, sym).map(|attr| attr.span())
50-
}
51-
5247
fn check_and_search_item(id: ItemId, ctxt: &mut EntryContext<'_>) {
53-
if !matches!(ctxt.tcx.def_kind(id.owner_id), DefKind::Fn) {
54-
for attr in [sym::rustc_main] {
55-
if let Some(span) = attr_span_by_symbol(ctxt, id, attr) {
56-
ctxt.tcx.dcx().emit_err(AttrOnlyInFunctions { span, attr });
57-
}
58-
}
59-
return;
60-
}
61-
6248
let at_root = ctxt.tcx.opt_local_parent(id.owner_id.def_id) == Some(CRATE_DEF_ID);
6349

6450
let attrs = ctxt.tcx.hir_attrs(id.hir_id());
6551
let entry_point_type = rustc_ast::entry::entry_point_type(
66-
attrs,
52+
find_attr!(attrs, AttributeKind::RustcMain),
6753
at_root,
6854
ctxt.tcx.opt_item_name(id.owner_id.to_def_id()),
6955
);

compiler/rustc_passes/src/errors.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -843,14 +843,6 @@ pub(crate) struct FeaturePreviouslyDeclared<'a> {
843843
pub prev_declared: &'a str,
844844
}
845845

846-
#[derive(Diagnostic)]
847-
#[diag(passes_attr_only_in_functions)]
848-
pub(crate) struct AttrOnlyInFunctions {
849-
#[primary_span]
850-
pub span: Span,
851-
pub attr: Symbol,
852-
}
853-
854846
#[derive(Diagnostic)]
855847
#[diag(passes_multiple_rustc_main, code = E0137)]
856848
pub(crate) struct MultipleRustcMain {

0 commit comments

Comments
 (0)