Skip to content

Commit ac05571

Browse files
bors[bot]matklad
andauthored
Merge #3734
3734: Get rid of ItemOrMacro r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
2 parents 539e597 + db34abe commit ac05571

File tree

7 files changed

+21
-41
lines changed

7 files changed

+21
-41
lines changed

crates/ra_assists/src/handlers/add_explicit_type.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@ mod tests {
130130
fn add_explicit_type_works_for_macro_call() {
131131
check_assist(
132132
add_explicit_type,
133-
"macro_rules! v { () => {0u64} } fn f() { let a<|> = v!(); }",
134-
"macro_rules! v { () => {0u64} } fn f() { let a<|>: u64 = v!(); }",
133+
r"macro_rules! v { () => {0u64} } fn f() { let a<|> = v!(); }",
134+
r"macro_rules! v { () => {0u64} } fn f() { let a<|>: u64 = v!(); }",
135135
);
136136
}
137137

crates/ra_hir_def/src/body/lower.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,8 @@ impl ExprCollector<'_> {
563563
ast::ModuleItem::ImplDef(_)
564564
| ast::ModuleItem::UseItem(_)
565565
| ast::ModuleItem::ExternCrateItem(_)
566-
| ast::ModuleItem::Module(_) => continue,
566+
| ast::ModuleItem::Module(_)
567+
| ast::ModuleItem::MacroCall(_) => continue,
567568
};
568569
self.body.item_scope.define_def(def);
569570
if let Some(name) = name {

crates/ra_hir_def/src/nameres/raw.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -209,11 +209,8 @@ impl RawItemsCollector {
209209
current_module: Option<Idx<ModuleData>>,
210210
body: impl ast::ModuleItemOwner,
211211
) {
212-
for item_or_macro in body.items_with_macros() {
213-
match item_or_macro {
214-
ast::ItemOrMacro::Macro(m) => self.add_macro(current_module, m),
215-
ast::ItemOrMacro::Item(item) => self.add_item(current_module, item),
216-
}
212+
for item in body.items() {
213+
self.add_item(current_module, item)
217214
}
218215
}
219216

@@ -265,6 +262,10 @@ impl RawItemsCollector {
265262
ast::ModuleItem::StaticDef(it) => {
266263
(DefKind::Static(self.source_ast_id_map.ast_id(&it)), it.name())
267264
}
265+
ast::ModuleItem::MacroCall(it) => {
266+
self.add_macro(current_module, it);
267+
return;
268+
}
268269
};
269270
if let Some(name) = name {
270271
let name = name.as_name();

crates/ra_hir_expand/src/ast_id_map.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,6 @@ impl AstIdMap {
6868
bfs(node, |it| {
6969
if let Some(module_item) = ast::ModuleItem::cast(it.clone()) {
7070
res.alloc(module_item.syntax());
71-
} else if let Some(macro_call) = ast::MacroCall::cast(it) {
72-
res.alloc(macro_call.syntax());
7371
}
7472
});
7573
res

crates/ra_syntax/src/ast/generated.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4135,6 +4135,7 @@ pub enum ModuleItem {
41354135
ConstDef(ConstDef),
41364136
StaticDef(StaticDef),
41374137
Module(Module),
4138+
MacroCall(MacroCall),
41384139
}
41394140
impl From<StructDef> for ModuleItem {
41404141
fn from(node: StructDef) -> ModuleItem {
@@ -4196,6 +4197,11 @@ impl From<Module> for ModuleItem {
41964197
ModuleItem::Module(node)
41974198
}
41984199
}
4200+
impl From<MacroCall> for ModuleItem {
4201+
fn from(node: MacroCall) -> ModuleItem {
4202+
ModuleItem::MacroCall(node)
4203+
}
4204+
}
41994205
impl std::fmt::Display for ModuleItem {
42004206
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
42014207
std::fmt::Display::fmt(self.syntax(), f)
@@ -4205,7 +4211,7 @@ impl AstNode for ModuleItem {
42054211
fn can_cast(kind: SyntaxKind) -> bool {
42064212
match kind {
42074213
STRUCT_DEF | UNION_DEF | ENUM_DEF | FN_DEF | TRAIT_DEF | TYPE_ALIAS_DEF | IMPL_DEF
4208-
| USE_ITEM | EXTERN_CRATE_ITEM | CONST_DEF | STATIC_DEF | MODULE => true,
4214+
| USE_ITEM | EXTERN_CRATE_ITEM | CONST_DEF | STATIC_DEF | MODULE | MACRO_CALL => true,
42094215
_ => false,
42104216
}
42114217
}
@@ -4223,6 +4229,7 @@ impl AstNode for ModuleItem {
42234229
CONST_DEF => ModuleItem::ConstDef(ConstDef { syntax }),
42244230
STATIC_DEF => ModuleItem::StaticDef(StaticDef { syntax }),
42254231
MODULE => ModuleItem::Module(Module { syntax }),
4232+
MACRO_CALL => ModuleItem::MacroCall(MacroCall { syntax }),
42264233
_ => return None,
42274234
};
42284235
Some(res)
@@ -4241,6 +4248,7 @@ impl AstNode for ModuleItem {
42414248
ModuleItem::ConstDef(it) => &it.syntax,
42424249
ModuleItem::StaticDef(it) => &it.syntax,
42434250
ModuleItem::Module(it) => &it.syntax,
4251+
ModuleItem::MacroCall(it) => &it.syntax,
42444252
}
42454253
}
42464254
}

crates/ra_syntax/src/ast/traits.rs

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ use itertools::Itertools;
66

77
use crate::{
88
ast::{self, child_opt, children, AstChildren, AstNode, AstToken},
9-
match_ast,
10-
syntax_node::{SyntaxElementChildren, SyntaxNodeChildren},
9+
syntax_node::SyntaxElementChildren,
1110
};
1211

1312
pub trait TypeAscriptionOwner: AstNode {
@@ -46,38 +45,10 @@ pub trait FnDefOwner: AstNode {
4645
}
4746
}
4847

49-
#[derive(Debug, Clone, PartialEq, Eq)]
50-
pub enum ItemOrMacro {
51-
Item(ast::ModuleItem),
52-
Macro(ast::MacroCall),
53-
}
54-
5548
pub trait ModuleItemOwner: AstNode {
5649
fn items(&self) -> AstChildren<ast::ModuleItem> {
5750
children(self)
5851
}
59-
fn items_with_macros(&self) -> ItemOrMacroIter {
60-
ItemOrMacroIter(self.syntax().children())
61-
}
62-
}
63-
64-
#[derive(Debug)]
65-
pub struct ItemOrMacroIter(SyntaxNodeChildren);
66-
67-
impl Iterator for ItemOrMacroIter {
68-
type Item = ItemOrMacro;
69-
fn next(&mut self) -> Option<ItemOrMacro> {
70-
loop {
71-
let n = self.0.next()?;
72-
match_ast! {
73-
match n {
74-
ast::ModuleItem(it) => { return Some(ItemOrMacro::Item(it)) },
75-
ast::MacroCall(it) => { return Some(ItemOrMacro::Macro(it)) },
76-
_ => {},
77-
}
78-
}
79-
}
80-
}
8152
}
8253

8354
pub trait TypeParamsOwner: AstNode {

xtask/src/ast_src.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,7 @@ pub(crate) const AST_SRC: AstSrc = AstSrc {
566566
ConstDef,
567567
StaticDef,
568568
Module,
569+
MacroCall,
569570
}
570571

571572
enum ImplItem: AttrsOwner {

0 commit comments

Comments
 (0)