Skip to content

Commit

Permalink
Fixed #[inline] to be warned in fields, arms, macro defs
Browse files Browse the repository at this point in the history
Add visitors for checking #[inline]

Add visitors for checking #[inline] with struct field

Fix test for #[inline]

Add visitors for checking #[inline] with #[macro_export] macro

Add visitors for checking #[inline] without #[macro_export] macro

Add use alias with Visitor

Fix lint error

Reduce unnecessary variable

Co-authored-by: LingMan <LingMan@users.noreply.github.com>

Change error to warning

Add warning for checking field, arm with #[allow_internal_unstable]

Add name resolver

Formatting

Formatting

Fix error fixture

Add checking field, arm, macro def
  • Loading branch information
Danue1 committed Feb 1, 2021
1 parent e0d9f79 commit 8bbb2d0
Show file tree
Hide file tree
Showing 9 changed files with 341 additions and 53 deletions.
6 changes: 6 additions & 0 deletions compiler/rustc_hir/src/target.rs
Expand Up @@ -38,19 +38,22 @@ pub enum Target {
Enum,
Variant,
Struct,
Field,
Union,
Trait,
TraitAlias,
Impl,
Expression,
Statement,
Arm,
AssocConst,
Method(MethodKind),
AssocTy,
ForeignFn,
ForeignStatic,
ForeignTy,
GenericParam(GenericParamKind),
MacroDef,
}

impl Display for Target {
Expand All @@ -73,12 +76,14 @@ impl Display for Target {
Target::Enum => "enum",
Target::Variant => "enum variant",
Target::Struct => "struct",
Target::Field => "struct field",
Target::Union => "union",
Target::Trait => "trait",
Target::TraitAlias => "trait alias",
Target::Impl => "item",
Target::Expression => "expression",
Target::Statement => "statement",
Target::Arm => "match arm",
Target::AssocConst => "associated const",
Target::Method(_) => "method",
Target::AssocTy => "associated type",
Expand All @@ -90,6 +95,7 @@ impl Display for Target {
GenericParamKind::Lifetime => "lifetime parameter",
GenericParamKind::Const => "const parameter",
},
Target::MacroDef => "macro def",
}
)
}
Expand Down
18 changes: 18 additions & 0 deletions compiler/rustc_middle/src/hir/map/mod.rs
Expand Up @@ -8,6 +8,7 @@ use rustc_hir::def::{DefKind, Res};
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, CRATE_DEF_INDEX, LOCAL_CRATE};
use rustc_hir::definitions::{DefKey, DefPath, Definitions};
use rustc_hir::intravisit;
use rustc_hir::intravisit::Visitor;
use rustc_hir::itemlikevisit::ItemLikeVisitor;
use rustc_hir::*;
use rustc_index::vec::IndexVec;
Expand Down Expand Up @@ -494,6 +495,15 @@ impl<'hir> Map<'hir> {
}
}

pub fn visit_exported_macros_in_krate<V>(&self, visitor: &mut V)
where
V: Visitor<'hir>,
{
for id in self.krate().exported_macros {
visitor.visit_macro_def(self.expect_macro_def(id.hir_id));
}
}

/// Retrieves the `Node` corresponding to `id`, panicking if it cannot be found.
pub fn get(&self, id: HirId) -> Node<'hir> {
self.find(id).unwrap_or_else(|| bug!("couldn't find hir id {} in the HIR map", id))
Expand Down Expand Up @@ -802,6 +812,13 @@ impl<'hir> Map<'hir> {
}
}

pub fn expect_macro_def(&self, id: HirId) -> &'hir MacroDef<'hir> {
match self.find(id) {
Some(Node::MacroDef(macro_def)) => macro_def,
_ => bug!("expected macro def, found {}", self.node_to_string(id)),
}
}

pub fn expect_expr(&self, id: HirId) -> &'hir Expr<'hir> {
match self.find(id) {
Some(Node::Expr(expr)) => expr,
Expand All @@ -821,6 +838,7 @@ impl<'hir> Map<'hir> {
Node::GenericParam(param) => param.name.ident().name,
Node::Binding(&Pat { kind: PatKind::Binding(_, _, l, _), .. }) => l.name,
Node::Ctor(..) => self.name(self.get_parent_item(id)),
Node::MacroDef(md) => md.ident.name,
_ => return None,
})
}
Expand Down
308 changes: 263 additions & 45 deletions compiler/rustc_passes/src/check_attr.rs

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions src/test/ui/attr-usage-inline.rs
Expand Up @@ -6,4 +6,20 @@ fn f() {}
#[inline] //~ ERROR: attribute should be applied to function or closure
struct S;

struct I {
#[inline]
i: u8,
}

#[macro_export]
#[inline]
macro_rules! m_e {
() => {};
}

#[inline] //~ ERROR: attribute should be applied to function or closure
macro_rules! m {
() => {};
}

fn main() {}
8 changes: 7 additions & 1 deletion src/test/ui/attr-usage-inline.stderr
Expand Up @@ -6,6 +6,12 @@ LL | #[inline]
LL | struct S;
| --------- not a function or closure

error: aborting due to previous error
error[E0518]: attribute should be applied to function or closure
--> $DIR/attr-usage-inline.rs:20:1
|
LL | #[inline]
| ^^^^^^^^^ not a function or closure

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0518`.
13 changes: 13 additions & 0 deletions src/test/ui/internal/internal-unstable.rs
@@ -1,10 +1,17 @@
// aux-build:internal_unstable.rs

#![feature(allow_internal_unstable)]
#[allow(dead_code)]

#[macro_use]
extern crate internal_unstable;

struct Baz {
#[allow_internal_unstable]
//^ WARN `#[allow_internal_unstable]` is ignored on struct fields and match arms
baz: u8,
}

macro_rules! foo {
($e: expr, $f: expr) => {{
$e;
Expand Down Expand Up @@ -40,4 +47,10 @@ fn main() {
println!("{:?}", internal_unstable::unstable()); //~ ERROR use of unstable

bar!(internal_unstable::unstable()); //~ ERROR use of unstable

match true {
#[allow_internal_unstable]
//^ WARN `#[allow_internal_unstable]` is ignored on struct fields and match arms
_ => {}
}
}
10 changes: 5 additions & 5 deletions src/test/ui/internal/internal-unstable.stderr
@@ -1,37 +1,37 @@
error[E0658]: use of unstable library feature 'function'
--> $DIR/internal-unstable.rs:34:25
--> $DIR/internal-unstable.rs:41:25
|
LL | pass_through_allow!(internal_unstable::unstable());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: add `#![feature(function)]` to the crate attributes to enable

error[E0658]: use of unstable library feature 'function'
--> $DIR/internal-unstable.rs:36:27
--> $DIR/internal-unstable.rs:43:27
|
LL | pass_through_noallow!(internal_unstable::unstable());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: add `#![feature(function)]` to the crate attributes to enable

error[E0658]: use of unstable library feature 'function'
--> $DIR/internal-unstable.rs:40:22
--> $DIR/internal-unstable.rs:47:22
|
LL | println!("{:?}", internal_unstable::unstable());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: add `#![feature(function)]` to the crate attributes to enable

error[E0658]: use of unstable library feature 'function'
--> $DIR/internal-unstable.rs:42:10
--> $DIR/internal-unstable.rs:49:10
|
LL | bar!(internal_unstable::unstable());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: add `#![feature(function)]` to the crate attributes to enable

error[E0658]: use of unstable library feature 'function'
--> $DIR/internal-unstable.rs:12:9
--> $DIR/internal-unstable.rs:19:9
|
LL | internal_unstable::unstable();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
4 changes: 3 additions & 1 deletion src/test/ui/proc-macro/ambiguous-builtin-attrs.rs
Expand Up @@ -21,7 +21,9 @@ fn non_macro_expanded_location<#[repr(C)] T>() {
//~^ ERROR `repr` is ambiguous
//~| ERROR attribute should be applied to a struct, enum, or union
match 0u8 {
#[repr(C)] //~ ERROR `repr` is ambiguous
#[repr(C)]
//~^ ERROR `repr` is ambiguous
//~| ERROR attribute should be applied to a struct, enum, or union
_ => {}
}
}
Expand Down
11 changes: 10 additions & 1 deletion src/test/ui/proc-macro/ambiguous-builtin-attrs.stderr
@@ -1,5 +1,5 @@
error[E0425]: cannot find value `NonExistent` in this scope
--> $DIR/ambiguous-builtin-attrs.rs:32:5
--> $DIR/ambiguous-builtin-attrs.rs:34:5
|
LL | NonExistent;
| ^^^^^^^^^^^ not found in this scope
Expand Down Expand Up @@ -94,6 +94,15 @@ error[E0517]: attribute should be applied to a struct, enum, or union
LL | fn non_macro_expanded_location<#[repr(C)] T>() {
| ^ - not a struct, enum, or union

error[E0517]: attribute should be applied to a struct, enum, or union
--> $DIR/ambiguous-builtin-attrs.rs:24:16
|
LL | #[repr(C)]
| ^
...
LL | _ => {}
| ------- not a struct, enum, or union

error: aborting due to 8 previous errors

Some errors have detailed explanations: E0425, E0517, E0659.
Expand Down

0 comments on commit 8bbb2d0

Please sign in to comment.