Skip to content

Commit 6fba412

Browse files
committed
Further tweak spans in ast validation errors
1 parent 713a291 commit 6fba412

11 files changed

+91
-48
lines changed

src/librustc_ast_passes/ast_validation.rs

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc_ast::visit::{self, AssocCtxt, FnCtxt, FnKind, Visitor};
1313
use rustc_ast::walk_list;
1414
use rustc_ast_pretty::pprust;
1515
use rustc_data_structures::fx::FxHashMap;
16-
use rustc_errors::{error_code, struct_span_err, Applicability};
16+
use rustc_errors::{error_code, pluralize, struct_span_err, Applicability};
1717
use rustc_parse::validate_attr;
1818
use rustc_session::lint::builtin::PATTERNS_IN_FNS_WITHOUT_BODY;
1919
use rustc_session::lint::LintBuffer;
@@ -887,30 +887,63 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
887887
if is_auto == IsAuto::Yes {
888888
// Auto traits cannot have generics, super traits nor contain items.
889889
if !generics.params.is_empty() {
890-
struct_span_err!(
890+
let spans: Vec<_> = generics.params.iter().map(|i| i.ident.span).collect();
891+
let last = spans.iter().last().map(|s| *s);
892+
let len = spans.len();
893+
let mut err = struct_span_err!(
891894
self.session,
892-
item.span,
895+
spans,
893896
E0567,
894897
"auto traits cannot have generic parameters"
895-
)
896-
.emit();
898+
);
899+
if let Some(span) = last {
900+
err.span_label(
901+
span,
902+
&format!(
903+
"cannot have {these} generic parameter{s}",
904+
these = if len == 1 { "this" } else { "these" },
905+
s = pluralize!(len)
906+
),
907+
);
908+
}
909+
err.span_label(
910+
item.ident.span,
911+
"auto trait cannot have generic parameters",
912+
);
913+
err.emit();
897914
}
898915
if !bounds.is_empty() {
899-
struct_span_err!(
916+
let spans: Vec<_> = bounds.iter().map(|b| b.span()).collect();
917+
let last = spans.iter().last().map(|s| *s);
918+
let len = spans.len();
919+
let mut err = struct_span_err!(
900920
self.session,
901-
item.span,
921+
spans,
902922
E0568,
903923
"auto traits cannot have super traits"
904-
)
905-
.emit();
924+
);
925+
if let Some(span) = last {
926+
err.span_label(
927+
span,
928+
&format!(
929+
"cannot have {these} super trait{s}",
930+
these = if len == 1 { "this" } else { "these" },
931+
s = pluralize!(len)
932+
),
933+
);
934+
}
935+
err.span_label(item.ident.span, "auto trait cannot have super traits");
936+
err.emit();
906937
}
907938
if !trait_items.is_empty() {
939+
let spans: Vec<_> = trait_items.iter().map(|i| i.ident.span).collect();
908940
struct_span_err!(
909941
self.session,
910-
item.span,
942+
spans,
911943
E0380,
912944
"auto traits cannot have methods or associated items"
913945
)
946+
.span_label(item.ident.span, "auto trait cannot have items")
914947
.emit();
915948
}
916949
}
@@ -1157,9 +1190,13 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
11571190
}) = fk.header()
11581191
{
11591192
self.err_handler()
1160-
.struct_span_err(span, "functions cannot be both `const` and `async`")
1193+
.struct_span_err(
1194+
vec![*cspan, *aspan],
1195+
"functions cannot be both `const` and `async`",
1196+
)
11611197
.span_label(*cspan, "`const` because of this")
11621198
.span_label(*aspan, "`async` because of this")
1199+
.span_label(span, "") // Point at the fn header.
11631200
.emit();
11641201
}
11651202

src/test/ui/async-await/no-const-async.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: functions cannot be both `const` and `async`
2-
--> $DIR/no-const-async.rs:4:1
2+
--> $DIR/no-const-async.rs:4:5
33
|
44
LL | pub const async fn x() {}
5-
| ^^^^-----^-----^^^^^^^^^^
5+
| ----^^^^^-^^^^^----------
66
| | |
77
| | `async` because of this
88
| `const` because of this

src/test/ui/auto-trait-validation.stderr

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11
error[E0567]: auto traits cannot have generic parameters
2-
--> $DIR/auto-trait-validation.rs:3:1
2+
--> $DIR/auto-trait-validation.rs:3:20
33
|
44
LL | auto trait Generic<T> {}
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^
5+
| ------- ^ cannot have this generic parameter
6+
| |
7+
| auto trait cannot have generic parameters
68

79
error[E0568]: auto traits cannot have super traits
8-
--> $DIR/auto-trait-validation.rs:5:1
10+
--> $DIR/auto-trait-validation.rs:5:20
911
|
1012
LL | auto trait Bound : Copy {}
11-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
13+
| ----- ^^^^ cannot have this super trait
14+
| |
15+
| auto trait cannot have super traits
1216

1317
error[E0380]: auto traits cannot have methods or associated items
14-
--> $DIR/auto-trait-validation.rs:7:1
18+
--> $DIR/auto-trait-validation.rs:7:25
1519
|
1620
LL | auto trait MyTrait { fn foo() {} }
17-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
21+
| ------- ^^^
22+
| |
23+
| auto trait cannot have items
1824

1925
error: aborting due to 3 previous errors
2026

src/test/ui/issues/issue-23080-2.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
#![feature(optin_builtin_traits)]
44

55
unsafe auto trait Trait {
6-
//~^ ERROR E0380
7-
type Output;
6+
type Output; //~ ERROR E0380
87
}
98

109
fn call_method<T: Trait>(x: T) {}

src/test/ui/issues/issue-23080-2.stderr

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
error[E0380]: auto traits cannot have methods or associated items
2-
--> $DIR/issue-23080-2.rs:5:1
2+
--> $DIR/issue-23080-2.rs:6:10
33
|
4-
LL | / unsafe auto trait Trait {
5-
LL | |
6-
LL | | type Output;
7-
LL | | }
8-
| |_^
4+
LL | unsafe auto trait Trait {
5+
| ----- auto trait cannot have items
6+
LL | type Output;
7+
| ^^^^^^
98

109
error[E0275]: overflow evaluating the requirement `<() as Trait>::Output`
1110
|

src/test/ui/issues/issue-23080.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
#![feature(optin_builtin_traits)]
22

33
unsafe auto trait Trait {
4-
//~^ ERROR E0380
5-
fn method(&self) {
4+
fn method(&self) { //~ ERROR E0380
65
println!("Hello");
76
}
87
}

src/test/ui/issues/issue-23080.stderr

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
error[E0380]: auto traits cannot have methods or associated items
2-
--> $DIR/issue-23080.rs:3:1
2+
--> $DIR/issue-23080.rs:4:8
33
|
4-
LL | / unsafe auto trait Trait {
5-
LL | |
6-
LL | | fn method(&self) {
7-
LL | | println!("Hello");
8-
LL | | }
9-
LL | | }
10-
| |_^
4+
LL | unsafe auto trait Trait {
5+
| ----- auto trait cannot have items
6+
LL | fn method(&self) {
7+
| ^^^^^^
118

129
error: aborting due to previous error
1310

src/test/ui/parser/fn-header-semantic-fail.stderr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: functions cannot be both `const` and `async`
22
--> $DIR/fn-header-semantic-fail.rs:13:5
33
|
44
LL | const async unsafe extern "C" fn ff5() {} // OK.
5-
| -----^-----^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5+
| ^^^^^-^^^^^------------------------------
66
| | |
77
| | `async` because of this
88
| `const` because of this
@@ -45,7 +45,7 @@ error: functions cannot be both `const` and `async`
4545
--> $DIR/fn-header-semantic-fail.rs:21:9
4646
|
4747
LL | const async unsafe extern "C" fn ft5();
48-
| -----^-----^^^^^^^^^^^^^^^^^^^^^^^^^^^^
48+
| ^^^^^-^^^^^----------------------------
4949
| | |
5050
| | `async` because of this
5151
| `const` because of this
@@ -88,7 +88,7 @@ error: functions cannot be both `const` and `async`
8888
--> $DIR/fn-header-semantic-fail.rs:34:9
8989
|
9090
LL | const async unsafe extern "C" fn ft5() {}
91-
| -----^-----^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
91+
| ^^^^^-^^^^^------------------------------
9292
| | |
9393
| | `async` because of this
9494
| `const` because of this
@@ -97,7 +97,7 @@ error: functions cannot be both `const` and `async`
9797
--> $DIR/fn-header-semantic-fail.rs:46:9
9898
|
9999
LL | const async unsafe extern "C" fn fi5() {}
100-
| -----^-----^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
100+
| ^^^^^-^^^^^------------------------------
101101
| | |
102102
| | `async` because of this
103103
| `const` because of this
@@ -160,7 +160,7 @@ error: functions cannot be both `const` and `async`
160160
--> $DIR/fn-header-semantic-fail.rs:55:9
161161
|
162162
LL | const async unsafe extern "C" fn fe5();
163-
| -----^-----^^^^^^^^^^^^^^^^^^^^^^^^^^^^
163+
| ^^^^^-^^^^^----------------------------
164164
| | |
165165
| | `async` because of this
166166
| `const` because of this

src/test/ui/traits/traits-inductive-overflow-supertrait-oibit.stderr

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
error[E0568]: auto traits cannot have super traits
2-
--> $DIR/traits-inductive-overflow-supertrait-oibit.rs:7:1
2+
--> $DIR/traits-inductive-overflow-supertrait-oibit.rs:7:19
33
|
44
LL | auto trait Magic: Copy {}
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^
5+
| ----- ^^^^ cannot have this super trait
6+
| |
7+
| auto trait cannot have super traits
68

79
error[E0277]: the trait bound `NoClone: std::marker::Copy` is not satisfied
810
--> $DIR/traits-inductive-overflow-supertrait-oibit.rs:15:23

src/test/ui/typeck/typeck-auto-trait-no-supertraits-2.stderr

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
error[E0568]: auto traits cannot have super traits
2-
--> $DIR/typeck-auto-trait-no-supertraits-2.rs:3:1
2+
--> $DIR/typeck-auto-trait-no-supertraits-2.rs:3:20
33
|
44
LL | auto trait Magic : Sized where Option<Self> : Magic {}
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5+
| ----- ^^^^^ cannot have this super trait
6+
| |
7+
| auto trait cannot have super traits
68

79
error: aborting due to previous error
810

src/test/ui/typeck/typeck-auto-trait-no-supertraits.stderr

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
error[E0568]: auto traits cannot have super traits
2-
--> $DIR/typeck-auto-trait-no-supertraits.rs:27:1
2+
--> $DIR/typeck-auto-trait-no-supertraits.rs:27:19
33
|
44
LL | auto trait Magic: Copy {}
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^
5+
| ----- ^^^^ cannot have this super trait
6+
| |
7+
| auto trait cannot have super traits
68

79
error: aborting due to previous error
810

0 commit comments

Comments
 (0)