Tracking issue for `main` feature
aturon opened this issue ยท 25 comments
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!
Is there a reason to do this other than cosmetic preference for another name?
@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?
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.
The only place that comes close to this for me is osdev, and there, you use no_main
rather than main
, so...
We still have start
or whatever for doing this do we?
@rfcbot reviewed
@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.
Ah yes, we've got three entry points. I.. think this is how they work:
- First,
#[start]
, the receiver ofint argc
andchar **argv
. This is literally the symbolmain
(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 amain
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 tofn 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.
๐ 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.
The final comment period is now complete.
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 inBUILTIN_ATTRIBUTES
atsrc/libsyntax/feature_gate.rs:443
; - the code in
src/librustc/middle/entry.rs:89
(and mirrored code insrc/libsyntax/entry.rs:29
) should be adjusted to remove the case coveringelse 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.
I'd like to take this!
@cramertj great! =)
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.
This is particularly useful when using #[cfg(...)]
'd modules like in this intrinsic-alignment test.
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?
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.
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 The entry points #[start]
, #[lang = "start"]
, and #[main]
. I think @Ericson2314 was referencing this comment.
I see. I don't think any of them are stable, but not sure off the top of my head.
Thanks. I suppose at least the lang item definitely wouldn't be.
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.
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?
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.