Rollup merge of #76329 - GuillaumeGomez:doc-alias-crate-level, r=matthewjasper

Add check for doc alias attribute at crate level

Fixes #76298, #64734, #69365.

r? @ollie27
This commit is contained in:
Dylan DPC 2020-10-05 02:29:27 +02:00 committed by GitHub
commit e6e7ccc28d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 547 additions and 404 deletions

View File

@ -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,

View File

@ -13,12 +13,14 @@ 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;
use rustc_span::symbol::sym;
use rustc_span::Span;
use rustc_span::symbol::{sym, Symbol};
use rustc_span::{Span, DUMMY_SP};
pub(crate) fn target_from_impl_item<'tcx>(
tcx: TyCtxt<'tcx>,
@ -333,6 +335,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;
}
}
}
}
@ -808,9 +821,46 @@ fn is_c_like_enum(item: &Item<'_>) -> bool {
}
}
fn check_invalid_crate_level_attr(tcx: TyCtxt<'_>, attrs: &[Attribute]) {
const ATTRS_TO_CHECK: &[Symbol] = &[
sym::macro_export,
sym::repr,
sym::path,
sym::automatically_derived,
sym::start,
sym::main,
];
for attr in attrs {
for attr_to_check in ATTRS_TO_CHECK {
if tcx.sess.check_name(attr, *attr_to_check) {
tcx.sess
.struct_span_err(
attr.span,
&format!(
"`{}` attribute cannot be used at crate level",
attr_to_check.to_ident_string()
),
)
.emit();
}
}
}
}
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() {
CheckAttrVisitor { tcx }.check_attributes(
CRATE_HIR_ID,
tcx.hir().krate_attrs(),
&DUMMY_SP,
Target::Mod,
None,
);
check_invalid_crate_level_attr(tcx, tcx.hir().krate_attrs());
}
}
pub(crate) fn provide(providers: &mut Providers) {

View File

@ -78,29 +78,38 @@ fn entry_fn(tcx: TyCtxt<'_>, cnum: CrateNum) -> Option<(LocalDefId, EntryFnType)
// Beware, this is duplicated in `librustc_builtin_macros/test_harness.rs`
// (with `ast::Item`), so make sure to keep them in sync.
fn entry_point_type(sess: &Session, item: &Item<'_>, at_root: bool) -> EntryPointType {
match item.kind {
ItemKind::Fn(..) => {
if sess.contains_name(&item.attrs, sym::start) {
EntryPointType::Start
} else if sess.contains_name(&item.attrs, sym::main) {
EntryPointType::MainAttr
} else if item.ident.name == sym::main {
if at_root {
// This is a top-level function so can be `main`.
EntryPointType::MainNamed
} else {
EntryPointType::OtherMain
}
} else {
EntryPointType::None
}
if sess.contains_name(&item.attrs, sym::start) {
EntryPointType::Start
} else if sess.contains_name(&item.attrs, sym::main) {
EntryPointType::MainAttr
} else if item.ident.name == sym::main {
if at_root {
// This is a top-level function so can be `main`.
EntryPointType::MainNamed
} else {
EntryPointType::OtherMain
}
_ => EntryPointType::None,
} else {
EntryPointType::None
}
}
fn throw_attr_err(sess: &Session, span: Span, attr: &str) {
sess.struct_span_err(span, &format!("`{}` attribute can only be used on functions", attr))
.emit();
}
fn find_item(item: &Item<'_>, ctxt: &mut EntryContext<'_, '_>, at_root: bool) {
match entry_point_type(&ctxt.session, item, at_root) {
EntryPointType::None => (),
_ if !matches!(item.kind, ItemKind::Fn(..)) => {
if let Some(attr) = ctxt.session.find_by_name(item.attrs, sym::start) {
throw_attr_err(&ctxt.session, attr.span, "start");
}
if let Some(attr) = ctxt.session.find_by_name(item.attrs, sym::main) {
throw_attr_err(&ctxt.session, attr.span, "main");
}
}
EntryPointType::MainNamed => {
if ctxt.main_fn.is_none() {
ctxt.main_fn = Some((item.hir_id, item.span));
@ -137,7 +146,6 @@ fn find_item(item: &Item<'_>, ctxt: &mut EntryContext<'_, '_>, at_root: bool) {
.emit();
}
}
EntryPointType::None => (),
}
}

View File

@ -518,7 +518,7 @@ declare_lint! {
/// ### Example
///
/// ```rust
/// #![macro_export]
/// #![ignore]
/// ```
///
/// {{produces}}

View File

@ -0,0 +1,6 @@
#![feature(doc_alias)]
#![doc(alias = "crate-level-not-working")] //~ ERROR
#[doc(alias = "shouldn't work!")] //~ ERROR
pub fn foo() {}

View File

@ -0,0 +1,14 @@
error: '\'' character isn't allowed in `#[doc(alias = "...")]`
--> $DIR/doc-alias-crate-level.rs:5:7
|
LL | #[doc(alias = "shouldn't work!")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^
error: `#![doc(alias = "...")]` isn't allowed as a crate level attribute
--> $DIR/doc-alias-crate-level.rs:3:8
|
LL | #![doc(alias = "crate-level-not-working")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 2 previous errors

View File

@ -0,0 +1,7 @@
// compile-flags: -Zdeduplicate-diagnostics=no
#![feature(doc_alias)]
#![crate_type = "lib"]
#![doc(alias = "shouldn't work!")] //~ ERROR

View File

@ -0,0 +1,8 @@
error: '\'' character isn't allowed in `#[doc(alias = "...")]`
--> $DIR/doc-alias-crate-level.rs:7:8
|
LL | #![doc(alias = "shouldn't work!")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to previous error

View File

@ -1,3 +1,6 @@
//~ NOTE: not an `extern crate` item
//~^ NOTE: not a function or static
//~^^ NOTE: not a function or closure
// This is testing whether various builtin attributes signals an
// error or warning when put in "weird" places.
//
@ -7,9 +10,25 @@
// ignore-tidy-linelength
// Crate-level is accepted, though it is almost certainly unused?
#![macro_export]
//~^ ERROR: `macro_export` attribute cannot be used at crate level
#![main]
//~^ ERROR: `main` attribute cannot be used at crate level
#![start]
//~^ ERROR: `start` attribute cannot be used at crate level
#![repr()]
//~^ ERROR: `repr` attribute cannot be used at crate level
#![path = "3800"]
//~^ ERROR: `path` attribute cannot be used at crate level
#![automatically_derived]
//~^ ERROR: `automatically_derived` attribute cannot be used at crate level
#![no_mangle]
#![no_link]
//~^ ERROR: attribute should be applied to an `extern crate` item
#![export_name = "2200"]
//~^ ERROR: attribute should be applied to a function or static
#![inline]
//~^ ERROR: attribute should be applied to function or closure
#[inline]
//~^ ERROR attribute should be applied to function or closure
mod inline {
@ -88,4 +107,40 @@ mod export_name {
//~| NOTE not a function or static
}
#[main]
//~^ ERROR: `main` attribute can only be used on functions
mod main {
mod inner { #![main] }
//~^ ERROR: `main` attribute can only be used on functions
// for `fn f()` case, see feature-gate-main.rs
#[main] struct S;
//~^ ERROR: `main` attribute can only be used on functions
#[main] type T = S;
//~^ ERROR: `main` attribute can only be used on functions
#[main] impl S { }
//~^ ERROR: `main` attribute can only be used on functions
}
#[start]
//~^ ERROR: `start` attribute can only be used on functions
mod start {
mod inner { #![start] }
//~^ ERROR: `start` attribute can only be used on functions
// for `fn f()` case, see feature-gate-start.rs
#[start] struct S;
//~^ ERROR: `start` attribute can only be used on functions
#[start] type T = S;
//~^ ERROR: `start` attribute can only be used on functions
#[start] impl S { }
//~^ ERROR: `start` attribute can only be used on functions
}
fn main() {}

View File

@ -1,5 +1,5 @@
error: attribute must be of the form `#[inline]` or `#[inline(always|never)]`
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:22:5
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:41:5
|
LL | #[inline = "2100"] fn f() { }
| ^^^^^^^^^^^^^^^^^^
@ -8,8 +8,68 @@ LL | #[inline = "2100"] fn f() { }
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #57571 <https://github.com/rust-lang/rust/issues/57571>
error: `main` attribute can only be used on functions
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:110:1
|
LL | #[main]
| ^^^^^^^
error: `main` attribute can only be used on functions
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:113:17
|
LL | mod inner { #![main] }
| ^^^^^^^^
error: `main` attribute can only be used on functions
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:118:5
|
LL | #[main] struct S;
| ^^^^^^^
error: `main` attribute can only be used on functions
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:121:5
|
LL | #[main] type T = S;
| ^^^^^^^
error: `main` attribute can only be used on functions
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:124:5
|
LL | #[main] impl S { }
| ^^^^^^^
error: `start` attribute can only be used on functions
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:128:1
|
LL | #[start]
| ^^^^^^^^
error: `start` attribute can only be used on functions
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:131:17
|
LL | mod inner { #![start] }
| ^^^^^^^^^
error: `start` attribute can only be used on functions
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:136:5
|
LL | #[start] struct S;
| ^^^^^^^^
error: `start` attribute can only be used on functions
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:139:5
|
LL | #[start] type T = S;
| ^^^^^^^^
error: `start` attribute can only be used on functions
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:142:5
|
LL | #[start] impl S { }
| ^^^^^^^^
error[E0518]: attribute should be applied to function or closure
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:13:1
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:32:1
|
LL | #[inline]
| ^^^^^^^^^
@ -24,7 +84,7 @@ LL | | }
| |_- not a function or closure
error: attribute should be applied to an `extern crate` item
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:41:1
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:60:1
|
LL | #[no_link]
| ^^^^^^^^^^
@ -39,7 +99,7 @@ LL | | }
| |_- not an `extern crate` item
error: attribute should be applied to a function or static
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:67:1
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:86:1
|
LL | #[export_name = "2200"]
| ^^^^^^^^^^^^^^^^^^^^^^^
@ -53,84 +113,138 @@ LL | |
LL | | }
| |_- not a function or static
error: attribute should be applied to an `extern crate` item
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:26:1
|
LL | #![no_link]
| ^^^^^^^^^^^
error: attribute should be applied to a function or static
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:28:1
|
LL | #![export_name = "2200"]
| ^^^^^^^^^^^^^^^^^^^^^^^^
error[E0518]: attribute should be applied to function or closure
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:18:17
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:30:1
|
LL | #![inline]
| ^^^^^^^^^^
error: `macro_export` attribute cannot be used at crate level
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:13:1
|
LL | #![macro_export]
| ^^^^^^^^^^^^^^^^
error: `main` attribute cannot be used at crate level
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:15:1
|
LL | #![main]
| ^^^^^^^^
error: `start` attribute cannot be used at crate level
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:17:1
|
LL | #![start]
| ^^^^^^^^^
error: `repr` attribute cannot be used at crate level
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:19:1
|
LL | #![repr()]
| ^^^^^^^^^^
error: `path` attribute cannot be used at crate level
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:21:1
|
LL | #![path = "3800"]
| ^^^^^^^^^^^^^^^^^
error: `automatically_derived` attribute cannot be used at crate level
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:23:1
|
LL | #![automatically_derived]
| ^^^^^^^^^^^^^^^^^^^^^^^^^
error[E0518]: attribute should be applied to function or closure
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:37:17
|
LL | mod inner { #![inline] }
| ------------^^^^^^^^^^-- not a function or closure
error[E0518]: attribute should be applied to function or closure
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:28:5
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:47:5
|
LL | #[inline] struct S;
| ^^^^^^^^^ --------- not a function or closure
error[E0518]: attribute should be applied to function or closure
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:32:5
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:51:5
|
LL | #[inline] type T = S;
| ^^^^^^^^^ ----------- not a function or closure
error[E0518]: attribute should be applied to function or closure
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:36:5
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:55:5
|
LL | #[inline] impl S { }
| ^^^^^^^^^ ---------- not a function or closure
error: attribute should be applied to an `extern crate` item
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:46:17
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:65:17
|
LL | mod inner { #![no_link] }
| ------------^^^^^^^^^^^-- not an `extern crate` item
error: attribute should be applied to an `extern crate` item
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:50:5
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:69:5
|
LL | #[no_link] fn f() { }
| ^^^^^^^^^^ ---------- not an `extern crate` item
error: attribute should be applied to an `extern crate` item
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:54:5
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:73:5
|
LL | #[no_link] struct S;
| ^^^^^^^^^^ --------- not an `extern crate` item
error: attribute should be applied to an `extern crate` item
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:58:5
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:77:5
|
LL | #[no_link]type T = S;
| ^^^^^^^^^^----------- not an `extern crate` item
error: attribute should be applied to an `extern crate` item
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:62:5
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:81:5
|
LL | #[no_link] impl S { }
| ^^^^^^^^^^ ---------- not an `extern crate` item
error: attribute should be applied to a function or static
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:72:17
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:91:17
|
LL | mod inner { #![export_name="2200"] }
| ------------^^^^^^^^^^^^^^^^^^^^^^-- not a function or static
error: attribute should be applied to a function or static
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:78:5
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:97:5
|
LL | #[export_name = "2200"] struct S;
| ^^^^^^^^^^^^^^^^^^^^^^^ --------- not a function or static
error: attribute should be applied to a function or static
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:82:5
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:101:5
|
LL | #[export_name = "2200"] type T = S;
| ^^^^^^^^^^^^^^^^^^^^^^^ ----------- not a function or static
error: attribute should be applied to a function or static
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:86:5
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:105:5
|
LL | #[export_name = "2200"] impl S { }
| ^^^^^^^^^^^^^^^^^^^^^^^ ---------- not a function or static
error: aborting due to 17 previous errors
error: aborting due to 36 previous errors
For more information about this error, try `rustc --explain E0518`.

View File

@ -1,3 +1,6 @@
//~ NOTE not a function
//~^ NOTE not a foreign function or static
//~^^ NOTE not a function or static
// This test enumerates as many compiler-builtin ungated attributes as
// possible (that is, all the mutually compatible ones), and checks
// that we get "expected" (*) warnings for each in the various weird
@ -52,20 +55,8 @@
#![forbid(x5200)] //~ WARN unknown lint: `x5200`
#![deny(x5100)] //~ WARN unknown lint: `x5100`
#![macro_use] // (allowed if no argument; see issue-43160-gating-of-macro_use.rs)
#![macro_export] //~ WARN unused attribute
// skipping testing of cfg
// skipping testing of cfg_attr
#![main] //~ WARN unused attribute
#![start] //~ WARN unused attribute
// see issue-43106-gating-of-test.rs for crate-level; but non crate-level is below at "4200"
// see issue-43106-gating-of-bench.rs for crate-level; but non crate-level is below at "4100"
#![repr()]
//~^ WARN unused attribute
#![path = "3800"] //~ WARN unused attribute
#![automatically_derived] //~ WARN unused attribute
#![no_mangle]
#![no_link] //~ WARN unused attribute
// see issue-43106-gating-of-derive.rs
#![should_panic] //~ WARN unused attribute
#![ignore] //~ WARN unused attribute
#![no_implicit_prelude]
@ -75,12 +66,16 @@
// (cannot easily test gating of crate-level #[no_std]; but non crate-level is below at "2600")
#![proc_macro_derive()] //~ WARN unused attribute
#![doc = "2400"]
#![cold]
#![export_name = "2200"]
#![cold] //~ WARN attribute should be applied to a function
//~^ WARN
// see issue-43106-gating-of-builtin-attrs-error.rs
#![link()]
#![link_name = "1900"]
//~^ WARN attribute should be applied to a foreign function
//~^^ WARN this was previously accepted by the compiler
#![link_section = "1800"]
//~^ WARN attribute should be applied to a function or static
//~^^ WARN this was previously accepted by the compiler
// see issue-43106-gating-of-rustc_deprecated.rs
#![must_use]
// see issue-43106-gating-of-stable.rs
@ -254,42 +249,6 @@ mod plugin_registrar {
//~| HELP may be removed in a future compiler version
}
#[main]
//~^ WARN unused attribute
mod main {
mod inner { #![main] }
//~^ WARN unused attribute
// for `fn f()` case, see feature-gate-main.rs
#[main] struct S;
//~^ WARN unused attribute
#[main] type T = S;
//~^ WARN unused attribute
#[main] impl S { }
//~^ WARN unused attribute
}
#[start]
//~^ WARN unused attribute
mod start {
mod inner { #![start] }
//~^ WARN unused attribute
// for `fn f()` case, see feature-gate-start.rs
#[start] struct S;
//~^ WARN unused attribute
#[start] type T = S;
//~^ WARN unused attribute
#[start] impl S { }
//~^ WARN unused attribute
}
// At time of unit test authorship, if compiling without `--test` then
// non-crate-level #[test] attributes seem to be ignored.

View File

@ -2,7 +2,7 @@
#![feature(try_blocks)]
#![inline(never)]
#[inline(never)]
fn do_something_with<T>(_x: T) {}
// This test checks that borrows made and returned inside try blocks are properly constrained

View File

@ -2,7 +2,7 @@
#![feature(try_blocks)]
#![inline(never)]
#[inline(never)]
fn do_something_with<T>(_x: T) {}
// This test checks that borrows made and returned inside try blocks are properly constrained