Skip to content

Commit d4e3570

Browse files
committed
Auto merge of #80641 - Danue1:patch-1, r=oli-obk
Add visitors for checking #[inline] For #80564
2 parents 02b85d7 + 838f487 commit d4e3570

File tree

9 files changed

+344
-56
lines changed

9 files changed

+344
-56
lines changed

compiler/rustc_hir/src/target.rs

+6
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,22 @@ pub enum Target {
3838
Enum,
3939
Variant,
4040
Struct,
41+
Field,
4142
Union,
4243
Trait,
4344
TraitAlias,
4445
Impl,
4546
Expression,
4647
Statement,
48+
Arm,
4749
AssocConst,
4850
Method(MethodKind),
4951
AssocTy,
5052
ForeignFn,
5153
ForeignStatic,
5254
ForeignTy,
5355
GenericParam(GenericParamKind),
56+
MacroDef,
5457
}
5558

5659
impl Display for Target {
@@ -73,12 +76,14 @@ impl Display for Target {
7376
Target::Enum => "enum",
7477
Target::Variant => "enum variant",
7578
Target::Struct => "struct",
79+
Target::Field => "struct field",
7680
Target::Union => "union",
7781
Target::Trait => "trait",
7882
Target::TraitAlias => "trait alias",
7983
Target::Impl => "item",
8084
Target::Expression => "expression",
8185
Target::Statement => "statement",
86+
Target::Arm => "match arm",
8287
Target::AssocConst => "associated const",
8388
Target::Method(_) => "method",
8489
Target::AssocTy => "associated type",
@@ -90,6 +95,7 @@ impl Display for Target {
9095
GenericParamKind::Lifetime => "lifetime parameter",
9196
GenericParamKind::Const => "const parameter",
9297
},
98+
Target::MacroDef => "macro def",
9399
}
94100
)
95101
}

compiler/rustc_middle/src/hir/map/mod.rs

+18
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use rustc_hir::def::{DefKind, Res};
88
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, CRATE_DEF_INDEX, LOCAL_CRATE};
99
use rustc_hir::definitions::{DefKey, DefPath, Definitions};
1010
use rustc_hir::intravisit;
11+
use rustc_hir::intravisit::Visitor;
1112
use rustc_hir::itemlikevisit::ItemLikeVisitor;
1213
use rustc_hir::*;
1314
use rustc_index::vec::IndexVec;
@@ -494,6 +495,15 @@ impl<'hir> Map<'hir> {
494495
}
495496
}
496497

498+
pub fn visit_exported_macros_in_krate<V>(&self, visitor: &mut V)
499+
where
500+
V: Visitor<'hir>,
501+
{
502+
for id in self.krate().exported_macros {
503+
visitor.visit_macro_def(self.expect_macro_def(id.hir_id));
504+
}
505+
}
506+
497507
/// Retrieves the `Node` corresponding to `id`, panicking if it cannot be found.
498508
pub fn get(&self, id: HirId) -> Node<'hir> {
499509
self.find(id).unwrap_or_else(|| bug!("couldn't find hir id {} in the HIR map", id))
@@ -802,6 +812,13 @@ impl<'hir> Map<'hir> {
802812
}
803813
}
804814

815+
pub fn expect_macro_def(&self, id: HirId) -> &'hir MacroDef<'hir> {
816+
match self.find(id) {
817+
Some(Node::MacroDef(macro_def)) => macro_def,
818+
_ => bug!("expected macro def, found {}", self.node_to_string(id)),
819+
}
820+
}
821+
805822
pub fn expect_expr(&self, id: HirId) -> &'hir Expr<'hir> {
806823
match self.find(id) {
807824
Some(Node::Expr(expr)) => expr,
@@ -821,6 +838,7 @@ impl<'hir> Map<'hir> {
821838
Node::GenericParam(param) => param.name.ident().name,
822839
Node::Binding(&Pat { kind: PatKind::Binding(_, _, l, _), .. }) => l.name,
823840
Node::Ctor(..) => self.name(self.get_parent_item(id)),
841+
Node::MacroDef(md) => md.ident.name,
824842
_ => return None,
825843
})
826844
}

compiler/rustc_passes/src/check_attr.rs

+263-45
Large diffs are not rendered by default.

src/test/ui/attr-usage-inline.rs

+16
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,20 @@ fn f() {}
66
#[inline] //~ ERROR: attribute should be applied to function or closure
77
struct S;
88

9+
struct I {
10+
#[inline]
11+
i: u8,
12+
}
13+
14+
#[macro_export]
15+
#[inline]
16+
macro_rules! m_e {
17+
() => {};
18+
}
19+
20+
#[inline] //~ ERROR: attribute should be applied to function or closure
21+
macro_rules! m {
22+
() => {};
23+
}
24+
925
fn main() {}

src/test/ui/attr-usage-inline.stderr

+7-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ LL | #[inline]
66
LL | struct S;
77
| --------- not a function or closure
88

9-
error: aborting due to previous error
9+
error[E0518]: attribute should be applied to function or closure
10+
--> $DIR/attr-usage-inline.rs:20:1
11+
|
12+
LL | #[inline]
13+
| ^^^^^^^^^ not a function or closure
14+
15+
error: aborting due to 2 previous errors
1016

1117
For more information about this error, try `rustc --explain E0518`.

src/test/ui/internal/internal-unstable.rs

+13
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
// aux-build:internal_unstable.rs
22

33
#![feature(allow_internal_unstable)]
4+
#[allow(dead_code)]
45

56
#[macro_use]
67
extern crate internal_unstable;
78

9+
struct Baz {
10+
#[allow_internal_unstable]
11+
//^ WARN `#[allow_internal_unstable]` is ignored on struct fields and match arms
12+
baz: u8,
13+
}
14+
815
macro_rules! foo {
916
($e: expr, $f: expr) => {{
1017
$e;
@@ -40,4 +47,10 @@ fn main() {
4047
println!("{:?}", internal_unstable::unstable()); //~ ERROR use of unstable
4148

4249
bar!(internal_unstable::unstable()); //~ ERROR use of unstable
50+
51+
match true {
52+
#[allow_internal_unstable]
53+
//^ WARN `#[allow_internal_unstable]` is ignored on struct fields and match arms
54+
_ => {}
55+
}
4356
}

src/test/ui/internal/internal-unstable.stderr

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,37 @@
11
error[E0658]: use of unstable library feature 'function'
2-
--> $DIR/internal-unstable.rs:34:25
2+
--> $DIR/internal-unstable.rs:41:25
33
|
44
LL | pass_through_allow!(internal_unstable::unstable());
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
77
= help: add `#![feature(function)]` to the crate attributes to enable
88

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

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

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

3333
error[E0658]: use of unstable library feature 'function'
34-
--> $DIR/internal-unstable.rs:12:9
34+
--> $DIR/internal-unstable.rs:19:9
3535
|
3636
LL | internal_unstable::unstable();
3737
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^

src/test/ui/proc-macro/ambiguous-builtin-attrs.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ fn non_macro_expanded_location<#[repr(C)] T>() {
2121
//~^ ERROR `repr` is ambiguous
2222
//~| ERROR attribute should be applied to a struct, enum, or union
2323
match 0u8 {
24-
#[repr(C)] //~ ERROR `repr` is ambiguous
24+
#[repr(C)]
25+
//~^ ERROR `repr` is ambiguous
26+
//~| ERROR attribute should be applied to a struct, enum, or union
2527
_ => {}
2628
}
2729
}

src/test/ui/proc-macro/ambiguous-builtin-attrs.stderr

+13-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0425]: cannot find value `NonExistent` in this scope
2-
--> $DIR/ambiguous-builtin-attrs.rs:32:5
2+
--> $DIR/ambiguous-builtin-attrs.rs:34:5
33
|
44
LL | NonExistent;
55
| ^^^^^^^^^^^ not found in this scope
@@ -61,14 +61,14 @@ LL | use builtin_attrs::*;
6161
= help: use `crate::repr` to refer to this attribute macro unambiguously
6262

6363
error[E0659]: `allow` is ambiguous (built-in attribute vs any other name)
64-
--> $DIR/ambiguous-builtin-attrs.rs:36:3
64+
--> $DIR/ambiguous-builtin-attrs.rs:38:3
6565
|
6666
LL | #[allow(unused)]
6767
| ^^^^^ ambiguous name
6868
|
6969
= note: `allow` could refer to a built-in attribute
7070
note: `allow` could also refer to the built-in attribute imported here
71-
--> $DIR/ambiguous-builtin-attrs.rs:35:5
71+
--> $DIR/ambiguous-builtin-attrs.rs:37:5
7272
|
7373
LL | use deny as allow;
7474
| ^^^^^^^^^^^^^
@@ -94,7 +94,16 @@ error[E0517]: attribute should be applied to a struct, enum, or union
9494
LL | fn non_macro_expanded_location<#[repr(C)] T>() {
9595
| ^ - not a struct, enum, or union
9696

97-
error: aborting due to 8 previous errors
97+
error[E0517]: attribute should be applied to a struct, enum, or union
98+
--> $DIR/ambiguous-builtin-attrs.rs:24:16
99+
|
100+
LL | #[repr(C)]
101+
| ^
102+
...
103+
LL | _ => {}
104+
| ------- not a struct, enum, or union
105+
106+
error: aborting due to 9 previous errors
98107

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

0 commit comments

Comments
 (0)