Skip to content

Commit b04fda8

Browse files
Add doc(alias) attribute checks for associated consts and associated types
1 parent 23a2ba6 commit b04fda8

File tree

3 files changed

+57
-9
lines changed

3 files changed

+57
-9
lines changed

src/librustdoc/visit_ast.rs

+28-2
Original file line numberDiff line numberDiff line change
@@ -591,8 +591,14 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
591591
// Don't duplicate impls when inlining or if it's implementing a trait, we'll pick
592592
// them up regardless of where they're located.
593593
if !self.inlining && of_trait.is_none() {
594-
let items =
595-
items.iter().map(|item| self.cx.tcx.hir().impl_item(item.id)).collect();
594+
let items = items
595+
.iter()
596+
.map(|item| {
597+
let item = self.cx.tcx.hir().impl_item(item.id);
598+
self.check_impl_doc_alias_attr(item);
599+
item
600+
})
601+
.collect();
596602
let i = Impl {
597603
unsafety,
598604
polarity,
@@ -608,11 +614,31 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
608614
vis: &item.vis,
609615
};
610616
om.impls.push(i);
617+
} else if of_trait.is_some() {
618+
for item in items.iter() {
619+
self.check_impl_doc_alias_attr(self.cx.tcx.hir().impl_item(item.id));
620+
}
611621
}
612622
}
613623
}
614624
}
615625

626+
fn check_impl_doc_alias_attr(&self, item: &hir::ImplItem<'_>) {
627+
match item.kind {
628+
hir::ImplItemKind::Const(_, _) => check_doc_alias_attrs(
629+
&item.attrs,
630+
"const in implementation block",
631+
self.cx.sess().diagnostic(),
632+
),
633+
hir::ImplItemKind::TyAlias(_) => check_doc_alias_attrs(
634+
&item.attrs,
635+
"type alias in implementation block",
636+
self.cx.sess().diagnostic(),
637+
),
638+
hir::ImplItemKind::Fn(_, _) => {}
639+
}
640+
}
641+
616642
fn visit_foreign_item(
617643
&mut self,
618644
item: &'tcx hir::ForeignItem<'_>,
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
11
#![feature(doc_alias)]
22

33
pub struct Bar;
4-
pub trait Foo {}
4+
pub trait Foo {
5+
type X;
6+
fn foo() -> Self::X;
7+
}
58

69
#[doc(alias = "foo")] //~ ERROR
710
extern {}
811

912
#[doc(alias = "bar")] //~ ERROR
10-
impl Bar {}
13+
impl Bar {
14+
#[doc(alias = "const")] //~ ERROR
15+
const A: u32 = 0;
16+
}
1117

1218
#[doc(alias = "foobar")] //~ ERROR
13-
impl Foo for Bar {}
19+
impl Foo for Bar {
20+
#[doc(alias = "assoc")] //~ ERROR
21+
type X = i32;
22+
fn foo() -> Self::X { 0 }
23+
}
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,32 @@
11
error: `#[doc(alias = "...")]` isn't allowed on extern block
2-
--> $DIR/check-doc-alias-attr-location.rs:6:7
2+
--> $DIR/check-doc-alias-attr-location.rs:9:7
33
|
44
LL | #[doc(alias = "foo")]
55
| ^^^^^^^^^^^^^
66

77
error: `#[doc(alias = "...")]` isn't allowed on implementation block
8-
--> $DIR/check-doc-alias-attr-location.rs:9:7
8+
--> $DIR/check-doc-alias-attr-location.rs:12:7
99
|
1010
LL | #[doc(alias = "bar")]
1111
| ^^^^^^^^^^^^^
1212

13+
error: `#[doc(alias = "...")]` isn't allowed on const in implementation block
14+
--> $DIR/check-doc-alias-attr-location.rs:14:11
15+
|
16+
LL | #[doc(alias = "const")]
17+
| ^^^^^^^^^^^^^^^
18+
1319
error: `#[doc(alias = "...")]` isn't allowed on implementation block
14-
--> $DIR/check-doc-alias-attr-location.rs:12:7
20+
--> $DIR/check-doc-alias-attr-location.rs:18:7
1521
|
1622
LL | #[doc(alias = "foobar")]
1723
| ^^^^^^^^^^^^^^^^
1824

19-
error: aborting due to 3 previous errors
25+
error: `#[doc(alias = "...")]` isn't allowed on type alias in implementation block
26+
--> $DIR/check-doc-alias-attr-location.rs:20:11
27+
|
28+
LL | #[doc(alias = "assoc")]
29+
| ^^^^^^^^^^^^^^^
30+
31+
error: aborting due to 5 previous errors
2032

0 commit comments

Comments
 (0)