Encode disabled proc-macros via boolean flag, not special Expander

This commit is contained in:
Lukas Wirth 2024-02-12 13:34:38 +01:00
parent ab50ec9863
commit e2a985e93f
8 changed files with 91 additions and 148 deletions

View file

@ -31,16 +31,6 @@ pub trait ProcMacroExpander: fmt::Debug + Send + Sync + RefUnwindSafe {
call_site: Span,
mixed_site: Span,
) -> Result<tt::Subtree, ProcMacroExpansionError>;
/// If this returns `false`, expansions via [`expand`](ProcMacroExpander::expand) will always
/// return the input subtree or will always return an error.
///
/// This is used to skip any additional expansion-related work,
/// e.g. to make sure we do not touch the syntax tree in any way
/// if a proc macro will never be expanded.
fn should_expand(&self) -> bool {
true
}
}
#[derive(Debug)]
@ -59,6 +49,7 @@ pub struct ProcMacro {
pub name: SmolStr,
pub kind: ProcMacroKind,
pub expander: sync::Arc<dyn ProcMacroExpander>,
pub disabled: bool,
}
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
@ -66,31 +57,35 @@ pub struct CustomProcMacroExpander {
proc_macro_id: ProcMacroId,
}
const DUMMY_ID: u32 = !0;
const DISABLED_ID: u32 = !1;
impl CustomProcMacroExpander {
const DUMMY_ID: u32 = !0;
const DISABLED_ID: u32 = !1;
pub fn new(proc_macro_id: ProcMacroId) -> Self {
assert_ne!(proc_macro_id.0, DUMMY_ID);
assert_ne!(proc_macro_id.0, Self::DUMMY_ID);
assert_ne!(proc_macro_id.0, Self::DISABLED_ID);
Self { proc_macro_id }
}
pub fn dummy() -> Self {
Self { proc_macro_id: ProcMacroId(DUMMY_ID) }
/// A dummy expander that always errors. This is used for proc-macros that are missing, usually
/// due to them not being built yet.
pub const fn dummy() -> Self {
Self { proc_macro_id: ProcMacroId(Self::DUMMY_ID) }
}
/// The macro was not yet resolved.
pub fn is_dummy(&self) -> bool {
self.proc_macro_id.0 == DUMMY_ID
pub const fn is_dummy(&self) -> bool {
self.proc_macro_id.0 == Self::DUMMY_ID
}
pub fn disabled() -> Self {
Self { proc_macro_id: ProcMacroId(DISABLED_ID) }
/// A dummy expander that always errors. This expander is used for macros that have been disabled.
pub const fn disabled() -> Self {
Self { proc_macro_id: ProcMacroId(Self::DISABLED_ID) }
}
/// The macro is explicitly disabled and cannot be expanded.
pub fn is_disabled(&self) -> bool {
self.proc_macro_id.0 == DISABLED_ID
pub const fn is_disabled(&self) -> bool {
self.proc_macro_id.0 == Self::DISABLED_ID
}
pub fn expand(
@ -105,11 +100,11 @@ impl CustomProcMacroExpander {
mixed_site: Span,
) -> ExpandResult<tt::Subtree> {
match self.proc_macro_id {
ProcMacroId(DUMMY_ID) => ExpandResult::new(
ProcMacroId(Self::DUMMY_ID) => ExpandResult::new(
tt::Subtree::empty(tt::DelimSpan { open: call_site, close: call_site }),
ExpandError::UnresolvedProcMacro(def_crate),
),
ProcMacroId(DISABLED_ID) => ExpandResult::new(
ProcMacroId(Self::DISABLED_ID) => ExpandResult::new(
tt::Subtree::empty(tt::DelimSpan { open: call_site, close: call_site }),
ExpandError::MacroDisabled,
),
@ -135,7 +130,7 @@ impl CustomProcMacroExpander {
);
return ExpandResult::new(
tt::Subtree::empty(tt::DelimSpan { open: call_site, close: call_site }),
ExpandError::other("Internal error"),
ExpandError::other("Internal error: proc-macro index out of bounds"),
);
}
};