Skip to content

Commit f4b9d32

Browse files
committed
Auto merge of #80686 - GuillaumeGomez:error-doc-alias-same-name, r=jyn514
Error when #[doc(alias)] has same name as the item Something I came across when reviewing some doc alias PRs. r? `@jyn514`
2 parents f412fb5 + 9714ac0 commit f4b9d32

File tree

5 files changed

+36
-1
lines changed

5 files changed

+36
-1
lines changed

compiler/rustc_passes/src/check_attr.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ impl CheckAttrVisitor<'tcx> {
310310
.sess
311311
.struct_span_err(
312312
meta.name_value_literal_span().unwrap_or_else(|| meta.span()),
313-
&format!("{:?} character isn't allowed in `#[doc(alias = \"...\")]`", c,),
313+
&format!("{:?} character isn't allowed in `#[doc(alias = \"...\")]`", c),
314314
)
315315
.emit();
316316
return false;
@@ -358,6 +358,17 @@ impl CheckAttrVisitor<'tcx> {
358358
.emit();
359359
return false;
360360
}
361+
let item_name = self.tcx.hir().name(hir_id);
362+
if item_name.to_string() == doc_alias {
363+
self.tcx
364+
.sess
365+
.struct_span_err(
366+
meta.span(),
367+
&format!("`#[doc(alias = \"...\")]` is the same as the item's name"),
368+
)
369+
.emit();
370+
return false;
371+
}
361372
true
362373
}
363374

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#![crate_type = "lib"]
2+
3+
#[doc(alias = "Foo")] //~ ERROR
4+
pub struct Foo;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: `#[doc(alias = "...")]` is the same as the item's name
2+
--> $DIR/doc-alias-same-name.rs:3:7
3+
|
4+
LL | #[doc(alias = "Foo")]
5+
| ^^^^^^^^^^^^^
6+
7+
error: aborting due to previous error
8+

src/test/ui/doc-alias-same-name.rs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#![crate_type = "lib"]
2+
3+
#[doc(alias = "Foo")] //~ ERROR
4+
pub struct Foo;
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: `#[doc(alias = "...")]` is the same as the item's name
2+
--> $DIR/doc-alias-same-name.rs:3:7
3+
|
4+
LL | #[doc(alias = "Foo")]
5+
| ^^^^^^^^^^^^^
6+
7+
error: aborting due to previous error
8+

0 commit comments

Comments
 (0)