Provide a span if main function is not present in crate

Unfortunately, the diagnotic machinery does not cope well with an empty
span which can happen if the crate is empty, in which case we merely set
a spanless note.
This commit is contained in:
Mark Rousskov 2019-09-08 13:06:49 -04:00
parent 74d5c70b17
commit b7f20d06ea
42 changed files with 176 additions and 126 deletions

View file

@ -16,16 +16,16 @@ struct EntryContext<'a, 'tcx> {
map: &'a hir_map::Map<'tcx>,
/// The top-level function called 'main'.
/// The top-level function called `main`.
main_fn: Option<(HirId, Span)>,
/// The function that has attribute named 'main'.
/// The function that has attribute named `main`.
attr_main_fn: Option<(HirId, Span)>,
/// The function that has the attribute 'start' on it.
start_fn: Option<(HirId, Span)>,
/// The functions that one might think are 'main' but aren't, e.g.
/// The functions that one might think are `main` but aren't, e.g.
/// main functions not defined at the top level. For diagnostics.
non_main_fns: Vec<(HirId, Span)> ,
}
@ -88,7 +88,7 @@ fn entry_point_type(item: &Item, at_root: bool) -> EntryPointType {
EntryPointType::MainAttr
} else if item.ident.name == sym::main {
if at_root {
// This is a top-level function so can be 'main'.
// This is a top-level function so can be `main`.
EntryPointType::MainNamed
} else {
EntryPointType::OtherMain
@ -109,7 +109,7 @@ fn find_item(item: &Item, ctxt: &mut EntryContext<'_, '_>, at_root: bool) {
ctxt.main_fn = Some((item.hir_id, item.span));
} else {
span_err!(ctxt.session, item.span, E0136,
"multiple 'main' functions");
"multiple `main` functions");
}
},
EntryPointType::OtherMain => {
@ -130,7 +130,7 @@ fn find_item(item: &Item, ctxt: &mut EntryContext<'_, '_>, at_root: bool) {
if ctxt.start_fn.is_none() {
ctxt.start_fn = Some((item.hir_id, item.span));
} else {
struct_span_err!(ctxt.session, item.span, E0138, "multiple 'start' functions")
struct_span_err!(ctxt.session, item.span, E0138, "multiple `start` functions")
.span_label(ctxt.start_fn.unwrap().1, "previous `start` function here")
.span_label(item.span, "multiple `start` functions")
.emit();
@ -148,34 +148,48 @@ fn configure_main(tcx: TyCtxt<'_>, visitor: &EntryContext<'_, '_>) -> Option<(De
} else if let Some((hir_id, _)) = visitor.main_fn {
Some((tcx.hir().local_def_id(hir_id), EntryFnType::Main))
} else {
// There is no main function.
let mut err = struct_err!(tcx.sess, E0601,
"`main` function not found in crate `{}`", tcx.crate_name(LOCAL_CRATE));
if !visitor.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 \
but you have one or more functions named 'main' that are not \
defined at the crate level. Either move the definition or \
attach the `#[main]` attribute to override this behavior.");
for &(_, span) in &visitor.non_main_fns {
err.span_note(span, "here is a function named 'main'");
}
err.emit();
} else {
if let Some(ref filename) = tcx.sess.local_crate_source_file {
err.note(&format!("consider adding a `main` function to `{}`", filename.display()));
}
if tcx.sess.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/");
}
err.emit();
}
no_main_err(tcx, visitor);
None
}
}
fn no_main_err(tcx: TyCtxt<'_>, visitor: &EntryContext<'_, '_>) {
// There is no main function.
let mut err = struct_err!(tcx.sess, E0601,
"`main` function not found in crate `{}`", tcx.crate_name(LOCAL_CRATE));
let filename = &tcx.sess.local_crate_source_file;
let note = if !visitor.non_main_fns.is_empty() {
for &(_, span) in &visitor.non_main_fns {
err.span_note(span, "here is a function named `main`");
}
err.note("you have one or more functions named `main` not defined at the crate level");
err.help("either move the `main` function definitions or attach the `#[main]` attribute \
to one of them");
// There were some functions named `main` though. Try to give the user a hint.
format!("the main function must be defined at the crate level{}",
filename.as_ref().map(|f| format!(" (in `{}`)", f.display())).unwrap_or_default())
} else if let Some(filename) = filename {
format!("consider adding a `main` function to `{}`", filename.display())
} else {
String::from("consider adding a `main` function at the crate level")
};
let sp = tcx.hir().krate().span;
// The file may be empty, which leads to the diagnostic machinery not emitting this
// note. This is a relatively simple way to detect that case and emit a span-less
// note instead.
if let Ok(_) = tcx.sess.source_map().lookup_line(sp.lo()) {
err.set_span(sp);
err.span_label(sp, &note);
} else {
err.note(&note);
}
if tcx.sess.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/");
}
err.emit();
}
pub fn find_entry_point(tcx: TyCtxt<'_>) -> Option<(DefId, EntryFnType)> {
tcx.entry_fn(LOCAL_CRATE)
}

View file

@ -13,3 +13,5 @@ use rustc_macros::HashStable;
#[derive(HashStable)]
//~^ use of unstable library feature 'rustc_private'
struct Test;
fn main() {}

View file

@ -1,7 +1,3 @@
error[E0601]: `main` function not found in crate `hash_stable_is_unstable`
|
= note: consider adding a `main` function to `$DIR/hash-stable-is-unstable.rs`
error[E0658]: use of unstable library feature 'rustc_private': this crate is being loaded from the sysroot, an unstable location; did you mean to load this crate from crates.io via `Cargo.toml` instead?
--> $DIR/hash-stable-is-unstable.rs:3:1
|
@ -47,7 +43,6 @@ LL | #[derive(HashStable)]
= note: for more information, see https://github.com/rust-lang/rust/issues/27812
= help: add `#![feature(rustc_private)]` to the crate attributes to enable
error: aborting due to 6 previous errors
error: aborting due to 5 previous errors
Some errors have detailed explanations: E0601, E0658.
For more information about an error, try `rustc --explain E0601`.
For more information about this error, try `rustc --explain E0658`.

View file

@ -31,3 +31,5 @@ union U2 { f: Box<dyn Iterator<Item: Copy>> }
union U3 { f: dyn Iterator<Item: 'static> }
//~^ ERROR associated type bounds are not allowed within structs, enums, or unions
//~| ERROR could not find defining uses
fn main() {}

View file

@ -52,10 +52,6 @@ error: associated type bounds are not allowed within structs, enums, or unions
LL | union U3 { f: dyn Iterator<Item: 'static> }
| ^^^^^^^^^^^^^
error[E0601]: `main` function not found in crate `inside_adt`
|
= note: consider adding a `main` function to `$DIR/inside-adt.rs`
error: could not find defining uses
--> $DIR/inside-adt.rs:5:29
|
@ -110,6 +106,5 @@ error: could not find defining uses
LL | union U3 { f: dyn Iterator<Item: 'static> }
| ^^^^^^^^^^^^^
error: aborting due to 19 previous errors
error: aborting due to 18 previous errors
For more information about this error, try `rustc --explain E0601`.

View file

@ -1,6 +1,9 @@
error[E0601]: `main` function not found in crate `cfg_attr_cfg_2`
--> $DIR/cfg-attr-cfg-2.rs:8:1
|
= note: consider adding a `main` function to `$DIR/cfg-attr-cfg-2.rs`
LL | / #[cfg_attr(foo, cfg(bar))]
LL | | fn main() { }
| |_____________^ consider adding a `main` function to `$DIR/cfg-attr-cfg-2.rs`
error: aborting due to previous error

View file

@ -1,6 +1,8 @@
error[E0601]: `main` function not found in crate `cfg_in_crate_1`
--> $DIR/cfg-in-crate-1.rs:3:1
|
= note: consider adding a `main` function to `$DIR/cfg-in-crate-1.rs`
LL | #![cfg(bar)]
| ^^^^^^^^^^^^ consider adding a `main` function to `$DIR/cfg-in-crate-1.rs`
error: aborting due to previous error

View file

@ -1,6 +1,14 @@
error[E0601]: `main` function not found in crate `continue_after_missing_main`
--> $DIR/continue-after-missing-main.rs:1:1
|
= note: consider adding a `main` function to `$DIR/continue-after-missing-main.rs`
LL | / #![allow(dead_code)]
LL | |
LL | | // error-pattern:`main` function not found in crate
LL | |
... |
LL | |
LL | | }
| |_^ consider adding a `main` function to `$DIR/continue-after-missing-main.rs`
error: aborting due to previous error

View file

@ -1,6 +1,14 @@
error[E0601]: `main` function not found in crate `continue_after_missing_main`
--> $DIR/continue-after-missing-main.rs:1:1
|
= note: consider adding a `main` function to `$DIR/continue-after-missing-main.rs`
LL | / #![allow(dead_code)]
LL | |
LL | | // error-pattern:`main` function not found in crate
LL | |
... |
LL | |
LL | | }
| |_^ consider adding a `main` function to `$DIR/continue-after-missing-main.rs`
error[E0623]: lifetime mismatch
--> $DIR/continue-after-missing-main.rs:30:56

View file

@ -1,6 +1,10 @@
error[E0601]: `main` function not found in crate `elided_test`
--> $DIR/elided-test.rs:5:1
|
= note: consider adding a `main` function to `$DIR/elided-test.rs`
LL | / #[test]
LL | | fn main() {
LL | | }
| |_^ consider adding a `main` function to `$DIR/elided-test.rs`
error: aborting due to previous error

View file

@ -1,4 +1,4 @@
error[E0138]: multiple 'start' functions
error[E0138]: multiple `start` functions
--> $DIR/E0138.rs:7:1
|
LL | fn foo(argc: isize, argv: *const *const u8) -> isize { 0 }

View file

@ -1 +1 @@
// Test for main function not found.
//~ ERROR `main` function not found

View file

@ -1,6 +1,8 @@
error[E0601]: `main` function not found in crate `E0601`
--> $DIR/E0601.rs:1:37
|
= note: consider adding a `main` function to `$DIR/E0601.rs`
LL |
| ^ consider adding a `main` function to `$DIR/E0601.rs`
error: aborting due to previous error

View file

@ -2,3 +2,5 @@
trait Foo {}
#[derive(Foo::Anything)] //~ ERROR failed to resolve: partially resolved path in a derive macro
struct S;
fn main() {}

View file

@ -4,11 +4,6 @@ error[E0433]: failed to resolve: partially resolved path in a derive macro
LL | #[derive(Foo::Anything)]
| ^^^^^^^^^^^^^ partially resolved path in a derive macro
error[E0601]: `main` function not found in crate `issue_46101`
|
= note: consider adding a `main` function to `$DIR/issue-46101.rs`
error: aborting due to previous error
error: aborting due to 2 previous errors
Some errors have detailed explanations: E0433, E0601.
For more information about an error, try `rustc --explain E0433`.
For more information about this error, try `rustc --explain E0433`.

View file

@ -1,2 +1,3 @@
#![allow(unused_variables)]; //~ ERROR expected item, found `;`
//~^ ERROR `main` function
fn foo() {}

View file

@ -5,8 +5,12 @@ LL | #![allow(unused_variables)];
| ^ help: remove this semicolon
error[E0601]: `main` function not found in crate `issue_49040`
--> $DIR/issue-49040.rs:1:1
|
= note: consider adding a `main` function to `$DIR/issue-49040.rs`
LL | / #![allow(unused_variables)];
LL | |
LL | | fn foo() {}
| |__^ consider adding a `main` function to `$DIR/issue-49040.rs`
error: aborting due to 2 previous errors

View file

@ -15,3 +15,5 @@ impl A {
}
}
}
fn main() {}

View file

@ -10,11 +10,6 @@ error[E0425]: cannot find value `banana` in this scope
LL | banana: banana
| ^^^^^^ help: you might have meant to use the available field: `self.banana`
error[E0601]: `main` function not found in crate `issue_60057`
|
= note: consider adding a `main` function to `$DIR/issue-60057.rs`
error: aborting due to 2 previous errors
error: aborting due to 3 previous errors
Some errors have detailed explanations: E0425, E0601.
For more information about an error, try `rustc --explain E0425`.
For more information about this error, try `rustc --explain E0425`.

View file

@ -11,7 +11,7 @@ fn main() {
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/
"},"level":"error","spans":[],"children":[{"message":"consider adding a `main` function to `$DIR/json-short.rs`","code":null,"level":"note","spans":[],"children":[],"rendered":null}],"rendered":"error[E0601]: `main` function not found in crate `json_short`
"},"level":"error","spans":[{"file_name":"$DIR/json-short.rs","byte_start":76,"byte_end":76,"line_start":2,"line_end":2,"column_start":63,"column_end":63,"is_primary":true,"text":[{"text":"// compile-flags: --json=diagnostic-short --error-format=json","highlight_start":63,"highlight_end":63}],"label":"consider adding a `main` function to `$DIR/json-short.rs`","suggested_replacement":null,"suggestion_applicability":null,"expansion":null}],"children":[],"rendered":"$DIR/json-short.rs:2:63: error[E0601]: `main` function not found in crate `json_short`
"}
{"message":"aborting due to previous error","code":null,"level":"error","spans":[],"children":[],"rendered":"error: aborting due to previous error
"}

View file

@ -7,3 +7,5 @@ fn third<T, U, 'a>() {}
//~^ ERROR lifetime parameters must be declared prior to type parameters
fn fourth<'a, T, 'b, U, 'c, V>() {}
//~^ ERROR lifetime parameters must be declared prior to type parameters
fn main() {}

View file

@ -22,10 +22,5 @@ error: lifetime parameters must be declared prior to type parameters
LL | fn fourth<'a, T, 'b, U, 'c, V>() {}
| --------^^-----^^---- help: reorder the parameters: lifetimes, then types: `<'a, 'b, 'c, T, U, V>`
error[E0601]: `main` function not found in crate `lifetime_before_type_params`
|
= note: consider adding a `main` function to `$DIR/lifetime-before-type-params.rs`
error: aborting due to 4 previous errors
error: aborting due to 5 previous errors
For more information about this error, try `rustc --explain E0601`.

View file

@ -1,4 +1,5 @@
mod m {
//~^ ERROR `main` function not found
// An inferred main entry point (that doesn't use #[main])
// must appear at the top of the crate
fn main() { }

View file

@ -1,11 +1,21 @@
error[E0601]: `main` function not found in crate `main_wrong_location`
--> $DIR/main-wrong-location.rs:1:1
|
= note: the main function must be defined at the crate level but you have one or more functions named 'main' that are not defined at the crate level. Either move the definition or attach the `#[main]` attribute to override this behavior.
note: here is a function named 'main'
--> $DIR/main-wrong-location.rs:4:5
LL | / mod m {
LL | |
LL | | // An inferred main entry point (that doesn't use #[main])
LL | | // must appear at the top of the crate
LL | | fn main() { }
LL | | }
| |_^ the main function must be defined at the crate level (in `$DIR/main-wrong-location.rs`)
|
note: here is a function named `main`
--> $DIR/main-wrong-location.rs:5:5
|
LL | fn main() { }
| ^^^^^^^^^^^^^
= note: you have one or more functions named `main` not defined at the crate level
= help: either move the `main` function definitions or attach the `#[main]` attribute to one of them
error: aborting due to previous error

View file

@ -1,6 +1,8 @@
error[E0601]: `main` function not found in crate `missing_main`
--> $DIR/missing-main.rs:2:1
|
= note: consider adding a `main` function to `$DIR/missing-main.rs`
LL | fn mian() { }
| ^^^^^^^^^^^^^ consider adding a `main` function to `$DIR/missing-main.rs`
error: aborting due to previous error

View file

@ -3,6 +3,7 @@
// Test that we can recover from missing braces in the parser.
trait Foo {
//~^ ERROR `main` function not found
fn bar() {
let x = foo();
//~^ ERROR cannot find function `foo` in this scope

View file

@ -1,8 +1,9 @@
error: this file contains an un-closed delimiter
--> $DIR/parser-recovery-1.rs:15:55
--> $DIR/parser-recovery-1.rs:16:55
|
LL | trait Foo {
| - un-closed delimiter
LL |
LL | fn bar() {
| - this delimiter might not be properly closed...
...
@ -13,26 +14,34 @@ LL | }
| ^
error: unexpected token: `;`
--> $DIR/parser-recovery-1.rs:12:15
--> $DIR/parser-recovery-1.rs:13:15
|
LL | let x = y.;
| ^
error[E0425]: cannot find function `foo` in this scope
--> $DIR/parser-recovery-1.rs:7:17
--> $DIR/parser-recovery-1.rs:8:17
|
LL | let x = foo();
| ^^^ not found in this scope
error[E0425]: cannot find value `y` in this scope
--> $DIR/parser-recovery-1.rs:12:13
--> $DIR/parser-recovery-1.rs:13:13
|
LL | let x = y.;
| ^ not found in this scope
error[E0601]: `main` function not found in crate `parser_recovery_1`
--> $DIR/parser-recovery-1.rs:5:1
|
= note: consider adding a `main` function to `$DIR/parser-recovery-1.rs`
LL | / trait Foo {
LL | |
LL | | fn bar() {
LL | | let x = foo();
... |
LL | |
LL | | }
| |______________________________________________________^ consider adding a `main` function to `$DIR/parser-recovery-1.rs`
error: aborting due to 5 previous errors

View file

@ -1,4 +1,7 @@
fn foo() { //~ NOTE un-closed delimiter
//~^ ERROR `main` function not found
//~^^ NOTE main function must be defined
//~^^^ NOTE you have one or more functions
match Some(10) {
//~^ NOTE this delimiter might not be properly closed...
Some(y) => { panic!(); }
@ -11,5 +14,5 @@ fn bar() {
while (i < 1000) {}
}
fn main() {} //~ NOTE here is a function named 'main'
fn main() {} //~ NOTE here is a function named `main`
//~ ERROR this file contains an un-closed delimiter

View file

@ -1,8 +1,9 @@
error: this file contains an un-closed delimiter
--> $DIR/issue-2354.rs:15:66
--> $DIR/issue-2354.rs:18:66
|
LL | fn foo() {
| - un-closed delimiter
...
LL | match Some(10) {
| - this delimiter might not be properly closed...
...
@ -13,13 +14,24 @@ LL |
| ^
error[E0601]: `main` function not found in crate `issue_2354`
--> $DIR/issue-2354.rs:1:1
|
= note: the main function must be defined at the crate level but you have one or more functions named 'main' that are not defined at the crate level. Either move the definition or attach the `#[main]` attribute to override this behavior.
note: here is a function named 'main'
--> $DIR/issue-2354.rs:14:1
LL | / fn foo() {
LL | |
LL | |
LL | |
... |
LL | | fn main() {}
LL | |
| |_________________________________________________________________^ the main function must be defined at the crate level (in `$DIR/issue-2354.rs`)
|
note: here is a function named `main`
--> $DIR/issue-2354.rs:17:1
|
LL | fn main() {}
| ^^^^^^^^^^^^
= note: you have one or more functions named `main` not defined at the crate level
= help: either move the `main` function definitions or attach the `#[main]` attribute to one of them
error: aborting due to 2 previous errors

View file

@ -2,3 +2,5 @@
static c: char =
'nope' //~ ERROR: character literal may only contain one codepoint
;
fn main() {}

View file

@ -8,10 +8,5 @@ help: if you meant to write a `str` literal, use double quotes
LL | "nope"
| ^^^^^^
error[E0601]: `main` function not found in crate `lex_bad_char_literals_2`
|
= note: consider adding a `main` function to `$DIR/lex-bad-char-literals-2.rs`
error: aborting due to previous error
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0601`.

View file

@ -1,5 +1,5 @@
error: incorrect close delimiter: `}`
--> $DIR/unclosed_delim_mod.rs:5:1
--> $DIR/unclosed_delim_mod.rs:7:1
|
LL | pub fn new() -> Result<Value, ()> {
| - close delimiter possibly meant for this

View file

@ -1,3 +1,5 @@
fn main() {}
pub struct Value {}
pub fn new() -> Result<Value, ()> {
Ok(Value {

View file

@ -1,5 +1,5 @@
error: incorrect close delimiter: `}`
--> $DIR/unclosed_delim_mod.rs:5:1
--> $DIR/unclosed_delim_mod.rs:7:1
|
LL | pub fn new() -> Result<Value, ()> {
| - close delimiter possibly meant for this
@ -9,10 +9,5 @@ LL | }
LL | }
| ^ incorrect close delimiter
error[E0601]: `main` function not found in crate `unclosed_delim_mod`
|
= note: consider adding a `main` function to `$DIR/unclosed_delim_mod.rs`
error: aborting due to previous error
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0601`.

View file

@ -3,3 +3,5 @@
foo!(); //~ ERROR cannot find macro `foo!` in this scope
pub(in ::bar) struct Baz {} //~ ERROR cannot determine resolution for the visibility
fn main() {}

View file

@ -10,10 +10,5 @@ error: cannot find macro `foo!` in this scope
LL | foo!();
| ^^^
error[E0601]: `main` function not found in crate `visibility_indeterminate`
|
= note: consider adding a `main` function to `$DIR/visibility-indeterminate.rs`
error: aborting due to 2 previous errors
error: aborting due to 3 previous errors
For more information about this error, try `rustc --explain E0601`.

View file

@ -1,2 +1,3 @@
#[rustc_diagnostic_item = "foomp"] //~ ERROR compiler internal support for linting
struct Foomp;
fn main() {}

View file

@ -7,11 +7,6 @@ LL | #[rustc_diagnostic_item = "foomp"]
= note: for more information, see https://github.com/rust-lang/rust/issues/29642
= help: add `#![feature(rustc_attrs)]` to the crate attributes to enable
error[E0601]: `main` function not found in crate `diagnostic_item`
|
= note: consider adding a `main` function to `$DIR/diagnostic_item.rs`
error: aborting due to previous error
error: aborting due to 2 previous errors
Some errors have detailed explanations: E0601, E0658.
For more information about an error, try `rustc --explain E0601`.
For more information about this error, try `rustc --explain E0658`.

View file

@ -24,3 +24,5 @@ where
.map(move |shift| ((self >> T::from(shift)) & T::from(1)).try_into().unwrap())
}
}
fn main() {}

View file

@ -1,7 +1,3 @@
error[E0601]: `main` function not found in crate `issue_60564`
|
= note: consider adding a `main` function to `$DIR/issue-60564.rs`
error: type parameter `E` is part of concrete type but not used in parameter list for the `impl Trait` type alias
--> $DIR/issue-60564.rs:20:49
|
@ -20,6 +16,5 @@ error: could not find defining uses
LL | type IterBitsIter<T, E, I> = impl std::iter::Iterator<Item = I>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 3 previous errors
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0601`.

View file

@ -13,3 +13,4 @@ impl Reactor {
}
// This case isn't currently being handled gracefully, including for completeness.
fn main() {}

View file

@ -14,17 +14,13 @@ LL | input_cells: Vec::new()
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #42238 <https://github.com/rust-lang/rust/issues/42238>
error[E0601]: `main` function not found in crate `issue_34255_1`
|
= note: consider adding a `main` function to `$DIR/issue-34255-1.rs`
error[E0107]: wrong number of type arguments: expected 1, found 0
--> $DIR/issue-34255-1.rs:7:22
|
LL | input_cells: Vec::new()
| ^^^^^^^^^^ expected 1 type argument
error: aborting due to 4 previous errors
error: aborting due to 3 previous errors
Some errors have detailed explanations: E0107, E0425, E0601.
Some errors have detailed explanations: E0107, E0425.
For more information about an error, try `rustc --explain E0107`.