Skip to content

Commit c30ec5a

Browse files
committed
Check for duplicate arguments in #[rustc_must_implement_one_of]
1 parent 4ccfa97 commit c30ec5a

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

compiler/rustc_typeck/src/collect.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1303,6 +1303,32 @@ fn trait_def(tcx: TyCtxt<'_>, def_id: DefId) -> ty::TraitDef {
13031303
});
13041304

13051305
(errors.count() == 0).then_some(list)
1306+
})
1307+
// Check for duplicates
1308+
.and_then(|list| {
1309+
let mut set: FxHashSet<&Ident> = FxHashSet::default();
1310+
let mut no_dups = true;
1311+
1312+
for ident in &*list {
1313+
if let Some(dup) = set.replace(ident) {
1314+
let dup2 = set.get(&dup).copied().unwrap(); // We've just inserted it
1315+
1316+
tcx.sess
1317+
.struct_span_err(
1318+
vec![dup.span, dup2.span],
1319+
"Functions names are duplicated",
1320+
)
1321+
.note(
1322+
"All `#[rustc_must_implement_one_of]` arguments \
1323+
must be unique",
1324+
)
1325+
.emit();
1326+
1327+
no_dups = false;
1328+
}
1329+
}
1330+
1331+
no_dups.then_some(list)
13061332
});
13071333

13081334
ty::TraitDef::new(
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#![feature(rustc_attrs)]
2+
3+
#[rustc_must_implement_one_of(a, a)]
4+
//~^ Functions names are duplicated
5+
trait Trait {
6+
fn a() {}
7+
}
8+
9+
#[rustc_must_implement_one_of(b, a, a, c, b, c)]
10+
//~^ Functions names are duplicated
11+
//~| Functions names are duplicated
12+
//~| Functions names are duplicated
13+
trait Trait1 {
14+
fn a() {}
15+
fn b() {}
16+
fn c() {}
17+
}
18+
19+
fn main() {}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
error: Functions names are duplicated
2+
--> $DIR/rustc_must_implement_one_of_duplicates.rs:3:31
3+
|
4+
LL | #[rustc_must_implement_one_of(a, a)]
5+
| ^ ^
6+
|
7+
= note: All `#[rustc_must_implement_one_of]` arguments must be unique
8+
9+
error: Functions names are duplicated
10+
--> $DIR/rustc_must_implement_one_of_duplicates.rs:9:34
11+
|
12+
LL | #[rustc_must_implement_one_of(b, a, a, c, b, c)]
13+
| ^ ^
14+
|
15+
= note: All `#[rustc_must_implement_one_of]` arguments must be unique
16+
17+
error: Functions names are duplicated
18+
--> $DIR/rustc_must_implement_one_of_duplicates.rs:9:31
19+
|
20+
LL | #[rustc_must_implement_one_of(b, a, a, c, b, c)]
21+
| ^ ^
22+
|
23+
= note: All `#[rustc_must_implement_one_of]` arguments must be unique
24+
25+
error: Functions names are duplicated
26+
--> $DIR/rustc_must_implement_one_of_duplicates.rs:9:40
27+
|
28+
LL | #[rustc_must_implement_one_of(b, a, a, c, b, c)]
29+
| ^ ^
30+
|
31+
= note: All `#[rustc_must_implement_one_of]` arguments must be unique
32+
33+
error: aborting due to 4 previous errors
34+

0 commit comments

Comments
 (0)