Skip to content

Commit

Permalink
Auto merge of rust-lang#84217 - crlf0710:remove_main_attr_pure, r=pet…
Browse files Browse the repository at this point in the history
…rochenkov

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`
  • Loading branch information
bors committed Apr 16, 2021
2 parents 710da44 + fc35703 commit d4bc912
Show file tree
Hide file tree
Showing 29 changed files with 59 additions and 214 deletions.
10 changes: 0 additions & 10 deletions compiler/rustc_ast_passes/src/feature_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,16 +366,6 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
over time"
);
}
if self.sess.contains_name(&i.attrs[..], sym::main) {
gate_feature_post!(
&self,
main,
i.span,
"declaration of a non-standard `#[main]` \
function may change over time, for now \
a top-level `fn main()` is required"
);
}
}

ast::ItemKind::Struct(..) => {
Expand Down
12 changes: 6 additions & 6 deletions compiler/rustc_builtin_macros/src/test_harness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ fn entry_point_type(sess: &Session, item: &ast::Item, depth: usize) -> EntryPoin
ast::ItemKind::Fn(..) => {
if sess.contains_name(&item.attrs, sym::start) {
EntryPointType::Start
} else if sess.contains_name(&item.attrs, sym::main) {
} else if sess.contains_name(&item.attrs, sym::rustc_main) {
EntryPointType::MainAttr
} else if item.ident.name == sym::main {
if depth == 1 {
Expand Down Expand Up @@ -187,7 +187,7 @@ impl<'a> MutVisitor for EntryPointCleaner<'a> {
let attrs = attrs
.into_iter()
.filter(|attr| {
!self.sess.check_name(attr, sym::main)
!self.sess.check_name(attr, sym::rustc_main)
&& !self.sess.check_name(attr, sym::start)
})
.chain(iter::once(allow_dead_code))
Expand Down Expand Up @@ -220,7 +220,7 @@ fn generate_test_harness(
let expn_id = ext_cx.resolver.expansion_for_ast_pass(
DUMMY_SP,
AstPass::TestHarness,
&[sym::main, sym::test, sym::rustc_attrs],
&[sym::test, sym::rustc_attrs],
None,
);
let def_site = DUMMY_SP.with_def_site_ctxt(expn_id);
Expand All @@ -247,7 +247,7 @@ fn generate_test_harness(
/// By default this expands to
///
/// ```
/// #[main]
/// #[rustc_main]
/// pub fn main() {
/// extern crate test;
/// test::test_main_static(&[
Expand Down Expand Up @@ -297,8 +297,8 @@ fn mk_main(cx: &mut TestCtxt<'_>) -> P<ast::Item> {
let test_extern_stmt =
ecx.stmt_item(sp, ecx.item(sp, test_id, vec![], ast::ItemKind::ExternCrate(None)));

// #[main]
let main_meta = ecx.meta_word(sp, sym::main);
// #[rustc_main]
let main_meta = ecx.meta_word(sp, sym::rustc_main);
let main_attr = ecx.attribute(main_meta);

// pub fn main() { ... }
Expand Down
6 changes: 4 additions & 2 deletions compiler/rustc_error_codes/src/error_codes/E0137.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#### Note: this error code is no longer emitted by the compiler.

More than one function was declared with the `#[main]` attribute.

Erroneous code example:

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

```
```compile_fail
#![feature(main)]
#[main]
Expand Down
3 changes: 0 additions & 3 deletions compiler/rustc_feature/src/active.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,6 @@ declare_features! (
/// Allows using the `box $expr` syntax.
(active, box_syntax, "1.0.0", Some(49733), None),

/// Allows using `#[main]` to replace the entrypoint `#[lang = "start"]` calls.
(active, main, "1.0.0", Some(29634), None),

/// Allows using `#[start]` on a function indicating that it is the program entrypoint.
(active, start, "1.0.0", Some(29633), None),

Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_feature/src/builtin_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,10 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
rustc_specialization_trait, Normal, template!(Word),
"the `#[rustc_specialization_trait]` attribute is used to check specializations"
),
rustc_attr!(
rustc_main, Normal, template!(Word),
"the `#[rustc_main]` attribute is used internally to specify test entry point function",
),

// ==========================================================================
// Internal attributes, Testing:
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_feature/src/removed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ declare_features! (
(removed, link_args, "1.53.0", Some(29596), None,
Some("removed in favor of using `-C link-arg=ARG` on command line, \
which is available from cargo build scripts with `cargo:rustc-link-arg` now")),
/// Allows using `#[main]` to replace the entrypoint `#[lang = "start"]` calls.
(removed, main, "1.53.0", Some(29634), None, None),

// -------------------------------------------------------------------------
// feature-group-end: removed features
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_passes/src/check_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1489,7 +1489,7 @@ fn check_invalid_crate_level_attr(tcx: TyCtxt<'_>, attrs: &[Attribute]) {
sym::path,
sym::automatically_derived,
sym::start,
sym::main,
sym::rustc_main,
];

for attr in attrs {
Expand Down
11 changes: 4 additions & 7 deletions compiler/rustc_passes/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ fn entry_point_type(ctxt: &EntryContext<'_, '_>, item: &Item<'_>, at_root: bool)
let attrs = ctxt.map.attrs(item.hir_id());
if ctxt.session.contains_name(attrs, sym::start) {
EntryPointType::Start
} else if ctxt.session.contains_name(attrs, sym::main) {
} else if ctxt.session.contains_name(attrs, sym::rustc_main) {
EntryPointType::MainAttr
} else if item.ident.name == sym::main {
if at_root {
Expand All @@ -111,8 +111,8 @@ fn find_item(item: &Item<'_>, ctxt: &mut EntryContext<'_, '_>, at_root: bool) {
if let Some(attr) = ctxt.session.find_by_name(attrs, sym::start) {
throw_attr_err(&ctxt.session, attr.span, "start");
}
if let Some(attr) = ctxt.session.find_by_name(attrs, sym::main) {
throw_attr_err(&ctxt.session, attr.span, "main");
if let Some(attr) = ctxt.session.find_by_name(attrs, sym::rustc_main) {
throw_attr_err(&ctxt.session, attr.span, "rustc_main");
}
}
EntryPointType::MainNamed => {
Expand Down Expand Up @@ -193,10 +193,7 @@ fn no_main_err(tcx: TyCtxt<'_>, visitor: &EntryContext<'_, '_>) {
err.span_note(span, "here is a function named `main`");
}
err.note("you have one or more functions named `main` not defined at the crate level");
err.help(
"either move the `main` function definitions or attach the `#[main]` attribute \
to one of them",
);
err.help("consider moving the `main` function definitions");
// There were some functions named `main` though. Try to give the user a hint.
format!(
"the main function must be defined at the crate level{}",
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1008,6 +1008,7 @@ symbols! {
rustc_layout_scalar_valid_range_start,
rustc_legacy_const_generics,
rustc_macro_transparency,
rustc_main,
rustc_mir,
rustc_nonnull_optimization_guaranteed,
rustc_object_lifetime_default,
Expand Down
11 changes: 0 additions & 11 deletions src/test/ui/attr-main-2.rs

This file was deleted.

8 changes: 0 additions & 8 deletions src/test/ui/attr-main.rs

This file was deleted.

8 changes: 0 additions & 8 deletions src/test/ui/attr.rs

This file was deleted.

8 changes: 0 additions & 8 deletions src/test/ui/error-codes/E0137.rs

This file was deleted.

12 changes: 0 additions & 12 deletions src/test/ui/error-codes/E0137.stderr

This file was deleted.

2 changes: 0 additions & 2 deletions src/test/ui/feature-gates/feature-gate-main.rs

This file was deleted.

12 changes: 0 additions & 12 deletions src/test/ui/feature-gates/feature-gate-main.stderr

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

#![macro_export]
//~^ ERROR: `macro_export` attribute cannot be used at crate level
#![main]
//~^ ERROR: `main` attribute cannot be used at crate level
#![rustc_main] //~ ERROR: the `#[rustc_main]` attribute is used internally to specify
//~^ ERROR: `rustc_main` attribute cannot be used at crate level
#![start]
//~^ ERROR: `start` attribute cannot be used at crate level
#![repr()]
Expand Down Expand Up @@ -106,24 +106,6 @@ mod export_name {
//~| NOTE not a function or static
}

#[main]
//~^ ERROR: `main` attribute can only be used on functions
mod main {
mod inner { #![main] }
//~^ ERROR: `main` attribute can only be used on functions

// for `fn f()` case, see feature-gate-main.rs

#[main] struct S;
//~^ ERROR: `main` attribute can only be used on functions

#[main] type T = S;
//~^ ERROR: `main` attribute can only be used on functions

#[main] impl S { }
//~^ ERROR: `main` attribute can only be used on functions
}

#[start]
//~^ ERROR: `start` attribute can only be used on functions
mod start {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
error[E0658]: the `#[rustc_main]` attribute is used internally to specify test entry point function
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:14:1
|
LL | #![rustc_main]
| ^^^^^^^^^^^^^^
|
= help: add `#![feature(rustc_attrs)]` to the crate attributes to enable

error: attribute must be of the form `#[inline]` or `#[inline(always|never)]`
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:40:5
|
Expand All @@ -8,62 +16,32 @@ LL | #[inline = "2100"] fn f() { }
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #57571 <https://github.com/rust-lang/rust/issues/57571>

error: `main` attribute can only be used on functions
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:109:1
|
LL | #[main]
| ^^^^^^^

error: `main` attribute can only be used on functions
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:112:17
|
LL | mod inner { #![main] }
| ^^^^^^^^

error: `main` attribute can only be used on functions
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:117:5
|
LL | #[main] struct S;
| ^^^^^^^

error: `main` attribute can only be used on functions
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:120:5
|
LL | #[main] type T = S;
| ^^^^^^^

error: `main` attribute can only be used on functions
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:123:5
|
LL | #[main] impl S { }
| ^^^^^^^

error: `start` attribute can only be used on functions
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:127:1
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:109:1
|
LL | #[start]
| ^^^^^^^^

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

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

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

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

error: `main` attribute cannot be used at crate level
error: `rustc_main` attribute cannot be used at crate level
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:14:1
|
LL | #![main]
| ^^^^^^^^
LL | #![rustc_main]
| ^^^^^^^^^^^^^^

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

error: aborting due to 36 previous errors
error: aborting due to 32 previous errors

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

0 comments on commit d4bc912

Please sign in to comment.