Skip to content

Commit 7ed42a3

Browse files
committed
hir_def: refactor expand_macro_type and cleanups
1 parent 14918a3 commit 7ed42a3

File tree

3 files changed

+33
-123
lines changed

3 files changed

+33
-123
lines changed

crates/hir_def/src/body.rs

Lines changed: 16 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use hir_expand::{
1919
use la_arena::{Arena, ArenaMap};
2020
use profile::Count;
2121
use rustc_hash::FxHashMap;
22-
use syntax::{ast, AstNode, AstPtr, SyntaxNode};
22+
use syntax::{ast, AstNode, AstPtr};
2323

2424
pub use lower::LowerCtx;
2525

@@ -98,14 +98,11 @@ impl Expander {
9898
}
9999
}
100100

101-
fn enter_expand_intern(
101+
pub(crate) fn enter_expand<T: ast::AstNode>(
102102
&mut self,
103103
db: &dyn DefDatabase,
104104
macro_call: ast::MacroCall,
105-
) -> Result<
106-
ExpandResult<Option<(SyntaxNode, impl FnMut(&dyn DefDatabase) -> Mark + '_)>>,
107-
UnresolvedMacro,
108-
> {
105+
) -> Result<ExpandResult<Option<(Mark, T)>>, UnresolvedMacro> {
109106
if self.recursion_limit + 1 > EXPANSION_RECURSION_LIMIT {
110107
cov_mark::hit!(your_stack_belongs_to_me);
111108
return Ok(ExpandResult::str_err(
@@ -150,55 +147,6 @@ impl Expander {
150147
}
151148
};
152149

153-
let this = self;
154-
155-
let advance_state = move |db: &dyn DefDatabase| {
156-
this.recursion_limit += 1;
157-
let mark = Mark {
158-
file_id: this.current_file_id,
159-
ast_id_map: mem::take(&mut this.ast_id_map),
160-
bomb: DropBomb::new("expansion mark dropped"),
161-
};
162-
this.cfg_expander.hygiene = Hygiene::new(db.upcast(), file_id);
163-
this.current_file_id = file_id;
164-
this.ast_id_map = db.ast_id_map(file_id);
165-
mark
166-
};
167-
168-
Ok(ExpandResult { value: Some((raw_node, advance_state)), err })
169-
}
170-
171-
pub(crate) fn enter_expand_raw(
172-
&mut self,
173-
db: &dyn DefDatabase,
174-
macro_call: ast::MacroCall,
175-
) -> Result<ExpandResult<Option<(Mark, SyntaxNode)>>, UnresolvedMacro> {
176-
let (raw_node, mut advance_state, err) = match self.enter_expand_intern(db, macro_call)? {
177-
ExpandResult { value: Some((raw_node, advance_state)), err } => {
178-
(raw_node, advance_state, err)
179-
}
180-
ExpandResult { value: None, err } => return Ok(ExpandResult { value: None, err }),
181-
};
182-
183-
log::debug!("macro expansion {:#?}", raw_node);
184-
185-
let mark = advance_state(db);
186-
187-
Ok(ExpandResult { value: Some((mark, raw_node)), err })
188-
}
189-
190-
pub(crate) fn enter_expand<T: ast::AstNode>(
191-
&mut self,
192-
db: &dyn DefDatabase,
193-
macro_call: ast::MacroCall,
194-
) -> Result<ExpandResult<Option<(Mark, T)>>, UnresolvedMacro> {
195-
let (raw_node, mut advance_state, err) = match self.enter_expand_intern(db, macro_call)? {
196-
ExpandResult { value: Some((raw_node, advance_state)), err } => {
197-
(raw_node, advance_state, err)
198-
}
199-
ExpandResult { value: None, err } => return Ok(ExpandResult { value: None, err }),
200-
};
201-
202150
let node = match T::cast(raw_node) {
203151
Some(it) => it,
204152
None => {
@@ -209,7 +157,15 @@ impl Expander {
209157

210158
log::debug!("macro expansion {:#?}", node.syntax());
211159

212-
let mark = advance_state(db);
160+
self.recursion_limit += 1;
161+
let mark = Mark {
162+
file_id: self.current_file_id,
163+
ast_id_map: mem::take(&mut self.ast_id_map),
164+
bomb: DropBomb::new("expansion mark dropped"),
165+
};
166+
self.cfg_expander.hygiene = Hygiene::new(db.upcast(), file_id);
167+
self.current_file_id = file_id;
168+
self.ast_id_map = db.ast_id_map(file_id);
213169

214170
Ok(ExpandResult { value: Some((mark, node)), err })
215171
}
@@ -234,6 +190,10 @@ impl Expander {
234190
&self.cfg_expander.cfg_options
235191
}
236192

193+
pub(crate) fn current_file_id(&self) -> HirFileId {
194+
self.current_file_id
195+
}
196+
237197
fn parse_path(&mut self, path: ast::Path) -> Option<Path> {
238198
let ctx = LowerCtx::with_hygiene(&self.cfg_expander.hygiene);
239199
Path::from_src(path, &ctx)

crates/hir_def/src/type_ref.rs

Lines changed: 14 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
//! HIR for references to types. Paths in these are not yet resolved. They can
22
//! be directly created from an ast::TypeRef, without further queries.
3-
use std::borrow::Cow;
43
54
use hir_expand::{ast_id_map::FileAstId, name::Name, ExpandResult, InFile};
6-
use syntax::{algo::SyntaxRewriter, ast, AstNode, SyntaxKind, SyntaxNode};
5+
use syntax::ast;
76

87
use crate::{
98
body::{Expander, LowerCtx},
@@ -207,16 +206,6 @@ impl TypeRef {
207206
TypeRef::Tuple(Vec::new())
208207
}
209208

210-
pub fn has_macro_calls(&self) -> bool {
211-
let mut has_macro_call = false;
212-
self.walk(&mut |ty_ref| {
213-
if let TypeRef::Macro(_) = ty_ref {
214-
has_macro_call |= true
215-
}
216-
});
217-
has_macro_call
218-
}
219-
220209
pub fn walk(&self, f: &mut impl FnMut(&TypeRef)) {
221210
go(self, f);
222211

@@ -315,68 +304,29 @@ impl TypeBound {
315304
}
316305
}
317306

318-
pub fn expand_type_ref<'a>(
307+
pub fn expand_macro_type(
319308
db: &dyn DefDatabase,
320309
module_id: ModuleId,
321-
type_ref: &'a TypeRef,
322-
) -> Option<Cow<'a, TypeRef>> {
323-
let macro_call = match type_ref {
310+
macro_type: &TypeRef,
311+
) -> Option<TypeRef> {
312+
let macro_call = match macro_type {
324313
TypeRef::Macro(macro_call) => macro_call,
325-
_ => return Some(Cow::Borrowed(type_ref)),
314+
_ => panic!("expected TypeRef::Macro"),
326315
};
327316

328317
let file_id = macro_call.file_id;
329318
let macro_call = macro_call.to_node(db.upcast());
330319

331320
let mut expander = Expander::new(db, file_id, module_id);
332-
let expanded = expand(db, &mut expander, &macro_call, true)?;
333-
334-
let node = ast::Type::cast(expanded)?;
335-
336-
let ctx = LowerCtx::new(db, file_id);
337-
return Some(Cow::Owned(TypeRef::from_ast(&ctx, node)));
338-
339-
fn expand(
340-
db: &dyn DefDatabase,
341-
expander: &mut Expander,
342-
macro_call: &ast::MacroCall,
343-
expect_type: bool,
344-
) -> Option<SyntaxNode> {
345-
let (mark, mut expanded) = match expander.enter_expand_raw(db, macro_call.clone()) {
346-
Ok(ExpandResult { value: Some((mark, expanded)), .. }) => (mark, expanded),
347-
_ => return None,
348-
};
349-
350-
if expect_type && !ast::Type::can_cast(expanded.kind()) {
321+
let (file_id, expanded) = match expander.enter_expand::<ast::Type>(db, macro_call.clone()) {
322+
Ok(ExpandResult { value: Some((mark, expanded)), .. }) => {
323+
let file_id = expander.current_file_id();
351324
expander.exit(db, mark);
352-
return None;
353-
}
354-
355-
if ast::MacroType::can_cast(expanded.kind()) {
356-
expanded = expanded.first_child()?; // MACRO_CALL
325+
(file_id, expanded)
357326
}
327+
_ => return None,
328+
};
358329

359-
let mut rewriter = SyntaxRewriter::default();
360-
361-
let children = expanded.descendants().filter_map(ast::MacroCall::cast);
362-
for child in children {
363-
if let Some(new_node) = expand(db, expander, &child, false) {
364-
if expanded == *child.syntax() {
365-
expanded = new_node;
366-
} else {
367-
let parent = child.syntax().parent();
368-
let old_node = match &parent {
369-
Some(node) if node.kind() == SyntaxKind::MACRO_TYPE => node,
370-
_ => child.syntax(),
371-
};
372-
rewriter.replace(old_node, &new_node)
373-
}
374-
}
375-
}
376-
377-
expander.exit(db, mark);
378-
379-
let res = rewriter.rewrite(&expanded);
380-
Some(res)
381-
}
330+
let ctx = LowerCtx::new(db, file_id);
331+
return Some(TypeRef::from_ast(&ctx, expanded));
382332
}

crates/hir_ty/src/lower.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use hir_def::{
1515
generics::{TypeParamProvenance, WherePredicate, WherePredicateTypeTarget},
1616
path::{GenericArg, Path, PathSegment, PathSegments},
1717
resolver::{HasResolver, Resolver, TypeNs},
18-
type_ref::{expand_type_ref, TraitRef as HirTraitRef, TypeBound, TypeRef},
18+
type_ref::{expand_macro_type, TraitRef as HirTraitRef, TypeBound, TypeRef},
1919
AdtId, AssocContainerId, AssocItemId, ConstId, ConstParamId, EnumId, EnumVariantId, FunctionId,
2020
GenericDefId, HasModule, ImplId, LocalFieldId, Lookup, StaticId, StructId, TraitId,
2121
TypeAliasId, TypeParamId, UnionId, VariantId,
@@ -289,8 +289,8 @@ impl<'a> TyLoweringContext<'a> {
289289
}
290290
mt @ TypeRef::Macro(_) => {
291291
if let Some(module_id) = self.resolver.module() {
292-
match expand_type_ref(self.db.upcast(), module_id, mt) {
293-
Some(type_ref) => self.lower_ty(type_ref.as_ref()),
292+
match expand_macro_type(self.db.upcast(), module_id, mt) {
293+
Some(type_ref) => self.lower_ty(&type_ref),
294294
None => TyKind::Error.intern(&Interner),
295295
}
296296
} else {

0 commit comments

Comments
 (0)