2024-01-30 08:51:28 +00:00
|
|
|
//! Proc Macro Expander stuff
|
2020-03-18 09:47:59 +00:00
|
|
|
|
2023-12-18 11:09:54 +00:00
|
|
|
use core::fmt;
|
|
|
|
use std::{panic::RefUnwindSafe, sync};
|
|
|
|
|
2023-12-18 12:30:41 +00:00
|
|
|
use base_db::{CrateId, Env};
|
2024-07-16 10:43:58 +00:00
|
|
|
use intern::Symbol;
|
2023-12-18 11:09:54 +00:00
|
|
|
use rustc_hash::FxHashMap;
|
2023-12-18 12:30:41 +00:00
|
|
|
use span::Span;
|
2020-03-18 09:47:59 +00:00
|
|
|
|
2024-07-29 12:52:40 +00:00
|
|
|
use crate::{db::ExpandDatabase, tt, ExpandError, ExpandErrorKind, ExpandResult};
|
2021-10-27 12:35:58 +00:00
|
|
|
|
2023-12-18 11:09:54 +00:00
|
|
|
#[derive(Copy, Clone, Eq, PartialEq, Debug, Hash)]
|
|
|
|
pub enum ProcMacroKind {
|
|
|
|
CustomDerive,
|
2024-03-21 08:33:17 +00:00
|
|
|
Bang,
|
2023-12-18 11:09:54 +00:00
|
|
|
Attr,
|
|
|
|
}
|
|
|
|
|
2024-07-26 12:36:13 +00:00
|
|
|
/// A proc-macro expander implementation.
|
2023-12-18 11:09:54 +00:00
|
|
|
pub trait ProcMacroExpander: fmt::Debug + Send + Sync + RefUnwindSafe {
|
2024-07-26 12:36:13 +00:00
|
|
|
/// Run the expander with the given input subtree, optional attribute input subtree (for
|
|
|
|
/// [`ProcMacroKind::Attr`]), environment variables, and span information.
|
2023-12-18 11:09:54 +00:00
|
|
|
fn expand(
|
|
|
|
&self,
|
|
|
|
subtree: &tt::Subtree,
|
|
|
|
attrs: Option<&tt::Subtree>,
|
|
|
|
env: &Env,
|
2023-12-18 12:30:41 +00:00
|
|
|
def_site: Span,
|
|
|
|
call_site: Span,
|
|
|
|
mixed_site: Span,
|
2023-12-18 11:09:54 +00:00
|
|
|
) -> Result<tt::Subtree, ProcMacroExpansionError>;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum ProcMacroExpansionError {
|
2024-07-26 12:36:13 +00:00
|
|
|
/// The proc-macro panicked.
|
2023-12-18 11:09:54 +00:00
|
|
|
Panic(String),
|
2024-07-26 12:36:13 +00:00
|
|
|
/// The server itself errored out.
|
2023-12-18 11:09:54 +00:00
|
|
|
System(String),
|
|
|
|
}
|
|
|
|
|
2024-07-26 12:36:13 +00:00
|
|
|
pub type ProcMacroLoadResult = Result<Vec<ProcMacro>, (String, bool)>;
|
|
|
|
type StoredProcMacroLoadResult = Result<Box<[ProcMacro]>, (Box<str>, bool)>;
|
|
|
|
|
|
|
|
#[derive(Default, Debug)]
|
|
|
|
pub struct ProcMacrosBuilder(FxHashMap<CrateId, StoredProcMacroLoadResult>);
|
|
|
|
impl ProcMacrosBuilder {
|
|
|
|
pub fn insert(&mut self, proc_macros_crate: CrateId, proc_macro: ProcMacroLoadResult) {
|
|
|
|
self.0.insert(
|
|
|
|
proc_macros_crate,
|
|
|
|
match proc_macro {
|
|
|
|
Ok(it) => Ok(it.into_boxed_slice()),
|
|
|
|
Err((e, hard_err)) => Err((e.into_boxed_str(), hard_err)),
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
pub fn build(mut self) -> ProcMacros {
|
|
|
|
self.0.shrink_to_fit();
|
|
|
|
ProcMacros(self.0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default, Debug)]
|
|
|
|
pub struct ProcMacros(FxHashMap<CrateId, StoredProcMacroLoadResult>);
|
|
|
|
|
|
|
|
impl FromIterator<(CrateId, ProcMacroLoadResult)> for ProcMacros {
|
|
|
|
fn from_iter<T: IntoIterator<Item = (CrateId, ProcMacroLoadResult)>>(iter: T) -> Self {
|
|
|
|
let mut builder = ProcMacrosBuilder::default();
|
|
|
|
for (k, v) in iter {
|
|
|
|
builder.insert(k, v);
|
|
|
|
}
|
|
|
|
builder.build()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ProcMacros {
|
2024-07-29 12:52:40 +00:00
|
|
|
fn get(&self, krate: CrateId, idx: u32, err_span: Span) -> Result<&ProcMacro, ExpandError> {
|
2024-07-26 12:36:13 +00:00
|
|
|
let proc_macros = match self.0.get(&krate) {
|
|
|
|
Some(Ok(proc_macros)) => proc_macros,
|
|
|
|
Some(Err(_)) | None => {
|
2024-07-29 12:52:40 +00:00
|
|
|
return Err(ExpandError::other(
|
|
|
|
err_span,
|
|
|
|
"internal error: no proc macros for crate",
|
|
|
|
));
|
2024-07-26 12:36:13 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
proc_macros.get(idx as usize).ok_or_else(|| {
|
2024-07-29 12:52:40 +00:00
|
|
|
ExpandError::other(err_span,
|
2024-07-26 12:36:13 +00:00
|
|
|
format!(
|
|
|
|
"internal error: proc-macro index out of bounds: the length is {} but the index is {}",
|
|
|
|
proc_macros.len(),
|
|
|
|
idx
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
2023-12-18 11:09:54 +00:00
|
|
|
|
2024-07-26 12:36:13 +00:00
|
|
|
pub fn get_error_for_crate(&self, krate: CrateId) -> Option<(&str, bool)> {
|
|
|
|
self.0.get(&krate).and_then(|it| it.as_ref().err()).map(|(e, hard_err)| (&**e, *hard_err))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Fetch the [`CustomProcMacroExpander`]s and their corresponding names for the given crate.
|
|
|
|
pub fn for_crate(
|
|
|
|
&self,
|
|
|
|
krate: CrateId,
|
|
|
|
def_site_ctx: span::SyntaxContextId,
|
|
|
|
) -> Option<Box<[(crate::name::Name, CustomProcMacroExpander, bool)]>> {
|
|
|
|
match self.0.get(&krate) {
|
|
|
|
Some(Ok(proc_macros)) => Some({
|
|
|
|
proc_macros
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.map(|(idx, it)| {
|
|
|
|
let name = crate::name::Name::new_symbol(it.name.clone(), def_site_ctx);
|
|
|
|
(name, CustomProcMacroExpander::new(idx as u32), it.disabled)
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
}),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-12-18 11:09:54 +00:00
|
|
|
|
2024-07-26 12:36:13 +00:00
|
|
|
/// A loaded proc-macro.
|
2023-12-18 11:09:54 +00:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct ProcMacro {
|
2024-07-26 12:36:13 +00:00
|
|
|
/// The name of the proc macro.
|
2024-07-16 10:43:58 +00:00
|
|
|
pub name: Symbol,
|
2023-12-18 11:09:54 +00:00
|
|
|
pub kind: ProcMacroKind,
|
2024-07-26 12:36:13 +00:00
|
|
|
/// The expander handle for this proc macro.
|
2023-12-18 11:09:54 +00:00
|
|
|
pub expander: sync::Arc<dyn ProcMacroExpander>,
|
2024-07-26 12:36:13 +00:00
|
|
|
/// Whether this proc-macro is disabled for early name resolution. Notably, the
|
|
|
|
/// [`Self::expander`] is still usable.
|
2024-02-12 12:34:38 +00:00
|
|
|
pub disabled: bool,
|
2023-12-18 11:09:54 +00:00
|
|
|
}
|
|
|
|
|
2024-07-26 12:36:13 +00:00
|
|
|
/// A custom proc-macro expander handle. This handle together with its crate resolves to a [`ProcMacro`]
|
2020-03-18 09:47:59 +00:00
|
|
|
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
|
2023-12-18 11:09:54 +00:00
|
|
|
pub struct CustomProcMacroExpander {
|
2024-07-26 12:36:13 +00:00
|
|
|
proc_macro_id: u32,
|
2020-03-18 09:47:59 +00:00
|
|
|
}
|
|
|
|
|
2023-12-18 11:09:54 +00:00
|
|
|
impl CustomProcMacroExpander {
|
2024-07-26 12:36:13 +00:00
|
|
|
const MISSING_EXPANDER: u32 = !0;
|
2024-02-12 12:34:38 +00:00
|
|
|
const DISABLED_ID: u32 = !1;
|
2024-07-26 12:36:13 +00:00
|
|
|
const PROC_MACRO_ATTR_DISABLED: u32 = !2;
|
2024-02-12 12:34:38 +00:00
|
|
|
|
2024-07-26 12:36:13 +00:00
|
|
|
pub fn new(proc_macro_id: u32) -> Self {
|
|
|
|
assert_ne!(proc_macro_id, Self::MISSING_EXPANDER);
|
|
|
|
assert_ne!(proc_macro_id, Self::DISABLED_ID);
|
|
|
|
assert_ne!(proc_macro_id, Self::PROC_MACRO_ATTR_DISABLED);
|
2023-05-20 08:38:25 +00:00
|
|
|
Self { proc_macro_id }
|
2020-09-18 14:43:50 +00:00
|
|
|
}
|
|
|
|
|
2024-07-26 12:36:13 +00:00
|
|
|
/// An expander that always errors due to the actual proc-macro expander missing.
|
|
|
|
pub const fn missing_expander() -> Self {
|
|
|
|
Self { proc_macro_id: Self::MISSING_EXPANDER }
|
2021-05-31 11:37:11 +00:00
|
|
|
}
|
|
|
|
|
2024-02-12 12:34:38 +00:00
|
|
|
/// A dummy expander that always errors. This expander is used for macros that have been disabled.
|
|
|
|
pub const fn disabled() -> Self {
|
2024-07-26 12:36:13 +00:00
|
|
|
Self { proc_macro_id: Self::DISABLED_ID }
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A dummy expander that always errors. This expander is used for attribute macros when
|
|
|
|
/// proc-macro attribute expansion is disabled.
|
|
|
|
pub const fn disabled_proc_attr() -> Self {
|
|
|
|
Self { proc_macro_id: Self::PROC_MACRO_ATTR_DISABLED }
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The macro-expander is missing or has yet to be build.
|
|
|
|
pub const fn is_missing(&self) -> bool {
|
|
|
|
self.proc_macro_id == Self::MISSING_EXPANDER
|
2023-11-17 14:25:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// The macro is explicitly disabled and cannot be expanded.
|
2024-02-12 12:34:38 +00:00
|
|
|
pub const fn is_disabled(&self) -> bool {
|
2024-07-26 12:36:13 +00:00
|
|
|
self.proc_macro_id == Self::DISABLED_ID
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The macro is explicitly disabled due to proc-macro attribute expansion being disabled.
|
|
|
|
pub const fn is_disabled_proc_attr(&self) -> bool {
|
|
|
|
self.proc_macro_id == Self::PROC_MACRO_ATTR_DISABLED
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The macro is explicitly disabled due to proc-macro attribute expansion being disabled.
|
2024-07-29 12:52:40 +00:00
|
|
|
pub fn as_expand_error(&self, def_crate: CrateId) -> Option<ExpandErrorKind> {
|
2024-07-26 12:36:13 +00:00
|
|
|
match self.proc_macro_id {
|
2024-07-29 12:52:40 +00:00
|
|
|
Self::PROC_MACRO_ATTR_DISABLED => Some(ExpandErrorKind::ProcMacroAttrExpansionDisabled),
|
|
|
|
Self::DISABLED_ID => Some(ExpandErrorKind::MacroDisabled),
|
|
|
|
Self::MISSING_EXPANDER => Some(ExpandErrorKind::MissingProcMacroExpander(def_crate)),
|
2024-07-26 12:36:13 +00:00
|
|
|
_ => None,
|
|
|
|
}
|
2023-11-17 14:25:20 +00:00
|
|
|
}
|
|
|
|
|
2020-03-18 09:47:59 +00:00
|
|
|
pub fn expand(
|
2020-05-26 18:12:13 +00:00
|
|
|
self,
|
2023-03-13 15:33:52 +00:00
|
|
|
db: &dyn ExpandDatabase,
|
2023-03-13 15:49:32 +00:00
|
|
|
def_crate: CrateId,
|
2020-12-11 13:57:50 +00:00
|
|
|
calling_crate: CrateId,
|
2020-03-18 12:56:46 +00:00
|
|
|
tt: &tt::Subtree,
|
2021-05-31 11:37:11 +00:00
|
|
|
attr_arg: Option<&tt::Subtree>,
|
2023-12-18 12:30:41 +00:00
|
|
|
def_site: Span,
|
|
|
|
call_site: Span,
|
|
|
|
mixed_site: Span,
|
2021-08-20 12:34:46 +00:00
|
|
|
) -> ExpandResult<tt::Subtree> {
|
2020-09-18 14:43:50 +00:00
|
|
|
match self.proc_macro_id {
|
2024-07-26 12:36:13 +00:00
|
|
|
Self::PROC_MACRO_ATTR_DISABLED => ExpandResult::new(
|
|
|
|
tt::Subtree::empty(tt::DelimSpan { open: call_site, close: call_site }),
|
2024-07-29 12:52:40 +00:00
|
|
|
ExpandError::new(call_site, ExpandErrorKind::ProcMacroAttrExpansionDisabled),
|
2024-07-26 12:36:13 +00:00
|
|
|
),
|
|
|
|
Self::MISSING_EXPANDER => ExpandResult::new(
|
2023-12-01 12:56:25 +00:00
|
|
|
tt::Subtree::empty(tt::DelimSpan { open: call_site, close: call_site }),
|
2024-07-29 12:52:40 +00:00
|
|
|
ExpandError::new(call_site, ExpandErrorKind::MissingProcMacroExpander(def_crate)),
|
2023-12-01 12:56:25 +00:00
|
|
|
),
|
2024-07-26 12:36:13 +00:00
|
|
|
Self::DISABLED_ID => ExpandResult::new(
|
2023-11-17 14:25:20 +00:00
|
|
|
tt::Subtree::empty(tt::DelimSpan { open: call_site, close: call_site }),
|
2024-07-29 12:52:40 +00:00
|
|
|
ExpandError::new(call_site, ExpandErrorKind::MacroDisabled),
|
2023-11-17 14:25:20 +00:00
|
|
|
),
|
2024-07-26 12:36:13 +00:00
|
|
|
id => {
|
2023-03-25 15:42:52 +00:00
|
|
|
let proc_macros = db.proc_macros();
|
2024-07-29 12:52:40 +00:00
|
|
|
let proc_macro = match proc_macros.get(def_crate, id, call_site) {
|
2024-07-26 12:36:13 +00:00
|
|
|
Ok(proc_macro) => proc_macro,
|
|
|
|
Err(e) => {
|
2023-04-16 17:20:48 +00:00
|
|
|
return ExpandResult::new(
|
2023-12-01 12:56:25 +00:00
|
|
|
tt::Subtree::empty(tt::DelimSpan { open: call_site, close: call_site }),
|
2024-07-26 12:36:13 +00:00
|
|
|
e,
|
|
|
|
)
|
2022-02-21 18:14:06 +00:00
|
|
|
}
|
2021-08-20 12:34:46 +00:00
|
|
|
};
|
2020-09-18 14:43:50 +00:00
|
|
|
|
2023-03-25 15:42:52 +00:00
|
|
|
let krate_graph = db.crate_graph();
|
2020-12-11 13:57:50 +00:00
|
|
|
// Proc macros have access to the environment variables of the invoking crate.
|
|
|
|
let env = &krate_graph[calling_crate].env;
|
2023-11-28 15:28:51 +00:00
|
|
|
match proc_macro.expander.expand(tt, attr_arg, env, def_site, call_site, mixed_site)
|
|
|
|
{
|
2021-10-27 12:35:58 +00:00
|
|
|
Ok(t) => ExpandResult::ok(t),
|
|
|
|
Err(err) => match err {
|
|
|
|
// Don't discard the item in case something unexpected happened while expanding attributes
|
|
|
|
ProcMacroExpansionError::System(text)
|
|
|
|
if proc_macro.kind == ProcMacroKind::Attr =>
|
|
|
|
{
|
2024-07-29 12:52:40 +00:00
|
|
|
ExpandResult {
|
|
|
|
value: tt.clone(),
|
|
|
|
err: Some(ExpandError::other(call_site, text)),
|
|
|
|
}
|
2021-10-27 12:35:58 +00:00
|
|
|
}
|
|
|
|
ProcMacroExpansionError::System(text)
|
2023-12-01 12:56:25 +00:00
|
|
|
| ProcMacroExpansionError::Panic(text) => ExpandResult::new(
|
|
|
|
tt::Subtree::empty(tt::DelimSpan { open: call_site, close: call_site }),
|
2024-07-29 12:52:40 +00:00
|
|
|
ExpandError::new(
|
|
|
|
call_site,
|
|
|
|
ExpandErrorKind::ProcMacroPanic(text.into_boxed_str()),
|
|
|
|
),
|
2023-12-01 12:56:25 +00:00
|
|
|
),
|
2021-10-27 12:35:58 +00:00
|
|
|
},
|
|
|
|
}
|
2020-09-18 14:43:50 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-18 09:47:59 +00:00
|
|
|
}
|
|
|
|
}
|