Skip to content

Attribute to allow naming the entry point something other than main #4207

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

Closed
ILyoan opened this issue Dec 17, 2012 · 9 comments
Closed

Attribute to allow naming the entry point something other than main #4207

ILyoan opened this issue Dec 17, 2012 · 9 comments
Labels
A-attributes Area: Attributes (`#[…]`, `#![…]`) C-enhancement Category: An issue proposing an enhancement or a PR with one.

Comments

@ILyoan
Copy link
Contributor

ILyoan commented Dec 17, 2012

How about we add a new command line option to change the entry point from "fn main()" to another?

for example
$ rustc a.rs --entry=myEntry

@brson
Copy link
Contributor

brson commented Dec 17, 2012

How about a function attribute instead:

#[main]
fn my_main() { }

@ILyoan
Copy link
Contributor Author

ILyoan commented Dec 20, 2012

Yeah, that also would be a good option.
But I can't grasp fully how function attributes work in rustc yet,
so I may need your help

@brson
Copy link
Contributor

brson commented Dec 20, 2012

This will require some refactoring to clean up the way rustc deals with main, but should otherwise be straightforward.

There are primarily two passes that deal with main, resolve and trans, but these two passes don't quite collaborate as much as they need to in order to make this work.

Resolve is currently responsible for locating the main function and putting a reference to it into the compilation session, so other passes may know the main function. This is used, for example, by the type checker to verify that main has the correct signature.

Trans is currently responsible for generating some wrappers around the main function necessary to start up the runtime. Trans also is (incorrectly) responsible for the check that there is only a single main function (test here). Notably, trans does not use the information about main that is generated by resolve - it duplicates the search for the main function.

Here's how I suggest this be fixed:

  • move the duplicate main check from trans to resolve - resolve will become the only pass capable of figuring out which function is main.
  • change trans to use the main function indicated in the session so that all compiler passes agree on which function is main
  • in resolve, look at each function's attributes to see if it has #[main], using the attribute accessors here.

It's not clear to me whether the presence of a #[main] attribute should silently override an existing function named main or if having both should trigger the multiple main error. I think that, if there's a function with #[main] then having other functions named main should probably be allowed, specifically for the following reason:

The 'test' pass, that generates a binary for running #[test] functions, synthesizes a main function and will have to be aware of this attribute. The current strategy it uses is to remove any existing functions named main from the AST so as not to trigger the multiple main error. There has been an issue open (#2403) to fix this behavior for a while. What it should do instead is add #[main] to the synthesized main function and strip #[main] from all other functions in the AST.

@brson
Copy link
Contributor

brson commented Dec 20, 2012

@graydon Pinging you to see if you agree.

@brson
Copy link
Contributor

brson commented Dec 20, 2012

@ILyoan do you need to be able to compile the same codebase using different main functions, or do you simply need main to have a different name? This attribute strategy will not work so well for compiling the same codebase with multiple entry points, though conditional compilation probably covers that use case.

@ILyoan
Copy link
Contributor Author

ILyoan commented Dec 20, 2012

@brson
it seems like that the modification will not be a simple work :)
for the last question, just different name is sufficient for me.
Thanks

@graydon
Copy link
Contributor

graydon commented Dec 20, 2012

Attribute is best, I agree. Goal is to keep anything required for a build expressed in-code, command line options only for adjusting build flavours.

@catamorphism
Copy link
Contributor

Changed the title to reflect the preferred solution.

@luqmana
Copy link
Member

luqmana commented Jan 16, 2013

Guess this issue should be closed now since it's fixed in incoming.

@brson brson closed this as completed Jan 16, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-attributes Area: Attributes (`#[…]`, `#![…]`) C-enhancement Category: An issue proposing an enhancement or a PR with one.
Projects
None yet
Development

No branches or pull requests

5 participants