Skip to content

Commit 83f553c

Browse files
committed
Address review comments
1 parent 09fff50 commit 83f553c

14 files changed

+140
-10
lines changed

src/librustc/hir/def.rs

+17
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,22 @@ impl NonMacroAttrKind {
333333
NonMacroAttrKind::LegacyPluginHelper => "legacy plugin helper attribute",
334334
}
335335
}
336+
337+
pub fn article(self) -> &'static str {
338+
match self {
339+
NonMacroAttrKind::Registered => "an",
340+
_ => "a",
341+
}
342+
}
343+
344+
/// Users of some attributes cannot mark them as used, so they are considered always used.
345+
pub fn is_used(self) -> bool {
346+
match self {
347+
NonMacroAttrKind::Tool | NonMacroAttrKind::DeriveHelper => true,
348+
NonMacroAttrKind::Builtin | NonMacroAttrKind::Registered |
349+
NonMacroAttrKind::LegacyPluginHelper => false,
350+
}
351+
}
336352
}
337353

338354
impl<Id> Res<Id> {
@@ -389,6 +405,7 @@ impl<Id> Res<Id> {
389405
pub fn article(&self) -> &'static str {
390406
match *self {
391407
Res::Def(kind, _) => kind.article(),
408+
Res::NonMacroAttr(kind) => kind.article(),
392409
Res::Err => "an",
393410
_ => "a",
394411
}

src/librustc_resolve/build_reduced_graph.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,7 @@ impl<'a> Resolver<'a> {
141141
crate fn get_macro(&mut self, res: Res) -> Option<Lrc<SyntaxExtension>> {
142142
match res {
143143
Res::Def(DefKind::Macro(..), def_id) => self.get_macro_by_def_id(def_id),
144-
Res::NonMacroAttr(attr_kind) =>
145-
Some(self.non_macro_attr(attr_kind == NonMacroAttrKind::Tool)),
144+
Res::NonMacroAttr(attr_kind) => Some(self.non_macro_attr(attr_kind.is_used())),
146145
_ => None,
147146
}
148147
}

src/librustc_resolve/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1472,7 +1472,7 @@ impl<'a> Resolver<'a> {
14721472
Scope::MacroRules(..) => true,
14731473
Scope::CrateRoot => true,
14741474
Scope::Module(..) => true,
1475-
Scope::RegisteredAttrs => true,
1475+
Scope::RegisteredAttrs => use_prelude,
14761476
Scope::MacroUsePrelude => use_prelude || rust_2015,
14771477
Scope::BuiltinAttrs => true,
14781478
Scope::LegacyPluginHelpers => use_prelude || rust_2015,

src/librustc_resolve/macros.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ fn fast_print_path(path: &ast::Path) -> Symbol {
9494
}
9595
}
9696

97+
/// The code common between processing `#![register_tool]` and `#![register_attr]`.
9798
fn registered_idents(
9899
sess: &Session,
99100
attrs: &[ast::Attribute],
@@ -832,7 +833,8 @@ impl<'a> Resolver<'a> {
832833
res: Option<Res>, span: Span) {
833834
if let Some(Res::NonMacroAttr(kind)) = res {
834835
if kind != NonMacroAttrKind::Tool && binding.map_or(true, |b| b.is_import()) {
835-
let msg = format!("cannot use a {} through an import", kind.descr());
836+
let msg =
837+
format!("cannot use {} {} through an import", kind.article(), kind.descr());
836838
let mut err = self.session.struct_span_err(span, &msg);
837839
if let Some(binding) = binding {
838840
err.span_note(binding.span, &format!("the {} imported here", kind.descr()));

src/libsyntax/feature_gate/active.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -524,10 +524,10 @@ declare_features! (
524524
(active, abi_efiapi, "1.40.0", Some(65815), None),
525525

526526
/// Allows using the `#[register_attr]` attribute.
527-
(active, register_attr, "1.41.0", Some(29642), None),
527+
(active, register_attr, "1.41.0", Some(66080), None),
528528

529529
/// Allows using the `#[register_attr]` attribute.
530-
(active, register_tool, "1.41.0", Some(44690), None),
530+
(active, register_tool, "1.41.0", Some(66079), None),
531531

532532
// -------------------------------------------------------------------------
533533
// feature-group-end: actual feature gates

src/libsyntax/feature_gate/builtin_attrs.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -330,11 +330,11 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
330330
gated!(ffi_returns_twice, Whitelisted, template!(Word), experimental!(ffi_returns_twice)),
331331
gated!(track_caller, Whitelisted, template!(Word), experimental!(track_caller)),
332332
gated!(
333-
register_attr, Whitelisted, template!(List: "attr1, attr2, ..."),
333+
register_attr, CrateLevel, template!(List: "attr1, attr2, ..."),
334334
experimental!(register_attr),
335335
),
336336
gated!(
337-
register_tool, Whitelisted, template!(List: "tool1, tool2, ..."),
337+
register_tool, CrateLevel, template!(List: "tool1, tool2, ..."),
338338
experimental!(register_tool),
339339
),
340340

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// edition:2018
2+
3+
#![feature(register_attr)]
4+
#![feature(register_tool)]
5+
6+
#![register_attr(attr)]
7+
#![register_tool(tool)]
8+
9+
use attr as renamed_attr; // OK
10+
use tool as renamed_tool; // OK
11+
12+
#[renamed_attr] //~ ERROR cannot use an explicitly registered attribute through an import
13+
#[renamed_tool::attr] //~ ERROR cannot use a tool module through an import
14+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
error: cannot use an explicitly registered attribute through an import
2+
--> $DIR/register-attr-tool-import.rs:12:3
3+
|
4+
LL | #[renamed_attr]
5+
| ^^^^^^^^^^^^
6+
|
7+
note: the explicitly registered attribute imported here
8+
--> $DIR/register-attr-tool-import.rs:9:5
9+
|
10+
LL | use attr as renamed_attr; // OK
11+
| ^^^^^^^^^^^^^^^^^^^^
12+
13+
error: cannot use a tool module through an import
14+
--> $DIR/register-attr-tool-import.rs:13:3
15+
|
16+
LL | #[renamed_tool::attr]
17+
| ^^^^^^^^^^^^
18+
|
19+
note: the tool module imported here
20+
--> $DIR/register-attr-tool-import.rs:10:5
21+
|
22+
LL | use tool as renamed_tool; // OK
23+
| ^^^^^^^^^^^^^^^^^^^^
24+
25+
error: aborting due to 2 previous errors
26+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#![feature(register_attr)]
2+
#![feature(register_tool)]
3+
4+
#![register_attr(attr)]
5+
#![register_tool(tool)]
6+
7+
#[no_implicit_prelude]
8+
mod m {
9+
#[attr] //~ ERROR cannot find attribute `attr` in this scope
10+
#[tool::attr] //~ ERROR failed to resolve: use of undeclared type or module `tool`
11+
fn check() {}
12+
}
13+
14+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0433]: failed to resolve: use of undeclared type or module `tool`
2+
--> $DIR/register-attr-tool-prelude.rs:10:7
3+
|
4+
LL | #[tool::attr]
5+
| ^^^^ use of undeclared type or module `tool`
6+
7+
error: cannot find attribute `attr` in this scope
8+
--> $DIR/register-attr-tool-prelude.rs:9:7
9+
|
10+
LL | #[attr]
11+
| ^^^^
12+
13+
error: aborting due to 2 previous errors
14+
15+
For more information about this error, try `rustc --explain E0433`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#![deny(unused)]
2+
3+
#![feature(register_attr)]
4+
#![feature(register_tool)]
5+
6+
#[register_attr(attr)] //~ ERROR crate-level attribute should be an inner attribute
7+
//~| ERROR unused attribute
8+
#[register_tool(tool)] //~ ERROR crate-level attribute should be an inner attribute
9+
//~| ERROR unused attribute
10+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
error: unused attribute
2+
--> $DIR/register-attr-tool-unused.rs:6:1
3+
|
4+
LL | #[register_attr(attr)]
5+
| ^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
note: lint level defined here
8+
--> $DIR/register-attr-tool-unused.rs:1:9
9+
|
10+
LL | #![deny(unused)]
11+
| ^^^^^^
12+
= note: `#[deny(unused_attributes)]` implied by `#[deny(unused)]`
13+
14+
error: crate-level attribute should be an inner attribute: add an exclamation mark: `#![foo]`
15+
--> $DIR/register-attr-tool-unused.rs:6:1
16+
|
17+
LL | #[register_attr(attr)]
18+
| ^^^^^^^^^^^^^^^^^^^^^^
19+
20+
error: unused attribute
21+
--> $DIR/register-attr-tool-unused.rs:8:1
22+
|
23+
LL | #[register_tool(tool)]
24+
| ^^^^^^^^^^^^^^^^^^^^^^
25+
26+
error: crate-level attribute should be an inner attribute: add an exclamation mark: `#![foo]`
27+
--> $DIR/register-attr-tool-unused.rs:8:1
28+
|
29+
LL | #[register_tool(tool)]
30+
| ^^^^^^^^^^^^^^^^^^^^^^
31+
32+
error: aborting due to 4 previous errors
33+

src/test/ui/feature-gates/feature-gate-register_attr.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0658]: the `#[register_attr]` attribute is an experimental feature
44
LL | #![register_attr(attr)]
55
| ^^^^^^^^^^^^^^^^^^^^^^^
66
|
7-
= note: for more information, see https://github.com/rust-lang/rust/issues/29642
7+
= note: for more information, see https://github.com/rust-lang/rust/issues/66080
88
= help: add `#![feature(register_attr)]` to the crate attributes to enable
99

1010
error: aborting due to previous error

src/test/ui/feature-gates/feature-gate-register_tool.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0658]: the `#[register_tool]` attribute is an experimental feature
44
LL | #![register_tool(tool)]
55
| ^^^^^^^^^^^^^^^^^^^^^^^
66
|
7-
= note: for more information, see https://github.com/rust-lang/rust/issues/44690
7+
= note: for more information, see https://github.com/rust-lang/rust/issues/66079
88
= help: add `#![feature(register_tool)]` to the crate attributes to enable
99

1010
error: aborting due to previous error

0 commit comments

Comments
 (0)