Skip to content

Commit 467a7ab

Browse files
committed
Discern between various kinds of non-macro attributes
1 parent b239743 commit 467a7ab

14 files changed

+73
-40
lines changed

src/librustc/hir/def.rs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,18 @@ pub enum CtorKind {
2828
Fictive,
2929
}
3030

31+
#[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
32+
pub enum NonMacroAttrKind {
33+
/// Single-segment attribute defined by the language (`#[inline]`)
34+
Builtin,
35+
/// Multi-segment custom attribute living in a "tool module" (`#[rustfmt::skip]`).
36+
Tool,
37+
/// Single-segment custom attribute registered by a derive macro (`#[serde(default)]`).
38+
DeriveHelper,
39+
/// Single-segment custom attribute not registered in any way (`#[my_attr]`).
40+
Custom,
41+
}
42+
3143
#[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
3244
pub enum Def {
3345
// Type namespace
@@ -68,7 +80,7 @@ pub enum Def {
6880

6981
// Macro namespace
7082
Macro(DefId, MacroKind),
71-
NonMacroAttr, // e.g. `#[inline]` or `#[rustfmt::skip]`
83+
NonMacroAttr(NonMacroAttrKind), // e.g. `#[inline]` or `#[rustfmt::skip]`
7284

7385
GlobalAsm(DefId),
7486

@@ -242,6 +254,17 @@ impl CtorKind {
242254
}
243255
}
244256

257+
impl NonMacroAttrKind {
258+
fn descr(self) -> &'static str {
259+
match self {
260+
NonMacroAttrKind::Builtin => "built-in attribute",
261+
NonMacroAttrKind::Tool => "tool attribute",
262+
NonMacroAttrKind::DeriveHelper => "derive helper attribute",
263+
NonMacroAttrKind::Custom => "custom attribute",
264+
}
265+
}
266+
}
267+
245268
impl Def {
246269
pub fn def_id(&self) -> DefId {
247270
match *self {
@@ -262,7 +285,7 @@ impl Def {
262285
Def::PrimTy(..) |
263286
Def::SelfTy(..) |
264287
Def::ToolMod |
265-
Def::NonMacroAttr |
288+
Def::NonMacroAttr(..) |
266289
Def::Err => {
267290
bug!("attempted .def_id() on invalid def: {:?}", self)
268291
}
@@ -304,7 +327,7 @@ impl Def {
304327
Def::Macro(.., macro_kind) => macro_kind.descr(),
305328
Def::GlobalAsm(..) => "global asm",
306329
Def::ToolMod => "tool module",
307-
Def::NonMacroAttr => "non-macro attribute",
330+
Def::NonMacroAttr(attr_kind) => attr_kind.descr(),
308331
Def::Err => "unresolved item",
309332
}
310333
}

src/librustc/ich/impls_hir.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -988,6 +988,13 @@ impl_stable_hash_for!(enum hir::def::CtorKind {
988988
Fictive
989989
});
990990

991+
impl_stable_hash_for!(enum hir::def::NonMacroAttrKind {
992+
Builtin,
993+
Tool,
994+
DeriveHelper,
995+
Custom,
996+
});
997+
991998
impl_stable_hash_for!(enum hir::def::Def {
992999
Mod(def_id),
9931000
Struct(def_id),
@@ -1017,7 +1024,7 @@ impl_stable_hash_for!(enum hir::def::Def {
10171024
Macro(def_id, macro_kind),
10181025
GlobalAsm(def_id),
10191026
ToolMod,
1020-
NonMacroAttr,
1027+
NonMacroAttr(attr_kind),
10211028
Err
10221029
});
10231030

src/librustc_resolve/build_reduced_graph.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -630,8 +630,8 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
630630
pub fn get_macro(&mut self, def: Def) -> Lrc<SyntaxExtension> {
631631
let def_id = match def {
632632
Def::Macro(def_id, ..) => def_id,
633-
Def::NonMacroAttr => return Lrc::new(SyntaxExtension::NonMacroAttr),
634-
_ => panic!("Expected Def::Macro(..) or Def::NonMacroAttr"),
633+
Def::NonMacroAttr(..) => return Lrc::new(SyntaxExtension::NonMacroAttr),
634+
_ => panic!("expected `Def::Macro` or `Def::NonMacroAttr`"),
635635
};
636636
if let Some(ext) = self.macro_map.get(&def_id) {
637637
return ext.clone();

src/librustc_resolve/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3514,7 +3514,8 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
35143514
if let Some(next_module) = binding.module() {
35153515
module = Some(next_module);
35163516
} else if def == Def::ToolMod && i + 1 != path.len() {
3517-
return PathResult::NonModule(PathResolution::new(Def::NonMacroAttr))
3517+
let def = Def::NonMacroAttr(NonMacroAttrKind::Tool);
3518+
return PathResult::NonModule(PathResolution::new(def));
35183519
} else if def == Def::Err {
35193520
return PathResult::NonModule(err_path_resolution());
35203521
} else if opt_ns.is_some() && (is_last || maybe_assoc) {

src/librustc_resolve/macros.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use build_reduced_graph::{BuildReducedGraphVisitor, IsMacroExport};
1515
use resolve_imports::ImportResolver;
1616
use rustc::hir::def_id::{DefId, BUILTIN_MACROS_CRATE, CRATE_DEF_INDEX, DefIndex,
1717
DefIndexAddressSpace};
18-
use rustc::hir::def::{Def, Export};
18+
use rustc::hir::def::{Def, Export, NonMacroAttrKind};
1919
use rustc::hir::map::{self, DefCollector};
2020
use rustc::{ty, lint};
2121
use rustc::middle::cstore::CrateStore;
@@ -329,16 +329,17 @@ impl<'a, 'crateloader: 'a> base::Resolver for Resolver<'a, 'crateloader> {
329329
if let Def::Macro(_, MacroKind::ProcMacroStub) = def {
330330
self.report_proc_macro_stub(invoc.span());
331331
return Err(Determinacy::Determined);
332-
} else if let Def::NonMacroAttr = def {
333-
if let InvocationKind::Attr { .. } = invoc.kind {
332+
} else if let Def::NonMacroAttr(attr_kind) = def {
333+
let is_attr = if let InvocationKind::Attr { .. } = invoc.kind { true } else { false };
334+
if is_attr && attr_kind == NonMacroAttrKind::Tool {
334335
if !self.session.features_untracked().tool_attributes {
335336
feature_err(&self.session.parse_sess, "tool_attributes",
336337
invoc.span(), GateIssue::Language,
337338
"tool attributes are unstable").emit();
338339
}
339340
return Ok(Some(Lrc::new(SyntaxExtension::NonMacroAttr)));
340341
} else {
341-
self.report_non_macro_attr(invoc.path_span());
342+
self.report_non_macro_attr(invoc.path_span(), def);
342343
return Err(Determinacy::Determined);
343344
}
344345
}
@@ -363,8 +364,8 @@ impl<'a, 'crateloader: 'a> base::Resolver for Resolver<'a, 'crateloader> {
363364
if let Def::Macro(_, MacroKind::ProcMacroStub) = def {
364365
self.report_proc_macro_stub(path.span);
365366
return Err(Determinacy::Determined);
366-
} else if let Def::NonMacroAttr = def {
367-
self.report_non_macro_attr(path.span);
367+
} else if let Def::NonMacroAttr(..) = def {
368+
self.report_non_macro_attr(path.span, def);
368369
return Err(Determinacy::Determined);
369370
}
370371
self.unused_macros.remove(&def.def_id());
@@ -396,9 +397,8 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
396397
"can't use a procedural macro from the same crate that defines it");
397398
}
398399

399-
fn report_non_macro_attr(&self, span: Span) {
400-
self.session.span_err(span,
401-
"expected a macro, found non-macro attribute");
400+
fn report_non_macro_attr(&self, span: Span, def: Def) {
401+
self.session.span_err(span, &format!("expected a macro, found {}", def.kind_name()));
402402
}
403403

404404
fn resolve_invoc_to_def(&mut self, invoc: &mut Invocation, scope: Mark, force: bool)
@@ -481,7 +481,8 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
481481
"generic arguments in macro path");
482482
});
483483
}
484-
if kind != MacroKind::Bang && path.segments.len() > 1 && def != Ok(Def::NonMacroAttr) {
484+
if kind != MacroKind::Bang && path.segments.len() > 1 &&
485+
def != Ok(Def::NonMacroAttr(NonMacroAttrKind::Tool)) {
485486
if !self.session.features_untracked().proc_macro_path_invoc {
486487
emit_feature_err(
487488
&self.session.parse_sess,
@@ -647,8 +648,9 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
647648
}
648649
WhereToResolve::BuiltinAttrs => {
649650
if is_builtin_attr_name(ident.name) {
650-
let binding = (Def::NonMacroAttr, ty::Visibility::Public,
651-
ident.span, Mark::root()).to_name_binding(self.arenas);
651+
let binding = (Def::NonMacroAttr(NonMacroAttrKind::Builtin),
652+
ty::Visibility::Public, ident.span, Mark::root())
653+
.to_name_binding(self.arenas);
652654
Ok(MacroBinding::Global(binding))
653655
} else {
654656
Err(Determinacy::Determined)

src/librustc_save_analysis/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -812,7 +812,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
812812
HirDef::Macro(..) |
813813
HirDef::GlobalAsm(..) |
814814
HirDef::ToolMod |
815-
HirDef::NonMacroAttr |
815+
HirDef::NonMacroAttr(..) |
816816
HirDef::Err => None,
817817
}
818818
}

src/test/ui/issue-11692-2.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@
1010

1111
fn main() {
1212
concat!(test!());
13-
//~^ ERROR expected a macro, found non-macro attribute
13+
//~^ ERROR expected a macro, found built-in attribute
1414
}

src/test/ui/issue-11692-2.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: expected a macro, found non-macro attribute
1+
error: expected a macro, found built-in attribute
22
--> $DIR/issue-11692-2.rs:12:13
33
|
44
LL | concat!(test!());

src/test/ui/macro-path-prelude-fail-3.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010

1111
#![feature(use_extern_macros)]
1212

13-
#[derive(inline)] //~ ERROR expected a macro, found non-macro attribute
13+
#[derive(inline)] //~ ERROR expected a macro, found built-in attribute
1414
struct S;
1515

1616
fn main() {
17-
inline!(); //~ ERROR expected a macro, found non-macro attribute
17+
inline!(); //~ ERROR expected a macro, found built-in attribute
1818
}

src/test/ui/macro-path-prelude-fail-3.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
error: expected a macro, found non-macro attribute
1+
error: expected a macro, found built-in attribute
22
--> $DIR/macro-path-prelude-fail-3.rs:13:10
33
|
4-
LL | #[derive(inline)] //~ ERROR expected a macro, found non-macro attribute
4+
LL | #[derive(inline)] //~ ERROR expected a macro, found built-in attribute
55
| ^^^^^^
66

7-
error: expected a macro, found non-macro attribute
7+
error: expected a macro, found built-in attribute
88
--> $DIR/macro-path-prelude-fail-3.rs:17:5
99
|
10-
LL | inline!(); //~ ERROR expected a macro, found non-macro attribute
10+
LL | inline!(); //~ ERROR expected a macro, found built-in attribute
1111
| ^^^^^^
1212

1313
error: aborting due to 2 previous errors

src/test/ui/tool-attributes-misplaced-1.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#![feature(tool_attributes)]
1212

1313
type A = rustfmt; //~ ERROR expected type, found tool module `rustfmt`
14-
type B = rustfmt::skip; //~ ERROR expected type, found non-macro attribute `rustfmt::skip`
14+
type B = rustfmt::skip; //~ ERROR expected type, found tool attribute `rustfmt::skip`
1515

1616
#[derive(rustfmt)] //~ ERROR cannot find derive macro `rustfmt` in this scope
1717
struct S;
@@ -24,5 +24,5 @@ fn main() {
2424
rustfmt; //~ ERROR expected value, found tool module `rustfmt`
2525
rustfmt!(); //~ ERROR cannot find macro `rustfmt!` in this scope
2626

27-
rustfmt::skip; //~ ERROR expected value, found non-macro attribute `rustfmt::skip`
27+
rustfmt::skip; //~ ERROR expected value, found tool attribute `rustfmt::skip`
2828
}

src/test/ui/tool-attributes-misplaced-1.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ error[E0573]: expected type, found tool module `rustfmt`
2222
LL | type A = rustfmt; //~ ERROR expected type, found tool module `rustfmt`
2323
| ^^^^^^^ not a type
2424

25-
error[E0573]: expected type, found non-macro attribute `rustfmt::skip`
25+
error[E0573]: expected type, found tool attribute `rustfmt::skip`
2626
--> $DIR/tool-attributes-misplaced-1.rs:14:10
2727
|
28-
LL | type B = rustfmt::skip; //~ ERROR expected type, found non-macro attribute `rustfmt::skip`
28+
LL | type B = rustfmt::skip; //~ ERROR expected type, found tool attribute `rustfmt::skip`
2929
| ^^^^^^^^^^^^^ not a type
3030

3131
error[E0423]: expected value, found tool module `rustfmt`
@@ -34,10 +34,10 @@ error[E0423]: expected value, found tool module `rustfmt`
3434
LL | rustfmt; //~ ERROR expected value, found tool module `rustfmt`
3535
| ^^^^^^^ not a value
3636

37-
error[E0423]: expected value, found non-macro attribute `rustfmt::skip`
37+
error[E0423]: expected value, found tool attribute `rustfmt::skip`
3838
--> $DIR/tool-attributes-misplaced-1.rs:27:5
3939
|
40-
LL | rustfmt::skip; //~ ERROR expected value, found non-macro attribute `rustfmt::skip`
40+
LL | rustfmt::skip; //~ ERROR expected value, found tool attribute `rustfmt::skip`
4141
| ^^^^^^^^^^^^^ not a value
4242

4343
error: aborting due to 7 previous errors

src/test/ui/tool-attributes-misplaced-2.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010

1111
#![feature(tool_attributes)]
1212

13-
#[derive(rustfmt::skip)] //~ ERROR expected a macro, found non-macro attribute
13+
#[derive(rustfmt::skip)] //~ ERROR expected a macro, found tool attribute
1414
struct S;
1515

1616
fn main() {
17-
rustfmt::skip!(); //~ ERROR expected a macro, found non-macro attribute
17+
rustfmt::skip!(); //~ ERROR expected a macro, found tool attribute
1818
}

src/test/ui/tool-attributes-misplaced-2.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
error: expected a macro, found non-macro attribute
1+
error: expected a macro, found tool attribute
22
--> $DIR/tool-attributes-misplaced-2.rs:13:10
33
|
4-
LL | #[derive(rustfmt::skip)] //~ ERROR expected a macro, found non-macro attribute
4+
LL | #[derive(rustfmt::skip)] //~ ERROR expected a macro, found tool attribute
55
| ^^^^^^^^^^^^^
66

7-
error: expected a macro, found non-macro attribute
7+
error: expected a macro, found tool attribute
88
--> $DIR/tool-attributes-misplaced-2.rs:17:5
99
|
10-
LL | rustfmt::skip!(); //~ ERROR expected a macro, found non-macro attribute
10+
LL | rustfmt::skip!(); //~ ERROR expected a macro, found tool attribute
1111
| ^^^^^^^^^^^^^
1212

1313
error: aborting due to 2 previous errors

0 commit comments

Comments
 (0)