Skip to content

Commit c29fb80

Browse files
committed
Reapply the derive helper changes from master
1 parent 8994c6d commit c29fb80

File tree

1 file changed

+38
-37
lines changed

1 file changed

+38
-37
lines changed

src/librustc_resolve/macros.rs

+38-37
Original file line numberDiff line numberDiff line change
@@ -554,35 +554,35 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
554554
// (open, not controlled).
555555
// 3. Standard library prelude (de-facto closed, controlled).
556556
// (Macro NS)
557-
// 1-2. `macro_rules` (open, not controlled), loop through legacy scopes. Have higher
557+
// 1-3. Derive helpers (open, not controlled). All ambiguities with other names
558+
// are currently reported as errors. They should be higher in priority than preludes
559+
// and probably even names in modules according to the "general principles" above. They
560+
// also should be subject to restricted shadowing because are effectively produced by
561+
// derives (you need to resolve the derive first to add helpers into scope), but they
562+
// should be available before the derive is expanded for compatibility.
563+
// It's mess in general, so we are being conservative for now.
564+
// 1-3. `macro_rules` (open, not controlled), loop through legacy scopes. Have higher
558565
// priority than prelude macros, but create ambiguities with macros in modules.
559-
// 1-2. Names in modules (both normal `mod`ules and blocks), loop through hygienic parents
566+
// 1-3. Names in modules (both normal `mod`ules and blocks), loop through hygienic parents
560567
// (open, not controlled). Have higher priority than prelude macros, but create
561568
// ambiguities with `macro_rules`.
562-
// 3. `macro_use` prelude (open, the open part is from macro expansions, not controlled).
563-
// 3a. User-defined prelude from macro-use
569+
// 4. `macro_use` prelude (open, the open part is from macro expansions, not controlled).
570+
// 4a. User-defined prelude from macro-use
564571
// (open, the open part is from macro expansions, not controlled).
565-
// 3b. Standard library prelude is currently implemented as `macro-use` (closed, controlled)
572+
// 4b. Standard library prelude is currently implemented as `macro-use` (closed, controlled)
566573
// 5. Language prelude: builtin macros (closed, controlled, except for legacy plugins).
567574
// 6. Language prelude: builtin attributes (closed, controlled).
568-
// 3-6. Legacy plugin helpers (open, not controlled). Similar to derive helpers,
575+
// 4-6. Legacy plugin helpers (open, not controlled). Similar to derive helpers,
569576
// but introduced by legacy plugins using `register_attribute`. Priority is somewhere
570577
// in prelude, not sure where exactly (creates ambiguities with any other prelude names).
571-
// N (unordered). Derive helpers (open, not controlled). All ambiguities with other names
572-
// are currently reported as errors. They should be higher in priority than preludes
573-
// and maybe even names in modules according to the "general principles" above. They
574-
// also should be subject to restricted shadowing because are effectively produced by
575-
// derives (you need to resolve the derive first to add helpers into scope), but they
576-
// should be available before the derive is expanded for compatibility.
577-
// It's mess in general, so we are being conservative for now.
578578

579579
enum WhereToResolve<'a> {
580+
DeriveHelpers,
580581
MacroRules(LegacyScope<'a>),
581582
Module(Module<'a>),
582583
MacroUsePrelude,
583584
BuiltinMacros,
584585
BuiltinAttrs,
585-
DeriveHelpers,
586586
LegacyPluginHelpers,
587587
ExternPrelude,
588588
ToolPrelude,
@@ -616,10 +616,30 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
616616
let mut innermost_result: Option<(&NameBinding, Flags, /* conflicts with */ Flags)> = None;
617617

618618
// Go through all the scopes and try to resolve the name.
619-
let mut where_to_resolve = WhereToResolve::MacroRules(parent_scope.legacy);
619+
let mut where_to_resolve = WhereToResolve::DeriveHelpers;
620620
let mut use_prelude = !parent_scope.module.no_implicit_prelude;
621621
loop {
622622
let result = match where_to_resolve {
623+
WhereToResolve::DeriveHelpers => {
624+
let mut result = Err(Determinacy::Determined);
625+
for derive in &parent_scope.derives {
626+
let parent_scope = ParentScope { derives: Vec::new(), ..*parent_scope };
627+
if let Ok((_, ext)) = self.resolve_macro_to_def(derive, MacroKind::Derive,
628+
&parent_scope, force) {
629+
if let SyntaxExtension::ProcMacroDerive(_, helper_attrs, _) = &*ext {
630+
if helper_attrs.contains(&ident.name) {
631+
let binding =
632+
(Def::NonMacroAttr(NonMacroAttrKind::DeriveHelper),
633+
ty::Visibility::Public, derive.span, Mark::root())
634+
.to_name_binding(self.arenas);
635+
result = Ok((binding, Flags::DERIVE_HELPERS, Flags::all()));
636+
break;
637+
}
638+
}
639+
}
640+
}
641+
result
642+
}
623643
WhereToResolve::MacroRules(legacy_scope) => match legacy_scope {
624644
LegacyScope::Binding(legacy_binding) if ident == legacy_binding.ident =>
625645
Ok((legacy_binding.binding, Flags::MACRO_RULES, Flags::MODULE)),
@@ -660,26 +680,6 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
660680
Err(Determinacy::Determined)
661681
}
662682
}
663-
WhereToResolve::DeriveHelpers => {
664-
let mut result = Err(Determinacy::Determined);
665-
for derive in &parent_scope.derives {
666-
let parent_scope = ParentScope { derives: Vec::new(), ..*parent_scope };
667-
if let Ok((_, ext)) = self.resolve_macro_to_def(derive, MacroKind::Derive,
668-
&parent_scope, force) {
669-
if let SyntaxExtension::ProcMacroDerive(_, helper_attrs, _) = &*ext {
670-
if helper_attrs.contains(&ident.name) {
671-
let binding =
672-
(Def::NonMacroAttr(NonMacroAttrKind::DeriveHelper),
673-
ty::Visibility::Public, derive.span, Mark::root())
674-
.to_name_binding(self.arenas);
675-
result = Ok((binding, Flags::DERIVE_HELPERS, Flags::all()));
676-
break;
677-
}
678-
}
679-
}
680-
}
681-
result
682-
}
683683
WhereToResolve::LegacyPluginHelpers => {
684684
if self.session.plugin_attributes.borrow().iter()
685685
.any(|(name, _)| ident.name == &**name) {
@@ -747,6 +747,8 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
747747

748748
macro_rules! continue_search { () => {
749749
where_to_resolve = match where_to_resolve {
750+
WhereToResolve::DeriveHelpers =>
751+
WhereToResolve::MacroRules(parent_scope.legacy),
750752
WhereToResolve::MacroRules(legacy_scope) => match legacy_scope {
751753
LegacyScope::Binding(binding) =>
752754
WhereToResolve::MacroRules(binding.parent_legacy_scope),
@@ -770,8 +772,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
770772
}
771773
WhereToResolve::MacroUsePrelude => WhereToResolve::BuiltinMacros,
772774
WhereToResolve::BuiltinMacros => WhereToResolve::BuiltinAttrs,
773-
WhereToResolve::BuiltinAttrs => WhereToResolve::DeriveHelpers,
774-
WhereToResolve::DeriveHelpers => WhereToResolve::LegacyPluginHelpers,
775+
WhereToResolve::BuiltinAttrs => WhereToResolve::LegacyPluginHelpers,
775776
WhereToResolve::LegacyPluginHelpers => break, // nowhere else to search
776777
WhereToResolve::ExternPrelude => WhereToResolve::ToolPrelude,
777778
WhereToResolve::ToolPrelude => WhereToResolve::StdLibPrelude,

0 commit comments

Comments
 (0)