Skip to content

Commit

Permalink
Auto merge of #80686 - GuillaumeGomez:error-doc-alias-same-name, r=jy…
Browse files Browse the repository at this point in the history
…n514

Error when #[doc(alias)] has same name as the item

Something I came across when reviewing some doc alias PRs.

r? `@jyn514`
  • Loading branch information
bors committed Jan 5, 2021
2 parents f412fb5 + 9714ac0 commit f4b9d32
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 1 deletion.
13 changes: 12 additions & 1 deletion compiler/rustc_passes/src/check_attr.rs
Expand Up @@ -310,7 +310,7 @@ impl CheckAttrVisitor<'tcx> {
.sess
.struct_span_err(
meta.name_value_literal_span().unwrap_or_else(|| meta.span()),
&format!("{:?} character isn't allowed in `#[doc(alias = \"...\")]`", c,),
&format!("{:?} character isn't allowed in `#[doc(alias = \"...\")]`", c),
)
.emit();
return false;
Expand Down Expand Up @@ -358,6 +358,17 @@ impl CheckAttrVisitor<'tcx> {
.emit();
return false;
}
let item_name = self.tcx.hir().name(hir_id);
if item_name.to_string() == doc_alias {
self.tcx
.sess
.struct_span_err(
meta.span(),
&format!("`#[doc(alias = \"...\")]` is the same as the item's name"),
)
.emit();
return false;
}
true
}

Expand Down
4 changes: 4 additions & 0 deletions src/test/rustdoc-ui/doc-alias-same-name.rs
@@ -0,0 +1,4 @@
#![crate_type = "lib"]

#[doc(alias = "Foo")] //~ ERROR
pub struct Foo;
8 changes: 8 additions & 0 deletions src/test/rustdoc-ui/doc-alias-same-name.stderr
@@ -0,0 +1,8 @@
error: `#[doc(alias = "...")]` is the same as the item's name
--> $DIR/doc-alias-same-name.rs:3:7
|
LL | #[doc(alias = "Foo")]
| ^^^^^^^^^^^^^

error: aborting due to previous error

4 changes: 4 additions & 0 deletions src/test/ui/doc-alias-same-name.rs
@@ -0,0 +1,4 @@
#![crate_type = "lib"]

#[doc(alias = "Foo")] //~ ERROR
pub struct Foo;
8 changes: 8 additions & 0 deletions src/test/ui/doc-alias-same-name.stderr
@@ -0,0 +1,8 @@
error: `#[doc(alias = "...")]` is the same as the item's name
--> $DIR/doc-alias-same-name.rs:3:7
|
LL | #[doc(alias = "Foo")]
| ^^^^^^^^^^^^^

error: aborting due to previous error

0 comments on commit f4b9d32

Please sign in to comment.