Skip to content

Implement concat eager macro #3392

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 3, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
23 changes: 18 additions & 5 deletions crates/ra_hir_def/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ mod marks;
use std::hash::Hash;

use hir_expand::{
ast_id_map::FileAstId, db::AstDatabase, hygiene::Hygiene, AstId, HirFileId, InFile,
MacroCallId, MacroCallKind, MacroDefId,
ast_id_map::FileAstId, db::AstDatabase, eager::expand_eager_macro, hygiene::Hygiene, AstId,
HirFileId, InFile, MacroCallId, MacroCallKind, MacroDefId, MacroDefKind,
};
use ra_arena::{impl_arena_id, RawId};
use ra_db::{impl_intern_key, salsa, CrateId};
Expand Down Expand Up @@ -459,8 +459,21 @@ impl AsMacroCall for AstIdWithPath<ast::MacroCall> {
db: &impl AstDatabase,
resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
) -> Option<MacroCallId> {
let def = resolver(self.path.clone())?;
Some(def.as_call_id(db, MacroCallKind::FnLike(self.ast_id)))
let def: MacroDefId = resolver(self.path.clone())?;

if let MacroDefKind::BuiltInEager(_) = def.kind {
let macro_call = InFile::new(self.ast_id.file_id, self.ast_id.to_node(db));
let hygiene = Hygiene::new(db, self.ast_id.file_id);

Some(
expand_eager_macro(db, macro_call, def, &|path: ast::Path| {
resolver(path::ModPath::from_src(path, &hygiene)?)
})?
.into(),
)
} else {
Some(def.as_lazy_macro(db, MacroCallKind::FnLike(self.ast_id)).into())
}
}
}

Expand All @@ -471,6 +484,6 @@ impl AsMacroCall for AstIdWithPath<ast::ModuleItem> {
resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
) -> Option<MacroCallId> {
let def = resolver(self.path.clone())?;
Some(def.as_call_id(db, MacroCallKind::Attr(self.ast_id)))
Some(def.as_lazy_macro(db, MacroCallKind::Attr(self.ast_id)).into())
}
}
26 changes: 13 additions & 13 deletions crates/ra_hir_expand/src/builtin_derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use ra_syntax::{
};

use crate::db::AstDatabase;
use crate::{name, quote, MacroCallId, MacroDefId, MacroDefKind};
use crate::{name, quote, LazyMacroId, MacroDefId, MacroDefKind};

macro_rules! register_builtin {
( $($trait:ident => $expand:ident),* ) => {
Expand All @@ -22,7 +22,7 @@ macro_rules! register_builtin {
pub fn expand(
&self,
db: &dyn AstDatabase,
id: MacroCallId,
id: LazyMacroId,
tt: &tt::Subtree,
) -> Result<tt::Subtree, mbe::ExpandError> {
let expander = match *self {
Expand Down Expand Up @@ -155,71 +155,71 @@ fn expand_simple_derive(

fn copy_expand(
_db: &dyn AstDatabase,
_id: MacroCallId,
_id: LazyMacroId,
tt: &tt::Subtree,
) -> Result<tt::Subtree, mbe::ExpandError> {
expand_simple_derive(tt, quote! { std::marker::Copy })
}

fn clone_expand(
_db: &dyn AstDatabase,
_id: MacroCallId,
_id: LazyMacroId,
tt: &tt::Subtree,
) -> Result<tt::Subtree, mbe::ExpandError> {
expand_simple_derive(tt, quote! { std::clone::Clone })
}

fn default_expand(
_db: &dyn AstDatabase,
_id: MacroCallId,
_id: LazyMacroId,
tt: &tt::Subtree,
) -> Result<tt::Subtree, mbe::ExpandError> {
expand_simple_derive(tt, quote! { std::default::Default })
}

fn debug_expand(
_db: &dyn AstDatabase,
_id: MacroCallId,
_id: LazyMacroId,
tt: &tt::Subtree,
) -> Result<tt::Subtree, mbe::ExpandError> {
expand_simple_derive(tt, quote! { std::fmt::Debug })
}

fn hash_expand(
_db: &dyn AstDatabase,
_id: MacroCallId,
_id: LazyMacroId,
tt: &tt::Subtree,
) -> Result<tt::Subtree, mbe::ExpandError> {
expand_simple_derive(tt, quote! { std::hash::Hash })
}

fn eq_expand(
_db: &dyn AstDatabase,
_id: MacroCallId,
_id: LazyMacroId,
tt: &tt::Subtree,
) -> Result<tt::Subtree, mbe::ExpandError> {
expand_simple_derive(tt, quote! { std::cmp::Eq })
}

fn partial_eq_expand(
_db: &dyn AstDatabase,
_id: MacroCallId,
_id: LazyMacroId,
tt: &tt::Subtree,
) -> Result<tt::Subtree, mbe::ExpandError> {
expand_simple_derive(tt, quote! { std::cmp::PartialEq })
}

fn ord_expand(
_db: &dyn AstDatabase,
_id: MacroCallId,
_id: LazyMacroId,
tt: &tt::Subtree,
) -> Result<tt::Subtree, mbe::ExpandError> {
expand_simple_derive(tt, quote! { std::cmp::Ord })
}

fn partial_ord_expand(
_db: &dyn AstDatabase,
_id: MacroCallId,
_id: LazyMacroId,
tt: &tt::Subtree,
) -> Result<tt::Subtree, mbe::ExpandError> {
expand_simple_derive(tt, quote! { std::cmp::PartialOrd })
Expand All @@ -228,7 +228,7 @@ fn partial_ord_expand(
#[cfg(test)]
mod tests {
use super::*;
use crate::{test_db::TestDB, AstId, MacroCallKind, MacroCallLoc};
use crate::{test_db::TestDB, AstId, MacroCallId, MacroCallKind, MacroCallLoc};
use ra_db::{fixture::WithFixture, SourceDatabase};

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

let id = db.intern_macro(loc);
let id: MacroCallId = db.intern_macro(loc).into();
let parsed = db.parse_or_expand(id.as_file()).unwrap();

// FIXME text() for syntax nodes parsed from token tree looks weird
Expand Down
Loading