Skip to content

Commit ffb09df

Browse files
committed
Auto merge of #53053 - petrochenkov:custattr, r=alexcrichton
resolve: Support custom attributes when macro modularization is enabled Basically, if resolution of a single-segment attribute is a determined error, then we interpret it as a custom attribute. Since custom attributes are integrated into general macro resolution, `feature(custom_attribute)` now requires and implicitly enables macro modularization (`feature(use_extern_macros)`). Actually, a few other "advanced" macro features now implicitly enable macro modularization too (and one bug was found and fixed in process of enabling it). The first two commits are preliminary cleanups/refactorings.
2 parents 52c785b + 5088611 commit ffb09df

File tree

66 files changed

+295
-320
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+295
-320
lines changed

src/librustc/hir/def.rs

+26-3
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
// Both namespaces
7486
Err,
@@ -240,6 +252,17 @@ impl CtorKind {
240252
}
241253
}
242254

255+
impl NonMacroAttrKind {
256+
fn descr(self) -> &'static str {
257+
match self {
258+
NonMacroAttrKind::Builtin => "built-in attribute",
259+
NonMacroAttrKind::Tool => "tool attribute",
260+
NonMacroAttrKind::DeriveHelper => "derive helper attribute",
261+
NonMacroAttrKind::Custom => "custom attribute",
262+
}
263+
}
264+
}
265+
243266
impl Def {
244267
pub fn def_id(&self) -> DefId {
245268
match *self {
@@ -259,7 +282,7 @@ impl Def {
259282
Def::PrimTy(..) |
260283
Def::SelfTy(..) |
261284
Def::ToolMod |
262-
Def::NonMacroAttr |
285+
Def::NonMacroAttr(..) |
263286
Def::Err => {
264287
bug!("attempted .def_id() on invalid def: {:?}", self)
265288
}
@@ -300,7 +323,7 @@ impl Def {
300323
Def::SelfTy(..) => "self type",
301324
Def::Macro(.., macro_kind) => macro_kind.descr(),
302325
Def::ToolMod => "tool module",
303-
Def::NonMacroAttr => "non-macro attribute",
326+
Def::NonMacroAttr(attr_kind) => attr_kind.descr(),
304327
Def::Err => "unresolved item",
305328
}
306329
}

src/librustc/ich/impls_hir.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -990,6 +990,13 @@ impl_stable_hash_for!(enum hir::def::CtorKind {
990990
Fictive
991991
});
992992

993+
impl_stable_hash_for!(enum hir::def::NonMacroAttrKind {
994+
Builtin,
995+
Tool,
996+
DeriveHelper,
997+
Custom,
998+
});
999+
9931000
impl_stable_hash_for!(enum hir::def::Def {
9941001
Mod(def_id),
9951002
Struct(def_id),
@@ -1018,7 +1025,7 @@ impl_stable_hash_for!(enum hir::def::Def {
10181025
Label(node_id),
10191026
Macro(def_id, macro_kind),
10201027
ToolMod,
1021-
NonMacroAttr,
1028+
NonMacroAttr(attr_kind),
10221029
Err
10231030
});
10241031

src/librustc_resolve/build_reduced_graph.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -630,8 +630,10 @@ 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(attr_kind) => return Lrc::new(SyntaxExtension::NonMacroAttr {
634+
mark_used: attr_kind == NonMacroAttrKind::Tool,
635+
}),
636+
_ => panic!("expected `Def::Macro` or `Def::NonMacroAttr`"),
635637
};
636638
if let Some(ext) = self.macro_map.get(&def_id) {
637639
return ext.clone();

src/librustc_resolve/lib.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -3485,8 +3485,9 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
34853485
let binding = if let Some(module) = module {
34863486
self.resolve_ident_in_module(module, ident, ns, record_used, path_span)
34873487
} else if opt_ns == Some(MacroNS) {
3488-
self.resolve_lexical_macro_path_segment(ident, ns, record_used, path_span)
3489-
.map(MacroBinding::binding)
3488+
assert!(ns == TypeNS);
3489+
self.resolve_lexical_macro_path_segment(ident, ns, record_used, record_used,
3490+
false, path_span).map(MacroBinding::binding)
34903491
} else {
34913492
let record_used_id =
34923493
if record_used { crate_lint.node_id().or(Some(CRATE_NODE_ID)) } else { None };
@@ -3514,7 +3515,8 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
35143515
if let Some(next_module) = binding.module() {
35153516
module = Some(next_module);
35163517
} else if def == Def::ToolMod && i + 1 != path.len() {
3517-
return PathResult::NonModule(PathResolution::new(Def::NonMacroAttr))
3518+
let def = Def::NonMacroAttr(NonMacroAttrKind::Tool);
3519+
return PathResult::NonModule(PathResolution::new(def));
35183520
} else if def == Def::Err {
35193521
return PathResult::NonModule(err_path_resolution());
35203522
} else if opt_ns.is_some() && (is_last || maybe_assoc) {
@@ -4548,6 +4550,8 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
45484550
let result = self.resolve_lexical_macro_path_segment(ident,
45494551
MacroNS,
45504552
false,
4553+
false,
4554+
true,
45514555
attr.path.span);
45524556
if let Ok(binding) = result {
45534557
if let SyntaxExtension::AttrProcMacro(..) = *binding.binding().get_macro(self) {

0 commit comments

Comments
 (0)