mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-15 14:55:04 +00:00
Split out proc-macros from the CrateGraph
This commit is contained in:
parent
71b23360e7
commit
d154ea88f9
12 changed files with 141 additions and 176 deletions
|
@ -6,7 +6,7 @@ use std::{fmt, sync::Arc};
|
|||
use salsa::Durability;
|
||||
use vfs::FileId;
|
||||
|
||||
use crate::{CrateGraph, SourceDatabaseExt, SourceRoot, SourceRootId};
|
||||
use crate::{CrateGraph, ProcMacros, SourceDatabaseExt, SourceRoot, SourceRootId};
|
||||
|
||||
/// Encapsulate a bunch of raw `.set` calls on the database.
|
||||
#[derive(Default)]
|
||||
|
@ -14,6 +14,7 @@ pub struct Change {
|
|||
pub roots: Option<Vec<SourceRoot>>,
|
||||
pub files_changed: Vec<(FileId, Option<Arc<String>>)>,
|
||||
pub crate_graph: Option<CrateGraph>,
|
||||
pub proc_macros: Option<ProcMacros>,
|
||||
}
|
||||
|
||||
impl fmt::Debug for Change {
|
||||
|
@ -49,6 +50,10 @@ impl Change {
|
|||
self.crate_graph = Some(graph);
|
||||
}
|
||||
|
||||
pub fn set_proc_macros(&mut self, proc_macros: ProcMacros) {
|
||||
self.proc_macros = Some(proc_macros);
|
||||
}
|
||||
|
||||
pub fn apply(self, db: &mut dyn SourceDatabaseExt) {
|
||||
let _p = profile::span("RootDatabase::apply_change");
|
||||
if let Some(roots) = self.roots {
|
||||
|
@ -73,6 +78,9 @@ impl Change {
|
|||
if let Some(crate_graph) = self.crate_graph {
|
||||
db.set_crate_graph_with_durability(Arc::new(crate_graph), Durability::HIGH)
|
||||
}
|
||||
if let Some(proc_macros) = self.proc_macros {
|
||||
db.set_proc_macros_with_durability(Arc::new(proc_macros), Durability::HIGH)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -12,8 +12,8 @@ use vfs::{file_set::FileSet, VfsPath};
|
|||
use crate::{
|
||||
input::{CrateName, CrateOrigin, LangCrateOrigin},
|
||||
Change, CrateDisplayName, CrateGraph, CrateId, Dependency, Edition, Env, FileId, FilePosition,
|
||||
FileRange, ProcMacro, ProcMacroExpander, ProcMacroExpansionError, SourceDatabaseExt,
|
||||
SourceRoot, SourceRootId,
|
||||
FileRange, ProcMacro, ProcMacroExpander, ProcMacroExpansionError, ProcMacros,
|
||||
SourceDatabaseExt, SourceRoot, SourceRootId,
|
||||
};
|
||||
|
||||
pub const WORKSPACE: SourceRootId = SourceRootId(0);
|
||||
|
@ -100,7 +100,7 @@ impl ChangeFixture {
|
|||
|
||||
pub fn parse_with_proc_macros(
|
||||
ra_fixture: &str,
|
||||
mut proc_macros: Vec<(String, ProcMacro)>,
|
||||
mut proc_macro_defs: Vec<(String, ProcMacro)>,
|
||||
) -> ChangeFixture {
|
||||
let (mini_core, proc_macro_names, fixture) = Fixture::parse(ra_fixture);
|
||||
let mut change = Change::new();
|
||||
|
@ -160,7 +160,6 @@ impl ChangeFixture {
|
|||
meta.cfg.clone(),
|
||||
meta.cfg,
|
||||
meta.env,
|
||||
Ok(Vec::new()),
|
||||
false,
|
||||
origin,
|
||||
meta.target_data_layout
|
||||
|
@ -200,7 +199,6 @@ impl ChangeFixture {
|
|||
default_cfg.clone(),
|
||||
default_cfg,
|
||||
Env::default(),
|
||||
Ok(Vec::new()),
|
||||
false,
|
||||
CrateOrigin::CratesIo { repo: None, name: None },
|
||||
default_target_data_layout
|
||||
|
@ -244,7 +242,6 @@ impl ChangeFixture {
|
|||
CfgOptions::default(),
|
||||
CfgOptions::default(),
|
||||
Env::default(),
|
||||
Ok(Vec::new()),
|
||||
false,
|
||||
CrateOrigin::Lang(LangCrateOrigin::Core),
|
||||
target_layout.clone(),
|
||||
|
@ -257,12 +254,13 @@ impl ChangeFixture {
|
|||
}
|
||||
}
|
||||
|
||||
let mut proc_macros = ProcMacros::default();
|
||||
if !proc_macro_names.is_empty() {
|
||||
let proc_lib_file = file_id;
|
||||
file_id.0 += 1;
|
||||
|
||||
proc_macros.extend(default_test_proc_macros());
|
||||
let (proc_macro, source) = filter_test_proc_macros(&proc_macro_names, proc_macros);
|
||||
proc_macro_defs.extend(default_test_proc_macros());
|
||||
let (proc_macro, source) = filter_test_proc_macros(&proc_macro_names, proc_macro_defs);
|
||||
let mut fs = FileSet::default();
|
||||
fs.insert(
|
||||
proc_lib_file,
|
||||
|
@ -282,11 +280,11 @@ impl ChangeFixture {
|
|||
CfgOptions::default(),
|
||||
CfgOptions::default(),
|
||||
Env::default(),
|
||||
Ok(proc_macro),
|
||||
true,
|
||||
CrateOrigin::CratesIo { repo: None, name: None },
|
||||
target_layout,
|
||||
);
|
||||
proc_macros.insert(proc_macros_crate, Ok(proc_macro));
|
||||
|
||||
for krate in all_crates {
|
||||
crate_graph
|
||||
|
@ -305,6 +303,7 @@ impl ChangeFixture {
|
|||
roots.push(root);
|
||||
change.set_roots(roots);
|
||||
change.set_crate_graph(crate_graph);
|
||||
change.set_proc_macros(proc_macros);
|
||||
|
||||
ChangeFixture { file_position, files, change }
|
||||
}
|
||||
|
|
|
@ -15,6 +15,8 @@ use syntax::SmolStr;
|
|||
use tt::token_id::Subtree;
|
||||
use vfs::{file_set::FileSet, AnchoredPath, FileId, VfsPath};
|
||||
|
||||
pub type ProcMacros = FxHashMap<CrateId, ProcMacroLoadResult>;
|
||||
|
||||
/// Files are grouped into source roots. A source root is a directory on the
|
||||
/// file systems which is watched for changes. Typically it corresponds to a
|
||||
/// Rust crate. Source roots *might* be nested: in this case, a file belongs to
|
||||
|
@ -269,7 +271,6 @@ pub struct CrateData {
|
|||
pub target_layout: TargetLayoutLoadResult,
|
||||
pub env: Env,
|
||||
pub dependencies: Vec<Dependency>,
|
||||
pub proc_macro: ProcMacroLoadResult,
|
||||
pub origin: CrateOrigin,
|
||||
pub is_proc_macro: bool,
|
||||
}
|
||||
|
@ -322,7 +323,6 @@ impl CrateGraph {
|
|||
cfg_options: CfgOptions,
|
||||
potential_cfg_options: CfgOptions,
|
||||
env: Env,
|
||||
proc_macro: ProcMacroLoadResult,
|
||||
is_proc_macro: bool,
|
||||
origin: CrateOrigin,
|
||||
target_layout: Result<Arc<str>, Arc<str>>,
|
||||
|
@ -335,7 +335,6 @@ impl CrateGraph {
|
|||
cfg_options,
|
||||
potential_cfg_options,
|
||||
env,
|
||||
proc_macro,
|
||||
dependencies: Vec::new(),
|
||||
origin,
|
||||
target_layout,
|
||||
|
@ -460,7 +459,12 @@ impl CrateGraph {
|
|||
///
|
||||
/// The ids of the crates in the `other` graph are shifted by the return
|
||||
/// amount.
|
||||
pub fn extend(&mut self, other: CrateGraph) -> u32 {
|
||||
pub fn extend(
|
||||
&mut self,
|
||||
other: CrateGraph,
|
||||
proc_macros: &mut ProcMacros,
|
||||
other_proc_macros: ProcMacros,
|
||||
) -> u32 {
|
||||
let start = self.arena.len() as u32;
|
||||
self.arena.extend(other.arena.into_iter().map(|(id, mut data)| {
|
||||
let new_id = id.shift(start);
|
||||
|
@ -469,6 +473,8 @@ impl CrateGraph {
|
|||
}
|
||||
(new_id, data)
|
||||
}));
|
||||
proc_macros
|
||||
.extend(other_proc_macros.into_iter().map(|(id, macros)| (id.shift(start), macros)));
|
||||
start
|
||||
}
|
||||
|
||||
|
@ -645,7 +651,6 @@ mod tests {
|
|||
CfgOptions::default(),
|
||||
CfgOptions::default(),
|
||||
Env::default(),
|
||||
Ok(Vec::new()),
|
||||
false,
|
||||
CrateOrigin::CratesIo { repo: None, name: None },
|
||||
Err("".into()),
|
||||
|
@ -658,7 +663,6 @@ mod tests {
|
|||
CfgOptions::default(),
|
||||
CfgOptions::default(),
|
||||
Env::default(),
|
||||
Ok(Vec::new()),
|
||||
false,
|
||||
CrateOrigin::CratesIo { repo: None, name: None },
|
||||
Err("".into()),
|
||||
|
@ -671,7 +675,6 @@ mod tests {
|
|||
CfgOptions::default(),
|
||||
CfgOptions::default(),
|
||||
Env::default(),
|
||||
Ok(Vec::new()),
|
||||
false,
|
||||
CrateOrigin::CratesIo { repo: None, name: None },
|
||||
Err("".into()),
|
||||
|
@ -698,7 +701,6 @@ mod tests {
|
|||
CfgOptions::default(),
|
||||
CfgOptions::default(),
|
||||
Env::default(),
|
||||
Ok(Vec::new()),
|
||||
false,
|
||||
CrateOrigin::CratesIo { repo: None, name: None },
|
||||
Err("".into()),
|
||||
|
@ -711,7 +713,6 @@ mod tests {
|
|||
CfgOptions::default(),
|
||||
CfgOptions::default(),
|
||||
Env::default(),
|
||||
Ok(Vec::new()),
|
||||
false,
|
||||
CrateOrigin::CratesIo { repo: None, name: None },
|
||||
Err("".into()),
|
||||
|
@ -735,7 +736,6 @@ mod tests {
|
|||
CfgOptions::default(),
|
||||
CfgOptions::default(),
|
||||
Env::default(),
|
||||
Ok(Vec::new()),
|
||||
false,
|
||||
CrateOrigin::CratesIo { repo: None, name: None },
|
||||
Err("".into()),
|
||||
|
@ -748,7 +748,6 @@ mod tests {
|
|||
CfgOptions::default(),
|
||||
CfgOptions::default(),
|
||||
Env::default(),
|
||||
Ok(Vec::new()),
|
||||
false,
|
||||
CrateOrigin::CratesIo { repo: None, name: None },
|
||||
Err("".into()),
|
||||
|
@ -761,7 +760,6 @@ mod tests {
|
|||
CfgOptions::default(),
|
||||
CfgOptions::default(),
|
||||
Env::default(),
|
||||
Ok(Vec::new()),
|
||||
false,
|
||||
CrateOrigin::CratesIo { repo: None, name: None },
|
||||
Err("".into()),
|
||||
|
@ -785,7 +783,6 @@ mod tests {
|
|||
CfgOptions::default(),
|
||||
CfgOptions::default(),
|
||||
Env::default(),
|
||||
Ok(Vec::new()),
|
||||
false,
|
||||
CrateOrigin::CratesIo { repo: None, name: None },
|
||||
Err("".into()),
|
||||
|
@ -798,7 +795,6 @@ mod tests {
|
|||
CfgOptions::default(),
|
||||
CfgOptions::default(),
|
||||
Env::default(),
|
||||
Ok(Vec::new()),
|
||||
false,
|
||||
CrateOrigin::CratesIo { repo: None, name: None },
|
||||
Err("".into()),
|
||||
|
|
|
@ -16,7 +16,7 @@ pub use crate::{
|
|||
input::{
|
||||
CrateData, CrateDisplayName, CrateGraph, CrateId, CrateName, CrateOrigin, Dependency,
|
||||
Edition, Env, LangCrateOrigin, ProcMacro, ProcMacroExpander, ProcMacroExpansionError,
|
||||
ProcMacroId, ProcMacroKind, ProcMacroLoadResult, SourceRoot, SourceRootId,
|
||||
ProcMacroId, ProcMacroKind, ProcMacroLoadResult, ProcMacros, SourceRoot, SourceRootId,
|
||||
TargetLayoutLoadResult,
|
||||
},
|
||||
};
|
||||
|
@ -73,6 +73,10 @@ pub trait SourceDatabase: FileLoader + std::fmt::Debug {
|
|||
/// The crate graph.
|
||||
#[salsa::input]
|
||||
fn crate_graph(&self) -> Arc<CrateGraph>;
|
||||
|
||||
/// The crate graph.
|
||||
#[salsa::input]
|
||||
fn proc_macros(&self) -> Arc<ProcMacros>;
|
||||
}
|
||||
|
||||
fn parse_query(db: &dyn SourceDatabase, file_id: FileId) -> Parse<ast::SourceFile> {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue