Skip to content

Commit 69894ce

Browse files
committed
resolve: Introduce a separate NonMacroAttrKind for legacy derive helpers
1 parent e7ee4d6 commit 69894ce

File tree

3 files changed

+21
-20
lines changed

3 files changed

+21
-20
lines changed

compiler/rustc_hir/src/def.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ pub enum NonMacroAttrKind {
3939
Tool,
4040
/// Single-segment custom attribute registered by a derive macro (`#[serde(default)]`).
4141
DeriveHelper,
42+
/// Single-segment custom attribute registered by a derive macro
43+
/// but used before that derive macro was expanded (deprecated).
44+
DeriveHelperCompat,
4245
/// Single-segment custom attribute registered with `#[register_attr]`.
4346
Registered,
4447
}
@@ -370,7 +373,9 @@ impl NonMacroAttrKind {
370373
match self {
371374
NonMacroAttrKind::Builtin => "built-in attribute",
372375
NonMacroAttrKind::Tool => "tool attribute",
373-
NonMacroAttrKind::DeriveHelper => "derive helper attribute",
376+
NonMacroAttrKind::DeriveHelper | NonMacroAttrKind::DeriveHelperCompat => {
377+
"derive helper attribute"
378+
}
374379
NonMacroAttrKind::Registered => "explicitly registered attribute",
375380
}
376381
}
@@ -385,7 +390,9 @@ impl NonMacroAttrKind {
385390
/// Users of some attributes cannot mark them as used, so they are considered always used.
386391
pub fn is_used(self) -> bool {
387392
match self {
388-
NonMacroAttrKind::Tool | NonMacroAttrKind::DeriveHelper => true,
393+
NonMacroAttrKind::Tool
394+
| NonMacroAttrKind::DeriveHelper
395+
| NonMacroAttrKind::DeriveHelperCompat => true,
389396
NonMacroAttrKind::Builtin | NonMacroAttrKind::Registered => false,
390397
}
391398
}

compiler/rustc_resolve/src/diagnostics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,7 @@ impl<'a> Resolver<'a> {
609609
}
610610
}
611611
Scope::DeriveHelpersCompat => {
612-
let res = Res::NonMacroAttr(NonMacroAttrKind::DeriveHelper);
612+
let res = Res::NonMacroAttr(NonMacroAttrKind::DeriveHelperCompat);
613613
if filter_fn(res) {
614614
for derive in parent_scope.derives {
615615
let parent_scope = &ParentScope { derives: &[], ..*parent_scope };

compiler/rustc_resolve/src/macros.rs

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -568,10 +568,9 @@ impl<'a> Resolver<'a> {
568568
struct Flags: u8 {
569569
const MACRO_RULES = 1 << 0;
570570
const MODULE = 1 << 1;
571-
const DERIVE_HELPER_COMPAT = 1 << 2;
572-
const MISC_SUGGEST_CRATE = 1 << 3;
573-
const MISC_SUGGEST_SELF = 1 << 4;
574-
const MISC_FROM_PRELUDE = 1 << 5;
571+
const MISC_SUGGEST_CRATE = 1 << 2;
572+
const MISC_SUGGEST_SELF = 1 << 3;
573+
const MISC_FROM_PRELUDE = 1 << 4;
575574
}
576575
}
577576

@@ -646,14 +645,11 @@ impl<'a> Resolver<'a> {
646645
) {
647646
Ok((Some(ext), _)) => {
648647
if ext.helper_attrs.contains(&ident.name) {
649-
let binding = (
650-
Res::NonMacroAttr(NonMacroAttrKind::DeriveHelper),
651-
ty::Visibility::Public,
648+
result = ok(
649+
Res::NonMacroAttr(NonMacroAttrKind::DeriveHelperCompat),
652650
derive.span,
653-
ExpnId::root(),
654-
)
655-
.to_name_binding(this.arenas);
656-
result = Ok((binding, Flags::DERIVE_HELPER_COMPAT));
651+
this.arenas,
652+
);
657653
break;
658654
}
659655
}
@@ -799,17 +795,15 @@ impl<'a> Resolver<'a> {
799795
let (res, innermost_res) = (binding.res(), innermost_binding.res());
800796
if res != innermost_res {
801797
let builtin = Res::NonMacroAttr(NonMacroAttrKind::Builtin);
802-
let is_derive_helper_compat = |res, flags: Flags| {
803-
res == Res::NonMacroAttr(NonMacroAttrKind::DeriveHelper)
804-
&& flags.contains(Flags::DERIVE_HELPER_COMPAT)
805-
};
798+
let derive_helper_compat =
799+
Res::NonMacroAttr(NonMacroAttrKind::DeriveHelperCompat);
806800

807801
let ambiguity_error_kind = if is_import {
808802
Some(AmbiguityKind::Import)
809803
} else if innermost_res == builtin || res == builtin {
810804
Some(AmbiguityKind::BuiltinAttr)
811-
} else if is_derive_helper_compat(innermost_res, innermost_flags)
812-
|| is_derive_helper_compat(res, flags)
805+
} else if innermost_res == derive_helper_compat
806+
|| res == derive_helper_compat
813807
{
814808
Some(AmbiguityKind::DeriveHelper)
815809
} else if innermost_flags.contains(Flags::MACRO_RULES)

0 commit comments

Comments
 (0)