aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-05-15 11:17:42 +0100
committerGitHub <[email protected]>2020-05-15 11:17:42 +0100
commit982b92f966518a0e24632fafdc18d7b5ab6928b4 (patch)
tree41aa35146d8b3b2b039d79a38a157d0f2d5cab8d /crates
parent05db35dafb47db355e202c9176bd8a752b7390d7 (diff)
parent20f7068b68b99610926b375d53d3721b878ff86c (diff)
Merge #4431
4431: Store proc-macro result in salsa db r=matklad a=edwin0cheng Fixed #4315 Co-authored-by: Edwin Cheng <[email protected]>
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_hir_expand/src/db.rs36
-rw-r--r--crates/ra_mbe/src/lib.rs2
-rw-r--r--crates/ra_tt/src/lib.rs2
3 files changed, 37 insertions, 3 deletions
diff --git a/crates/ra_hir_expand/src/db.rs b/crates/ra_hir_expand/src/db.rs
index 4c12d0a15..bf30d7151 100644
--- a/crates/ra_hir_expand/src/db.rs
+++ b/crates/ra_hir_expand/src/db.rs
@@ -34,7 +34,12 @@ impl TokenExpander {
34 // FIXME switch these to ExpandResult as well 34 // FIXME switch these to ExpandResult as well
35 TokenExpander::Builtin(it) => it.expand(db, id, tt).into(), 35 TokenExpander::Builtin(it) => it.expand(db, id, tt).into(),
36 TokenExpander::BuiltinDerive(it) => it.expand(db, id, tt).into(), 36 TokenExpander::BuiltinDerive(it) => it.expand(db, id, tt).into(),
37 TokenExpander::ProcMacro(it) => it.expand(db, id, tt).into(), 37 TokenExpander::ProcMacro(_) => {
38 // We store the result in salsa db to prevent non-determinisc behavior in
39 // some proc-macro implementation
40 // See #4315 for details
41 db.expand_proc_macro(id.into()).into()
42 }
38 } 43 }
39 } 44 }
40 45
@@ -75,6 +80,8 @@ pub trait AstDatabase: SourceDatabase {
75 80
76 #[salsa::interned] 81 #[salsa::interned]
77 fn intern_eager_expansion(&self, eager: EagerCallLoc) -> EagerMacroId; 82 fn intern_eager_expansion(&self, eager: EagerCallLoc) -> EagerMacroId;
83
84 fn expand_proc_macro(&self, call: MacroCallId) -> Result<tt::Subtree, mbe::ExpandError>;
78} 85}
79 86
80/// This expands the given macro call, but with different arguments. This is 87/// This expands the given macro call, but with different arguments. This is
@@ -216,6 +223,33 @@ fn macro_expand_with_arg(
216 (Some(Arc::new(tt)), err.map(|e| format!("{:?}", e))) 223 (Some(Arc::new(tt)), err.map(|e| format!("{:?}", e)))
217} 224}
218 225
226pub(crate) fn expand_proc_macro(
227 db: &dyn AstDatabase,
228 id: MacroCallId,
229) -> Result<tt::Subtree, mbe::ExpandError> {
230 let lazy_id = match id {
231 MacroCallId::LazyMacro(id) => id,
232 MacroCallId::EagerMacro(_) => unreachable!(),
233 };
234
235 let loc = db.lookup_intern_macro(lazy_id);
236 let macro_arg = match db.macro_arg(id) {
237 Some(it) => it,
238 None => {
239 return Err(
240 tt::ExpansionError::Unknown("No arguments for proc-macro".to_string()).into()
241 )
242 }
243 };
244
245 let expander = match loc.def.kind {
246 MacroDefKind::CustomDerive(expander) => expander,
247 _ => unreachable!(),
248 };
249
250 expander.expand(db, lazy_id, &macro_arg.0)
251}
252
219pub(crate) fn parse_or_expand(db: &dyn AstDatabase, file_id: HirFileId) -> Option<SyntaxNode> { 253pub(crate) fn parse_or_expand(db: &dyn AstDatabase, file_id: HirFileId) -> Option<SyntaxNode> {
220 match file_id.0 { 254 match file_id.0 {
221 HirFileIdRepr::FileId(file_id) => Some(db.parse(file_id).tree().syntax().clone()), 255 HirFileIdRepr::FileId(file_id) => Some(db.parse(file_id).tree().syntax().clone()),
diff --git a/crates/ra_mbe/src/lib.rs b/crates/ra_mbe/src/lib.rs
index 1a020398e..9c450eaba 100644
--- a/crates/ra_mbe/src/lib.rs
+++ b/crates/ra_mbe/src/lib.rs
@@ -22,7 +22,7 @@ pub enum ParseError {
22 RepetitionEmtpyTokenTree, 22 RepetitionEmtpyTokenTree,
23} 23}
24 24
25#[derive(Debug, PartialEq, Eq)] 25#[derive(Debug, PartialEq, Eq, Clone)]
26pub enum ExpandError { 26pub enum ExpandError {
27 NoMatchingRule, 27 NoMatchingRule,
28 UnexpectedToken, 28 UnexpectedToken,
diff --git a/crates/ra_tt/src/lib.rs b/crates/ra_tt/src/lib.rs
index 5248e026c..342ddbe32 100644
--- a/crates/ra_tt/src/lib.rs
+++ b/crates/ra_tt/src/lib.rs
@@ -243,7 +243,7 @@ impl Subtree {
243 243
244pub mod buffer; 244pub mod buffer;
245 245
246#[derive(Debug, PartialEq, Eq)] 246#[derive(Debug, PartialEq, Eq, Clone)]
247pub enum ExpansionError { 247pub enum ExpansionError {
248 IOError(String), 248 IOError(String),
249 JsonError(String), 249 JsonError(String),