mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 21:05:02 +00:00
Make use of block_def_map
in body lowering
Removes the `local_scope` hack from `Expander` in favor of tracking the `DefMap` in use during body lowering
This commit is contained in:
parent
bbc6298600
commit
cdb0e25aaa
3 changed files with 19 additions and 18 deletions
|
@ -45,7 +45,7 @@ pub(crate) struct CfgExpander {
|
||||||
|
|
||||||
pub(crate) struct Expander {
|
pub(crate) struct Expander {
|
||||||
cfg_expander: CfgExpander,
|
cfg_expander: CfgExpander,
|
||||||
crate_def_map: Arc<DefMap>,
|
def_map: Arc<DefMap>,
|
||||||
current_file_id: HirFileId,
|
current_file_id: HirFileId,
|
||||||
ast_id_map: Arc<AstIdMap>,
|
ast_id_map: Arc<AstIdMap>,
|
||||||
module: ModuleId,
|
module: ModuleId,
|
||||||
|
@ -90,7 +90,7 @@ impl Expander {
|
||||||
let ast_id_map = db.ast_id_map(current_file_id);
|
let ast_id_map = db.ast_id_map(current_file_id);
|
||||||
Expander {
|
Expander {
|
||||||
cfg_expander,
|
cfg_expander,
|
||||||
crate_def_map,
|
def_map: crate_def_map,
|
||||||
current_file_id,
|
current_file_id,
|
||||||
ast_id_map,
|
ast_id_map,
|
||||||
module,
|
module,
|
||||||
|
@ -101,7 +101,6 @@ impl Expander {
|
||||||
pub(crate) fn enter_expand<T: ast::AstNode>(
|
pub(crate) fn enter_expand<T: ast::AstNode>(
|
||||||
&mut self,
|
&mut self,
|
||||||
db: &dyn DefDatabase,
|
db: &dyn DefDatabase,
|
||||||
local_scope: Option<&ItemScope>,
|
|
||||||
macro_call: ast::MacroCall,
|
macro_call: ast::MacroCall,
|
||||||
) -> ExpandResult<Option<(Mark, T)>> {
|
) -> ExpandResult<Option<(Mark, T)>> {
|
||||||
if self.recursion_limit + 1 > EXPANSION_RECURSION_LIMIT {
|
if self.recursion_limit + 1 > EXPANSION_RECURSION_LIMIT {
|
||||||
|
@ -111,18 +110,12 @@ impl Expander {
|
||||||
|
|
||||||
let macro_call = InFile::new(self.current_file_id, ¯o_call);
|
let macro_call = InFile::new(self.current_file_id, ¯o_call);
|
||||||
|
|
||||||
let resolver = |path: ModPath| -> Option<MacroDefId> {
|
let resolver =
|
||||||
if let Some(local_scope) = local_scope {
|
|path: ModPath| -> Option<MacroDefId> { self.resolve_path_as_macro(db, &path) };
|
||||||
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 mut err = None;
|
let mut err = None;
|
||||||
let call_id =
|
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);
|
err.get_or_insert(e);
|
||||||
});
|
});
|
||||||
let call_id = match call_id {
|
let call_id = match call_id {
|
||||||
|
@ -203,7 +196,7 @@ impl Expander {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn resolve_path_as_macro(&self, db: &dyn DefDatabase, path: &ModPath) -> Option<MacroDefId> {
|
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)
|
.resolve_path(db, self.module.local_id, path, BuiltinShadowMode::Other)
|
||||||
.0
|
.0
|
||||||
.take_macros()
|
.take_macros()
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
//! Transforms `ast::Expr` into an equivalent `hir_def::expr::Expr`
|
//! Transforms `ast::Expr` into an equivalent `hir_def::expr::Expr`
|
||||||
//! representation.
|
//! representation.
|
||||||
|
|
||||||
use std::{any::type_name, sync::Arc};
|
use std::{any::type_name, mem, sync::Arc};
|
||||||
|
|
||||||
use either::Either;
|
use either::Either;
|
||||||
use hir_expand::{
|
use hir_expand::{
|
||||||
|
@ -559,7 +559,7 @@ impl ExprCollector<'_> {
|
||||||
let outer_file = self.expander.current_file_id;
|
let outer_file = self.expander.current_file_id;
|
||||||
|
|
||||||
let macro_call = self.expander.to_source(AstPtr::new(&e));
|
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 {
|
match &res.err {
|
||||||
Some(ExpandError::UnresolvedProcMacro) => {
|
Some(ExpandError::UnresolvedProcMacro) => {
|
||||||
|
@ -696,11 +696,19 @@ impl ExprCollector<'_> {
|
||||||
|
|
||||||
fn collect_block(&mut self, block: ast::BlockExpr) -> ExprId {
|
fn collect_block(&mut self, block: ast::BlockExpr) -> ExprId {
|
||||||
let syntax_node_ptr = AstPtr::new(&block.clone().into());
|
let syntax_node_ptr = AstPtr::new(&block.clone().into());
|
||||||
|
let ast_id = self.expander.ast_id(&block);
|
||||||
|
let def_map = self.db.block_def_map(self.expander.module.krate, ast_id);
|
||||||
|
let prev_def_map = mem::replace(&mut self.expander.def_map, def_map);
|
||||||
|
|
||||||
self.collect_stmts_items(block.statements());
|
self.collect_stmts_items(block.statements());
|
||||||
let statements =
|
let statements =
|
||||||
block.statements().filter_map(|s| self.collect_stmt(s)).flatten().collect();
|
block.statements().filter_map(|s| self.collect_stmt(s)).flatten().collect();
|
||||||
let tail = block.tail_expr().map(|e| self.collect_expr(e));
|
let tail = block.tail_expr().map(|e| self.collect_expr(e));
|
||||||
self.alloc_expr(Expr::Block { statements, tail, label: None }, syntax_node_ptr)
|
let expr_id =
|
||||||
|
self.alloc_expr(Expr::Block { statements, tail, label: None }, syntax_node_ptr);
|
||||||
|
|
||||||
|
self.expander.def_map = prev_def_map;
|
||||||
|
expr_id
|
||||||
}
|
}
|
||||||
|
|
||||||
fn collect_stmts_items(&mut self, stmts: ast::AstChildren<ast::Stmt>) {
|
fn collect_stmts_items(&mut self, stmts: ast::AstChildren<ast::Stmt>) {
|
||||||
|
@ -830,7 +838,7 @@ impl ExprCollector<'_> {
|
||||||
if annotation == BindingAnnotation::Unannotated && subpat.is_none() {
|
if annotation == BindingAnnotation::Unannotated && subpat.is_none() {
|
||||||
// This could also be a single-segment path pattern. To
|
// This could also be a single-segment path pattern. To
|
||||||
// decide that, we need to try resolving the name.
|
// 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.db,
|
||||||
self.expander.module.local_id,
|
self.expander.module.local_id,
|
||||||
&name.clone().into(),
|
&name.clone().into(),
|
||||||
|
|
|
@ -262,7 +262,7 @@ fn collect_items(
|
||||||
let root = db.parse_or_expand(file_id).unwrap();
|
let root = db.parse_or_expand(file_id).unwrap();
|
||||||
let call = ast_id_map.get(call.ast_id).to_node(&root);
|
let call = ast_id_map.get(call.ast_id).to_node(&root);
|
||||||
|
|
||||||
if let Some((mark, mac)) = expander.enter_expand(db, None, call).value {
|
if let Some((mark, mac)) = expander.enter_expand(db, call).value {
|
||||||
let src: InFile<ast::MacroItems> = expander.to_source(mac);
|
let src: InFile<ast::MacroItems> = expander.to_source(mac);
|
||||||
let item_tree = db.item_tree(src.file_id);
|
let item_tree = db.item_tree(src.file_id);
|
||||||
let iter =
|
let iter =
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue