Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit d4bc912

Browse files
committed
Auto merge of rust-lang#84217 - crlf0710:remove_main_attr_pure, r=petrochenkov
Remove #[main] attribute. This removes the #[main] attribute support from the compiler according to the decisions within rust-lang#29634. For existing use cases within test harness generation, replaced it with a newly-introduced internal attribute `#[rustc_main]`. This is first part extracted from rust-lang#84062 . Closes rust-lang#29634. r? `@petrochenkov`
2 parents 710da44 + fc35703 commit d4bc912

29 files changed

+59
-214
lines changed

compiler/rustc_ast_passes/src/feature_gate.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -366,16 +366,6 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
366366
over time"
367367
);
368368
}
369-
if self.sess.contains_name(&i.attrs[..], sym::main) {
370-
gate_feature_post!(
371-
&self,
372-
main,
373-
i.span,
374-
"declaration of a non-standard `#[main]` \
375-
function may change over time, for now \
376-
a top-level `fn main()` is required"
377-
);
378-
}
379369
}
380370

381371
ast::ItemKind::Struct(..) => {

compiler/rustc_builtin_macros/src/test_harness.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ fn entry_point_type(sess: &Session, item: &ast::Item, depth: usize) -> EntryPoin
142142
ast::ItemKind::Fn(..) => {
143143
if sess.contains_name(&item.attrs, sym::start) {
144144
EntryPointType::Start
145-
} else if sess.contains_name(&item.attrs, sym::main) {
145+
} else if sess.contains_name(&item.attrs, sym::rustc_main) {
146146
EntryPointType::MainAttr
147147
} else if item.ident.name == sym::main {
148148
if depth == 1 {
@@ -187,7 +187,7 @@ impl<'a> MutVisitor for EntryPointCleaner<'a> {
187187
let attrs = attrs
188188
.into_iter()
189189
.filter(|attr| {
190-
!self.sess.check_name(attr, sym::main)
190+
!self.sess.check_name(attr, sym::rustc_main)
191191
&& !self.sess.check_name(attr, sym::start)
192192
})
193193
.chain(iter::once(allow_dead_code))
@@ -220,7 +220,7 @@ fn generate_test_harness(
220220
let expn_id = ext_cx.resolver.expansion_for_ast_pass(
221221
DUMMY_SP,
222222
AstPass::TestHarness,
223-
&[sym::main, sym::test, sym::rustc_attrs],
223+
&[sym::test, sym::rustc_attrs],
224224
None,
225225
);
226226
let def_site = DUMMY_SP.with_def_site_ctxt(expn_id);
@@ -247,7 +247,7 @@ fn generate_test_harness(
247247
/// By default this expands to
248248
///
249249
/// ```
250-
/// #[main]
250+
/// #[rustc_main]
251251
/// pub fn main() {
252252
/// extern crate test;
253253
/// test::test_main_static(&[
@@ -297,8 +297,8 @@ fn mk_main(cx: &mut TestCtxt<'_>) -> P<ast::Item> {
297297
let test_extern_stmt =
298298
ecx.stmt_item(sp, ecx.item(sp, test_id, vec![], ast::ItemKind::ExternCrate(None)));
299299

300-
// #[main]
301-
let main_meta = ecx.meta_word(sp, sym::main);
300+
// #[rustc_main]
301+
let main_meta = ecx.meta_word(sp, sym::rustc_main);
302302
let main_attr = ecx.attribute(main_meta);
303303

304304
// pub fn main() { ... }

compiler/rustc_error_codes/src/error_codes/E0137.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
#### Note: this error code is no longer emitted by the compiler.
2+
13
More than one function was declared with the `#[main]` attribute.
24

35
Erroneous code example:
46

5-
```compile_fail,E0137
7+
```compile_fail
68
#![feature(main)]
79
810
#[main]
@@ -16,7 +18,7 @@ This error indicates that the compiler found multiple functions with the
1618
`#[main]` attribute. This is an error because there must be a unique entry
1719
point into a Rust program. Example:
1820

19-
```
21+
```compile_fail
2022
#![feature(main)]
2123
2224
#[main]

compiler/rustc_feature/src/active.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,6 @@ declare_features! (
134134
/// Allows using the `box $expr` syntax.
135135
(active, box_syntax, "1.0.0", Some(49733), None),
136136

137-
/// Allows using `#[main]` to replace the entrypoint `#[lang = "start"]` calls.
138-
(active, main, "1.0.0", Some(29634), None),
139-
140137
/// Allows using `#[start]` on a function indicating that it is the program entrypoint.
141138
(active, start, "1.0.0", Some(29633), None),
142139

compiler/rustc_feature/src/builtin_attrs.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,10 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
536536
rustc_specialization_trait, Normal, template!(Word),
537537
"the `#[rustc_specialization_trait]` attribute is used to check specializations"
538538
),
539+
rustc_attr!(
540+
rustc_main, Normal, template!(Word),
541+
"the `#[rustc_main]` attribute is used internally to specify test entry point function",
542+
),
539543

540544
// ==========================================================================
541545
// Internal attributes, Testing:

compiler/rustc_feature/src/removed.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ declare_features! (
132132
(removed, link_args, "1.53.0", Some(29596), None,
133133
Some("removed in favor of using `-C link-arg=ARG` on command line, \
134134
which is available from cargo build scripts with `cargo:rustc-link-arg` now")),
135+
/// Allows using `#[main]` to replace the entrypoint `#[lang = "start"]` calls.
136+
(removed, main, "1.53.0", Some(29634), None, None),
135137

136138
// -------------------------------------------------------------------------
137139
// feature-group-end: removed features

compiler/rustc_passes/src/check_attr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1489,7 +1489,7 @@ fn check_invalid_crate_level_attr(tcx: TyCtxt<'_>, attrs: &[Attribute]) {
14891489
sym::path,
14901490
sym::automatically_derived,
14911491
sym::start,
1492-
sym::main,
1492+
sym::rustc_main,
14931493
];
14941494

14951495
for attr in attrs {

compiler/rustc_passes/src/entry.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ fn entry_point_type(ctxt: &EntryContext<'_, '_>, item: &Item<'_>, at_root: bool)
8484
let attrs = ctxt.map.attrs(item.hir_id());
8585
if ctxt.session.contains_name(attrs, sym::start) {
8686
EntryPointType::Start
87-
} else if ctxt.session.contains_name(attrs, sym::main) {
87+
} else if ctxt.session.contains_name(attrs, sym::rustc_main) {
8888
EntryPointType::MainAttr
8989
} else if item.ident.name == sym::main {
9090
if at_root {
@@ -111,8 +111,8 @@ fn find_item(item: &Item<'_>, ctxt: &mut EntryContext<'_, '_>, at_root: bool) {
111111
if let Some(attr) = ctxt.session.find_by_name(attrs, sym::start) {
112112
throw_attr_err(&ctxt.session, attr.span, "start");
113113
}
114-
if let Some(attr) = ctxt.session.find_by_name(attrs, sym::main) {
115-
throw_attr_err(&ctxt.session, attr.span, "main");
114+
if let Some(attr) = ctxt.session.find_by_name(attrs, sym::rustc_main) {
115+
throw_attr_err(&ctxt.session, attr.span, "rustc_main");
116116
}
117117
}
118118
EntryPointType::MainNamed => {
@@ -193,10 +193,7 @@ fn no_main_err(tcx: TyCtxt<'_>, visitor: &EntryContext<'_, '_>) {
193193
err.span_note(span, "here is a function named `main`");
194194
}
195195
err.note("you have one or more functions named `main` not defined at the crate level");
196-
err.help(
197-
"either move the `main` function definitions or attach the `#[main]` attribute \
198-
to one of them",
199-
);
196+
err.help("consider moving the `main` function definitions");
200197
// There were some functions named `main` though. Try to give the user a hint.
201198
format!(
202199
"the main function must be defined at the crate level{}",

compiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,6 +1008,7 @@ symbols! {
10081008
rustc_layout_scalar_valid_range_start,
10091009
rustc_legacy_const_generics,
10101010
rustc_macro_transparency,
1011+
rustc_main,
10111012
rustc_mir,
10121013
rustc_nonnull_optimization_guaranteed,
10131014
rustc_object_lifetime_default,

src/test/ui/attr-main-2.rs

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)