Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tracking issue for main feature #29634

Closed
aturon opened this issue Nov 5, 2015 · 25 comments · Fixed by #84217
Closed

Tracking issue for main feature #29634

aturon opened this issue Nov 5, 2015 · 25 comments · Fixed by #84217
Labels
C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@aturon
Copy link
Member

aturon commented Nov 5, 2015

Tracks stabilzation of the #[main] attribute, which allows an arbitrary function to be tagged as main.

Status Update

Decision was reached to remove this feature and looking for someone to implement; here is a list of instructions, feel free to reach out to @nikomatsakis for more tips!

@aturon aturon added T-lang Relevant to the language team, which will review and decide on the PR/issue. B-unstable Blocker: Implemented in the nightly compiler and unstable. labels Nov 5, 2015
@SimonSapin
Copy link
Contributor

Is there a reason to do this other than cosmetic preference for another name?

@nikomatsakis
Copy link
Contributor

@rfcbot fcp close

I propose that we just remove this feature altogether. I don't see a lot of clamoring for it. =) There are three users according to @brson's survey though -- @brson, was that just a grep that cratesio or what?

@rfcbot
Copy link

rfcbot commented Nov 11, 2016

Team member @nikomatsakis has proposed to close this. The next step is review by the rest of the tagged teams:

No concerns currently listed.

Once these reviewers reach consensus, this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

See this document for info about what commands tagged team members can give me.

@steveklabnik
Copy link
Member

The only place that comes close to this for me is osdev, and there, you use no_main rather than main, so...

@nrc
Copy link
Member

nrc commented Nov 13, 2016

We still have start or whatever for doing this do we?

@withoutboats
Copy link
Contributor

@rfcbot reviewed

@nikomatsakis
Copy link
Contributor

@nrc I guess that is slightly different? I think that #[start] configures at a higher-level than main? Perhaps @alexcrichton or @brson can expound on that.

@alexcrichton
Copy link
Member

Ah yes, we've got three entry points. I.. think this is how they work:

  • First, #[start], the receiver of int argc and char **argv. This is literally the symbol main (or what is called by that symbol generated in the compiler).
  • Next, there's #[lang = "start"]. If no #[start] exists in the crate graph then the compiler generates a main function that calls this. This functions receives argc/argv along with a third argument that is a function pointer to the #[main] function (defined below). Importantly, #[lang = "start"] can be located in a library. For example it's located in the standard library (libstd).
  • Finally, #[main], the main function for an executable. This is passed no arguments and is called by #[lang = "start"] (if it decides to). The standard library uses this to initialize itself and then call the Rust program. This, if not specified, defaults to fn main at the top.

So to answer your question, this isn't the same as #[start]. To answer your other (possibly not yet asked) question, yes we have too many entry points.

@rfcbot
Copy link

rfcbot commented Dec 21, 2016

🔔 This is now entering its final comment period, as per the review above. 🔔

psst @nikomatsakis, I wasn't able to add the final-comment-period label, please do so.

@rfcbot
Copy link

rfcbot commented Jan 23, 2017

The final comment period is now complete.

@nikomatsakis nikomatsakis added the E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. label Jan 24, 2017
@nikomatsakis
Copy link
Contributor

nikomatsakis commented Jan 24, 2017

We have no officially decided to REMOVE the #[main] feature and would welcome any help from someone who wishes to do it!

Here are the steps to take:

  • remove the feature from the list src/libsyntax/feature_gate.rs:136 and add it to the list of removed features found around line 316
  • the attribute main should be removed from the list in BUILTIN_ATTRIBUTES at src/libsyntax/feature_gate.rs:443;
  • the code in src/librustc/middle/entry.rs:89 (and mirrored code in src/libsyntax/entry.rs:29) should be adjusted to remove the case covering else if attr::contains_name(&item.attrs, "main") {
  • the feature gate code from src/libsyntax/feature_gate.rs:1056 can be removed
  • presumably the code in and around src/libsyntax/test.rs:206 that references "main" can be removed too
  • write a test trying to use the feature and show that it errors out

These line numbers may drift over time. :/ For the records, I found them mostly by running ripgrep '"main"' =)

UPDATE: #39282 is a good example of how to do a generic stabilization PR.

@nikomatsakis nikomatsakis added the E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. label Jan 25, 2017
@cramertj
Copy link
Member

I'd like to take this!

@nikomatsakis
Copy link
Contributor

@cramertj great! =)

@cramertj
Copy link
Member

Something that's come up as I've been removing the feature that I don't see discussed here: the #[main] attribute allows you to specify main functions that aren't defined at crate-level. I'm not sure how useful/idiomatic this is, but it is used in the existing test harness and a couple other places.

@cramertj
Copy link
Member

This is particularly useful when using #[cfg(...)]'d modules like in this intrinsic-alignment test.

@cramertj
Copy link
Member

cramertj commented Feb 14, 2017

Update on this, since it's been a while: I implemented the change, but it broke the test harness, which previously used the #[main] attribute in order to override the program's existing main fn. I explored a few other options, such as replacing/renaming the existing main function with the test harness, but it was pretty unwieldy. As I mentioned to @nikomatsakis over email, I think the best way forward is to either keep the #[main] feature around for internal use only (the easiest solution, but leaves the codebase cluttered with obsolete features), or to implement the test harness using #[start] (which, if I understand correctly, requires manually running some sort of initialization for std). What do you all think?

@nikomatsakis nikomatsakis removed E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. labels Mar 14, 2017
@nikomatsakis
Copy link
Contributor

I'm removing the E-easy / E-mentor tags. Based on @cramertj's experience, I don't actually know what's the best way forward at the moment, so i wouldn't consider this easy.

@Ericson2314
Copy link
Contributor

Ericson2314 commented May 8, 2017

Sorry to hijack a bit, but are any of the others currently stable? I'd like to get a definitive list of what would benefit from a general solution to the "needs provides" problem.

Oh and thanks for the list @alexcrichton. I don't recall all three described side-by-side elsewhere so that was handy.

@nikomatsakis
Copy link
Contributor

@Ericson2314

are any of the others currently stable?

other whats?

@cramertj
Copy link
Member

cramertj commented May 8, 2017

@nikomatsakis The entry points #[start], #[lang = "start"], and #[main]. I think @Ericson2314 was referencing this comment.

@nikomatsakis
Copy link
Contributor

I see. I don't think any of them are stable, but not sure off the top of my head.

@Ericson2314
Copy link
Contributor

Thanks. I suppose at least the lang item definitely wouldn't be.

@Mark-Simulacrum Mark-Simulacrum added the C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. label Jul 22, 2017
@steveklabnik
Copy link
Member

Triage: pretty sure that this is still where it was back in 2017. Since the previous issue was with test harness stuff, I wonder if and when custom test harnesses happen, it will alleviate this or not.

@cramertj
Copy link
Member

I've been meaning to start more chats about revamping the test harness anyways to work with panic = "abort"-- perhaps on an internals thread / mini-working-group?

@Cerber-Ursi
Copy link
Contributor

Just run into the issue preparing this playground. Looks like the compiler error E0601 is currently misleading, since it recommends the main attribute, even on stable where it is unavailable at all. Probably we should drop the corresponding part from the error message at all, as it seems that this attribute is considered to be not for public use anyway.

@camelid camelid assigned camelid and unassigned camelid Nov 23, 2020
@bors bors closed this as completed in d4bc912 Apr 16, 2021
@crlf0710 crlf0710 removed the B-unstable Blocker: Implemented in the nightly compiler and unstable. label May 10, 2021
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Feb 9, 2022
…henkov

Complete removal of #[main] attribute from compiler

resolves rust-lang#93786

---

The `#[main]` attribute was mostly removed from the language in rust-lang#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 rust-lang#84062 (comment), the removal of `#[main]` was expected to eliminate this conflict (previously reported as rust-lang#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 rust-lang#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`.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Feb 9, 2022
…henkov

Complete removal of #[main] attribute from compiler

resolves rust-lang#93786

---

The `#[main]` attribute was mostly removed from the language in rust-lang#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 rust-lang#84062 (comment), the removal of `#[main]` was expected to eliminate this conflict (previously reported as rust-lang#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 rust-lang#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`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet