let macro_call =
self.find_file(actual_macro_call.syntax().clone()).with_value(actual_macro_call);
let sa = self.analyze2(macro_call.map(|it| it.syntax()), None);
+ let krate = sa.resolver.krate()?;
let macro_call_id = macro_call
- .as_call_id(self.db, |path| sa.resolver.resolve_path_as_macro(self.db, &path))?;
+ .as_call_id(self.db, krate, |path| sa.resolver.resolve_path_as_macro(self.db, &path))?;
hir_expand::db::expand_hypothetical(self.db, macro_call_id, hypothetical_args, token_to_map)
}
db: &dyn HirDatabase,
macro_call: InFile<&ast::MacroCall>,
) -> Option<HirFileId> {
- let macro_call_id = macro_call.as_call_id(db.upcast(), |path| {
+ let krate = self.resolver.krate()?;
+ let macro_call_id = macro_call.as_call_id(db.upcast(), krate, |path| {
self.resolver.resolve_path_as_macro(db.upcast(), &path)
})?;
Some(macro_call_id.as_file())
let macro_call = InFile::new(self.current_file_id, ¯o_call);
- if let Some(call_id) = macro_call.as_call_id(db, |path| {
+ if let Some(call_id) = macro_call.as_call_id(db, self.crate_def_map.krate, |path| {
if let Some(local_scope) = local_scope {
if let Some(def) = path.as_ident().and_then(|n| local_scope.get_legacy_macro(n)) {
return Some(def);
fn as_call_id(
&self,
db: &dyn db::DefDatabase,
+ krate: CrateId,
resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
) -> Option<MacroCallId>;
}
fn as_call_id(
&self,
db: &dyn db::DefDatabase,
+ krate: CrateId,
resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
) -> Option<MacroCallId> {
let ast_id = AstId::new(self.file_id, db.ast_id_map(self.file_id).ast_id(self.value));
let h = Hygiene::new(db.upcast(), self.file_id);
let path = path::ModPath::from_src(self.value.path()?, &h)?;
- AstIdWithPath::new(ast_id.file_id, ast_id.value, path).as_call_id(db, resolver)
+ AstIdWithPath::new(ast_id.file_id, ast_id.value, path).as_call_id(db, krate, resolver)
}
}
fn as_call_id(
&self,
db: &dyn db::DefDatabase,
+ krate: CrateId,
resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
) -> Option<MacroCallId> {
let def: MacroDefId = resolver(self.path.clone())?;
let hygiene = Hygiene::new(db.upcast(), self.ast_id.file_id);
Some(
- expand_eager_macro(db.upcast(), macro_call, def, &|path: ast::Path| {
+ expand_eager_macro(db.upcast(), krate, macro_call, def, &|path: ast::Path| {
resolver(path::ModPath::from_src(path, &hygiene)?)
})?
.into(),
)
} else {
- Some(def.as_lazy_macro(db.upcast(), MacroCallKind::FnLike(self.ast_id)).into())
+ Some(def.as_lazy_macro(db.upcast(), krate, MacroCallKind::FnLike(self.ast_id)).into())
}
}
}
fn as_call_id(
&self,
db: &dyn db::DefDatabase,
+ krate: CrateId,
resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
) -> Option<MacroCallId> {
let def = resolver(self.path.clone())?;
Some(
def.as_lazy_macro(
db.upcast(),
+ krate,
MacroCallKind::Attr(self.ast_id, self.path.segments.last()?.to_string()),
)
.into(),
return false;
}
- if let Some(call_id) = directive.ast_id.as_call_id(self.db, |path| {
- let resolved_res = self.def_map.resolve_path_fp_with_macro(
- self.db,
- ResolveMode::Other,
- directive.module_id,
- &path,
- BuiltinShadowMode::Module,
- );
- resolved_res.resolved_def.take_macros()
- }) {
+ if let Some(call_id) =
+ directive.ast_id.as_call_id(self.db, self.def_map.krate, |path| {
+ let resolved_res = self.def_map.resolve_path_fp_with_macro(
+ self.db,
+ ResolveMode::Other,
+ directive.module_id,
+ &path,
+ BuiltinShadowMode::Module,
+ );
+ resolved_res.resolved_def.take_macros()
+ })
+ {
resolved.push((directive.module_id, call_id, directive.depth));
res = ReachedFixedPoint::No;
return false;
true
});
attribute_macros.retain(|directive| {
- if let Some(call_id) = directive
- .ast_id
- .as_call_id(self.db, |path| self.resolve_attribute_macro(&directive, &path))
+ if let Some(call_id) =
+ directive.ast_id.as_call_id(self.db, self.def_map.krate, |path| {
+ self.resolve_attribute_macro(&directive, &path)
+ })
{
resolved.push((directive.module_id, call_id, 0));
res = ReachedFixedPoint::No;
}
// Case 2: try to resolve in legacy scope and expand macro_rules
- if let Some(macro_call_id) = ast_id.as_call_id(self.def_collector.db, |path| {
- path.as_ident().and_then(|name| {
- self.def_collector.def_map[self.module_id].scope.get_legacy_macro(&name)
+ if let Some(macro_call_id) =
+ ast_id.as_call_id(self.def_collector.db, self.def_collector.def_map.krate, |path| {
+ path.as_ident().and_then(|name| {
+ self.def_collector.def_map[self.module_id].scope.get_legacy_macro(&name)
+ })
})
- }) {
+ {
self.def_collector.unexpanded_macros.push(MacroDirective {
module_id: self.module_id,
ast_id,
match_ast,
};
-use crate::db::AstDatabase;
-use crate::{guess_crate, name, quote, LazyMacroId, MacroCallId, MacroDefId, MacroDefKind};
+use crate::{db::AstDatabase, name, quote, LazyMacroId, MacroDefId, MacroDefKind};
macro_rules! register_builtin {
( $($trait:ident => $expand:ident),* ) => {
fn find_builtin_crate(db: &dyn AstDatabase, id: LazyMacroId) -> tt::TokenTree {
// FIXME: make hygiene works for builtin derive macro
// such that $crate can be used here.
-
- let m: MacroCallId = id.into();
- let file_id = m.as_file().original_file(db);
let cg = db.crate_graph();
- let krate = match guess_crate(db, file_id) {
- Some(krate) => krate,
- None => {
- let tt = quote! { core };
- return tt.token_trees[0].clone();
- }
- };
+ let krate = db.lookup_intern_macro(id).krate;
// XXX
// All crates except core itself should have a dependency on core,
#[cfg(test)]
mod tests {
- use super::*;
- use crate::{test_db::TestDB, AstId, MacroCallId, MacroCallKind, MacroCallLoc};
use name::{known, Name};
- use ra_db::{fixture::WithFixture, SourceDatabase};
+ use ra_db::{fixture::WithFixture, CrateId, SourceDatabase};
+
+ use crate::{test_db::TestDB, AstId, MacroCallId, MacroCallKind, MacroCallLoc};
+
+ use super::*;
fn expand_builtin_derive(s: &str, name: Name) -> String {
let def = find_builtin_derive(&name).unwrap();
let attr_id = AstId::new(file_id.into(), ast_id_map.ast_id(&items[0]));
- let loc = MacroCallLoc { def, kind: MacroCallKind::Attr(attr_id, name.to_string()) };
+ let loc = MacroCallLoc {
+ def,
+ krate: CrateId(0),
+ kind: MacroCallKind::Attr(attr_id, name.to_string()),
+ };
let id: MacroCallId = db.intern_macro(loc).into();
let parsed = db.parse_or_expand(id.as_file()).unwrap();
//! Builtin macro
-use crate::db::AstDatabase;
use crate::{
- ast::{self, AstToken, HasStringValue},
- name, AstId, CrateId, MacroDefId, MacroDefKind, TextSize,
+ db::AstDatabase, name, quote, AstId, CrateId, EagerMacroId, LazyMacroId, MacroCallId,
+ MacroDefId, MacroDefKind, TextSize,
};
-use crate::{guess_crate, quote, EagerMacroId, LazyMacroId, MacroCallId};
use either::Either;
use mbe::parse_to_token_tree;
use ra_db::FileId;
use ra_parser::FragmentKind;
+use ra_syntax::ast::{self, AstToken, HasStringValue};
macro_rules! register_builtin {
( LAZY: $(($name:ident, $kind: ident) => $expand:ident),* , EAGER: $(($e_name:ident, $e_kind: ident) => $e_expand:ident),* ) => {
}
fn get_env_inner(db: &dyn AstDatabase, arg_id: EagerMacroId, key: &str) -> Option<String> {
- let call_id: MacroCallId = arg_id.into();
- let original_file = call_id.as_file().original_file(db);
- let krate = guess_crate(db, original_file)?;
+ let krate = db.lookup_intern_eager_expansion(arg_id).krate;
db.crate_graph()[krate].env.get(key)
}
let expander = find_by_name(¯o_calls[0].name().unwrap().as_name()).unwrap();
+ let krate = CrateId(0);
let file_id = match expander {
Either::Left(expander) => {
// the first one should be a macro_rules
let loc = MacroCallLoc {
def,
+ krate,
kind: MacroCallKind::FnLike(AstId::new(
file_id.into(),
ast_id_map.ast_id(¯o_calls[1]),
Either::Right(expander) => {
// the first one should be a macro_rules
let def = MacroDefId {
- krate: Some(CrateId(0)),
+ krate: Some(krate),
ast_id: Some(AstId::new(file_id.into(), ast_id_map.ast_id(¯o_calls[0]))),
kind: MacroDefKind::BuiltInEager(expander),
local_inner: false,
def,
fragment: FragmentKind::Expr,
subtree: Arc::new(parsed_args.clone()),
+ krate,
file_id: file_id.into(),
}
});
def,
fragment,
subtree: Arc::new(subtree),
+ krate,
file_id: file_id.into(),
};
EagerCallLoc, EagerMacroId, InFile, MacroCallId, MacroCallKind, MacroDefId, MacroDefKind,
};
+use ra_db::CrateId;
use ra_parser::FragmentKind;
use ra_syntax::{algo::SyntaxRewriter, SyntaxNode};
use std::sync::Arc;
pub fn expand_eager_macro(
db: &dyn AstDatabase,
+ krate: CrateId,
macro_call: InFile<ast::MacroCall>,
def: MacroDefId,
resolver: &dyn Fn(ast::Path) -> Option<MacroDefId>,
def,
fragment: FragmentKind::Expr,
subtree: Arc::new(parsed_args.clone()),
+ krate,
file_id: macro_call.file_id,
}
});
let result = eager_macro_recur(
db,
InFile::new(arg_file_id.as_file(), parsed_args.syntax_node()),
+ krate,
resolver,
)?;
let subtree = to_subtree(&result)?;
if let MacroDefKind::BuiltInEager(eager) = def.kind {
let (subtree, fragment) = eager.expand(db, arg_id, &subtree).ok()?;
- let eager =
- EagerCallLoc { def, fragment, subtree: Arc::new(subtree), file_id: macro_call.file_id };
+ let eager = EagerCallLoc {
+ def,
+ fragment,
+ subtree: Arc::new(subtree),
+ krate,
+ file_id: macro_call.file_id,
+ };
Some(db.intern_eager_expansion(eager))
} else {
db: &dyn AstDatabase,
def: &MacroDefId,
macro_call: InFile<ast::MacroCall>,
+ krate: CrateId,
) -> Option<InFile<SyntaxNode>> {
let ast_id = db.ast_id_map(macro_call.file_id).ast_id(¯o_call.value);
let id: MacroCallId =
- def.as_lazy_macro(db, MacroCallKind::FnLike(macro_call.with_value(ast_id))).into();
+ def.as_lazy_macro(db, krate, MacroCallKind::FnLike(macro_call.with_value(ast_id))).into();
db.parse_or_expand(id.as_file()).map(|node| InFile::new(id.as_file(), node))
}
fn eager_macro_recur(
db: &dyn AstDatabase,
curr: InFile<SyntaxNode>,
+ krate: CrateId,
macro_resolver: &dyn Fn(ast::Path) -> Option<MacroDefId>,
) -> Option<SyntaxNode> {
let original = curr.value.clone();
let def: MacroDefId = macro_resolver(child.path()?)?;
let insert = match def.kind {
MacroDefKind::BuiltInEager(_) => {
- let id: MacroCallId =
- expand_eager_macro(db, curr.with_value(child.clone()), def, macro_resolver)?
- .into();
+ let id: MacroCallId = expand_eager_macro(
+ db,
+ krate,
+ curr.with_value(child.clone()),
+ def,
+ macro_resolver,
+ )?
+ .into();
db.parse_or_expand(id.as_file())?
}
MacroDefKind::Declarative
| MacroDefKind::BuiltIn(_)
| MacroDefKind::BuiltInDerive(_)
| MacroDefKind::CustomDerive(_) => {
- let expanded = lazy_expand(db, &def, curr.with_value(child.clone()))?;
+ let expanded = lazy_expand(db, &def, curr.with_value(child.clone()), krate)?;
// replace macro inside
- eager_macro_recur(db, expanded, macro_resolver)?
+ eager_macro_recur(db, expanded, krate, macro_resolver)?
}
};
}
impl MacroDefId {
- pub fn as_lazy_macro(self, db: &dyn db::AstDatabase, kind: MacroCallKind) -> LazyMacroId {
- db.intern_macro(MacroCallLoc { def: self, kind })
+ pub fn as_lazy_macro(
+ self,
+ db: &dyn db::AstDatabase,
+ krate: CrateId,
+ kind: MacroCallKind,
+ ) -> LazyMacroId {
+ db.intern_macro(MacroCallLoc { def: self, krate, kind })
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct MacroCallLoc {
pub(crate) def: MacroDefId,
+ pub(crate) krate: CrateId,
pub(crate) kind: MacroCallKind,
}
pub(crate) def: MacroDefId,
pub(crate) fragment: FragmentKind,
pub(crate) subtree: Arc<tt::Subtree>,
+ pub(crate) krate: CrateId,
pub(crate) file_id: HirFileId,
}
self.with_value(self.value.syntax())
}
}
-
-// FIXME: this is obviously wrong, there shouldn't be any guesing here
-fn guess_crate(db: &dyn db::AstDatabase, file_id: FileId) -> Option<CrateId> {
- db.relevant_crates(file_id).iter().next().copied()
-}