Skip to content

Commit d5b562d

Browse files
committed
Improve error checking and reporting
- Check for valid attributes more reliably - Don't bloat the error index for boring errors - Do use real error codes for the interesting ones
1 parent 2187e7a commit d5b562d

File tree

9 files changed

+165
-49
lines changed

9 files changed

+165
-49
lines changed

src/librustc/diagnostics.rs

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2116,34 +2116,6 @@ struct Foo;
21162116
```
21172117
"##,
21182118

2119-
E0713: r##"
2120-
This error indicates that a `#[marker]` attribute was incorrectly placed
2121-
on something other than a trait.
2122-
2123-
Examples of erroneous code:
2124-
2125-
```compile_fail,E0713
2126-
# #![feature(marker_trait_attr)]
2127-
2128-
#[marker]
2129-
struct Foo { }
2130-
```
2131-
"##,
2132-
2133-
E0714: r##"
2134-
This error indicates that a `#[marker]` attribute had a value. The
2135-
`#[marker]` should be empty.
2136-
2137-
Examples of erroneous code:
2138-
2139-
```compile_fail,E0714
2140-
# #![feature(marker_trait_attr)]
2141-
2142-
#[marker(anything)]
2143-
trait Foo {}
2144-
```
2145-
"##,
2146-
21472119
}
21482120

21492121

src/librustc/hir/check_attr.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -123,22 +123,17 @@ impl<'a, 'tcx> CheckAttrVisitor<'a, 'tcx> {
123123
match target {
124124
Target::Trait => { /* Valid */ },
125125
_ => {
126-
struct_span_err!(self.tcx.sess,
127-
attr.span,
128-
E0713,
129-
"attribute can only be applied to a trait")
126+
self.tcx.sess
127+
.struct_span_err(attr.span, "attribute can only be applied to a trait")
130128
.span_label(item.span, "not a trait")
131129
.emit();
132130
return;
133131
}
134132
}
135133

136-
if attr.meta_item_list().is_some() || attr.value_str().is_some() {
137-
struct_span_err!(self.tcx.sess,
138-
attr.span,
139-
E0714,
140-
"attribute should be empty")
141-
.span_label(item.span, "not empty")
134+
if !attr.is_word() {
135+
self.tcx.sess
136+
.struct_span_err(attr.span, "attribute should be empty")
142137
.emit();
143138
}
144139
}

src/librustc_typeck/check/wfcheck.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -308,10 +308,12 @@ fn check_trait<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, item: &hir::Item) {
308308
let trait_def = tcx.trait_def(trait_def_id);
309309
if trait_def.is_marker {
310310
for associated_def_id in &*tcx.associated_item_def_ids(trait_def_id) {
311-
tcx.sess.struct_span_err(
312-
tcx.def_span(*associated_def_id),
313-
"marker traits cannot have associated items",
314-
).emit();
311+
struct_span_err!(
312+
tcx.sess,
313+
tcx.def_span(*associated_def_id),
314+
E0714,
315+
"marker traits cannot have associated items",
316+
).emit();
315317
}
316318
}
317319

src/librustc_typeck/diagnostics.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4750,6 +4750,22 @@ ambiguity for some types, we disallow calling methods on raw pointers when
47504750
the type is unknown.
47514751
"##,
47524752

4753+
E0714: r##"
4754+
A `#[marker]` trait contained an associated item.
4755+
4756+
The items of marker traits cannot be overridden, so there's no need to have them
4757+
when they cannot be changed per-type anyway. If you wanted them for ergonomic
4758+
reasons, consider making an extension trait instead.
4759+
"##,
4760+
4761+
E0715: r##"
4762+
An `impl` for a `#[marker]` trait tried to override an associated item.
4763+
4764+
Because marker traits are allowed to have multiple implementations for the same
4765+
type, it's not allowed to override anything in those implementations, as it
4766+
would be ambiguous which override should actually be used.
4767+
"##,
4768+
47534769
}
47544770

47554771
register_diagnostics! {
@@ -4833,5 +4849,4 @@ register_diagnostics! {
48334849
E0641, // cannot cast to/from a pointer with an unknown kind
48344850
E0645, // trait aliases not finished
48354851
E0698, // type inside generator must be known in this context
4836-
E0715, // impls for marker traits cannot contain items
48374852
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(marker_trait_attr)]
12+
13+
#[marker] //~ ERROR attribute can only be applied to a trait
14+
struct Struct {}
15+
16+
#[marker] //~ ERROR attribute can only be applied to a trait
17+
impl Struct {}
18+
19+
#[marker] //~ ERROR attribute can only be applied to a trait
20+
union Union {
21+
x: i32,
22+
}
23+
24+
#[marker] //~ ERROR attribute can only be applied to a trait
25+
const CONST: usize = 10;
26+
27+
#[marker] //~ ERROR attribute can only be applied to a trait
28+
fn function() {}
29+
30+
#[marker] //~ ERROR attribute can only be applied to a trait
31+
type Type = ();
32+
33+
fn main() {}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
error: attribute can only be applied to a trait
2+
--> $DIR/marker-attribute-on-non-trait.rs:13:1
3+
|
4+
LL | #[marker] //~ ERROR attribute can only be applied to a trait
5+
| ^^^^^^^^^
6+
LL | struct Struct {}
7+
| ---------------- not a trait
8+
9+
error: attribute can only be applied to a trait
10+
--> $DIR/marker-attribute-on-non-trait.rs:16:1
11+
|
12+
LL | #[marker] //~ ERROR attribute can only be applied to a trait
13+
| ^^^^^^^^^
14+
LL | impl Struct {}
15+
| -------------- not a trait
16+
17+
error: attribute can only be applied to a trait
18+
--> $DIR/marker-attribute-on-non-trait.rs:19:1
19+
|
20+
LL | #[marker] //~ ERROR attribute can only be applied to a trait
21+
| ^^^^^^^^^
22+
LL | / union Union {
23+
LL | | x: i32,
24+
LL | | }
25+
| |_- not a trait
26+
27+
error: attribute can only be applied to a trait
28+
--> $DIR/marker-attribute-on-non-trait.rs:24:1
29+
|
30+
LL | #[marker] //~ ERROR attribute can only be applied to a trait
31+
| ^^^^^^^^^
32+
LL | const CONST: usize = 10;
33+
| ------------------------ not a trait
34+
35+
error: attribute can only be applied to a trait
36+
--> $DIR/marker-attribute-on-non-trait.rs:27:1
37+
|
38+
LL | #[marker] //~ ERROR attribute can only be applied to a trait
39+
| ^^^^^^^^^
40+
LL | fn function() {}
41+
| ---------------- not a trait
42+
43+
error: attribute can only be applied to a trait
44+
--> $DIR/marker-attribute-on-non-trait.rs:30:1
45+
|
46+
LL | #[marker] //~ ERROR attribute can only be applied to a trait
47+
| ^^^^^^^^^
48+
LL | type Type = ();
49+
| --------------- not a trait
50+
51+
error: aborting due to 6 previous errors
52+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(marker_trait_attr)]
12+
#![feature(unrestricted_attribute_tokens)]
13+
14+
#[marker(always)]
15+
trait Marker1 {}
16+
//~^^ ERROR attribute should be empty
17+
18+
#[marker("never")]
19+
trait Marker2 {}
20+
//~^^ ERROR attribute should be empty
21+
22+
#[marker(key = value)]
23+
trait Marker3 {}
24+
//~^^ ERROR attribute should be empty
25+
26+
fn main() {}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error: attribute should be empty
2+
--> $DIR/marker-attribute-with-values.rs:14:1
3+
|
4+
LL | #[marker(always)]
5+
| ^^^^^^^^^^^^^^^^^
6+
7+
error: attribute should be empty
8+
--> $DIR/marker-attribute-with-values.rs:18:1
9+
|
10+
LL | #[marker("never")]
11+
| ^^^^^^^^^^^^^^^^^^
12+
13+
error: attribute should be empty
14+
--> $DIR/marker-attribute-with-values.rs:22:1
15+
|
16+
LL | #[marker(key = value)]
17+
| ^^^^^^^^^^^^^^^^^^^^^^
18+
19+
error: aborting due to 3 previous errors
20+
Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,39 @@
1-
error: marker traits cannot have associated items
1+
error[E0714]: marker traits cannot have associated items
22
--> $DIR/marker-trait-with-associated-items.rs:16:5
33
|
44
LL | const N: usize;
55
| ^^^^^^^^^^^^^^^
66

7-
error: marker traits cannot have associated items
7+
error[E0714]: marker traits cannot have associated items
88
--> $DIR/marker-trait-with-associated-items.rs:22:5
99
|
1010
LL | type Output;
1111
| ^^^^^^^^^^^^
1212

13-
error: marker traits cannot have associated items
13+
error[E0714]: marker traits cannot have associated items
1414
--> $DIR/marker-trait-with-associated-items.rs:28:5
1515
|
1616
LL | fn foo();
1717
| ^^^^^^^^^
1818

19-
error: marker traits cannot have associated items
19+
error[E0714]: marker traits cannot have associated items
2020
--> $DIR/marker-trait-with-associated-items.rs:34:5
2121
|
2222
LL | const N: usize = 43;
2323
| ^^^^^^^^^^^^^^^^^^^^
2424

25-
error: marker traits cannot have associated items
25+
error[E0714]: marker traits cannot have associated items
2626
--> $DIR/marker-trait-with-associated-items.rs:40:5
2727
|
2828
LL | type Output = ();
2929
| ^^^^^^^^^^^^^^^^^
3030

31-
error: marker traits cannot have associated items
31+
error[E0714]: marker traits cannot have associated items
3232
--> $DIR/marker-trait-with-associated-items.rs:46:5
3333
|
3434
LL | fn foo() {}
3535
| ^^^^^^^^^^^
3636

3737
error: aborting due to 6 previous errors
3838

39+
For more information about this error, try `rustc --explain E0714`.

0 commit comments

Comments
 (0)