Skip to content

Commit a45b79d

Browse files
authored
Rollup merge of rust-lang#48706 - ehuss:main-not-found-in-crate, r=estebank
Add crate name to "main function not found" error message. Fixes rust-lang#44798 and rust-lang/cargo#4948. I was wondering if it might be cleaner to update the ui tests to add a simple `fn main() {}` for the unrelated tests. Let me know if you would prefer that.
2 parents ff2d506 + 5257275 commit a45b79d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+117
-109
lines changed

src/librustc/middle/entry.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ impl<'a, 'tcx> ItemLikeVisitor<'tcx> for EntryContext<'a, 'tcx> {
5555
}
5656
}
5757

58-
pub fn find_entry_point(session: &Session, hir_map: &hir_map::Map) {
58+
pub fn find_entry_point(session: &Session,
59+
hir_map: &hir_map::Map,
60+
crate_name: &str) {
5961
let any_exe = session.crate_types.borrow().iter().any(|ty| {
6062
*ty == config::CrateTypeExecutable
6163
});
@@ -81,7 +83,7 @@ pub fn find_entry_point(session: &Session, hir_map: &hir_map::Map) {
8183

8284
hir_map.krate().visit_all_item_likes(&mut ctxt);
8385

84-
configure_main(&mut ctxt);
86+
configure_main(&mut ctxt, crate_name);
8587
}
8688

8789
// Beware, this is duplicated in libsyntax/entry.rs, make sure to keep
@@ -150,7 +152,7 @@ fn find_item(item: &Item, ctxt: &mut EntryContext, at_root: bool) {
150152
}
151153
}
152154

153-
fn configure_main(this: &mut EntryContext) {
155+
fn configure_main(this: &mut EntryContext, crate_name: &str) {
154156
if this.start_fn.is_some() {
155157
*this.session.entry_fn.borrow_mut() = this.start_fn;
156158
this.session.entry_type.set(Some(config::EntryStart));
@@ -162,7 +164,8 @@ fn configure_main(this: &mut EntryContext) {
162164
this.session.entry_type.set(Some(config::EntryMain));
163165
} else {
164166
// No main function
165-
let mut err = struct_err!(this.session, E0601, "main function not found");
167+
let mut err = struct_err!(this.session, E0601,
168+
"`main` function not found in crate `{}`", crate_name);
166169
if !this.non_main_fns.is_empty() {
167170
// There were some functions named 'main' though. Try to give the user a hint.
168171
err.note("the main function must be defined at the crate level \
@@ -175,6 +178,9 @@ fn configure_main(this: &mut EntryContext) {
175178
err.emit();
176179
this.session.abort_if_errors();
177180
} else {
181+
if let Some(ref filename) = this.session.local_crate_source_file {
182+
err.note(&format!("consider adding a `main` function to `{}`", filename.display()));
183+
}
178184
if this.session.teach(&err.get_code().unwrap()) {
179185
err.note("If you don't know the basics of Rust, you can go look to the Rust Book \
180186
to get started: https://doc.rust-lang.org/book/");

src/librustc_driver/driver.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -979,7 +979,7 @@ pub fn phase_3_run_analysis_passes<'tcx, F, R>(trans: &TransCrate,
979979

980980
time(sess,
981981
"looking for entry point",
982-
|| middle::entry::find_entry_point(sess, &hir_map));
982+
|| middle::entry::find_entry_point(sess, &hir_map, name));
983983

984984
sess.plugin_registrar_fn.set(time(sess, "looking for plugin registrar", || {
985985
plugin::build::find_plugin_registrar(sess.diagnostic(), &hir_map)

src/test/compile-fail/cfg-attr-cfg-2.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: main function not found
11+
// error-pattern: `main` function not found
1212
// compile-flags: --cfg foo
1313

1414
// main is conditionally compiled, but the conditional compilation

src/test/compile-fail/cfg-in-crate-1.rs

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

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

1313
#![cfg(bar)]

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: main 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/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:main function not found
11+
// error-pattern: `main` function not found
1212
fn mian() { }

src/test/ui/error-codes/E0522.rs

+2
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@ fn cookie() -> ! {
1515
//~^^ ERROR definition of an unknown language item: `cookie` [E0522]
1616
loop {}
1717
}
18+
19+
fn main() {}

src/test/ui/error-codes/E0522.stderr

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
error[E0601]: main function not found
2-
31
error[E0522]: definition of an unknown language item: `cookie`
42
--> $DIR/E0522.rs:13:1
53
|
64
LL | #[lang = "cookie"]
75
| ^^^^^^^^^^^^^^^^^^ definition of unknown language item `cookie`
86

9-
error: aborting due to 2 previous errors
7+
error: aborting due to previous error
108

11-
Some errors occurred: E0522, E0601.
12-
For more information about an error, try `rustc --explain E0522`.
9+
For more information about this error, try `rustc --explain E0522`.

src/test/ui/error-codes/E0601.rs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright 2018 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+
// Test for main function not found.

src/test/ui/error-codes/E0601.stderr

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
error[E0601]: `main` function not found in crate `E0601`
2+
|
3+
= note: consider adding a `main` function to `$DIR/E0601.rs`
4+
5+
error: aborting due to previous error
6+
7+
For more information about this error, try `rustc --explain E0601`.

src/test/ui/feature-gate-i128_type2.rs

+2
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,5 @@ fn test3_2() {
3030
enum A { //~ ERROR 128-bit type is unstable
3131
A(u64)
3232
}
33+
34+
fn main() {}

src/test/ui/feature-gate-i128_type2.stderr

+2-5
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ LL | let x: u128 = 0; //~ ERROR 128-bit type is unstable
3030
|
3131
= help: add #![feature(i128_type)] to the crate attributes to enable
3232

33-
error[E0601]: main function not found
34-
3533
error[E0658]: repr with 128-bit type is unstable (see issue #35118)
3634
--> $DIR/feature-gate-i128_type2.rs:30:1
3735
|
@@ -42,7 +40,6 @@ LL | | }
4240
|
4341
= help: add #![feature(repr128)] to the crate attributes to enable
4442

45-
error: aborting due to 6 previous errors
43+
error: aborting due to 5 previous errors
4644

47-
Some errors occurred: E0601, E0658.
48-
For more information about an error, try `rustc --explain E0601`.
45+
For more information about this error, try `rustc --explain E0658`.

src/test/ui/feature-gate/issue-43106-gating-of-bench.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: main function not found
11+
// error-pattern: `main` function not found
1212

1313
// At time of authorship, a crate-level #![bench] with no `--test`
1414
// will cause compilation to error unconditionally with "main function

src/test/ui/feature-gate/issue-43106-gating-of-bench.stderr

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
error[E0601]: main function not found
1+
error[E0601]: `main` function not found in crate `issue_43106_gating_of_bench`
2+
|
3+
= note: consider adding a `main` function to `$DIR/issue-43106-gating-of-bench.rs`
24

35
error: aborting due to previous error
46

src/test/ui/feature-gate/issue-43106-gating-of-inline.rs

+2
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,5 @@ mod inline {
3535
#[inline = "2100"] impl S { }
3636
//~^ ERROR attribute should be applied to function
3737
}
38+
39+
fn main() {}

src/test/ui/feature-gate/issue-43106-gating-of-inline.stderr

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
error[E0601]: main function not found
2-
31
error[E0518]: attribute should be applied to function
42
--> $DIR/issue-43106-gating-of-inline.rs:21:1
53
|
@@ -39,7 +37,6 @@ error[E0518]: attribute should be applied to function
3937
LL | #[inline = "2100"] impl S { }
4038
| ^^^^^^^^^^^^^^^^^^ ---------- not a function
4139

42-
error: aborting due to 6 previous errors
40+
error: aborting due to 5 previous errors
4341

44-
Some errors occurred: E0518, E0601.
45-
For more information about an error, try `rustc --explain E0518`.
42+
For more information about this error, try `rustc --explain E0518`.

src/test/ui/feature-gate/issue-43106-gating-of-macro_escape.rs

+4
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,9 @@
1313
// `#![macro_escape]` is incompatible with crate-level `#![macro_use]`
1414
// already present in issue-43106-gating-of-builtin-attrs.
1515

16+
// must-compile-successfully
17+
1618
#![macro_escape]
1719
//~^ WARN macro_escape is a deprecated synonym for macro_use
20+
21+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
warning: macro_escape is a deprecated synonym for macro_use
2-
--> $DIR/issue-43106-gating-of-macro_escape.rs:16:1
2+
--> $DIR/issue-43106-gating-of-macro_escape.rs:18:1
33
|
44
LL | #![macro_escape]
55
| ^^^^^^^^^^^^^^^^
66
|
77
= help: consider an outer attribute, #[macro_use] mod ...
88

9-
error[E0601]: main function not found
10-
11-
error: aborting due to previous error
12-
13-
For more information about this error, try `rustc --explain E0601`.

src/test/ui/feature-gate/issue-43106-gating-of-proc_macro_derive.rs

+2
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,5 @@ mod proc_macro_derive2 {
4040
#[proc_macro_derive = "2500"] impl S { }
4141
//~^ ERROR the `#[proc_macro_derive]` attribute may only be used on bare functions
4242
}
43+
44+
fn main() {}

src/test/ui/feature-gate/issue-43106-gating-of-proc_macro_derive.stderr

+1-4
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,5 @@ error: the `#[proc_macro_derive]` attribute may only be used on bare functions
3434
LL | #[proc_macro_derive = "2500"] impl S { }
3535
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3636

37-
error[E0601]: main function not found
37+
error: aborting due to 6 previous errors
3838

39-
error: aborting due to 7 previous errors
40-
41-
For more information about this error, try `rustc --explain E0601`.

src/test/ui/feature-gate/issue-43106-gating-of-rustc_deprecated.rs

+1
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,4 @@ mod rustc_deprecated {
3636
//~^ ERROR stability attributes may not be used outside of the standard library
3737
}
3838

39+
fn main() {}

src/test/ui/feature-gate/issue-43106-gating-of-rustc_deprecated.stderr

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
error[E0601]: main function not found
2-
31
error: stability attributes may not be used outside of the standard library
42
--> $DIR/issue-43106-gating-of-rustc_deprecated.rs:17:1
53
|
@@ -42,6 +40,5 @@ error: stability attributes may not be used outside of the standard library
4240
LL | #[rustc_deprecated = "1500"] impl S { }
4341
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4442

45-
error: aborting due to 8 previous errors
43+
error: aborting due to 7 previous errors
4644

47-
For more information about this error, try `rustc --explain E0601`.

src/test/ui/feature-gate/issue-43106-gating-of-stable.rs

+2
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,5 @@ mod stable {
3535
#[stable = "1300"] impl S { }
3636
//~^ ERROR stability attributes may not be used outside of the standard library
3737
}
38+
39+
fn main() {}

src/test/ui/feature-gate/issue-43106-gating-of-stable.stderr

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
error[E0601]: main function not found
2-
31
error: stability attributes may not be used outside of the standard library
42
--> $DIR/issue-43106-gating-of-stable.rs:17:1
53
|
@@ -42,6 +40,5 @@ error: stability attributes may not be used outside of the standard library
4240
LL | #[stable = "1300"] impl S { }
4341
| ^^^^^^^^^^^^^^^^^^
4442

45-
error: aborting due to 8 previous errors
43+
error: aborting due to 7 previous errors
4644

47-
For more information about this error, try `rustc --explain E0601`.

src/test/ui/feature-gate/issue-43106-gating-of-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: main function not found
11+
// error-pattern: `main` function not found
1212

1313
// At time of authorship, crate-level #[test] attribute with no
1414
// `--test` signals unconditional error complaining of missing main

src/test/ui/feature-gate/issue-43106-gating-of-test.stderr

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
error[E0601]: main function not found
1+
error[E0601]: `main` function not found in crate `issue_43106_gating_of_test`
2+
|
3+
= note: consider adding a `main` function to `$DIR/issue-43106-gating-of-test.rs`
24

35
error: aborting due to previous error
46

src/test/ui/feature-gate/issue-43106-gating-of-unstable.rs

+2
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,5 @@ mod unstable {
3535
#[unstable = "1200"] impl S { }
3636
//~^ ERROR stability attributes may not be used outside of the standard library
3737
}
38+
39+
fn main() {}

src/test/ui/feature-gate/issue-43106-gating-of-unstable.stderr

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
error[E0601]: main function not found
2-
31
error: stability attributes may not be used outside of the standard library
42
--> $DIR/issue-43106-gating-of-unstable.rs:17:1
53
|
@@ -42,6 +40,5 @@ error: stability attributes may not be used outside of the standard library
4240
LL | #[unstable = "1200"] impl S { }
4341
| ^^^^^^^^^^^^^^^^^^^^
4442

45-
error: aborting due to 8 previous errors
43+
error: aborting due to 7 previous errors
4644

47-
For more information about this error, try `rustc --explain E0601`.

src/test/ui/generator/yield-in-const.rs

+2
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@
1212

1313
const A: u8 = { yield 3u8; 3u8};
1414
//~^ ERROR yield statement outside
15+
16+
fn main() {}
+2-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
error[E0601]: main function not found
2-
31
error[E0627]: yield statement outside of generator literal
42
--> $DIR/yield-in-const.rs:13:17
53
|
64
LL | const A: u8 = { yield 3u8; 3u8};
75
| ^^^^^^^^^
86

9-
error: aborting due to 2 previous errors
7+
error: aborting due to previous error
108

11-
Some errors occurred: E0601, E0627.
12-
For more information about an error, try `rustc --explain E0601`.
9+
For more information about this error, try `rustc --explain E0627`.

src/test/ui/generator/yield-in-static.rs

+2
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@
1212

1313
static B: u8 = { yield 3u8; 3u8};
1414
//~^ ERROR yield statement outside
15+
16+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
error[E0601]: main function not found
2-
31
error[E0627]: yield statement outside of generator literal
42
--> $DIR/yield-in-static.rs:13:18
53
|
64
LL | static B: u8 = { yield 3u8; 3u8};
75
| ^^^^^^^^^
86

9-
error: aborting due to 2 previous errors
7+
error: aborting due to previous error
108

11-
Some errors occurred: E0601, E0627.
12-
For more information about an error, try `rustc --explain E0601`.
9+
For more information about this error, try `rustc --explain E0627`.

src/test/ui/imports/macro-paths.rs

+2
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,5 @@ fn g() {
3636
mod baz { pub use two_macros::m; }
3737
}
3838
}
39+
40+
fn main() {}

src/test/ui/imports/macro-paths.stderr

+2-5
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@ LL | | }
3636
| |_^
3737
= note: macro-expanded items do not shadow when used in a macro invocation path
3838

39-
error[E0601]: main function not found
39+
error: aborting due to 2 previous errors
4040

41-
error: aborting due to 3 previous errors
42-
43-
Some errors occurred: E0601, E0659.
44-
For more information about an error, try `rustc --explain E0601`.
41+
For more information about this error, try `rustc --explain E0659`.

src/test/ui/imports/macros.rs

+2
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,5 @@ mod m4 {
4949
use two_macros::m;
5050
m!(); //~ ERROR ambiguous
5151
}
52+
53+
fn main() {}

src/test/ui/imports/macros.stderr

+2-5
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,6 @@ LL | use two_macros::m;
5151
| ^^^^^^^^^^^^^
5252
= note: macro-expanded macro imports do not shadow
5353

54-
error[E0601]: main function not found
54+
error: aborting due to 3 previous errors
5555

56-
error: aborting due to 4 previous errors
57-
58-
Some errors occurred: E0601, E0659.
59-
For more information about an error, try `rustc --explain E0601`.
56+
For more information about this error, try `rustc --explain E0659`.

src/test/ui/in-band-lifetimes/mismatched_trait_impl-2.rs

+2
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,5 @@ impl Deref for Struct {
2020
}
2121
}
2222
//~^^^^ ERROR cannot infer an appropriate lifetime for lifetime parameter
23+
24+
fn main() {}

0 commit comments

Comments
 (0)