Skip to content

Commit 2187e7a

Browse files
committed
Enforce that #[marker] traits cannot have associated items
1 parent 7cee7ee commit 2187e7a

File tree

3 files changed

+99
-0
lines changed

3 files changed

+99
-0
lines changed

src/librustc_typeck/check/wfcheck.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,17 @@ fn check_type_defn<'a, 'tcx, F>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
304304

305305
fn check_trait<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, item: &hir::Item) {
306306
let trait_def_id = tcx.hir.local_def_id(item.id);
307+
308+
let trait_def = tcx.trait_def(trait_def_id);
309+
if trait_def.is_marker {
310+
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();
315+
}
316+
}
317+
307318
for_item(tcx, item).with_fcx(|fcx, _| {
308319
check_where_clauses(tcx, fcx, item.span, trait_def_id, None);
309320
vec![]
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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(associated_type_defaults)]
13+
14+
#[marker]
15+
trait MarkerConst {
16+
const N: usize;
17+
//~^ ERROR marker traits cannot have associated items
18+
}
19+
20+
#[marker]
21+
trait MarkerType {
22+
type Output;
23+
//~^ ERROR marker traits cannot have associated items
24+
}
25+
26+
#[marker]
27+
trait MarkerFn {
28+
fn foo();
29+
//~^ ERROR marker traits cannot have associated items
30+
}
31+
32+
#[marker]
33+
trait MarkerConstWithDefault {
34+
const N: usize = 43;
35+
//~^ ERROR marker traits cannot have associated items
36+
}
37+
38+
#[marker]
39+
trait MarkerTypeWithDefault {
40+
type Output = ();
41+
//~^ ERROR marker traits cannot have associated items
42+
}
43+
44+
#[marker]
45+
trait MarkerFnWithDefault {
46+
fn foo() {}
47+
//~^ ERROR marker traits cannot have associated items
48+
}
49+
50+
fn main() {}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
error: marker traits cannot have associated items
2+
--> $DIR/marker-trait-with-associated-items.rs:16:5
3+
|
4+
LL | const N: usize;
5+
| ^^^^^^^^^^^^^^^
6+
7+
error: marker traits cannot have associated items
8+
--> $DIR/marker-trait-with-associated-items.rs:22:5
9+
|
10+
LL | type Output;
11+
| ^^^^^^^^^^^^
12+
13+
error: marker traits cannot have associated items
14+
--> $DIR/marker-trait-with-associated-items.rs:28:5
15+
|
16+
LL | fn foo();
17+
| ^^^^^^^^^
18+
19+
error: marker traits cannot have associated items
20+
--> $DIR/marker-trait-with-associated-items.rs:34:5
21+
|
22+
LL | const N: usize = 43;
23+
| ^^^^^^^^^^^^^^^^^^^^
24+
25+
error: marker traits cannot have associated items
26+
--> $DIR/marker-trait-with-associated-items.rs:40:5
27+
|
28+
LL | type Output = ();
29+
| ^^^^^^^^^^^^^^^^^
30+
31+
error: marker traits cannot have associated items
32+
--> $DIR/marker-trait-with-associated-items.rs:46:5
33+
|
34+
LL | fn foo() {}
35+
| ^^^^^^^^^^^
36+
37+
error: aborting due to 6 previous errors
38+

0 commit comments

Comments
 (0)