Skip to content

Commit 625a6f3

Browse files
committed
Auto merge of rust-lang#14519 - Veykril:hir-def-refac, r=Veykril
internal: Don't recreate Hygiene unnecessarily
2 parents a1e8535 + f742943 commit 625a6f3

File tree

9 files changed

+35
-51
lines changed

9 files changed

+35
-51
lines changed

crates/hir-def/src/body.rs

+8
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ impl CfgExpander {
7777
let attrs = self.parse_attrs(db, owner);
7878
attrs.is_cfg_enabled(&self.cfg_options)
7979
}
80+
81+
pub(crate) fn hygiene(&self) -> &Hygiene {
82+
&self.hygiene
83+
}
8084
}
8185

8286
impl Expander {
@@ -181,6 +185,10 @@ impl Expander {
181185
mark.bomb.defuse();
182186
}
183187

188+
pub fn ctx<'a>(&self, db: &'a dyn DefDatabase) -> LowerCtx<'a> {
189+
LowerCtx::new(db, &self.cfg_expander.hygiene, self.current_file_id)
190+
}
191+
184192
pub(crate) fn to_source<T>(&self, value: T) -> InFile<T> {
185193
InFile { file_id: self.current_file_id, value }
186194
}

crates/hir-def/src/body/lower.rs

+18-5
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ use crate::{
3939
RecordFieldPat, RecordLitField, Statement,
4040
},
4141
item_scope::BuiltinShadowMode,
42-
item_tree::ItemTree,
4342
lang_item::LangItem,
4443
path::{GenericArgs, Path},
4544
type_ref::{Mutability, Rawness, TypeRef},
@@ -53,7 +52,11 @@ pub struct LowerCtx<'a> {
5352
}
5453

5554
impl<'a> LowerCtx<'a> {
56-
pub fn new(db: &'a dyn DefDatabase, file_id: HirFileId) -> Self {
55+
pub fn new(db: &'a dyn DefDatabase, hygiene: &Hygiene, file_id: HirFileId) -> Self {
56+
LowerCtx { db, hygiene: hygiene.clone(), ast_id_map: Some((file_id, OnceCell::new())) }
57+
}
58+
59+
pub fn with_file_id(db: &'a dyn DefDatabase, file_id: HirFileId) -> Self {
5760
LowerCtx {
5861
db,
5962
hygiene: Hygiene::new(db.upcast(), file_id),
@@ -230,7 +233,7 @@ impl ExprCollector<'_> {
230233
}
231234

232235
fn ctx(&self) -> LowerCtx<'_> {
233-
LowerCtx::new(self.db, self.expander.current_file_id)
236+
self.expander.ctx(self.db)
234237
}
235238

236239
fn alloc_expr(&mut self, expr: Expr, ptr: ExprPtr) -> ExprId {
@@ -973,8 +976,18 @@ impl ExprCollector<'_> {
973976
block: ast::BlockExpr,
974977
mk_block: impl FnOnce(Option<BlockId>, Box<[Statement]>, Option<ExprId>) -> Expr,
975978
) -> ExprId {
976-
let block_id = if ItemTree::block_has_items(self.db, self.expander.current_file_id, &block)
977-
{
979+
let block_has_items = {
980+
let statement_has_item = block.statements().any(|stmt| match stmt {
981+
ast::Stmt::Item(_) => true,
982+
// Macro calls can be both items and expressions. The syntax library always treats
983+
// them as expressions here, so we undo that.
984+
ast::Stmt::ExprStmt(es) => matches!(es.expr(), Some(ast::Expr::MacroExpr(_))),
985+
_ => false,
986+
});
987+
statement_has_item || matches!(block.tail_expr(), Some(ast::Expr::MacroExpr(_)))
988+
};
989+
990+
let block_id = if block_has_items {
978991
let file_local_id = self.ast_id_map.ast_id(&block);
979992
let ast_id = AstId::new(self.expander.current_file_id, file_local_id);
980993
Some(self.db.intern_block(BlockLoc {

crates/hir-def/src/data/adt.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ fn lower_struct(
473473
trace: &mut Trace<FieldData, Either<ast::TupleField, ast::RecordField>>,
474474
ast: &InFile<ast::StructKind>,
475475
) -> StructKind {
476-
let ctx = LowerCtx::new(db, ast.file_id);
476+
let ctx = LowerCtx::new(db, &expander.hygiene(), ast.file_id);
477477

478478
match &ast.value {
479479
ast::StructKind::Tuple(fl) => {

crates/hir-def/src/generics.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use hir_expand::{
1212
use intern::Interned;
1313
use la_arena::{Arena, ArenaMap, Idx};
1414
use once_cell::unsync::Lazy;
15-
use std::ops::DerefMut;
1615
use stdx::impl_from;
1716
use syntax::ast::{self, HasGenericParams, HasName, HasTypeBounds};
1817

@@ -328,7 +327,7 @@ impl GenericParams {
328327
pub(crate) fn fill_implicit_impl_trait_args(
329328
&mut self,
330329
db: &dyn DefDatabase,
331-
expander: &mut impl DerefMut<Target = Expander>,
330+
expander: &mut Expander,
332331
type_ref: &TypeRef,
333332
) {
334333
type_ref.walk(&mut |type_ref| {
@@ -350,7 +349,7 @@ impl GenericParams {
350349
let macro_call = mc.to_node(db.upcast());
351350
match expander.enter_expand::<ast::Type>(db, macro_call) {
352351
Ok(ExpandResult { value: Some((mark, expanded)), .. }) => {
353-
let ctx = LowerCtx::new(db, expander.current_file_id());
352+
let ctx = expander.ctx(db);
354353
let type_ref = TypeRef::from_ast(&ctx, expanded);
355354
self.fill_implicit_impl_trait_args(db, expander, &type_ref);
356355
expander.exit(db, mark);

crates/hir-def/src/item_tree.rs

-8
Original file line numberDiff line numberDiff line change
@@ -152,14 +152,6 @@ impl ItemTree {
152152
&self.top_level
153153
}
154154

155-
pub fn block_has_items(
156-
db: &dyn DefDatabase,
157-
file_id: HirFileId,
158-
block: &ast::BlockExpr,
159-
) -> bool {
160-
lower::Ctx::new(db, file_id).block_has_items(block)
161-
}
162-
163155
/// Returns the inner attributes of the source file.
164156
pub fn top_level_attrs(&self, db: &dyn DefDatabase, krate: CrateId) -> Attrs {
165157
Attrs::filter(

crates/hir-def/src/item_tree/lower.rs

+1-29
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ impl<'a> Ctx<'a> {
2929
db,
3030
tree: ItemTree::default(),
3131
source_ast_id_map: db.ast_id_map(file),
32-
body_ctx: crate::body::LowerCtx::new(db, file),
32+
body_ctx: crate::body::LowerCtx::with_file_id(db, file),
3333
}
3434
}
3535

@@ -101,34 +101,6 @@ impl<'a> Ctx<'a> {
101101
self.tree
102102
}
103103

104-
pub(super) fn block_has_items(mut self, block: &ast::BlockExpr) -> bool {
105-
let statement_has_item = block
106-
.statements()
107-
.find_map(|stmt| match stmt {
108-
ast::Stmt::Item(item) => self.lower_mod_item(&item),
109-
// Macro calls can be both items and expressions. The syntax library always treats
110-
// them as expressions here, so we undo that.
111-
ast::Stmt::ExprStmt(es) => match es.expr()? {
112-
ast::Expr::MacroExpr(expr) => self.lower_mod_item(&expr.macro_call()?.into()),
113-
_ => None,
114-
},
115-
_ => None,
116-
})
117-
.is_some();
118-
if statement_has_item {
119-
return true;
120-
}
121-
122-
if let Some(ast::Expr::MacroExpr(expr)) = block.tail_expr() {
123-
if let Some(call) = expr.macro_call() {
124-
if let Some(_) = self.lower_mod_item(&call.into()) {
125-
return true;
126-
}
127-
}
128-
}
129-
false
130-
}
131-
132104
fn data(&mut self) -> &mut ItemTreeData {
133105
self.tree.data_mut()
134106
}

crates/hir-ty/src/lower.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use chalk_ir::{
1818

1919
use either::Either;
2020
use hir_def::{
21-
body::{Expander, LowerCtx},
21+
body::Expander,
2222
builtin_type::BuiltinType,
2323
data::adt::StructKind,
2424
generics::{
@@ -380,7 +380,7 @@ impl<'a> TyLoweringContext<'a> {
380380
let macro_call = macro_call.to_node(self.db.upcast());
381381
match expander.enter_expand::<ast::Type>(self.db.upcast(), macro_call) {
382382
Ok(ExpandResult { value: Some((mark, expanded)), .. }) => {
383-
let ctx = LowerCtx::new(self.db.upcast(), expander.current_file_id());
383+
let ctx = expander.ctx(self.db.upcast());
384384
let type_ref = TypeRef::from_ast(&ctx, expanded);
385385

386386
drop(expander);

crates/hir/src/semantics.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1065,7 +1065,7 @@ impl<'db> SemanticsImpl<'db> {
10651065

10661066
fn resolve_type(&self, ty: &ast::Type) -> Option<Type> {
10671067
let analyze = self.analyze(ty.syntax())?;
1068-
let ctx = body::LowerCtx::new(self.db.upcast(), analyze.file_id);
1068+
let ctx = body::LowerCtx::with_file_id(self.db.upcast(), analyze.file_id);
10691069
let ty = hir_ty::TyLoweringContext::new(self.db, &analyze.resolver)
10701070
.lower_ty(&crate::TypeRef::from_ast(&ctx, ty.clone()));
10711071
Some(Type::new_with_resolver(self.db, &analyze.resolver, ty))
@@ -1672,7 +1672,7 @@ impl<'a> SemanticsScope<'a> {
16721672
/// Resolve a path as-if it was written at the given scope. This is
16731673
/// necessary a heuristic, as it doesn't take hygiene into account.
16741674
pub fn speculative_resolve(&self, path: &ast::Path) -> Option<PathResolution> {
1675-
let ctx = body::LowerCtx::new(self.db.upcast(), self.file_id);
1675+
let ctx = body::LowerCtx::with_file_id(self.db.upcast(), self.file_id);
16761676
let path = Path::from_src(path.clone(), &ctx)?;
16771677
resolve_hir_path(self.db, &self.resolver, &path)
16781678
}

crates/hir/src/source_analyzer.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ impl SourceAnalyzer {
463463
db: &dyn HirDatabase,
464464
macro_call: InFile<&ast::MacroCall>,
465465
) -> Option<Macro> {
466-
let ctx = body::LowerCtx::new(db.upcast(), macro_call.file_id);
466+
let ctx = body::LowerCtx::with_file_id(db.upcast(), macro_call.file_id);
467467
let path = macro_call.value.path().and_then(|ast| Path::from_src(ast, &ctx))?;
468468
self.resolver.resolve_path_as_macro(db.upcast(), path.mod_path()?).map(|it| it.into())
469469
}

0 commit comments

Comments
 (0)