Skip to content

Commit aecf26d

Browse files
committed
feat: Add config to replace specific proc-macros with dummy expanders
1 parent 68bc12c commit aecf26d

File tree

6 files changed

+68
-17
lines changed

6 files changed

+68
-17
lines changed

crates/hir_def/src/db.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ pub trait InternDatabase: SourceDatabase {
5555
pub trait DefDatabase: InternDatabase + AstDatabase + Upcast<dyn AstDatabase> {
5656
#[salsa::input]
5757
fn enable_proc_attr_macros(&self) -> bool;
58+
#[salsa::input]
59+
fn enablse_proc_attr_macros(&self) -> bool;
5860

5961
#[salsa::invoke(ItemTree::file_item_tree_query)]
6062
fn file_item_tree(&self, file_id: HirFileId) -> Arc<ItemTree>;

crates/project_model/src/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ fn rooted_project_json(data: ProjectJsonData) -> ProjectJson {
8888
}
8989

9090
fn to_crate_graph(project_workspace: ProjectWorkspace) -> CrateGraph {
91-
project_workspace.to_crate_graph(&mut |_| Vec::new(), &mut {
91+
project_workspace.to_crate_graph(&Default::default(), &mut |_, _| Vec::new(), &mut {
9292
let mut counter = 0;
9393
move |_path| {
9494
counter += 1;

crates/project_model/src/workspace.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -387,10 +387,14 @@ impl ProjectWorkspace {
387387

388388
pub fn to_crate_graph(
389389
&self,
390-
load_proc_macro: &mut dyn FnMut(&AbsPath) -> Vec<ProcMacro>,
390+
dummy_replace: &FxHashMap<Box<str>, Box<[Box<str>]>>,
391+
load_proc_macro: &mut dyn FnMut(&AbsPath, &[Box<str>]) -> Vec<ProcMacro>,
391392
load: &mut dyn FnMut(&AbsPath) -> Option<FileId>,
392393
) -> CrateGraph {
393394
let _p = profile::span("ProjectWorkspace::to_crate_graph");
395+
let load_proc_macro = &mut |crate_name: &_, path: &_| {
396+
load_proc_macro(path, dummy_replace.get(crate_name).map(|it| &**it).unwrap_or_default())
397+
};
394398

395399
let mut crate_graph = match self {
396400
ProjectWorkspace::Json { project, sysroot, rustc_cfg } => project_json_to_crate_graph(
@@ -432,7 +436,7 @@ impl ProjectWorkspace {
432436

433437
fn project_json_to_crate_graph(
434438
rustc_cfg: Vec<CfgFlag>,
435-
load_proc_macro: &mut dyn FnMut(&AbsPath) -> Vec<ProcMacro>,
439+
load_proc_macro: &mut dyn FnMut(&str, &AbsPath) -> Vec<ProcMacro>,
436440
load: &mut dyn FnMut(&AbsPath) -> Option<FileId>,
437441
project: &ProjectJson,
438442
sysroot: &Option<Sysroot>,
@@ -452,7 +456,12 @@ fn project_json_to_crate_graph(
452456
})
453457
.map(|(crate_id, krate, file_id)| {
454458
let env = krate.env.clone().into_iter().collect();
455-
let proc_macro = krate.proc_macro_dylib_path.clone().map(|it| load_proc_macro(&it));
459+
let proc_macro = krate.proc_macro_dylib_path.clone().map(|it| {
460+
load_proc_macro(
461+
krate.display_name.as_ref().map(|it| it.canonical_name()).unwrap_or(""),
462+
&it,
463+
)
464+
});
456465

457466
let target_cfgs = match krate.target.as_deref() {
458467
Some(target) => {
@@ -513,7 +522,7 @@ fn project_json_to_crate_graph(
513522
fn cargo_to_crate_graph(
514523
rustc_cfg: Vec<CfgFlag>,
515524
override_cfg: &CfgOverrides,
516-
load_proc_macro: &mut dyn FnMut(&AbsPath) -> Vec<ProcMacro>,
525+
load_proc_macro: &mut dyn FnMut(&str, &AbsPath) -> Vec<ProcMacro>,
517526
load: &mut dyn FnMut(&AbsPath) -> Option<FileId>,
518527
cargo: &CargoWorkspace,
519528
build_scripts: &WorkspaceBuildScripts,
@@ -571,7 +580,7 @@ fn cargo_to_crate_graph(
571580
&cargo[pkg],
572581
build_scripts.outputs.get(pkg),
573582
cfg_options,
574-
load_proc_macro,
583+
&mut |path| load_proc_macro(&cargo[tgt].name, path),
575584
file_id,
576585
&cargo[tgt].name,
577586
);
@@ -702,7 +711,7 @@ fn handle_rustc_crates(
702711
load: &mut dyn FnMut(&AbsPath) -> Option<FileId>,
703712
crate_graph: &mut CrateGraph,
704713
cfg_options: &CfgOptions,
705-
load_proc_macro: &mut dyn FnMut(&AbsPath) -> Vec<ProcMacro>,
714+
load_proc_macro: &mut dyn FnMut(&str, &AbsPath) -> Vec<ProcMacro>,
706715
pkg_to_lib_crate: &mut FxHashMap<la_arena::Idx<crate::PackageData>, CrateId>,
707716
public_deps: &SysrootPublicDeps,
708717
cargo: &CargoWorkspace,
@@ -738,7 +747,7 @@ fn handle_rustc_crates(
738747
&rustc_workspace[pkg],
739748
None,
740749
cfg_options,
741-
load_proc_macro,
750+
&mut |path| load_proc_macro(&rustc_workspace[tgt].name, path),
742751
file_id,
743752
&rustc_workspace[tgt].name,
744753
);

crates/rust-analyzer/src/cli/load_cargo.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ pub fn load_workspace(
6666
};
6767

6868
let crate_graph = ws.to_crate_graph(
69-
&mut |path: &AbsPath| load_proc_macro(proc_macro_client.as_ref(), path),
69+
&Default::default(),
70+
&mut |path: &AbsPath, _| load_proc_macro(proc_macro_client.as_ref(), path, &[]),
7071
&mut |path: &AbsPath| {
7172
let contents = loader.load_sync(path);
7273
let path = vfs::VfsPath::from(path.to_path_buf());

crates/rust-analyzer/src/config.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ config_data! {
301301
/// Internal config, path to proc-macro server executable (typically,
302302
/// this is rust-analyzer itself, but we override this in tests).
303303
procMacro_server: Option<PathBuf> = "null",
304+
procMacro_dummies: FxHashMap<Box<str>, Box<[Box<str>]>> = "{}",
304305

305306
/// Command to be executed instead of 'cargo' for runnables.
306307
runnables_overrideCargo: Option<String> = "null",
@@ -716,6 +717,9 @@ impl Config {
716717
};
717718
Some((path, vec!["proc-macro".into()]))
718719
}
720+
pub fn dummy_replacements(&self) -> &FxHashMap<Box<str>, Box<[Box<str>]>> {
721+
&self.data.procMacro_dummies
722+
}
719723
pub fn expand_proc_attr_macros(&self) -> bool {
720724
self.data.experimental_procAttrMacros
721725
}

crates/rust-analyzer/src/reload.rs

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use ide_db::base_db::{
1010
};
1111
use proc_macro_api::{MacroDylib, ProcMacroServer};
1212
use project_model::{ProjectWorkspace, WorkspaceBuildScripts};
13+
use syntax::SmolStr;
1314
use vfs::{file_set::FileSetConfig, AbsPath, AbsPathBuf, ChangeKind};
1415

1516
use crate::{
@@ -290,6 +291,9 @@ impl GlobalState {
290291
}
291292
},
292293
};
294+
self.analysis_host
295+
.raw_database_mut()
296+
.set_enable_proc_attr_macros(self.config.expand_proc_attr_macros());
293297
}
294298

295299
let watch = match files_config.watcher {
@@ -306,8 +310,9 @@ impl GlobalState {
306310
// Create crate graph from all the workspaces
307311
let crate_graph = {
308312
let proc_macro_client = self.proc_macro_client.as_ref();
309-
let mut load_proc_macro =
310-
move |path: &AbsPath| load_proc_macro(proc_macro_client, path);
313+
let mut load_proc_macro = move |path: &AbsPath, dummy_replace: &_| {
314+
load_proc_macro(proc_macro_client, path, dummy_replace)
315+
};
311316

312317
let vfs = &mut self.vfs.write().0;
313318
let loader = &mut self.loader;
@@ -328,7 +333,11 @@ impl GlobalState {
328333

329334
let mut crate_graph = CrateGraph::default();
330335
for ws in self.workspaces.iter() {
331-
crate_graph.extend(ws.to_crate_graph(&mut load_proc_macro, &mut load));
336+
crate_graph.extend(ws.to_crate_graph(
337+
self.config.dummy_replacements(),
338+
&mut load_proc_macro,
339+
&mut load,
340+
));
332341
}
333342
crate_graph
334343
};
@@ -505,7 +514,11 @@ impl SourceRootConfig {
505514
}
506515
}
507516

508-
pub(crate) fn load_proc_macro(client: Option<&ProcMacroServer>, path: &AbsPath) -> Vec<ProcMacro> {
517+
pub(crate) fn load_proc_macro(
518+
client: Option<&ProcMacroServer>,
519+
path: &AbsPath,
520+
dummy_replace: &[Box<str>],
521+
) -> Vec<ProcMacro> {
509522
let dylib = match MacroDylib::new(path.to_path_buf()) {
510523
Ok(it) => it,
511524
Err(err) => {
@@ -532,17 +545,25 @@ pub(crate) fn load_proc_macro(client: Option<&ProcMacroServer>, path: &AbsPath)
532545
Vec::new()
533546
}
534547
})
535-
.map(expander_to_proc_macro)
548+
.map(|expander| expander_to_proc_macro(expander, dummy_replace))
536549
.collect();
537550

538-
fn expander_to_proc_macro(expander: proc_macro_api::ProcMacro) -> ProcMacro {
539-
let name = expander.name().into();
551+
fn expander_to_proc_macro(
552+
expander: proc_macro_api::ProcMacro,
553+
dummy_replace: &[Box<str>],
554+
) -> ProcMacro {
555+
let name = SmolStr::from(expander.name());
540556
let kind = match expander.kind() {
541557
proc_macro_api::ProcMacroKind::CustomDerive => ProcMacroKind::CustomDerive,
542558
proc_macro_api::ProcMacroKind::FuncLike => ProcMacroKind::FuncLike,
543559
proc_macro_api::ProcMacroKind::Attr => ProcMacroKind::Attr,
544560
};
545-
let expander = Arc::new(Expander(expander));
561+
let expander: Arc<dyn ProcMacroExpander> =
562+
if dummy_replace.iter().any(|replace| &**replace == name) {
563+
Arc::new(DummyExpander)
564+
} else {
565+
Arc::new(Expander(expander))
566+
};
546567
ProcMacro { name, kind, expander }
547568
}
548569

@@ -564,6 +585,20 @@ pub(crate) fn load_proc_macro(client: Option<&ProcMacroServer>, path: &AbsPath)
564585
}
565586
}
566587
}
588+
589+
#[derive(Debug)]
590+
struct DummyExpander;
591+
592+
impl ProcMacroExpander for DummyExpander {
593+
fn expand(
594+
&self,
595+
subtree: &tt::Subtree,
596+
_: Option<&tt::Subtree>,
597+
_: &Env,
598+
) -> Result<tt::Subtree, ProcMacroExpansionError> {
599+
Ok(subtree.clone())
600+
}
601+
}
567602
}
568603

569604
pub(crate) fn should_refresh_for_change(path: &AbsPath, change_kind: ChangeKind) -> bool {

0 commit comments

Comments
 (0)