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

File tree

55 files changed

+117
-109
lines changed

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

Lines changed: 10 additions & 4 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 2 additions & 5 deletions
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

Lines changed: 11 additions & 0 deletions
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

Lines changed: 7 additions & 0 deletions
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`.

0 commit comments

Comments
 (0)