Skip to content

Commit 4c30a75

Browse files
authored
Rollup merge of rust-lang#93142 - estebank:missing-main, r=wesleywiser
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.
2 parents c42d846 + c7ff23f commit 4c30a75

16 files changed

+45
-80
lines changed

compiler/rustc_passes/src/entry.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -218,9 +218,9 @@ fn no_main_err(tcx: TyCtxt<'_>, visitor: &EntryContext<'_, '_>) {
218218
// The file may be empty, which leads to the diagnostic machinery not emitting this
219219
// note. This is a relatively simple way to detect that case and emit a span-less
220220
// note instead.
221-
if tcx.sess.source_map().lookup_line(sp.lo()).is_ok() {
222-
err.set_span(sp);
223-
err.span_label(sp, &note);
221+
if tcx.sess.source_map().lookup_line(sp.hi()).is_ok() {
222+
err.set_span(sp.shrink_to_hi());
223+
err.span_label(sp.shrink_to_hi(), &note);
224224
} else {
225225
err.note(&note);
226226
}

src/test/ui/attributes/issue-90873.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#![u=||{static d=||1;}]
22
//~^ unexpected token
33
//~| cannot find attribute `u` in this scope
4-
//~| `main` function not found in crate `issue_90873`
54
//~| missing type for `static` item
65

76
#![a={impl std::ops::Neg for i8 {}}]
87
//~^ ERROR unexpected token
98
//~| ERROR cannot find attribute `a` in this scope
9+
//~| ERROR `main` function not found in crate `issue_90873`

src/test/ui/attributes/issue-90873.stderr

+5-11
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ LL | #![u=||{static d=||1;}]
1010
error: unexpected token: `{
1111
impl std::ops::Neg for i8 {}
1212
}`
13-
--> $DIR/issue-90873.rs:7:6
13+
--> $DIR/issue-90873.rs:6:6
1414
|
1515
LL | #![a={impl std::ops::Neg for i8 {}}]
1616
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -22,22 +22,16 @@ LL | #![u=||{static d=||1;}]
2222
| ^
2323

2424
error: cannot find attribute `a` in this scope
25-
--> $DIR/issue-90873.rs:7:4
25+
--> $DIR/issue-90873.rs:6:4
2626
|
2727
LL | #![a={impl std::ops::Neg for i8 {}}]
2828
| ^
2929

3030
error[E0601]: `main` function not found in crate `issue_90873`
31-
--> $DIR/issue-90873.rs:1:1
31+
--> $DIR/issue-90873.rs:6:37
3232
|
33-
LL | / #![u=||{static d=||1;}]
34-
LL | |
35-
LL | |
36-
LL | |
37-
LL | |
38-
LL | |
39-
LL | | #![a={impl std::ops::Neg for i8 {}}]
40-
| |____________________________________^ consider adding a `main` function to `$DIR/issue-90873.rs`
33+
LL | #![a={impl std::ops::Neg for i8 {}}]
34+
| ^ consider adding a `main` function to `$DIR/issue-90873.rs`
4135

4236
error: missing type for `static` item
4337
--> $DIR/issue-90873.rs:1:16

src/test/ui/conditional-compilation/cfg-attr-cfg-2.stderr

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
error[E0601]: `main` function not found in crate `cfg_attr_cfg_2`
2-
--> $DIR/cfg-attr-cfg-2.rs:8:1
2+
--> $DIR/cfg-attr-cfg-2.rs:9:14
33
|
4-
LL | / #[cfg_attr(foo, cfg(bar))]
5-
LL | | fn main() { }
6-
| |_____________^ consider adding a `main` function to `$DIR/cfg-attr-cfg-2.rs`
4+
LL | fn main() { }
5+
| ^ consider adding a `main` function to `$DIR/cfg-attr-cfg-2.rs`
76

87
error: aborting due to previous error
98

src/test/ui/conditional-compilation/cfg-in-crate-1.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0601]: `main` function not found in crate `cfg_in_crate_1`
2-
--> $DIR/cfg-in-crate-1.rs:3:1
2+
--> $DIR/cfg-in-crate-1.rs:3:13
33
|
44
LL | #![cfg(bar)]
5-
| ^^^^^^^^^^^^ consider adding a `main` function to `$DIR/cfg-in-crate-1.rs`
5+
| ^ consider adding a `main` function to `$DIR/cfg-in-crate-1.rs`
66

77
error: aborting due to previous error
88

src/test/ui/continue-after-missing-main.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![allow(dead_code)] //~ ERROR `main` function not found in crate
1+
#![allow(dead_code)]
22

33
struct Tableau<'a, MP> {
44
provider: &'a MP,
@@ -27,4 +27,4 @@ fn create_and_solve_subproblems<'data_provider, 'original_data, MP>(
2727
) {
2828
let _: AdaptedMatrixProvider<'original_data, MP> = tableau.provider().clone_with_extra_bound();
2929
//~^ ERROR lifetime mismatch
30-
}
30+
} //~ ERROR `main` function not found in crate

src/test/ui/continue-after-missing-main.stderr

+3-9
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
error[E0601]: `main` function not found in crate `continue_after_missing_main`
2-
--> $DIR/continue-after-missing-main.rs:1:1
2+
--> $DIR/continue-after-missing-main.rs:30:2
33
|
4-
LL | / #![allow(dead_code)]
5-
LL | |
6-
LL | | struct Tableau<'a, MP> {
7-
LL | | provider: &'a MP,
8-
... |
9-
LL | |
10-
LL | | }
11-
| |_^ consider adding a `main` function to `$DIR/continue-after-missing-main.rs`
4+
LL | }
5+
| ^ consider adding a `main` function to `$DIR/continue-after-missing-main.rs`
126

137
error[E0623]: lifetime mismatch
148
--> $DIR/continue-after-missing-main.rs:28:56

src/test/ui/elided-test.stderr

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
error[E0601]: `main` function not found in crate `elided_test`
2-
--> $DIR/elided-test.rs:5:1
2+
--> $DIR/elided-test.rs:7:2
33
|
4-
LL | / #[test]
5-
LL | | fn main() {
6-
LL | | }
7-
| |_^ consider adding a `main` function to `$DIR/elided-test.rs`
4+
LL | }
5+
| ^ consider adding a `main` function to `$DIR/elided-test.rs`
86

97
error: aborting due to previous error
108

Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#![feature(imported_main)]
22
#![feature(type_alias_impl_trait)]
33
#![allow(incomplete_features)]
4-
//~^^^ ERROR `main` function not found in crate
54
pub mod foo {
65
type MainFn = impl Fn();
76
//~^ ERROR could not find defining uses
@@ -11,4 +10,4 @@ pub mod foo {
1110
//~^ ERROR mismatched types [E0308]
1211
}
1312

14-
use foo::BAR as main;
13+
use foo::BAR as main; //~ ERROR `main` function not found in crate

src/test/ui/entry-point/imported_main_const_fn_item_type_forbidden.stderr

+7-13
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
11
error[E0601]: `main` function not found in crate `imported_main_const_fn_item_type_forbidden`
2-
--> $DIR/imported_main_const_fn_item_type_forbidden.rs:1:1
2+
--> $DIR/imported_main_const_fn_item_type_forbidden.rs:13:22
33
|
4-
LL | / #![feature(imported_main)]
5-
LL | | #![feature(type_alias_impl_trait)]
6-
LL | | #![allow(incomplete_features)]
7-
LL | |
8-
... |
9-
LL | |
10-
LL | | use foo::BAR as main;
11-
| |_____----------------^ consider adding a `main` function to `$DIR/imported_main_const_fn_item_type_forbidden.rs`
12-
| |
13-
| non-function item at `crate::main` is found
4+
LL | use foo::BAR as main;
5+
| ---------------- ^ consider adding a `main` function to `$DIR/imported_main_const_fn_item_type_forbidden.rs`
6+
| |
7+
| non-function item at `crate::main` is found
148

159
error[E0308]: mismatched types
16-
--> $DIR/imported_main_const_fn_item_type_forbidden.rs:10:29
10+
--> $DIR/imported_main_const_fn_item_type_forbidden.rs:9:29
1711
|
1812
LL | type MainFn = impl Fn();
1913
| --------- the expected opaque type
@@ -25,7 +19,7 @@ LL | pub const BAR: MainFn = bar;
2519
found fn item `fn() {bar}`
2620

2721
error: could not find defining uses
28-
--> $DIR/imported_main_const_fn_item_type_forbidden.rs:6:19
22+
--> $DIR/imported_main_const_fn_item_type_forbidden.rs:5:19
2923
|
3024
LL | type MainFn = impl Fn();
3125
| ^^^^^^^^^
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#![feature(imported_main)]
2-
//~^ ERROR `main` function not found in crate
32
pub mod foo {
43
pub const BAR: usize = 42;
54
}
65

7-
use foo::BAR as main;
6+
use foo::BAR as main; //~ ERROR `main` function not found in crate

src/test/ui/entry-point/imported_main_const_forbidden.stderr

+5-11
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
11
error[E0601]: `main` function not found in crate `imported_main_const_forbidden`
2-
--> $DIR/imported_main_const_forbidden.rs:1:1
2+
--> $DIR/imported_main_const_forbidden.rs:6:22
33
|
4-
LL | / #![feature(imported_main)]
5-
LL | |
6-
LL | | pub mod foo {
7-
LL | | pub const BAR: usize = 42;
8-
LL | | }
9-
LL | |
10-
LL | | use foo::BAR as main;
11-
| |_____----------------^ consider adding a `main` function to `$DIR/imported_main_const_forbidden.rs`
12-
| |
13-
| non-function item at `crate::main` is found
4+
LL | use foo::BAR as main;
5+
| ---------------- ^ consider adding a `main` function to `$DIR/imported_main_const_forbidden.rs`
6+
| |
7+
| non-function item at `crate::main` is found
148

159
error: aborting due to previous error
1610

src/test/ui/main-wrong-location.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
mod m {
2-
//~^ ERROR `main` function not found
32
// An inferred main entry point
43
// must appear at the top of the crate
54
fn main() { }
6-
}
5+
} //~ ERROR `main` function not found

src/test/ui/main-wrong-location.stderr

+4-9
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
error[E0601]: `main` function not found in crate `main_wrong_location`
2-
--> $DIR/main-wrong-location.rs:1:1
2+
--> $DIR/main-wrong-location.rs:5:2
33
|
4-
LL | / mod m {
5-
LL | |
6-
LL | | // An inferred main entry point
7-
LL | | // must appear at the top of the crate
8-
LL | | fn main() { }
9-
LL | | }
10-
| |_^ the main function must be defined at the crate level (in `$DIR/main-wrong-location.rs`)
4+
LL | }
5+
| ^ the main function must be defined at the crate level (in `$DIR/main-wrong-location.rs`)
116
|
127
note: here is a function named `main`
13-
--> $DIR/main-wrong-location.rs:5:5
8+
--> $DIR/main-wrong-location.rs:4:5
149
|
1510
LL | fn main() { }
1611
| ^^^^^^^^^^^^^

src/test/ui/missing/missing-main.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0601]: `main` function not found in crate `missing_main`
2-
--> $DIR/missing-main.rs:2:1
2+
--> $DIR/missing-main.rs:2:14
33
|
44
LL | fn mian() { }
5-
| ^^^^^^^^^^^^^ consider adding a `main` function to `$DIR/missing-main.rs`
5+
| ^ consider adding a `main` function to `$DIR/missing-main.rs`
66

77
error: aborting due to previous error
88

src/test/ui/parser/issues/issue-49040.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ LL | #![allow(unused_variables)];
55
| ^ help: remove this semicolon
66

77
error[E0601]: `main` function not found in crate `issue_49040`
8-
--> $DIR/issue-49040.rs:1:1
8+
--> $DIR/issue-49040.rs:1:29
99
|
1010
LL | #![allow(unused_variables)];
11-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ consider adding a `main` function to `$DIR/issue-49040.rs`
11+
| ^ consider adding a `main` function to `$DIR/issue-49040.rs`
1212

1313
error: aborting due to 2 previous errors
1414

0 commit comments

Comments
 (0)