Skip to content

Commit d4bc912

Browse files
committed
Auto merge of #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 #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 #84062 . Closes #29634. r? `@petrochenkov`
2 parents 710da44 + fc35703 commit d4bc912

29 files changed

+59
-214
lines changed

compiler/rustc_ast_passes/src/feature_gate.rs

-10
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

+6-6
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

+4-2
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

-3
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

+4
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

+2
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

+1-1
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

+4-7
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

+1
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

-11
This file was deleted.

src/test/ui/attr-main.rs

-8
This file was deleted.

src/test/ui/attr.rs

-8
This file was deleted.

src/test/ui/error-codes/E0137.rs

-8
This file was deleted.

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

-12
This file was deleted.

src/test/ui/feature-gates/feature-gate-main.rs

-2
This file was deleted.

src/test/ui/feature-gates/feature-gate-main.stderr

-12
This file was deleted.

src/test/ui/feature-gates/issue-43106-gating-of-builtin-attrs-error.rs

+2-20
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111

1212
#![macro_export]
1313
//~^ ERROR: `macro_export` attribute cannot be used at crate level
14-
#![main]
15-
//~^ ERROR: `main` attribute cannot be used at crate level
14+
#![rustc_main] //~ ERROR: the `#[rustc_main]` attribute is used internally to specify
15+
//~^ ERROR: `rustc_main` attribute cannot be used at crate level
1616
#![start]
1717
//~^ ERROR: `start` attribute cannot be used at crate level
1818
#![repr()]
@@ -106,24 +106,6 @@ mod export_name {
106106
//~| NOTE not a function or static
107107
}
108108

109-
#[main]
110-
//~^ ERROR: `main` attribute can only be used on functions
111-
mod main {
112-
mod inner { #![main] }
113-
//~^ ERROR: `main` attribute can only be used on functions
114-
115-
// for `fn f()` case, see feature-gate-main.rs
116-
117-
#[main] struct S;
118-
//~^ ERROR: `main` attribute can only be used on functions
119-
120-
#[main] type T = S;
121-
//~^ ERROR: `main` attribute can only be used on functions
122-
123-
#[main] impl S { }
124-
//~^ ERROR: `main` attribute can only be used on functions
125-
}
126-
127109
#[start]
128110
//~^ ERROR: `start` attribute can only be used on functions
129111
mod start {

src/test/ui/feature-gates/issue-43106-gating-of-builtin-attrs-error.stderr

+19-40
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
error[E0658]: the `#[rustc_main]` attribute is used internally to specify test entry point function
2+
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:14:1
3+
|
4+
LL | #![rustc_main]
5+
| ^^^^^^^^^^^^^^
6+
|
7+
= help: add `#![feature(rustc_attrs)]` to the crate attributes to enable
8+
19
error: attribute must be of the form `#[inline]` or `#[inline(always|never)]`
210
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:40:5
311
|
@@ -8,62 +16,32 @@ LL | #[inline = "2100"] fn f() { }
816
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
917
= note: for more information, see issue #57571 <https://github.com/rust-lang/rust/issues/57571>
1018

11-
error: `main` attribute can only be used on functions
12-
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:109:1
13-
|
14-
LL | #[main]
15-
| ^^^^^^^
16-
17-
error: `main` attribute can only be used on functions
18-
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:112:17
19-
|
20-
LL | mod inner { #![main] }
21-
| ^^^^^^^^
22-
23-
error: `main` attribute can only be used on functions
24-
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:117:5
25-
|
26-
LL | #[main] struct S;
27-
| ^^^^^^^
28-
29-
error: `main` attribute can only be used on functions
30-
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:120:5
31-
|
32-
LL | #[main] type T = S;
33-
| ^^^^^^^
34-
35-
error: `main` attribute can only be used on functions
36-
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:123:5
37-
|
38-
LL | #[main] impl S { }
39-
| ^^^^^^^
40-
4119
error: `start` attribute can only be used on functions
42-
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:127:1
20+
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:109:1
4321
|
4422
LL | #[start]
4523
| ^^^^^^^^
4624

4725
error: `start` attribute can only be used on functions
48-
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:130:17
26+
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:112:17
4927
|
5028
LL | mod inner { #![start] }
5129
| ^^^^^^^^^
5230

5331
error: `start` attribute can only be used on functions
54-
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:135:5
32+
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:117:5
5533
|
5634
LL | #[start] struct S;
5735
| ^^^^^^^^
5836

5937
error: `start` attribute can only be used on functions
60-
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:138:5
38+
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:120:5
6139
|
6240
LL | #[start] type T = S;
6341
| ^^^^^^^^
6442

6543
error: `start` attribute can only be used on functions
66-
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:141:5
44+
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:123:5
6745
|
6846
LL | #[start] impl S { }
6947
| ^^^^^^^^
@@ -137,11 +115,11 @@ error: `macro_export` attribute cannot be used at crate level
137115
LL | #![macro_export]
138116
| ^^^^^^^^^^^^^^^^
139117

140-
error: `main` attribute cannot be used at crate level
118+
error: `rustc_main` attribute cannot be used at crate level
141119
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:14:1
142120
|
143-
LL | #![main]
144-
| ^^^^^^^^
121+
LL | #![rustc_main]
122+
| ^^^^^^^^^^^^^^
145123

146124
error: `start` attribute cannot be used at crate level
147125
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:16:1
@@ -245,6 +223,7 @@ error: attribute should be applied to a function or static
245223
LL | #[export_name = "2200"] impl S { }
246224
| ^^^^^^^^^^^^^^^^^^^^^^^ ---------- not a function or static
247225

248-
error: aborting due to 36 previous errors
226+
error: aborting due to 32 previous errors
249227

250-
For more information about this error, try `rustc --explain E0518`.
228+
Some errors have detailed explanations: E0518, E0658.
229+
For more information about an error, try `rustc --explain E0518`.

0 commit comments

Comments
 (0)