Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/ra_db/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ rustc-hash = "1.1.0"
ra_syntax = { path = "../ra_syntax" }
ra_cfg = { path = "../ra_cfg" }
ra_prof = { path = "../ra_prof" }
ra_proc_macro = { path = "../ra_proc_macro" }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, I wonder if we can minimize the interface here...

Can we use something like this instead?

struct CrateData {
   proc_macros: FxHashMap<SmolStr, Arc<dyn Fn(tt::TokenTree) -> tt::TokenTree>>
}

Ie, define a really minimal proc macro interface directly in the ra_db crate, where the minimal interface is ideally an dyn Fn(), or maybe a named trait.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed to :

#[derive(Debug, Clone)]
pub struct ProcMacro {
    pub name: SmolStr,
    pub expander: Arc<dyn TokenExpander>,
}

impl Eq for ProcMacro {}
impl PartialEq for ProcMacro { ... }

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CrateData {
   ...
    pub proc_macro: Vec<ProcMacro>,
}

test_utils = { path = "../test_utils" }
4 changes: 4 additions & 0 deletions crates/ra_db/src/fixture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ fn with_single_file(db: &mut dyn SourceDatabaseExt, ra_fixture: &str) -> FileId
meta.cfg,
meta.env,
Default::default(),
Default::default(),
);
crate_graph
} else {
Expand All @@ -81,6 +82,7 @@ fn with_single_file(db: &mut dyn SourceDatabaseExt, ra_fixture: &str) -> FileId
CfgOptions::default(),
Env::default(),
Default::default(),
Default::default(),
);
crate_graph
};
Expand Down Expand Up @@ -130,6 +132,7 @@ fn with_files(db: &mut dyn SourceDatabaseExt, fixture: &str) -> Option<FilePosit
meta.cfg,
meta.env,
Default::default(),
Default::default(),
);
let prev = crates.insert(krate.clone(), crate_id);
assert!(prev.is_none());
Expand Down Expand Up @@ -167,6 +170,7 @@ fn with_files(db: &mut dyn SourceDatabaseExt, fixture: &str) -> Option<FilePosit
CfgOptions::default(),
Env::default(),
Default::default(),
Default::default(),
);
} else {
for (from, to) in crate_deps {
Expand Down
15 changes: 15 additions & 0 deletions crates/ra_db/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use rustc_hash::FxHashSet;

use crate::{RelativePath, RelativePathBuf};
use fmt::Display;
use ra_proc_macro::ProcMacro;

/// `FileId` is an integer which uniquely identifies a file. File paths are
/// messy and system-dependent, so most of the code should work directly with
Expand Down Expand Up @@ -115,6 +116,9 @@ impl Display for CrateName {
}
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct ProcMacroId(pub usize);

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CrateData {
pub root_file_id: FileId,
Expand All @@ -127,6 +131,7 @@ pub struct CrateData {
pub env: Env,
pub extern_source: ExternSource,
pub dependencies: Vec<Dependency>,
pub proc_macro: Vec<ProcMacro>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
Expand Down Expand Up @@ -166,6 +171,7 @@ impl CrateGraph {
cfg_options: CfgOptions,
env: Env,
extern_source: ExternSource,
proc_macro: Vec<ProcMacro>,
) -> CrateId {
let data = CrateData {
root_file_id: file_id,
Expand All @@ -174,6 +180,7 @@ impl CrateGraph {
cfg_options,
env,
extern_source,
proc_macro,
dependencies: Vec::new(),
};
let crate_id = CrateId(self.arena.len() as u32);
Expand Down Expand Up @@ -345,6 +352,7 @@ mod tests {
CfgOptions::default(),
Env::default(),
Default::default(),
Default::default(),
);
let crate2 = graph.add_crate_root(
FileId(2u32),
Expand All @@ -353,6 +361,7 @@ mod tests {
CfgOptions::default(),
Env::default(),
Default::default(),
Default::default(),
);
let crate3 = graph.add_crate_root(
FileId(3u32),
Expand All @@ -361,6 +370,7 @@ mod tests {
CfgOptions::default(),
Env::default(),
Default::default(),
Default::default(),
);
assert!(graph.add_dep(crate1, CrateName::new("crate2").unwrap(), crate2).is_ok());
assert!(graph.add_dep(crate2, CrateName::new("crate3").unwrap(), crate3).is_ok());
Expand All @@ -377,6 +387,7 @@ mod tests {
CfgOptions::default(),
Env::default(),
Default::default(),
Default::default(),
);
let crate2 = graph.add_crate_root(
FileId(2u32),
Expand All @@ -385,6 +396,7 @@ mod tests {
CfgOptions::default(),
Env::default(),
Default::default(),
Default::default(),
);
let crate3 = graph.add_crate_root(
FileId(3u32),
Expand All @@ -393,6 +405,7 @@ mod tests {
CfgOptions::default(),
Env::default(),
Default::default(),
Default::default(),
);
assert!(graph.add_dep(crate1, CrateName::new("crate2").unwrap(), crate2).is_ok());
assert!(graph.add_dep(crate2, CrateName::new("crate3").unwrap(), crate3).is_ok());
Expand All @@ -408,6 +421,7 @@ mod tests {
CfgOptions::default(),
Env::default(),
Default::default(),
Default::default(),
);
let crate2 = graph.add_crate_root(
FileId(2u32),
Expand All @@ -416,6 +430,7 @@ mod tests {
CfgOptions::default(),
Env::default(),
Default::default(),
Default::default(),
);
assert!(graph
.add_dep(crate1, CrateName::normalize_dashes("crate-name-with-dashes"), crate2)
Expand Down
3 changes: 2 additions & 1 deletion crates/ra_db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ pub use crate::{
cancellation::Canceled,
input::{
CrateGraph, CrateId, CrateName, Dependency, Edition, Env, ExternSource, ExternSourceId,
FileId, SourceRoot, SourceRootId,
FileId, ProcMacroId, SourceRoot, SourceRootId,
},
};
pub use ra_proc_macro::ProcMacro;
pub use relative_path::{RelativePath, RelativePathBuf};
pub use salsa;

Expand Down
18 changes: 13 additions & 5 deletions crates/ra_hir_def/src/nameres/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use hir_expand::{
HirFileId, MacroCallId, MacroDefId, MacroDefKind,
};
use ra_cfg::CfgOptions;
use ra_db::{CrateId, FileId};
use ra_syntax::ast;
use ra_db::{CrateId, FileId, ProcMacroId};
use ra_syntax::{ast, SmolStr};
use rustc_hash::FxHashMap;
use test_utils::tested_by;

Expand Down Expand Up @@ -53,6 +53,16 @@ pub(super) fn collect_defs(db: &dyn DefDatabase, mut def_map: CrateDefMap) -> Cr
}

let cfg_options = &crate_graph[def_map.krate].cfg_options;
let proc_macros = &crate_graph[def_map.krate].proc_macro;
let proc_macros = proc_macros
.iter()
.enumerate()
.map(|(idx, it)| {
// FIXME: a hacky way to create a Name from string.
let name = tt::Ident { text: SmolStr::new(&it.name()), id: tt::TokenId::unspecified() };
(name.as_name(), ProcMacroExpander::new(def_map.krate, ProcMacroId(idx)))
})
.collect();

let mut collector = DefCollector {
db,
Expand All @@ -65,9 +75,7 @@ pub(super) fn collect_defs(db: &dyn DefDatabase, mut def_map: CrateDefMap) -> Cr
unexpanded_attribute_macros: Vec::new(),
mod_dirs: FxHashMap::default(),
cfg_options,

// FIXME: pass proc-macro from crate-graph
proc_macros: Default::default(),
proc_macros,
};
collector.collect();
collector.finish()
Expand Down
30 changes: 14 additions & 16 deletions crates/ra_hir_expand/src/proc_macro.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,31 @@
//! Proc Macro Expander stub

use crate::{db::AstDatabase, LazyMacroId, MacroCallKind, MacroCallLoc};
use ra_db::CrateId;
use crate::{db::AstDatabase, LazyMacroId};
use ra_db::{CrateId, ProcMacroId};

#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
pub struct ProcMacroExpander {
krate: CrateId,
proc_macro_id: ProcMacroId,
}

impl ProcMacroExpander {
pub fn new(krate: CrateId) -> ProcMacroExpander {
ProcMacroExpander { krate }
pub fn new(krate: CrateId, proc_macro_id: ProcMacroId) -> ProcMacroExpander {
ProcMacroExpander { krate, proc_macro_id }
}

pub fn expand(
&self,
db: &dyn AstDatabase,
id: LazyMacroId,
_tt: &tt::Subtree,
_id: LazyMacroId,
tt: &tt::Subtree,
) -> Result<tt::Subtree, mbe::ExpandError> {
let loc: MacroCallLoc = db.lookup_intern_macro(id);
let name = match loc.kind {
MacroCallKind::FnLike(_) => return Err(mbe::ExpandError::ConversionError),
MacroCallKind::Attr(_, name) => name,
};

log::debug!("Proc-macro-expanding name = {}", name);

// Return nothing for now
return Ok(tt::Subtree::default());
let krate_graph = db.crate_graph();
let proc_macro = krate_graph[self.krate]
.proc_macro
.get(self.proc_macro_id.0)
.clone()
.ok_or_else(|| mbe::ExpandError::ConversionError)?;
proc_macro.custom_derive(tt)
}
}
1 change: 1 addition & 0 deletions crates/ra_ide/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ impl Analysis {
cfg_options,
Env::default(),
Default::default(),
Default::default(),
);
change.add_file(source_root, file_id, "main.rs".into(), Arc::new(text));
change.set_crate_graph(crate_graph);
Expand Down
2 changes: 2 additions & 0 deletions crates/ra_ide/src/mock_analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ impl MockAnalysis {
cfg_options,
Env::default(),
Default::default(),
Default::default(),
));
} else if path.ends_with("/lib.rs") {
let crate_name = path.parent().unwrap().file_name().unwrap();
Expand All @@ -113,6 +114,7 @@ impl MockAnalysis {
cfg_options,
Env::default(),
Default::default(),
Default::default(),
);
if let Some(root_crate) = root_crate {
crate_graph
Expand Down
1 change: 1 addition & 0 deletions crates/ra_ide/src/parent_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ mod tests {
CfgOptions::default(),
Env::default(),
Default::default(),
Default::default(),
);
let mut change = AnalysisChange::new();
change.set_crate_graph(crate_graph);
Expand Down
13 changes: 13 additions & 0 deletions crates/ra_proc_macro/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
edition = "2018"
name = "ra_proc_macro"
version = "0.1.0"
authors = ["rust-analyzer developers"]
publish = false

[lib]
doctest = false

[dependencies]
ra_tt = { path = "../ra_tt" }
ra_mbe = { path = "../ra_mbe" }
Loading