pub(crate) struct Expander {
cfg_expander: CfgExpander,
- crate_def_map: Arc<DefMap>,
+ def_map: Arc<DefMap>,
current_file_id: HirFileId,
ast_id_map: Arc<AstIdMap>,
module: ModuleId,
let ast_id_map = db.ast_id_map(current_file_id);
Expander {
cfg_expander,
- crate_def_map,
+ def_map: crate_def_map,
current_file_id,
ast_id_map,
module,
pub(crate) fn enter_expand<T: ast::AstNode>(
&mut self,
db: &dyn DefDatabase,
- local_scope: Option<&ItemScope>,
macro_call: ast::MacroCall,
) -> ExpandResult<Option<(Mark, T)>> {
if self.recursion_limit + 1 > EXPANSION_RECURSION_LIMIT {
let macro_call = InFile::new(self.current_file_id, ¯o_call);
- let resolver = |path: ModPath| -> Option<MacroDefId> {
- 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);
- }
- }
- self.resolve_path_as_macro(db, &path)
- };
+ let resolver =
+ |path: ModPath| -> Option<MacroDefId> { self.resolve_path_as_macro(db, &path) };
let mut err = None;
let call_id =
- macro_call.as_call_id_with_errors(db, self.crate_def_map.krate(), resolver, &mut |e| {
+ macro_call.as_call_id_with_errors(db, self.def_map.krate(), resolver, &mut |e| {
err.get_or_insert(e);
});
let call_id = match call_id {
}
fn resolve_path_as_macro(&self, db: &dyn DefDatabase, path: &ModPath) -> Option<MacroDefId> {
- self.crate_def_map
+ self.def_map
.resolve_path(db, self.module.local_id, path, BuiltinShadowMode::Other)
.0
.take_macros()
//! Transforms `ast::Expr` into an equivalent `hir_def::expr::Expr`
//! representation.
-use std::{any::type_name, sync::Arc};
+use std::{any::type_name, mem, sync::Arc};
use either::Either;
use hir_expand::{
item_tree::{ItemTree, ItemTreeId, ItemTreeNode},
path::{GenericArgs, Path},
type_ref::{Mutability, Rawness, TypeRef},
- AdtId, ConstLoc, ContainerId, DefWithBodyId, EnumLoc, FunctionLoc, Intern, ModuleDefId,
- StaticLoc, StructLoc, TraitLoc, TypeAliasLoc, UnionLoc,
+ AdtId, BlockLoc, ConstLoc, ContainerId, DefWithBodyId, EnumLoc, FunctionLoc, Intern,
+ ModuleDefId, StaticLoc, StructLoc, TraitLoc, TypeAliasLoc, UnionLoc,
};
use super::{diagnostics::BodyDiagnostic, ExprSource, PatSource};
fn alloc_expr_desugared(&mut self, expr: Expr) -> ExprId {
self.make_expr(expr, Err(SyntheticSyntax))
}
- fn empty_block(&mut self) -> ExprId {
- self.alloc_expr_desugared(Expr::Block { statements: Vec::new(), tail: None, label: None })
+ fn unit(&mut self) -> ExprId {
+ self.alloc_expr_desugared(Expr::Tuple { exprs: Vec::new() })
}
fn missing_expr(&mut self) -> ExprId {
self.alloc_expr_desugared(Expr::Missing)
MatchArm { pat, expr: then_branch, guard: None },
MatchArm {
pat: placeholder_pat,
- expr: else_branch.unwrap_or_else(|| self.empty_block()),
+ expr: else_branch.unwrap_or_else(|| self.unit()),
guard: None,
},
];
let outer_file = self.expander.current_file_id;
let macro_call = self.expander.to_source(AstPtr::new(&e));
- let res = self.expander.enter_expand(self.db, Some(&self.body.item_scope), e);
+ let res = self.expander.enter_expand(self.db, e);
match &res.err {
Some(ExpandError::UnresolvedProcMacro) => {
}
fn collect_block(&mut self, block: ast::BlockExpr) -> ExprId {
- let syntax_node_ptr = AstPtr::new(&block.clone().into());
+ let ast_id = self.expander.ast_id(&block);
+ let block_loc = BlockLoc { ast_id, module: self.expander.module };
+ let block_id = self.db.intern_block(block_loc);
+ let def_map = self.db.block_def_map(block_id);
+ let root = def_map.module_id(def_map.root());
+ let prev_def_map = mem::replace(&mut self.expander.def_map, def_map);
+ let prev_module = mem::replace(&mut self.expander.module, root);
+
self.collect_stmts_items(block.statements());
let statements =
block.statements().filter_map(|s| self.collect_stmt(s)).flatten().collect();
let tail = block.tail_expr().map(|e| self.collect_expr(e));
- self.alloc_expr(Expr::Block { statements, tail, label: None }, syntax_node_ptr)
+ let syntax_node_ptr = AstPtr::new(&block.clone().into());
+ let expr_id = self.alloc_expr(
+ Expr::Block { id: block_id, statements, tail, label: None },
+ syntax_node_ptr,
+ );
+
+ self.expander.def_map = prev_def_map;
+ self.expander.module = prev_module;
+ expr_id
}
fn collect_stmts_items(&mut self, stmts: ast::AstChildren<ast::Stmt>) {
if annotation == BindingAnnotation::Unannotated && subpat.is_none() {
// This could also be a single-segment path pattern. To
// decide that, we need to try resolving the name.
- let (resolved, _) = self.expander.crate_def_map.resolve_path(
+ let (resolved, _) = self.expander.def_map.resolve_path(
self.db,
self.expander.module.local_id,
&name.clone().into(),