From 91f360329926981802f8bf5a320517c3ddd37829 Mon Sep 17 00:00:00 2001 From: Esteban Kuber Date: Fri, 21 Jan 2022 00:15:39 +0000 Subject: [PATCH] Do not point at whole file missing `fn main` Only point at the end of the crate. We could try making it point at the beginning of the crate, but that is confused with `DUMMY_SP`, causing the output to be *worse*. This change will make it so that VSCode will *not* underline the whole file when `main` is missing, so other errors will be visible. --- compiler/rustc_passes/src/entry.rs | 6 +++--- src/test/ui/attributes/issue-90873.rs | 2 +- src/test/ui/attributes/issue-90873.stderr | 16 +++++---------- .../cfg-attr-cfg-2.stderr | 7 +++---- .../cfg-in-crate-1.stderr | 4 ++-- .../ui/continue-after-missing-main.nll.stderr | 12 +++-------- src/test/ui/continue-after-missing-main.rs | 4 ++-- .../ui/continue-after-missing-main.stderr | 12 +++-------- src/test/ui/elided-test.stderr | 8 +++----- ...orted_main_const_fn_item_type_forbidden.rs | 3 +-- ...d_main_const_fn_item_type_forbidden.stderr | 20 +++++++------------ .../imported_main_const_forbidden.rs | 3 +-- .../imported_main_const_forbidden.stderr | 16 +++++---------- src/test/ui/main-wrong-location.rs | 3 +-- src/test/ui/main-wrong-location.stderr | 13 ++++-------- src/test/ui/missing/missing-main.stderr | 4 ++-- src/test/ui/parser/issues/issue-49040.stderr | 4 ++-- .../clippy/tests/ui/crashes/ice-6250.stderr | 12 +++-------- .../clippy/tests/ui/crashes/ice-6251.stderr | 8 +++----- 19 files changed, 54 insertions(+), 103 deletions(-) diff --git a/compiler/rustc_passes/src/entry.rs b/compiler/rustc_passes/src/entry.rs index fdabe41dafaed..f5040a373c296 100644 --- a/compiler/rustc_passes/src/entry.rs +++ b/compiler/rustc_passes/src/entry.rs @@ -218,9 +218,9 @@ fn no_main_err(tcx: TyCtxt<'_>, visitor: &EntryContext<'_, '_>) { // 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 tcx.sess.source_map().lookup_line(sp.lo()).is_ok() { - err.set_span(sp); - err.span_label(sp, ¬e); + if tcx.sess.source_map().lookup_line(sp.hi()).is_ok() { + err.set_span(sp.shrink_to_hi()); + err.span_label(sp.shrink_to_hi(), ¬e); } else { err.note(¬e); } diff --git a/src/test/ui/attributes/issue-90873.rs b/src/test/ui/attributes/issue-90873.rs index 76708ea98305f..0f62d4153089a 100644 --- a/src/test/ui/attributes/issue-90873.rs +++ b/src/test/ui/attributes/issue-90873.rs @@ -1,9 +1,9 @@ #![u=||{static d=||1;}] //~^ unexpected token //~| cannot find attribute `u` in this scope -//~| `main` function not found in crate `issue_90873` //~| missing type for `static` item #![a={impl std::ops::Neg for i8 {}}] //~^ ERROR unexpected token //~| ERROR cannot find attribute `a` in this scope +//~| ERROR `main` function not found in crate `issue_90873` diff --git a/src/test/ui/attributes/issue-90873.stderr b/src/test/ui/attributes/issue-90873.stderr index 2718b65108cdd..fbdc05ef6f004 100644 --- a/src/test/ui/attributes/issue-90873.stderr +++ b/src/test/ui/attributes/issue-90873.stderr @@ -10,7 +10,7 @@ LL | #![u=||{static d=||1;}] error: unexpected token: `{ impl std::ops::Neg for i8 {} }` - --> $DIR/issue-90873.rs:7:6 + --> $DIR/issue-90873.rs:6:6 | LL | #![a={impl std::ops::Neg for i8 {}}] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -22,22 +22,16 @@ LL | #![u=||{static d=||1;}] | ^ error: cannot find attribute `a` in this scope - --> $DIR/issue-90873.rs:7:4 + --> $DIR/issue-90873.rs:6:4 | LL | #![a={impl std::ops::Neg for i8 {}}] | ^ error[E0601]: `main` function not found in crate `issue_90873` - --> $DIR/issue-90873.rs:1:1 + --> $DIR/issue-90873.rs:6:37 | -LL | / #![u=||{static d=||1;}] -LL | | -LL | | -LL | | -LL | | -LL | | -LL | | #![a={impl std::ops::Neg for i8 {}}] - | |____________________________________^ consider adding a `main` function to `$DIR/issue-90873.rs` +LL | #![a={impl std::ops::Neg for i8 {}}] + | ^ consider adding a `main` function to `$DIR/issue-90873.rs` error: missing type for `static` item --> $DIR/issue-90873.rs:1:16 diff --git a/src/test/ui/conditional-compilation/cfg-attr-cfg-2.stderr b/src/test/ui/conditional-compilation/cfg-attr-cfg-2.stderr index e9df780def5df..d61872c48ea96 100644 --- a/src/test/ui/conditional-compilation/cfg-attr-cfg-2.stderr +++ b/src/test/ui/conditional-compilation/cfg-attr-cfg-2.stderr @@ -1,9 +1,8 @@ error[E0601]: `main` function not found in crate `cfg_attr_cfg_2` - --> $DIR/cfg-attr-cfg-2.rs:8:1 + --> $DIR/cfg-attr-cfg-2.rs:9:14 | -LL | / #[cfg_attr(foo, cfg(bar))] -LL | | fn main() { } - | |_____________^ consider adding a `main` function to `$DIR/cfg-attr-cfg-2.rs` +LL | fn main() { } + | ^ consider adding a `main` function to `$DIR/cfg-attr-cfg-2.rs` error: aborting due to previous error diff --git a/src/test/ui/conditional-compilation/cfg-in-crate-1.stderr b/src/test/ui/conditional-compilation/cfg-in-crate-1.stderr index 0b5c3e0335586..ff72c43efbd08 100644 --- a/src/test/ui/conditional-compilation/cfg-in-crate-1.stderr +++ b/src/test/ui/conditional-compilation/cfg-in-crate-1.stderr @@ -1,8 +1,8 @@ error[E0601]: `main` function not found in crate `cfg_in_crate_1` - --> $DIR/cfg-in-crate-1.rs:3:1 + --> $DIR/cfg-in-crate-1.rs:3:13 | LL | #![cfg(bar)] - | ^^^^^^^^^^^^ consider adding a `main` function to `$DIR/cfg-in-crate-1.rs` + | ^ consider adding a `main` function to `$DIR/cfg-in-crate-1.rs` error: aborting due to previous error diff --git a/src/test/ui/continue-after-missing-main.nll.stderr b/src/test/ui/continue-after-missing-main.nll.stderr index c3eea2e7b8ad6..ebebabe349b85 100644 --- a/src/test/ui/continue-after-missing-main.nll.stderr +++ b/src/test/ui/continue-after-missing-main.nll.stderr @@ -1,14 +1,8 @@ error[E0601]: `main` function not found in crate `continue_after_missing_main` - --> $DIR/continue-after-missing-main.rs:1:1 + --> $DIR/continue-after-missing-main.rs:30:2 | -LL | / #![allow(dead_code)] -LL | | -LL | | struct Tableau<'a, MP> { -LL | | provider: &'a MP, -... | -LL | | -LL | | } - | |_^ consider adding a `main` function to `$DIR/continue-after-missing-main.rs` +LL | } + | ^ consider adding a `main` function to `$DIR/continue-after-missing-main.rs` error: aborting due to previous error diff --git a/src/test/ui/continue-after-missing-main.rs b/src/test/ui/continue-after-missing-main.rs index 55796408e9d79..1019cacce6421 100644 --- a/src/test/ui/continue-after-missing-main.rs +++ b/src/test/ui/continue-after-missing-main.rs @@ -1,4 +1,4 @@ -#![allow(dead_code)] //~ ERROR `main` function not found in crate +#![allow(dead_code)] struct Tableau<'a, MP> { provider: &'a MP, @@ -27,4 +27,4 @@ fn create_and_solve_subproblems<'data_provider, 'original_data, MP>( ) { let _: AdaptedMatrixProvider<'original_data, MP> = tableau.provider().clone_with_extra_bound(); //~^ ERROR lifetime mismatch -} +} //~ ERROR `main` function not found in crate diff --git a/src/test/ui/continue-after-missing-main.stderr b/src/test/ui/continue-after-missing-main.stderr index 439f9e5221f66..29e7dc1e56c73 100644 --- a/src/test/ui/continue-after-missing-main.stderr +++ b/src/test/ui/continue-after-missing-main.stderr @@ -1,14 +1,8 @@ error[E0601]: `main` function not found in crate `continue_after_missing_main` - --> $DIR/continue-after-missing-main.rs:1:1 + --> $DIR/continue-after-missing-main.rs:30:2 | -LL | / #![allow(dead_code)] -LL | | -LL | | struct Tableau<'a, MP> { -LL | | provider: &'a MP, -... | -LL | | -LL | | } - | |_^ consider adding a `main` function to `$DIR/continue-after-missing-main.rs` +LL | } + | ^ consider adding a `main` function to `$DIR/continue-after-missing-main.rs` error[E0623]: lifetime mismatch --> $DIR/continue-after-missing-main.rs:28:56 diff --git a/src/test/ui/elided-test.stderr b/src/test/ui/elided-test.stderr index 175bd033067bc..c74c307c492fe 100644 --- a/src/test/ui/elided-test.stderr +++ b/src/test/ui/elided-test.stderr @@ -1,10 +1,8 @@ error[E0601]: `main` function not found in crate `elided_test` - --> $DIR/elided-test.rs:5:1 + --> $DIR/elided-test.rs:7:2 | -LL | / #[test] -LL | | fn main() { -LL | | } - | |_^ consider adding a `main` function to `$DIR/elided-test.rs` +LL | } + | ^ consider adding a `main` function to `$DIR/elided-test.rs` error: aborting due to previous error diff --git a/src/test/ui/entry-point/imported_main_const_fn_item_type_forbidden.rs b/src/test/ui/entry-point/imported_main_const_fn_item_type_forbidden.rs index e0bb7dbfae9f1..dab9c687e2498 100644 --- a/src/test/ui/entry-point/imported_main_const_fn_item_type_forbidden.rs +++ b/src/test/ui/entry-point/imported_main_const_fn_item_type_forbidden.rs @@ -1,7 +1,6 @@ #![feature(imported_main)] #![feature(type_alias_impl_trait)] #![allow(incomplete_features)] -//~^^^ ERROR `main` function not found in crate pub mod foo { type MainFn = impl Fn(); //~^ ERROR could not find defining uses @@ -11,4 +10,4 @@ pub mod foo { //~^ ERROR mismatched types [E0308] } -use foo::BAR as main; +use foo::BAR as main; //~ ERROR `main` function not found in crate diff --git a/src/test/ui/entry-point/imported_main_const_fn_item_type_forbidden.stderr b/src/test/ui/entry-point/imported_main_const_fn_item_type_forbidden.stderr index c731c32832222..b9bc0262a56b2 100644 --- a/src/test/ui/entry-point/imported_main_const_fn_item_type_forbidden.stderr +++ b/src/test/ui/entry-point/imported_main_const_fn_item_type_forbidden.stderr @@ -1,19 +1,13 @@ error[E0601]: `main` function not found in crate `imported_main_const_fn_item_type_forbidden` - --> $DIR/imported_main_const_fn_item_type_forbidden.rs:1:1 + --> $DIR/imported_main_const_fn_item_type_forbidden.rs:13:22 | -LL | / #![feature(imported_main)] -LL | | #![feature(type_alias_impl_trait)] -LL | | #![allow(incomplete_features)] -LL | | -... | -LL | | -LL | | use foo::BAR as main; - | |_____----------------^ consider adding a `main` function to `$DIR/imported_main_const_fn_item_type_forbidden.rs` - | | - | non-function item at `crate::main` is found +LL | use foo::BAR as main; + | ---------------- ^ consider adding a `main` function to `$DIR/imported_main_const_fn_item_type_forbidden.rs` + | | + | non-function item at `crate::main` is found error[E0308]: mismatched types - --> $DIR/imported_main_const_fn_item_type_forbidden.rs:10:29 + --> $DIR/imported_main_const_fn_item_type_forbidden.rs:9:29 | LL | type MainFn = impl Fn(); | --------- the expected opaque type @@ -25,7 +19,7 @@ LL | pub const BAR: MainFn = bar; found fn item `fn() {bar}` error: could not find defining uses - --> $DIR/imported_main_const_fn_item_type_forbidden.rs:6:19 + --> $DIR/imported_main_const_fn_item_type_forbidden.rs:5:19 | LL | type MainFn = impl Fn(); | ^^^^^^^^^ diff --git a/src/test/ui/entry-point/imported_main_const_forbidden.rs b/src/test/ui/entry-point/imported_main_const_forbidden.rs index 989a6c97a8004..1508280c0fa57 100644 --- a/src/test/ui/entry-point/imported_main_const_forbidden.rs +++ b/src/test/ui/entry-point/imported_main_const_forbidden.rs @@ -1,7 +1,6 @@ #![feature(imported_main)] -//~^ ERROR `main` function not found in crate pub mod foo { pub const BAR: usize = 42; } -use foo::BAR as main; +use foo::BAR as main; //~ ERROR `main` function not found in crate diff --git a/src/test/ui/entry-point/imported_main_const_forbidden.stderr b/src/test/ui/entry-point/imported_main_const_forbidden.stderr index 4640513c2bb5f..9d8b40dc3c9bc 100644 --- a/src/test/ui/entry-point/imported_main_const_forbidden.stderr +++ b/src/test/ui/entry-point/imported_main_const_forbidden.stderr @@ -1,16 +1,10 @@ error[E0601]: `main` function not found in crate `imported_main_const_forbidden` - --> $DIR/imported_main_const_forbidden.rs:1:1 + --> $DIR/imported_main_const_forbidden.rs:6:22 | -LL | / #![feature(imported_main)] -LL | | -LL | | pub mod foo { -LL | | pub const BAR: usize = 42; -LL | | } -LL | | -LL | | use foo::BAR as main; - | |_____----------------^ consider adding a `main` function to `$DIR/imported_main_const_forbidden.rs` - | | - | non-function item at `crate::main` is found +LL | use foo::BAR as main; + | ---------------- ^ consider adding a `main` function to `$DIR/imported_main_const_forbidden.rs` + | | + | non-function item at `crate::main` is found error: aborting due to previous error diff --git a/src/test/ui/main-wrong-location.rs b/src/test/ui/main-wrong-location.rs index f3acd80a7a48f..d7deeaed99da2 100644 --- a/src/test/ui/main-wrong-location.rs +++ b/src/test/ui/main-wrong-location.rs @@ -1,6 +1,5 @@ mod m { -//~^ ERROR `main` function not found // An inferred main entry point // must appear at the top of the crate fn main() { } -} +} //~ ERROR `main` function not found diff --git a/src/test/ui/main-wrong-location.stderr b/src/test/ui/main-wrong-location.stderr index 754ff0f80eb9a..0058af9b79ebf 100644 --- a/src/test/ui/main-wrong-location.stderr +++ b/src/test/ui/main-wrong-location.stderr @@ -1,16 +1,11 @@ error[E0601]: `main` function not found in crate `main_wrong_location` - --> $DIR/main-wrong-location.rs:1:1 + --> $DIR/main-wrong-location.rs:5:2 | -LL | / mod m { -LL | | -LL | | // An inferred main entry point -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`) +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 + --> $DIR/main-wrong-location.rs:4:5 | LL | fn main() { } | ^^^^^^^^^^^^^ diff --git a/src/test/ui/missing/missing-main.stderr b/src/test/ui/missing/missing-main.stderr index 6a35f5117efd6..5113dc6ec08c4 100644 --- a/src/test/ui/missing/missing-main.stderr +++ b/src/test/ui/missing/missing-main.stderr @@ -1,8 +1,8 @@ error[E0601]: `main` function not found in crate `missing_main` - --> $DIR/missing-main.rs:2:1 + --> $DIR/missing-main.rs:2:14 | LL | fn mian() { } - | ^^^^^^^^^^^^^ consider adding a `main` function to `$DIR/missing-main.rs` + | ^ consider adding a `main` function to `$DIR/missing-main.rs` error: aborting due to previous error diff --git a/src/test/ui/parser/issues/issue-49040.stderr b/src/test/ui/parser/issues/issue-49040.stderr index 56befe3a0a75d..8af7838c79138 100644 --- a/src/test/ui/parser/issues/issue-49040.stderr +++ b/src/test/ui/parser/issues/issue-49040.stderr @@ -5,10 +5,10 @@ LL | #![allow(unused_variables)]; | ^ help: remove this semicolon error[E0601]: `main` function not found in crate `issue_49040` - --> $DIR/issue-49040.rs:1:1 + --> $DIR/issue-49040.rs:1:29 | LL | #![allow(unused_variables)]; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ consider adding a `main` function to `$DIR/issue-49040.rs` + | ^ consider adding a `main` function to `$DIR/issue-49040.rs` error: aborting due to 2 previous errors diff --git a/src/tools/clippy/tests/ui/crashes/ice-6250.stderr b/src/tools/clippy/tests/ui/crashes/ice-6250.stderr index 7ffbd7a64b341..878897c410cf5 100644 --- a/src/tools/clippy/tests/ui/crashes/ice-6250.stderr +++ b/src/tools/clippy/tests/ui/crashes/ice-6250.stderr @@ -1,14 +1,8 @@ error[E0601]: `main` function not found in crate `ice_6250` - --> $DIR/ice-6250.rs:4:1 + --> $DIR/ice-6250.rs:16:2 | -LL | / pub struct Cache { -LL | | data: Vec, -LL | | } -LL | | -... | -LL | | } -LL | | } - | |_^ consider adding a `main` function to `$DIR/ice-6250.rs` +LL | } + | ^ consider adding a `main` function to `$DIR/ice-6250.rs` error[E0308]: mismatched types --> $DIR/ice-6250.rs:12:14 diff --git a/src/tools/clippy/tests/ui/crashes/ice-6251.stderr b/src/tools/clippy/tests/ui/crashes/ice-6251.stderr index 14c71e884b6ea..77a3c2ba4ad09 100644 --- a/src/tools/clippy/tests/ui/crashes/ice-6251.stderr +++ b/src/tools/clippy/tests/ui/crashes/ice-6251.stderr @@ -1,10 +1,8 @@ error[E0601]: `main` function not found in crate `ice_6251` - --> $DIR/ice-6251.rs:4:1 + --> $DIR/ice-6251.rs:6:2 | -LL | / fn bug() -> impl Iterator { -LL | | std::iter::empty() -LL | | } - | |_^ consider adding a `main` function to `$DIR/ice-6251.rs` +LL | } + | ^ consider adding a `main` function to `$DIR/ice-6251.rs` error[E0277]: the size for values of type `[u8]` cannot be known at compilation time --> $DIR/ice-6251.rs:4:45