Skip to content

Commit

Permalink
Rollup merge of rust-lang#48706 - ehuss:main-not-found-in-crate, r=es…
Browse files Browse the repository at this point in the history
…tebank

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.
  • Loading branch information
kennytm authored Mar 15, 2018
2 parents ff2d506 + 5257275 commit a45b79d
Show file tree
Hide file tree
Showing 55 changed files with 117 additions and 109 deletions.
14 changes: 10 additions & 4 deletions src/librustc/middle/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ impl<'a, 'tcx> ItemLikeVisitor<'tcx> for EntryContext<'a, 'tcx> {
}
}

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

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

configure_main(&mut ctxt);
configure_main(&mut ctxt, crate_name);
}

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

fn configure_main(this: &mut EntryContext) {
fn configure_main(this: &mut EntryContext, crate_name: &str) {
if this.start_fn.is_some() {
*this.session.entry_fn.borrow_mut() = this.start_fn;
this.session.entry_type.set(Some(config::EntryStart));
Expand All @@ -162,7 +164,8 @@ fn configure_main(this: &mut EntryContext) {
this.session.entry_type.set(Some(config::EntryMain));
} else {
// No main function
let mut err = struct_err!(this.session, E0601, "main function not found");
let mut err = struct_err!(this.session, E0601,
"`main` function not found in crate `{}`", crate_name);
if !this.non_main_fns.is_empty() {
// There were some functions named 'main' though. Try to give the user a hint.
err.note("the main function must be defined at the crate level \
Expand All @@ -175,6 +178,9 @@ fn configure_main(this: &mut EntryContext) {
err.emit();
this.session.abort_if_errors();
} else {
if let Some(ref filename) = this.session.local_crate_source_file {
err.note(&format!("consider adding a `main` function to `{}`", filename.display()));
}
if this.session.teach(&err.get_code().unwrap()) {
err.note("If you don't know the basics of Rust, you can go look to the Rust Book \
to get started: https://doc.rust-lang.org/book/");
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_driver/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -979,7 +979,7 @@ pub fn phase_3_run_analysis_passes<'tcx, F, R>(trans: &TransCrate,

time(sess,
"looking for entry point",
|| middle::entry::find_entry_point(sess, &hir_map));
|| middle::entry::find_entry_point(sess, &hir_map, name));

sess.plugin_registrar_fn.set(time(sess, "looking for plugin registrar", || {
plugin::build::find_plugin_registrar(sess.diagnostic(), &hir_map)
Expand Down
2 changes: 1 addition & 1 deletion src/test/compile-fail/cfg-attr-cfg-2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//
// error-pattern: main function not found
// error-pattern: `main` function not found
// compile-flags: --cfg foo

// main is conditionally compiled, but the conditional compilation
Expand Down
2 changes: 1 addition & 1 deletion src/test/compile-fail/cfg-in-crate-1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// error-pattern:main function not found
// error-pattern: `main` function not found

#![cfg(bar)]
2 changes: 1 addition & 1 deletion src/test/compile-fail/elided-test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// error-pattern: main function not found
// error-pattern: `main` function not found

// Since we're not compiling a test runner this function should be elided
// and the build will fail because main doesn't exist
Expand Down
2 changes: 1 addition & 1 deletion src/test/compile-fail/missing-main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// error-pattern:main function not found
// error-pattern: `main` function not found
fn mian() { }
2 changes: 2 additions & 0 deletions src/test/ui/error-codes/E0522.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ fn cookie() -> ! {
//~^^ ERROR definition of an unknown language item: `cookie` [E0522]
loop {}
}

fn main() {}
7 changes: 2 additions & 5 deletions src/test/ui/error-codes/E0522.stderr
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
error[E0601]: main function not found

error[E0522]: definition of an unknown language item: `cookie`
--> $DIR/E0522.rs:13:1
|
LL | #[lang = "cookie"]
| ^^^^^^^^^^^^^^^^^^ definition of unknown language item `cookie`

error: aborting due to 2 previous errors
error: aborting due to previous error

Some errors occurred: E0522, E0601.
For more information about an error, try `rustc --explain E0522`.
For more information about this error, try `rustc --explain E0522`.
11 changes: 11 additions & 0 deletions src/test/ui/error-codes/E0601.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// Test for main function not found.
7 changes: 7 additions & 0 deletions src/test/ui/error-codes/E0601.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
error[E0601]: `main` function not found in crate `E0601`
|
= note: consider adding a `main` function to `$DIR/E0601.rs`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0601`.
2 changes: 2 additions & 0 deletions src/test/ui/feature-gate-i128_type2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,5 @@ fn test3_2() {
enum A { //~ ERROR 128-bit type is unstable
A(u64)
}

fn main() {}
7 changes: 2 additions & 5 deletions src/test/ui/feature-gate-i128_type2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ LL | let x: u128 = 0; //~ ERROR 128-bit type is unstable
|
= help: add #![feature(i128_type)] to the crate attributes to enable

error[E0601]: main function not found

error[E0658]: repr with 128-bit type is unstable (see issue #35118)
--> $DIR/feature-gate-i128_type2.rs:30:1
|
Expand All @@ -42,7 +40,6 @@ LL | | }
|
= help: add #![feature(repr128)] to the crate attributes to enable

error: aborting due to 6 previous errors
error: aborting due to 5 previous errors

Some errors occurred: E0601, E0658.
For more information about an error, try `rustc --explain E0601`.
For more information about this error, try `rustc --explain E0658`.
2 changes: 1 addition & 1 deletion src/test/ui/feature-gate/issue-43106-gating-of-bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// error-pattern: main function not found
// error-pattern: `main` function not found

// At time of authorship, a crate-level #![bench] with no `--test`
// will cause compilation to error unconditionally with "main function
Expand Down
4 changes: 3 additions & 1 deletion src/test/ui/feature-gate/issue-43106-gating-of-bench.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
error[E0601]: main function not found
error[E0601]: `main` function not found in crate `issue_43106_gating_of_bench`
|
= note: consider adding a `main` function to `$DIR/issue-43106-gating-of-bench.rs`

error: aborting due to previous error

Expand Down
2 changes: 2 additions & 0 deletions src/test/ui/feature-gate/issue-43106-gating-of-inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,5 @@ mod inline {
#[inline = "2100"] impl S { }
//~^ ERROR attribute should be applied to function
}

fn main() {}
7 changes: 2 additions & 5 deletions src/test/ui/feature-gate/issue-43106-gating-of-inline.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
error[E0601]: main function not found

error[E0518]: attribute should be applied to function
--> $DIR/issue-43106-gating-of-inline.rs:21:1
|
Expand Down Expand Up @@ -39,7 +37,6 @@ error[E0518]: attribute should be applied to function
LL | #[inline = "2100"] impl S { }
| ^^^^^^^^^^^^^^^^^^ ---------- not a function

error: aborting due to 6 previous errors
error: aborting due to 5 previous errors

Some errors occurred: E0518, E0601.
For more information about an error, try `rustc --explain E0518`.
For more information about this error, try `rustc --explain E0518`.
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,9 @@
// `#![macro_escape]` is incompatible with crate-level `#![macro_use]`
// already present in issue-43106-gating-of-builtin-attrs.

// must-compile-successfully

#![macro_escape]
//~^ WARN macro_escape is a deprecated synonym for macro_use

fn main() {}
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
warning: macro_escape is a deprecated synonym for macro_use
--> $DIR/issue-43106-gating-of-macro_escape.rs:16:1
--> $DIR/issue-43106-gating-of-macro_escape.rs:18:1
|
LL | #![macro_escape]
| ^^^^^^^^^^^^^^^^
|
= help: consider an outer attribute, #[macro_use] mod ...

error[E0601]: main function not found

error: aborting due to previous error

For more information about this error, try `rustc --explain E0601`.
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,5 @@ mod proc_macro_derive2 {
#[proc_macro_derive = "2500"] impl S { }
//~^ ERROR the `#[proc_macro_derive]` attribute may only be used on bare functions
}

fn main() {}
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,5 @@ error: the `#[proc_macro_derive]` attribute may only be used on bare functions
LL | #[proc_macro_derive = "2500"] impl S { }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0601]: main function not found
error: aborting due to 6 previous errors

error: aborting due to 7 previous errors

For more information about this error, try `rustc --explain E0601`.
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ mod rustc_deprecated {
//~^ ERROR stability attributes may not be used outside of the standard library
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
error[E0601]: main function not found

error: stability attributes may not be used outside of the standard library
--> $DIR/issue-43106-gating-of-rustc_deprecated.rs:17:1
|
Expand Down Expand Up @@ -42,6 +40,5 @@ error: stability attributes may not be used outside of the standard library
LL | #[rustc_deprecated = "1500"] impl S { }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 8 previous errors
error: aborting due to 7 previous errors

For more information about this error, try `rustc --explain E0601`.
2 changes: 2 additions & 0 deletions src/test/ui/feature-gate/issue-43106-gating-of-stable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,5 @@ mod stable {
#[stable = "1300"] impl S { }
//~^ ERROR stability attributes may not be used outside of the standard library
}

fn main() {}
5 changes: 1 addition & 4 deletions src/test/ui/feature-gate/issue-43106-gating-of-stable.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
error[E0601]: main function not found

error: stability attributes may not be used outside of the standard library
--> $DIR/issue-43106-gating-of-stable.rs:17:1
|
Expand Down Expand Up @@ -42,6 +40,5 @@ error: stability attributes may not be used outside of the standard library
LL | #[stable = "1300"] impl S { }
| ^^^^^^^^^^^^^^^^^^

error: aborting due to 8 previous errors
error: aborting due to 7 previous errors

For more information about this error, try `rustc --explain E0601`.
2 changes: 1 addition & 1 deletion src/test/ui/feature-gate/issue-43106-gating-of-test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// error-pattern: main function not found
// error-pattern: `main` function not found

// At time of authorship, crate-level #[test] attribute with no
// `--test` signals unconditional error complaining of missing main
Expand Down
4 changes: 3 additions & 1 deletion src/test/ui/feature-gate/issue-43106-gating-of-test.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
error[E0601]: main function not found
error[E0601]: `main` function not found in crate `issue_43106_gating_of_test`
|
= note: consider adding a `main` function to `$DIR/issue-43106-gating-of-test.rs`

error: aborting due to previous error

Expand Down
2 changes: 2 additions & 0 deletions src/test/ui/feature-gate/issue-43106-gating-of-unstable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,5 @@ mod unstable {
#[unstable = "1200"] impl S { }
//~^ ERROR stability attributes may not be used outside of the standard library
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
error[E0601]: main function not found

error: stability attributes may not be used outside of the standard library
--> $DIR/issue-43106-gating-of-unstable.rs:17:1
|
Expand Down Expand Up @@ -42,6 +40,5 @@ error: stability attributes may not be used outside of the standard library
LL | #[unstable = "1200"] impl S { }
| ^^^^^^^^^^^^^^^^^^^^

error: aborting due to 8 previous errors
error: aborting due to 7 previous errors

For more information about this error, try `rustc --explain E0601`.
2 changes: 2 additions & 0 deletions src/test/ui/generator/yield-in-const.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@

const A: u8 = { yield 3u8; 3u8};
//~^ ERROR yield statement outside

fn main() {}
7 changes: 2 additions & 5 deletions src/test/ui/generator/yield-in-const.stderr
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
error[E0601]: main function not found

error[E0627]: yield statement outside of generator literal
--> $DIR/yield-in-const.rs:13:17
|
LL | const A: u8 = { yield 3u8; 3u8};
| ^^^^^^^^^

error: aborting due to 2 previous errors
error: aborting due to previous error

Some errors occurred: E0601, E0627.
For more information about an error, try `rustc --explain E0601`.
For more information about this error, try `rustc --explain E0627`.
2 changes: 2 additions & 0 deletions src/test/ui/generator/yield-in-static.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@

static B: u8 = { yield 3u8; 3u8};
//~^ ERROR yield statement outside

fn main() {}
7 changes: 2 additions & 5 deletions src/test/ui/generator/yield-in-static.stderr
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
error[E0601]: main function not found

error[E0627]: yield statement outside of generator literal
--> $DIR/yield-in-static.rs:13:18
|
LL | static B: u8 = { yield 3u8; 3u8};
| ^^^^^^^^^

error: aborting due to 2 previous errors
error: aborting due to previous error

Some errors occurred: E0601, E0627.
For more information about an error, try `rustc --explain E0601`.
For more information about this error, try `rustc --explain E0627`.
2 changes: 2 additions & 0 deletions src/test/ui/imports/macro-paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,5 @@ fn g() {
mod baz { pub use two_macros::m; }
}
}

fn main() {}
7 changes: 2 additions & 5 deletions src/test/ui/imports/macro-paths.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ LL | | }
| |_^
= note: macro-expanded items do not shadow when used in a macro invocation path

error[E0601]: main function not found
error: aborting due to 2 previous errors

error: aborting due to 3 previous errors

Some errors occurred: E0601, E0659.
For more information about an error, try `rustc --explain E0601`.
For more information about this error, try `rustc --explain E0659`.
2 changes: 2 additions & 0 deletions src/test/ui/imports/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,5 @@ mod m4 {
use two_macros::m;
m!(); //~ ERROR ambiguous
}

fn main() {}
7 changes: 2 additions & 5 deletions src/test/ui/imports/macros.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,6 @@ LL | use two_macros::m;
| ^^^^^^^^^^^^^
= note: macro-expanded macro imports do not shadow

error[E0601]: main function not found
error: aborting due to 3 previous errors

error: aborting due to 4 previous errors

Some errors occurred: E0601, E0659.
For more information about an error, try `rustc --explain E0601`.
For more information about this error, try `rustc --explain E0659`.
2 changes: 2 additions & 0 deletions src/test/ui/in-band-lifetimes/mismatched_trait_impl-2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@ impl Deref for Struct {
}
}
//~^^^^ ERROR cannot infer an appropriate lifetime for lifetime parameter

fn main() {}
Loading

0 comments on commit a45b79d

Please sign in to comment.