Skip to content

Commit 84c2804

Browse files
authored
Rollup merge of #93753 - jeremyBanks:main-conflict, r=petrochenkov
Complete removal of #[main] attribute from compiler resolves #93786 --- The `#[main]` attribute was mostly removed from the language in #84217, but not completely. It is still recognized as a builtin attribute by the compiler, but it has no effect. However, this no-op attribute is no longer gated by `#[feature(main)]` (which no longer exists), so it's possible to include it in code *on stable* without any errors, which seems unintentional. For example, the following code is accepted ([playground link](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&code=%23%5Bmain%5D%0Afn%20main()%20%7B%0A%20%20%20%20println!(%22hello%20world%22)%3B%0A%7D%0A)). ```rust #[main] fn main() { println!("hello world"); } ``` Aside from that oddity, the existence of this attribute causes code like the following to fail ([playground link](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&code=use%20tokio%3A%3Amain%3B%0A%0A%23%5Bmain%5D%0Afn%20main()%20%7B%0A%20%20%20%20println!(%22hello%20world%22)%3B%0A%7D%0A)). According #84062 (comment), the removal of `#[main]` was expected to eliminate this conflict (previously reported as #62127). ```rust use tokio::main; #[main] fn main() { println!("hello world"); } ``` ``` error[E0659]: `main` is ambiguous --> src/main.rs:3:3 | 3 | #[main] | ^^^^ ambiguous name | = note: ambiguous because of a name conflict with a builtin attribute = note: `main` could refer to a built-in attribute ``` [This error message can be confusing](https://stackoverflow.com/q/71024443/1114), as the mostly-removed `#[main]` attribute is not mentioned in any documentation. Since the current availability of `#[main]` on stable seems unintentional, and to needlessly block use of the `main` identifier in the attribute namespace, this PR finishes removing the `#[main]` attribute as described in #29634 (comment) by deleting it from `builtin_attrs.rs`, and adds two test cases to ensure that the attribute is no longer accepted and no longer conflicts with other attributes imported as `main`.
2 parents 6d40850 + 475e4ee commit 84c2804

File tree

5 files changed

+35
-1
lines changed

5 files changed

+35
-1
lines changed

compiler/rustc_feature/src/builtin_attrs.rs

-1
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,6 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
339339
),
340340

341341
// Entry point:
342-
ungated!(main, Normal, template!(Word), WarnFollowing),
343342
ungated!(start, Normal, template!(Word), WarnFollowing),
344343
ungated!(no_start, CrateLevel, template!(Word), WarnFollowing),
345344
ungated!(no_main, CrateLevel, template!(Word), WarnFollowing),
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#[main] //~ ERROR cannot find attribute `main` in this scope
2+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: cannot find attribute `main` in this scope
2+
--> $DIR/main-removed-1.rs:1:3
3+
|
4+
LL | #[main]
5+
| ^^^^
6+
|
7+
= note: `main` is in scope, but it is a function, not an attribute
8+
9+
error: aborting due to previous error
10+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// force-host
2+
// no-prefer-dynamic
3+
4+
#![crate_type = "proc-macro"]
5+
6+
extern crate proc_macro;
7+
use proc_macro::TokenStream;
8+
9+
#[proc_macro_attribute]
10+
pub fn main(_: TokenStream, input: TokenStream) -> TokenStream {
11+
"fn main() { println!(\"Hello Tokyo!\"); }".parse().unwrap()
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// run-pass
2+
// aux-build:tokyo.rs
3+
// compile-flags:--extern tokyo
4+
// edition:2021
5+
6+
use tokyo::main;
7+
8+
#[main]
9+
fn main() {
10+
panic!("the #[main] macro should replace this with non-panicking code")
11+
}

0 commit comments

Comments
 (0)