Skip to content

Commit 3950a6d

Browse files
Run attributes check at crate level
1 parent 0e18017 commit 3950a6d

7 files changed

+305
-298
lines changed

compiler/rustc_passes/src/check_attr.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ use rustc_errors::{pluralize, struct_span_err};
1313
use rustc_hir as hir;
1414
use rustc_hir::def_id::LocalDefId;
1515
use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
16-
use rustc_hir::{self, FnSig, ForeignItem, ForeignItemKind, HirId, Item, ItemKind, TraitItem, CRATE_HIR_ID};
16+
use rustc_hir::{
17+
self, FnSig, ForeignItem, ForeignItemKind, HirId, Item, ItemKind, TraitItem, CRATE_HIR_ID,
18+
};
1719
use rustc_hir::{MethodKind, Target};
1820
use rustc_session::lint::builtin::{CONFLICTING_REPR_HINTS, UNUSED_ATTRIBUTES};
1921
use rustc_session::parse::feature_err;
@@ -823,9 +825,13 @@ fn check_mod_attrs(tcx: TyCtxt<'_>, module_def_id: LocalDefId) {
823825
tcx.hir()
824826
.visit_item_likes_in_module(module_def_id, &mut CheckAttrVisitor { tcx }.as_deep_visitor());
825827
if module_def_id.is_top_level_module() {
826-
for attr in tcx.hir().krate_attrs() {
827-
CheckAttrVisitor { tcx }.check_doc_alias(attr, CRATE_HIR_ID, Target::Mod);
828-
}
828+
CheckAttrVisitor { tcx }.check_attributes(
829+
CRATE_HIR_ID,
830+
tcx.hir().krate_attrs(),
831+
&tcx.hir().span(CRATE_HIR_ID),
832+
Target::Mod,
833+
None,
834+
);
829835
}
830836
}
831837

src/test/ui/feature-gate/issue-43106-gating-of-builtin-attrs-error.rs

-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77

88
// ignore-tidy-linelength
99

10-
// Crate-level is accepted, though it is almost certainly unused?
11-
#![inline]
12-
1310
#[inline]
1411
//~^ ERROR attribute should be applied to function or closure
1512
mod inline {

src/test/ui/feature-gate/issue-43106-gating-of-builtin-attrs-error.stderr

+17-17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: attribute must be of the form `#[inline]` or `#[inline(always|never)]`
2-
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:22:5
2+
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:19:5
33
|
44
LL | #[inline = "2100"] fn f() { }
55
| ^^^^^^^^^^^^^^^^^^
@@ -9,7 +9,7 @@ LL | #[inline = "2100"] fn f() { }
99
= note: for more information, see issue #57571 <https://github.com/rust-lang/rust/issues/57571>
1010

1111
error[E0518]: attribute should be applied to function or closure
12-
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:13:1
12+
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:10:1
1313
|
1414
LL | #[inline]
1515
| ^^^^^^^^^
@@ -24,7 +24,7 @@ LL | | }
2424
| |_- not a function or closure
2525

2626
error: attribute should be applied to an `extern crate` item
27-
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:41:1
27+
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:38:1
2828
|
2929
LL | #[no_link]
3030
| ^^^^^^^^^^
@@ -39,7 +39,7 @@ LL | | }
3939
| |_- not an `extern crate` item
4040

4141
error: attribute should be applied to a function or static
42-
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:67:1
42+
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:64:1
4343
|
4444
LL | #[export_name = "2200"]
4545
| ^^^^^^^^^^^^^^^^^^^^^^^
@@ -54,79 +54,79 @@ LL | | }
5454
| |_- not a function or static
5555

5656
error[E0518]: attribute should be applied to function or closure
57-
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:18:17
57+
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:15:17
5858
|
5959
LL | mod inner { #![inline] }
6060
| ------------^^^^^^^^^^-- not a function or closure
6161

6262
error[E0518]: attribute should be applied to function or closure
63-
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:28:5
63+
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:25:5
6464
|
6565
LL | #[inline] struct S;
6666
| ^^^^^^^^^ --------- not a function or closure
6767

6868
error[E0518]: attribute should be applied to function or closure
69-
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:32:5
69+
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:29:5
7070
|
7171
LL | #[inline] type T = S;
7272
| ^^^^^^^^^ ----------- not a function or closure
7373

7474
error[E0518]: attribute should be applied to function or closure
75-
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:36:5
75+
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:33:5
7676
|
7777
LL | #[inline] impl S { }
7878
| ^^^^^^^^^ ---------- not a function or closure
7979

8080
error: attribute should be applied to an `extern crate` item
81-
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:46:17
81+
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:43:17
8282
|
8383
LL | mod inner { #![no_link] }
8484
| ------------^^^^^^^^^^^-- not an `extern crate` item
8585

8686
error: attribute should be applied to an `extern crate` item
87-
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:50:5
87+
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:47:5
8888
|
8989
LL | #[no_link] fn f() { }
9090
| ^^^^^^^^^^ ---------- not an `extern crate` item
9191

9292
error: attribute should be applied to an `extern crate` item
93-
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:54:5
93+
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:51:5
9494
|
9595
LL | #[no_link] struct S;
9696
| ^^^^^^^^^^ --------- not an `extern crate` item
9797

9898
error: attribute should be applied to an `extern crate` item
99-
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:58:5
99+
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:55:5
100100
|
101101
LL | #[no_link]type T = S;
102102
| ^^^^^^^^^^----------- not an `extern crate` item
103103

104104
error: attribute should be applied to an `extern crate` item
105-
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:62:5
105+
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:59:5
106106
|
107107
LL | #[no_link] impl S { }
108108
| ^^^^^^^^^^ ---------- not an `extern crate` item
109109

110110
error: attribute should be applied to a function or static
111-
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:72:17
111+
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:69:17
112112
|
113113
LL | mod inner { #![export_name="2200"] }
114114
| ------------^^^^^^^^^^^^^^^^^^^^^^-- not a function or static
115115

116116
error: attribute should be applied to a function or static
117-
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:78:5
117+
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:75:5
118118
|
119119
LL | #[export_name = "2200"] struct S;
120120
| ^^^^^^^^^^^^^^^^^^^^^^^ --------- not a function or static
121121

122122
error: attribute should be applied to a function or static
123-
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:82:5
123+
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:79:5
124124
|
125125
LL | #[export_name = "2200"] type T = S;
126126
| ^^^^^^^^^^^^^^^^^^^^^^^ ----------- not a function or static
127127

128128
error: attribute should be applied to a function or static
129-
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:86:5
129+
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:83:5
130130
|
131131
LL | #[export_name = "2200"] impl S { }
132132
| ^^^^^^^^^^^^^^^^^^^^^^^ ---------- not a function or static

src/test/ui/feature-gate/issue-43106-gating-of-builtin-attrs.rs

+9-14
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
// ignore-tidy-linelength
3535

3636
#![feature(test, plugin_registrar)]
37+
//~^ NOTE not a function
38+
//~^^ NOTE not a foreign function or static
39+
//~^^^ NOTE not a function or static
3740
#![warn(unused_attributes, unknown_lints)]
3841
//~^ NOTE the lint level is defined here
3942
//~| NOTE the lint level is defined here
@@ -52,20 +55,8 @@
5255
#![forbid(x5200)] //~ WARN unknown lint: `x5200`
5356
#![deny(x5100)] //~ WARN unknown lint: `x5100`
5457
#![macro_use] // (allowed if no argument; see issue-43160-gating-of-macro_use.rs)
55-
#![macro_export] //~ WARN unused attribute
5658
// skipping testing of cfg
5759
// skipping testing of cfg_attr
58-
#![main] //~ WARN unused attribute
59-
#![start] //~ WARN unused attribute
60-
// see issue-43106-gating-of-test.rs for crate-level; but non crate-level is below at "4200"
61-
// see issue-43106-gating-of-bench.rs for crate-level; but non crate-level is below at "4100"
62-
#![repr()]
63-
//~^ WARN unused attribute
64-
#![path = "3800"] //~ WARN unused attribute
65-
#![automatically_derived] //~ WARN unused attribute
66-
#![no_mangle]
67-
#![no_link] //~ WARN unused attribute
68-
// see issue-43106-gating-of-derive.rs
6960
#![should_panic] //~ WARN unused attribute
7061
#![ignore] //~ WARN unused attribute
7162
#![no_implicit_prelude]
@@ -75,12 +66,16 @@
7566
// (cannot easily test gating of crate-level #[no_std]; but non crate-level is below at "2600")
7667
#![proc_macro_derive()] //~ WARN unused attribute
7768
#![doc = "2400"]
78-
#![cold]
79-
#![export_name = "2200"]
69+
#![cold] //~ WARN attribute should be applied to a function
70+
//~^ WARN
8071
// see issue-43106-gating-of-builtin-attrs-error.rs
8172
#![link()]
8273
#![link_name = "1900"]
74+
//~^ WARN attribute should be applied to a foreign function
75+
//~^^ WARN this was previously accepted by the compiler
8376
#![link_section = "1800"]
77+
//~^ WARN attribute should be applied to a function or static
78+
//~^^ WARN this was previously accepted by the compiler
8479
// see issue-43106-gating-of-rustc_deprecated.rs
8580
#![must_use]
8681
// see issue-43106-gating-of-stable.rs

0 commit comments

Comments
 (0)