Auto merge of #75888 - GuillaumeGomez:trait-impl-assoc-const-doc-alias, r=ollie27

Add check for doc alias on assoc const in trait impl

Fixes #73721.

r? @ollie27
This commit is contained in:
bors 2020-09-05 09:35:17 +00:00
commit 02fe30971e
3 changed files with 41 additions and 1 deletions

View File

@ -250,13 +250,23 @@ fn check_doc_alias(&self, attr: &Attribute, hir_id: HirId, target: Target) -> bo
None
}
}
Target::AssocConst => {
let parent_hir_id = self.tcx.hir().get_parent_item(hir_id);
let containing_item = self.tcx.hir().expect_item(parent_hir_id);
// We can't link to trait impl's consts.
let err = "associated constant in trait implementation block";
match containing_item.kind {
ItemKind::Impl { of_trait: Some(_), .. } => Some(err),
_ => None,
}
}
_ => None,
} {
self.tcx
.sess
.struct_span_err(
meta.span(),
&format!("`#[doc(alias = \"...\")]` isn't allowed on {}", err,),
&format!("`#[doc(alias = \"...\")]` isn't allowed on {}", err),
)
.emit();
}

View File

@ -0,0 +1,22 @@
#![feature(doc_alias)]
#![feature(trait_alias)]
pub struct Foo;
pub trait Bar {
const BAZ: u8;
}
impl Bar for Foo {
#[doc(alias = "CONST_BAZ")] //~ ERROR
const BAZ: u8 = 0;
}
impl Foo {
#[doc(alias = "CONST_FOO")] // ok!
pub const FOO: u8 = 0;
pub fn bar() -> u8 {
Self::FOO
}
}

View File

@ -0,0 +1,8 @@
error: `#[doc(alias = "...")]` isn't allowed on associated constant in trait implementation block
--> $DIR/doc-alias-assoc-const.rs:11:11
|
LL | #[doc(alias = "CONST_BAZ")]
| ^^^^^^^^^^^^^^^^^^^
error: aborting due to previous error