Skip to content

Add visitors for checking #[inline] #80641

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions compiler/rustc_hir/src/target.rs
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
17 changes: 13 additions & 4 deletions src/test/ui/proc-macro/ambiguous-builtin-attrs.stderr
Original file line number Diff line number Diff line change
@@ -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 @@ -61,14 +61,14 @@ LL | use builtin_attrs::*;
= help: use `crate::repr` to refer to this attribute macro unambiguously

error[E0659]: `allow` is ambiguous (built-in attribute vs any other name)
--> $DIR/ambiguous-builtin-attrs.rs:36:3
--> $DIR/ambiguous-builtin-attrs.rs:38:3
|
LL | #[allow(unused)]
| ^^^^^ ambiguous name
|
= note: `allow` could refer to a built-in attribute
note: `allow` could also refer to the built-in attribute imported here
--> $DIR/ambiguous-builtin-attrs.rs:35:5
--> $DIR/ambiguous-builtin-attrs.rs:37:5
|
LL | use deny as allow;
| ^^^^^^^^^^^^^
Expand All @@ -94,7 +94,16 @@ 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: aborting due to 8 previous errors
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 9 previous errors

Some errors have detailed explanations: E0425, E0517, E0659.
For more information about an error, try `rustc --explain E0425`.