Skip to content

Commit b4b7ccb

Browse files
committed
Add crate name to "main function not found" error message.
Fixes #44798 and rust-lang/cargo#4948.
1 parent 11d9959 commit b4b7ccb

26 files changed

+32
-29
lines changed

src/librustc/middle/entry.rs

Lines changed: 7 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 \

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/ui/error-codes/E0522.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0601]: main function not found
1+
error[E0601]: main function not found in crate E0522
22

33
error[E0522]: definition of an unknown language item: `cookie`
44
--> $DIR/E0522.rs:13:1

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ 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
33+
error[E0601]: main function not found in crate feature_gate_i128_type2
3434

3535
error[E0658]: repr with 128-bit type is unstable (see issue #35118)
3636
--> $DIR/feature-gate-i128_type2.rs:30:1

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0601]: main function not found
1+
error[E0601]: main function not found in crate issue_43106_gating_of_bench
22

33
error: aborting due to previous error
44

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0601]: main function not found
1+
error[E0601]: main function not found in crate issue_43106_gating_of_inline
22

33
error[E0518]: attribute should be applied to function
44
--> $DIR/issue-43106-gating-of-inline.rs:21:1

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ LL | #![macro_escape]
66
|
77
= help: consider an outer attribute, #[macro_use] mod ...
88

9-
error[E0601]: main function not found
9+
error[E0601]: main function not found in crate issue_43106_gating_of_macro_escape
1010

1111
error: aborting due to previous error
1212

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ 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[E0601]: main function not found in crate issue_43106_gating_of_proc_macro_derive
3838

3939
error: aborting due to 7 previous errors
4040

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0601]: main function not found
1+
error[E0601]: main function not found in crate issue_43106_gating_of_rustc_deprecated
22

33
error: stability attributes may not be used outside of the standard library
44
--> $DIR/issue-43106-gating-of-rustc_deprecated.rs:17:1

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0601]: main function not found
1+
error[E0601]: main function not found in crate issue_43106_gating_of_stable
22

33
error: stability attributes may not be used outside of the standard library
44
--> $DIR/issue-43106-gating-of-stable.rs:17:1

0 commit comments

Comments
 (0)