Skip to content

Commit db162df

Browse files
committed
Remove deps on tt_mbe
1 parent 72e68d0 commit db162df

File tree

10 files changed

+58
-49
lines changed

10 files changed

+58
-49
lines changed

Cargo.lock

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/ra_db/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ rustc-hash = "1.1.0"
1515
ra_syntax = { path = "../ra_syntax" }
1616
ra_cfg = { path = "../ra_cfg" }
1717
ra_prof = { path = "../ra_prof" }
18-
ra_proc_macro = { path = "../ra_proc_macro" }
18+
ra_tt = { path = "../ra_tt" }
1919
test_utils = { path = "../test_utils" }

crates/ra_db/src/input.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use std::{
1010
fmt, ops,
1111
path::{Path, PathBuf},
1212
str::FromStr,
13+
sync::Arc,
1314
};
1415

1516
use ra_cfg::CfgOptions;
@@ -19,7 +20,7 @@ use rustc_hash::FxHashSet;
1920

2021
use crate::{RelativePath, RelativePathBuf};
2122
use fmt::Display;
22-
use ra_proc_macro::ProcMacro;
23+
use ra_tt::TokenExpander;
2324

2425
/// `FileId` is an integer which uniquely identifies a file. File paths are
2526
/// messy and system-dependent, so most of the code should work directly with
@@ -117,7 +118,20 @@ impl Display for CrateName {
117118
}
118119

119120
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
120-
pub struct ProcMacroId(pub usize);
121+
pub struct ProcMacroId(pub u32);
122+
123+
#[derive(Debug, Clone)]
124+
pub struct ProcMacro {
125+
pub name: SmolStr,
126+
pub expander: Arc<dyn TokenExpander>,
127+
}
128+
129+
impl Eq for ProcMacro {}
130+
impl PartialEq for ProcMacro {
131+
fn eq(&self, other: &ProcMacro) -> bool {
132+
self.name == other.name && Arc::ptr_eq(&self.expander, &other.expander)
133+
}
134+
}
121135

122136
#[derive(Debug, Clone, PartialEq, Eq)]
123137
pub struct CrateData {
@@ -171,8 +185,11 @@ impl CrateGraph {
171185
cfg_options: CfgOptions,
172186
env: Env,
173187
extern_source: ExternSource,
174-
proc_macro: Vec<ProcMacro>,
188+
proc_macro: Vec<(SmolStr, Arc<dyn ra_tt::TokenExpander>)>,
175189
) -> CrateId {
190+
let proc_macro =
191+
proc_macro.into_iter().map(|(name, it)| ProcMacro { name, expander: it }).collect();
192+
176193
let data = CrateData {
177194
root_file_id: file_id,
178195
edition,

crates/ra_db/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ pub use crate::{
1515
FileId, ProcMacroId, SourceRoot, SourceRootId,
1616
},
1717
};
18-
pub use ra_proc_macro::ProcMacro;
1918
pub use relative_path::{RelativePath, RelativePathBuf};
2019
pub use salsa;
2120

crates/ra_hir_def/src/nameres/collector.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use hir_expand::{
1212
};
1313
use ra_cfg::CfgOptions;
1414
use ra_db::{CrateId, FileId, ProcMacroId};
15-
use ra_syntax::{ast, SmolStr};
15+
use ra_syntax::ast;
1616
use rustc_hash::FxHashMap;
1717
use test_utils::tested_by;
1818

@@ -59,8 +59,8 @@ pub(super) fn collect_defs(db: &dyn DefDatabase, mut def_map: CrateDefMap) -> Cr
5959
.enumerate()
6060
.map(|(idx, it)| {
6161
// FIXME: a hacky way to create a Name from string.
62-
let name = tt::Ident { text: SmolStr::new(&it.name()), id: tt::TokenId::unspecified() };
63-
(name.as_name(), ProcMacroExpander::new(def_map.krate, ProcMacroId(idx)))
62+
let name = tt::Ident { text: it.name.clone(), id: tt::TokenId::unspecified() };
63+
(name.as_name(), ProcMacroExpander::new(def_map.krate, ProcMacroId(idx as u32)))
6464
})
6565
.collect();
6666

crates/ra_hir_expand/src/proc_macro.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ impl ProcMacroExpander {
2323
let krate_graph = db.crate_graph();
2424
let proc_macro = krate_graph[self.krate]
2525
.proc_macro
26-
.get(self.proc_macro_id.0)
26+
.get(self.proc_macro_id.0 as usize)
2727
.clone()
2828
.ok_or_else(|| mbe::ExpandError::ConversionError)?;
29-
proc_macro.custom_derive(tt)
29+
30+
proc_macro.expander.expand(&tt, None).map_err(mbe::ExpandError::from)
3031
}
3132
}

crates/ra_mbe/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ pub enum ExpandError {
2828
BindingError(String),
2929
ConversionError,
3030
InvalidRepeat,
31+
ProcMacroError(tt::ExpansionError),
32+
}
33+
34+
impl From<tt::ExpansionError> for ExpandError {
35+
fn from(it: tt::ExpansionError) -> Self {
36+
ExpandError::ProcMacroError(it)
37+
}
3138
}
3239

3340
pub use crate::syntax_bridge::{

crates/ra_proc_macro/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,3 @@ doctest = false
1010

1111
[dependencies]
1212
ra_tt = { path = "../ra_tt" }
13-
ra_mbe = { path = "../ra_mbe" }

crates/ra_proc_macro/src/lib.rs

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,56 +5,29 @@
55
//! is used to provide basic infrastructure for communication between two
66
//! processes: Client (RA itself), Server (the external program)
77
8-
use ra_mbe::ExpandError;
9-
use ra_tt::Subtree;
8+
use ra_tt::{SmolStr, Subtree};
109
use std::{
1110
path::{Path, PathBuf},
1211
sync::Arc,
1312
};
1413

15-
trait ProcMacroExpander: std::fmt::Debug + Send + Sync + std::panic::RefUnwindSafe {
16-
fn custom_derive(&self, subtree: &Subtree, derive_name: &str) -> Result<Subtree, ExpandError>;
17-
}
18-
1914
#[derive(Debug, Clone, PartialEq, Eq)]
2015
pub struct ProcMacroProcessExpander {
2116
process: Arc<ProcMacroProcessSrv>,
17+
name: SmolStr,
2218
}
2319

24-
impl ProcMacroExpander for ProcMacroProcessExpander {
25-
fn custom_derive(
20+
impl ra_tt::TokenExpander for ProcMacroProcessExpander {
21+
fn expand(
2622
&self,
2723
_subtree: &Subtree,
28-
_derive_name: &str,
29-
) -> Result<Subtree, ExpandError> {
24+
_attr: Option<&Subtree>,
25+
) -> Result<Subtree, ra_tt::ExpansionError> {
3026
// FIXME: do nothing for now
3127
Ok(Subtree::default())
3228
}
3329
}
3430

35-
#[derive(Debug, Clone)]
36-
pub struct ProcMacro {
37-
expander: Arc<dyn ProcMacroExpander>,
38-
name: String,
39-
}
40-
41-
impl Eq for ProcMacro {}
42-
impl PartialEq for ProcMacro {
43-
fn eq(&self, other: &ProcMacro) -> bool {
44-
self.name == other.name && Arc::ptr_eq(&self.expander, &other.expander)
45-
}
46-
}
47-
48-
impl ProcMacro {
49-
pub fn name(&self) -> String {
50-
self.name.clone()
51-
}
52-
53-
pub fn custom_derive(&self, subtree: &Subtree) -> Result<Subtree, ExpandError> {
54-
self.expander.custom_derive(subtree, &self.name)
55-
}
56-
}
57-
5831
#[derive(Debug, Clone, PartialEq, Eq)]
5932
pub struct ProcMacroProcessSrv {
6033
path: PathBuf,
@@ -76,7 +49,10 @@ impl ProcMacroClient {
7649
ProcMacroClient::Dummy
7750
}
7851

79-
pub fn by_dylib_path(&self, _dylib_path: &Path) -> Vec<ProcMacro> {
52+
pub fn by_dylib_path(
53+
&self,
54+
_dylib_path: &Path,
55+
) -> Vec<(SmolStr, Arc<dyn ra_tt::TokenExpander>)> {
8056
// FIXME: return empty for now
8157
vec![]
8258
}

crates/ra_tt/src/lib.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@ macro_rules! impl_froms {
1414
}
1515
}
1616

17-
use std::fmt;
17+
use std::{
18+
fmt::{self, Debug},
19+
panic::RefUnwindSafe,
20+
};
1821

19-
use smol_str::SmolStr;
22+
pub use smol_str::SmolStr;
2023

2124
/// Represents identity of the token.
2225
///
@@ -184,3 +187,11 @@ impl Subtree {
184187
}
185188

186189
pub mod buffer;
190+
191+
#[derive(Debug, PartialEq, Eq)]
192+
pub enum ExpansionError {}
193+
194+
pub trait TokenExpander: Debug + Send + Sync + RefUnwindSafe {
195+
fn expand(&self, subtree: &Subtree, attrs: Option<&Subtree>)
196+
-> Result<Subtree, ExpansionError>;
197+
}

0 commit comments

Comments
 (0)