Skip to content

Commit

Permalink
Prevent #[doc(alias = "...")] at crate level
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Oct 3, 2020
1 parent 738d4a7 commit 0e68e1b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
4 changes: 2 additions & 2 deletions compiler/rustc_hir/src/target.rs
Expand Up @@ -9,13 +9,13 @@ use crate::{Item, ItemKind, TraitItem, TraitItemKind};

use std::fmt::{self, Display};

#[derive(Copy, Clone, PartialEq)]
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum MethodKind {
Trait { body: bool },
Inherent,
}

#[derive(Copy, Clone, PartialEq)]
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum Target {
ExternCrate,
Use,
Expand Down
18 changes: 17 additions & 1 deletion compiler/rustc_passes/src/check_attr.rs
Expand Up @@ -13,7 +13,7 @@ use rustc_errors::{pluralize, struct_span_err};
use rustc_hir as hir;
use rustc_hir::def_id::LocalDefId;
use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
use rustc_hir::{self, FnSig, ForeignItem, ForeignItemKind, HirId, Item, ItemKind, TraitItem};
use rustc_hir::{self, FnSig, ForeignItem, ForeignItemKind, HirId, Item, ItemKind, TraitItem, CRATE_HIR_ID};
use rustc_hir::{MethodKind, Target};
use rustc_session::lint::builtin::{CONFLICTING_REPR_HINTS, UNUSED_ATTRIBUTES};
use rustc_session::parse::feature_err;
Expand Down Expand Up @@ -333,6 +333,17 @@ impl CheckAttrVisitor<'tcx> {
.emit();
return false;
}
if CRATE_HIR_ID == hir_id {
self.tcx
.sess
.struct_span_err(
meta.span(),
"`#![doc(alias = \"...\")]` isn't allowed as a crate \
level attribute",
)
.emit();
return false;
}
}
}
}
Expand Down Expand Up @@ -811,6 +822,11 @@ fn is_c_like_enum(item: &Item<'_>) -> bool {
fn check_mod_attrs(tcx: TyCtxt<'_>, module_def_id: LocalDefId) {
tcx.hir()
.visit_item_likes_in_module(module_def_id, &mut CheckAttrVisitor { tcx }.as_deep_visitor());
if module_def_id.is_top_level_module() {
for attr in tcx.hir().krate_attrs() {
CheckAttrVisitor { tcx }.check_doc_alias(attr, CRATE_HIR_ID, Target::Mod);
}
}
}

pub(crate) fn provide(providers: &mut Providers) {
Expand Down

0 comments on commit 0e68e1b

Please sign in to comment.