Skip to content

Commit 65ded84

Browse files
committed
auto merge of #6119 : brson/rust/main, r=brson
r? @ILyoan This pulls all the logic for discovering the crate entry point into a new pass (out of resolve and typeck), then changes it so that main is only looked for at the crate level (`#[main]` can still be used anywhere). I don't understand the special android logic here and worry that I may have broken it.
2 parents 270b508 + 3970d02 commit 65ded84

File tree

12 files changed

+172
-89
lines changed

12 files changed

+172
-89
lines changed

src/librustc/driver/driver.rs

+3
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,9 @@ pub fn compile_rest(sess: Session,
225225
time(time_passes, ~"resolution", ||
226226
middle::resolve::resolve_crate(sess, lang_items, crate));
227227

228+
time(time_passes, ~"looking for entry point",
229+
|| middle::entry::find_entry_point(sess, crate, ast_map));
230+
228231
let freevars = time(time_passes, ~"freevar finding", ||
229232
freevars::annotate_freevars(def_map, crate));
230233

src/librustc/middle/entry.rs

+150
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use driver::session;
12+
use driver::session::Session;
13+
use syntax::parse::token::special_idents;
14+
use syntax::ast::{crate, node_id, item, item_fn};
15+
use syntax::codemap::span;
16+
use syntax::visit::{default_visitor, mk_vt, vt, Visitor, visit_crate, visit_item};
17+
use syntax::attr::{attrs_contains_name};
18+
use syntax::ast_map;
19+
use core::util;
20+
21+
struct EntryContext {
22+
session: Session,
23+
24+
ast_map: ast_map::map,
25+
26+
// The top-level function called 'main'
27+
main_fn: Option<(node_id, span)>,
28+
29+
// The function that has attribute named 'main'
30+
attr_main_fn: Option<(node_id, span)>,
31+
32+
// The function that has the attribute 'start' on it
33+
start_fn: Option<(node_id, span)>,
34+
35+
// The functions that one might think are 'main' but aren't, e.g.
36+
// main functions not defined at the top level. For diagnostics.
37+
non_main_fns: ~[(node_id, span)],
38+
}
39+
40+
type EntryVisitor = vt<@mut EntryContext>;
41+
42+
pub fn find_entry_point(session: Session, crate: @crate, ast_map: ast_map::map) {
43+
44+
// FIXME #4404 android JNI hacks
45+
if *session.building_library &&
46+
session.targ_cfg.os != session::os_android {
47+
// No need to find a main function
48+
return;
49+
}
50+
51+
let ctxt = @mut EntryContext {
52+
session: session,
53+
ast_map: ast_map,
54+
main_fn: None,
55+
attr_main_fn: None,
56+
start_fn: None,
57+
non_main_fns: ~[],
58+
};
59+
60+
visit_crate(crate, ctxt, mk_vt(@Visitor {
61+
visit_item: |item, ctxt, visitor| find_item(item, ctxt, visitor),
62+
.. *default_visitor()
63+
}));
64+
65+
configure_main(ctxt);
66+
}
67+
68+
fn find_item(item: @item, ctxt: @mut EntryContext, visitor: EntryVisitor) {
69+
match item.node {
70+
item_fn(*) => {
71+
if item.ident == special_idents::main {
72+
match ctxt.ast_map.find(&item.id) {
73+
Some(&ast_map::node_item(_, path)) => {
74+
if path.len() == 0 {
75+
// This is a top-level function so can be 'main'
76+
if ctxt.main_fn.is_none() {
77+
ctxt.main_fn = Some((item.id, item.span));
78+
} else {
79+
ctxt.session.span_err(
80+
item.span,
81+
~"multiple 'main' functions");
82+
}
83+
} else {
84+
// This isn't main
85+
ctxt.non_main_fns.push((item.id, item.span));
86+
}
87+
}
88+
_ => util::unreachable()
89+
}
90+
}
91+
92+
if attrs_contains_name(item.attrs, ~"main") {
93+
if ctxt.attr_main_fn.is_none() {
94+
ctxt.attr_main_fn = Some((item.id, item.span));
95+
} else {
96+
ctxt.session.span_err(
97+
item.span,
98+
~"multiple 'main' functions");
99+
}
100+
}
101+
102+
if attrs_contains_name(item.attrs, ~"start") {
103+
if ctxt.start_fn.is_none() {
104+
ctxt.start_fn = Some((item.id, item.span));
105+
} else {
106+
ctxt.session.span_err(
107+
item.span,
108+
~"multiple 'start' functions");
109+
}
110+
}
111+
}
112+
_ => ()
113+
}
114+
115+
visit_item(item, ctxt, visitor);
116+
}
117+
118+
fn configure_main(ctxt: @mut EntryContext) {
119+
let this = &mut *ctxt;
120+
if this.start_fn.is_some() {
121+
*this.session.entry_fn = this.start_fn;
122+
*this.session.entry_type = Some(session::EntryStart);
123+
} else if this.attr_main_fn.is_some() {
124+
*this.session.entry_fn = this.attr_main_fn;
125+
*this.session.entry_type = Some(session::EntryMain);
126+
} else if this.main_fn.is_some() {
127+
*this.session.entry_fn = this.main_fn;
128+
*this.session.entry_type = Some(session::EntryMain);
129+
} else {
130+
if !*this.session.building_library {
131+
// No main function
132+
this.session.err(~"main function not found");
133+
if !this.non_main_fns.is_empty() {
134+
// There were some functions named 'main' though. Try to give the user a hint.
135+
this.session.note(~"the main function must be defined at the crate level \
136+
but you have one or more functions named 'main' that are not \
137+
defined at the crate level. Either move the definition or \
138+
attach the `#[main]` attribute to override this behavior.");
139+
for this.non_main_fns.each |&(_, span)| {
140+
this.session.span_note(span, ~"here is a function named 'main'");
141+
}
142+
}
143+
this.session.abort_if_errors();
144+
} else {
145+
// If we *are* building a library, then we're on android where we still might
146+
// optionally want to translate main $4404
147+
assert!(this.session.targ_cfg.os == session::os_android);
148+
}
149+
}
150+
}

src/librustc/middle/resolve.rs

-79
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use driver::session;
1211
use driver::session::Session;
1312
use metadata::csearch::{each_path, get_trait_method_def_ids};
1413
use metadata::csearch::get_method_name_and_self_ty;
@@ -794,11 +793,6 @@ pub fn Resolver(session: Session,
794793
795794
namespaces: ~[ TypeNS, ValueNS ],
796795
797-
attr_main_fn: None,
798-
main_fns: ~[],
799-
800-
start_fn: None,
801-
802796
def_map: @mut HashMap::new(),
803797
export_map2: @mut HashMap::new(),
804798
trait_map: HashMap::new(),
@@ -856,15 +850,6 @@ pub struct Resolver {
856850
// The four namespaces.
857851
namespaces: ~[Namespace],
858852
859-
// The function that has attribute named 'main'
860-
attr_main_fn: Option<(node_id, span)>,
861-
862-
// The functions that could be main functions
863-
main_fns: ~[Option<(node_id, span)>],
864-
865-
// The function that has the attribute 'start' on it
866-
start_fn: Option<(node_id, span)>,
867-
868853
def_map: DefMap,
869854
export_map2: ExportMap2,
870855
trait_map: TraitMap,
@@ -885,7 +870,6 @@ pub impl Resolver {
885870
self.resolve_crate();
886871
self.session.abort_if_errors();
887872
888-
self.check_duplicate_main();
889873
self.check_for_unused_imports_if_necessary();
890874
}
891875
@@ -3544,40 +3528,6 @@ pub impl Resolver {
35443528
}
35453529

35463530
item_fn(ref fn_decl, _, _, ref generics, ref block) => {
3547-
// If this is the main function, we must record it in the
3548-
// session.
3549-
3550-
// FIXME #4404 android JNI hacks
3551-
if !*self.session.building_library ||
3552-
self.session.targ_cfg.os == session::os_android {
3553-
3554-
if self.attr_main_fn.is_none() &&
3555-
item.ident == special_idents::main {
3556-
3557-
self.main_fns.push(Some((item.id, item.span)));
3558-
}
3559-
3560-
if attrs_contains_name(item.attrs, ~"main") {
3561-
if self.attr_main_fn.is_none() {
3562-
self.attr_main_fn = Some((item.id, item.span));
3563-
} else {
3564-
self.session.span_err(
3565-
item.span,
3566-
~"multiple 'main' functions");
3567-
}
3568-
}
3569-
3570-
if attrs_contains_name(item.attrs, ~"start") {
3571-
if self.start_fn.is_none() {
3572-
self.start_fn = Some((item.id, item.span));
3573-
} else {
3574-
self.session.span_err(
3575-
item.span,
3576-
~"multiple 'start' functions");
3577-
}
3578-
}
3579-
}
3580-
35813531
self.resolve_function(OpaqueFunctionRibKind,
35823532
Some(fn_decl),
35833533
HasTypeParameters
@@ -5089,35 +5039,6 @@ pub impl Resolver {
50895039
}
50905040
}
50915041

5092-
//
5093-
// main function checking
5094-
//
5095-
// be sure that there is only one main function
5096-
//
5097-
fn check_duplicate_main(@mut self) {
5098-
let this = &mut *self;
5099-
if this.attr_main_fn.is_none() && this.start_fn.is_none() {
5100-
if this.main_fns.len() >= 1u {
5101-
let mut i = 1u;
5102-
while i < this.main_fns.len() {
5103-
let (_, dup_main_span) = this.main_fns[i].unwrap();
5104-
this.session.span_err(
5105-
dup_main_span,
5106-
~"multiple 'main' functions");
5107-
i += 1;
5108-
}
5109-
*this.session.entry_fn = this.main_fns[0];
5110-
*this.session.entry_type = Some(session::EntryMain);
5111-
}
5112-
} else if !this.start_fn.is_none() {
5113-
*this.session.entry_fn = this.start_fn;
5114-
*this.session.entry_type = Some(session::EntryStart);
5115-
} else {
5116-
*this.session.entry_fn = this.attr_main_fn;
5117-
*this.session.entry_type = Some(session::EntryMain);
5118-
}
5119-
}
5120-
51215042
//
51225043
// Unused import checking
51235044
//

src/librustc/middle/typeck/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ fn check_for_entry_fn(ccx: @mut CrateCtxt) {
393393
Some(session::EntryStart) => check_start_fn_ty(ccx, id, sp),
394394
None => tcx.sess.bug(~"entry function without a type")
395395
},
396-
None => tcx.sess.err(~"entry function not found")
396+
None => tcx.sess.bug(~"type checking without entry function")
397397
}
398398
}
399399
}

src/librustc/rustc.rc

+1
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ pub mod middle {
100100
pub mod lang_items;
101101
pub mod privacy;
102102
pub mod moves;
103+
pub mod entry;
103104
}
104105

105106
pub mod front {

src/test/compile-fail/elided-test.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// error-pattern: entry function not found
11+
// error-pattern: main function not found
1212

1313
// Since we're not compiling a test runner this function should be elided
1414
// and the build will fail because main doesn't exist

src/test/compile-fail/issue-2995.rs

+2
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,5 @@
1111
fn bad (p: *int) {
1212
let _q: &int = p as &int; //~ ERROR non-scalar cast
1313
}
14+
15+
fn main() { }

src/test/compile-fail/multiple-main.rs renamed to src/test/compile-fail/main-wrong-location.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
fn main() {
12-
}
13-
14-
mod foo {
15-
fn main() { //~ ERROR multiple 'main' functions
16-
}
17-
}
11+
mod m {
12+
// An inferred main entry point (that doesn't use #[main])
13+
// must appear at the top of the crate
14+
fn main() { } //~ NOTE here is a function named 'main'
15+
}

src/test/compile-fail/missing-main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// error-pattern:entry function not found
11+
// error-pattern:main function not found
1212
fn mian() { }

src/test/compile-fail/tag-variant-disr-dup.rs

+2
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,5 @@ enum color {
1919
black = 0x000000,
2020
white = 0x000000,
2121
}
22+
23+
fn main() { }

src/test/run-pass/dupe-first-attr.rc

+2
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,5 @@ mod hello;
2525

2626
#[cfg(target_os = "android")]
2727
mod hello;
28+
29+
fn main() { }

src/test/run-pass/intrinsic-alignment.rs

+4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ mod rusti {
2222
#[cfg(target_os = "macos")]
2323
#[cfg(target_os = "freebsd")]
2424
mod m {
25+
#[main]
2526
#[cfg(target_arch = "x86")]
2627
pub fn main() {
2728
unsafe {
@@ -30,6 +31,7 @@ mod m {
3031
}
3132
}
3233

34+
#[main]
3335
#[cfg(target_arch = "x86_64")]
3436
pub fn main() {
3537
unsafe {
@@ -41,6 +43,7 @@ mod m {
4143

4244
#[cfg(target_os = "win32")]
4345
mod m {
46+
#[main]
4447
#[cfg(target_arch = "x86")]
4548
pub fn main() {
4649
unsafe {
@@ -52,6 +55,7 @@ mod m {
5255

5356
#[cfg(target_os = "android")]
5457
mod m {
58+
#[main]
5559
#[cfg(target_arch = "arm")]
5660
pub fn main() {
5761
unsafe {

0 commit comments

Comments
 (0)