about summary refs log tree commit diff
path: root/src/test/ui/attributes
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2022-02-09 23:29:57 +0100
committerGitHub <noreply@github.com>2022-02-09 23:29:57 +0100
commit84c28041b486f229431b8c88fa24cb03e05fb70c (patch)
treed7b64ee9a30c1f4f15003ab24268105b123a7e42 /src/test/ui/attributes
parent6d40850e09966644d7c47742638fa53e4da7af1c (diff)
parent475e4eeb6557af820ee2b48f213f178eca5e11ce (diff)
downloadrust-84c28041b486f229431b8c88fa24cb03e05fb70c.tar.gz
rust-84c28041b486f229431b8c88fa24cb03e05fb70c.zip
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 https://github.com/rust-lang/rust/pull/84062#issuecomment-825038275, 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 https://github.com/rust-lang/rust/issues/29634#issuecomment-274951753 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`.
Diffstat (limited to 'src/test/ui/attributes')
-rw-r--r--src/test/ui/attributes/main-removed-1.rs2
-rw-r--r--src/test/ui/attributes/main-removed-1.stderr10
-rw-r--r--src/test/ui/attributes/main-removed-2/auxiliary/tokyo.rs12
-rw-r--r--src/test/ui/attributes/main-removed-2/main.rs11
4 files changed, 35 insertions, 0 deletions
diff --git a/src/test/ui/attributes/main-removed-1.rs b/src/test/ui/attributes/main-removed-1.rs
new file mode 100644
index 00000000000..0e887469d44
--- /dev/null
+++ b/src/test/ui/attributes/main-removed-1.rs
@@ -0,0 +1,2 @@
+#[main]  //~ ERROR cannot find attribute `main` in this scope
+fn main() {}
diff --git a/src/test/ui/attributes/main-removed-1.stderr b/src/test/ui/attributes/main-removed-1.stderr
new file mode 100644
index 00000000000..2422c5c3b62
--- /dev/null
+++ b/src/test/ui/attributes/main-removed-1.stderr
@@ -0,0 +1,10 @@
+error: cannot find attribute `main` in this scope
+  --> $DIR/main-removed-1.rs:1:3
+   |
+LL | #[main]
+   |   ^^^^
+   |
+   = note: `main` is in scope, but it is a function, not an attribute
+
+error: aborting due to previous error
+
diff --git a/src/test/ui/attributes/main-removed-2/auxiliary/tokyo.rs b/src/test/ui/attributes/main-removed-2/auxiliary/tokyo.rs
new file mode 100644
index 00000000000..196b5be2dd0
--- /dev/null
+++ b/src/test/ui/attributes/main-removed-2/auxiliary/tokyo.rs
@@ -0,0 +1,12 @@
+// force-host
+// no-prefer-dynamic
+
+#![crate_type = "proc-macro"]
+
+extern crate proc_macro;
+use proc_macro::TokenStream;
+
+#[proc_macro_attribute]
+pub fn main(_: TokenStream, input: TokenStream) -> TokenStream {
+    "fn main() { println!(\"Hello Tokyo!\"); }".parse().unwrap()
+}
diff --git a/src/test/ui/attributes/main-removed-2/main.rs b/src/test/ui/attributes/main-removed-2/main.rs
new file mode 100644
index 00000000000..e8fecf825fa
--- /dev/null
+++ b/src/test/ui/attributes/main-removed-2/main.rs
@@ -0,0 +1,11 @@
+// run-pass
+// aux-build:tokyo.rs
+// compile-flags:--extern tokyo
+// edition:2021
+
+use tokyo::main;
+
+#[main]
+fn main() {
+    panic!("the #[main] macro should replace this with non-panicking code")
+}