Skip to content

Commit 7a322f9

Browse files
Merge #3392
3392: Implement concat eager macro r=matklad a=edwin0cheng This PR implements the following things: 1. Add basic eager macro infrastructure by introducing `EagerCallId` such that the new `MacroCallId` is defined as : ``` #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub enum MacroCallId { LazyMacro(LazyMacroId), EagerMacro(EagerMacroId), } ``` 2. Add `concat!` builtin macro. Co-authored-by: Edwin Cheng <[email protected]>
2 parents 13b25d7 + 4d5e80c commit 7a322f9

File tree

11 files changed

+399
-82
lines changed

11 files changed

+399
-82
lines changed

crates/ra_hir_def/src/lib.rs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ mod marks;
4747
use std::hash::Hash;
4848

4949
use hir_expand::{
50-
ast_id_map::FileAstId, db::AstDatabase, hygiene::Hygiene, AstId, HirFileId, InFile,
51-
MacroCallId, MacroCallKind, MacroDefId,
50+
ast_id_map::FileAstId, db::AstDatabase, eager::expand_eager_macro, hygiene::Hygiene, AstId,
51+
HirFileId, InFile, MacroCallId, MacroCallKind, MacroDefId, MacroDefKind,
5252
};
5353
use ra_arena::{impl_arena_id, RawId};
5454
use ra_db::{impl_intern_key, salsa, CrateId};
@@ -459,8 +459,21 @@ impl AsMacroCall for AstIdWithPath<ast::MacroCall> {
459459
db: &impl AstDatabase,
460460
resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
461461
) -> Option<MacroCallId> {
462-
let def = resolver(self.path.clone())?;
463-
Some(def.as_call_id(db, MacroCallKind::FnLike(self.ast_id)))
462+
let def: MacroDefId = resolver(self.path.clone())?;
463+
464+
if let MacroDefKind::BuiltInEager(_) = def.kind {
465+
let macro_call = InFile::new(self.ast_id.file_id, self.ast_id.to_node(db));
466+
let hygiene = Hygiene::new(db, self.ast_id.file_id);
467+
468+
Some(
469+
expand_eager_macro(db, macro_call, def, &|path: ast::Path| {
470+
resolver(path::ModPath::from_src(path, &hygiene)?)
471+
})?
472+
.into(),
473+
)
474+
} else {
475+
Some(def.as_lazy_macro(db, MacroCallKind::FnLike(self.ast_id)).into())
476+
}
464477
}
465478
}
466479

@@ -471,6 +484,6 @@ impl AsMacroCall for AstIdWithPath<ast::ModuleItem> {
471484
resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
472485
) -> Option<MacroCallId> {
473486
let def = resolver(self.path.clone())?;
474-
Some(def.as_call_id(db, MacroCallKind::Attr(self.ast_id)))
487+
Some(def.as_lazy_macro(db, MacroCallKind::Attr(self.ast_id)).into())
475488
}
476489
}

crates/ra_hir_expand/src/builtin_derive.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use ra_syntax::{
99
};
1010

1111
use crate::db::AstDatabase;
12-
use crate::{name, quote, MacroCallId, MacroDefId, MacroDefKind};
12+
use crate::{name, quote, LazyMacroId, MacroDefId, MacroDefKind};
1313

1414
macro_rules! register_builtin {
1515
( $($trait:ident => $expand:ident),* ) => {
@@ -22,7 +22,7 @@ macro_rules! register_builtin {
2222
pub fn expand(
2323
&self,
2424
db: &dyn AstDatabase,
25-
id: MacroCallId,
25+
id: LazyMacroId,
2626
tt: &tt::Subtree,
2727
) -> Result<tt::Subtree, mbe::ExpandError> {
2828
let expander = match *self {
@@ -155,71 +155,71 @@ fn expand_simple_derive(
155155

156156
fn copy_expand(
157157
_db: &dyn AstDatabase,
158-
_id: MacroCallId,
158+
_id: LazyMacroId,
159159
tt: &tt::Subtree,
160160
) -> Result<tt::Subtree, mbe::ExpandError> {
161161
expand_simple_derive(tt, quote! { std::marker::Copy })
162162
}
163163

164164
fn clone_expand(
165165
_db: &dyn AstDatabase,
166-
_id: MacroCallId,
166+
_id: LazyMacroId,
167167
tt: &tt::Subtree,
168168
) -> Result<tt::Subtree, mbe::ExpandError> {
169169
expand_simple_derive(tt, quote! { std::clone::Clone })
170170
}
171171

172172
fn default_expand(
173173
_db: &dyn AstDatabase,
174-
_id: MacroCallId,
174+
_id: LazyMacroId,
175175
tt: &tt::Subtree,
176176
) -> Result<tt::Subtree, mbe::ExpandError> {
177177
expand_simple_derive(tt, quote! { std::default::Default })
178178
}
179179

180180
fn debug_expand(
181181
_db: &dyn AstDatabase,
182-
_id: MacroCallId,
182+
_id: LazyMacroId,
183183
tt: &tt::Subtree,
184184
) -> Result<tt::Subtree, mbe::ExpandError> {
185185
expand_simple_derive(tt, quote! { std::fmt::Debug })
186186
}
187187

188188
fn hash_expand(
189189
_db: &dyn AstDatabase,
190-
_id: MacroCallId,
190+
_id: LazyMacroId,
191191
tt: &tt::Subtree,
192192
) -> Result<tt::Subtree, mbe::ExpandError> {
193193
expand_simple_derive(tt, quote! { std::hash::Hash })
194194
}
195195

196196
fn eq_expand(
197197
_db: &dyn AstDatabase,
198-
_id: MacroCallId,
198+
_id: LazyMacroId,
199199
tt: &tt::Subtree,
200200
) -> Result<tt::Subtree, mbe::ExpandError> {
201201
expand_simple_derive(tt, quote! { std::cmp::Eq })
202202
}
203203

204204
fn partial_eq_expand(
205205
_db: &dyn AstDatabase,
206-
_id: MacroCallId,
206+
_id: LazyMacroId,
207207
tt: &tt::Subtree,
208208
) -> Result<tt::Subtree, mbe::ExpandError> {
209209
expand_simple_derive(tt, quote! { std::cmp::PartialEq })
210210
}
211211

212212
fn ord_expand(
213213
_db: &dyn AstDatabase,
214-
_id: MacroCallId,
214+
_id: LazyMacroId,
215215
tt: &tt::Subtree,
216216
) -> Result<tt::Subtree, mbe::ExpandError> {
217217
expand_simple_derive(tt, quote! { std::cmp::Ord })
218218
}
219219

220220
fn partial_ord_expand(
221221
_db: &dyn AstDatabase,
222-
_id: MacroCallId,
222+
_id: LazyMacroId,
223223
tt: &tt::Subtree,
224224
) -> Result<tt::Subtree, mbe::ExpandError> {
225225
expand_simple_derive(tt, quote! { std::cmp::PartialOrd })
@@ -228,7 +228,7 @@ fn partial_ord_expand(
228228
#[cfg(test)]
229229
mod tests {
230230
use super::*;
231-
use crate::{test_db::TestDB, AstId, MacroCallKind, MacroCallLoc};
231+
use crate::{test_db::TestDB, AstId, MacroCallId, MacroCallKind, MacroCallLoc};
232232
use ra_db::{fixture::WithFixture, SourceDatabase};
233233

234234
fn expand_builtin_derive(s: &str, expander: BuiltinDeriveExpander) -> String {
@@ -248,7 +248,7 @@ mod tests {
248248
kind: MacroCallKind::Attr(AstId::new(file_id.into(), ast_id_map.ast_id(&items[0]))),
249249
};
250250

251-
let id = db.intern_macro(loc);
251+
let id: MacroCallId = db.intern_macro(loc).into();
252252
let parsed = db.parse_or_expand(id.as_file()).unwrap();
253253

254254
// FIXME text() for syntax nodes parsed from token tree looks weird

0 commit comments

Comments
 (0)