Skip to content

Commit 3206b83

Browse files
Merge #2418
2418: Hide MacroCallLoc outside hir_expand r=matklad a=edwin0cheng This PR refactor `MacroCallLoc` such that it be hided to become implementation details of hir_expand. Co-authored-by: Edwin Cheng <edwin0cheng@gmail.com>
2 parents 141fca6 + 447268c commit 3206b83

File tree

4 files changed

+19
-13
lines changed

4 files changed

+19
-13
lines changed

crates/ra_hir/src/source_binder.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ use hir_def::{
1414
DefWithBodyId,
1515
};
1616
use hir_expand::{
17-
hygiene::Hygiene, name::AsName, AstId, HirFileId, MacroCallId, MacroCallLoc, MacroFileKind,
18-
Source,
17+
hygiene::Hygiene, name::AsName, AstId, HirFileId, MacroCallId, MacroFileKind, Source,
1918
};
2019
use ra_syntax::{
2120
ast::{self, AstNode},
@@ -451,9 +450,8 @@ impl SourceAnalyzer {
451450
macro_call.file_id,
452451
db.ast_id_map(macro_call.file_id).ast_id(macro_call.value),
453452
);
454-
let macro_call_loc = MacroCallLoc { def, ast_id };
455453
Some(Expansion {
456-
macro_call_id: db.intern_macro(macro_call_loc),
454+
macro_call_id: def.as_call_id(db, ast_id),
457455
macro_file_kind: to_macro_file_kind(macro_call.value),
458456
})
459457
}

crates/ra_hir_def/src/body.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ pub mod scope;
66
use std::{ops::Index, sync::Arc};
77

88
use hir_expand::{
9-
either::Either, hygiene::Hygiene, AstId, HirFileId, MacroCallLoc, MacroDefId, MacroFileKind,
10-
Source,
9+
either::Either, hygiene::Hygiene, AstId, HirFileId, MacroDefId, MacroFileKind, Source,
1110
};
1211
use ra_arena::{map::ArenaMap, Arena};
1312
use ra_syntax::{ast, AstNode, AstPtr};
@@ -47,7 +46,7 @@ impl Expander {
4746

4847
if let Some(path) = macro_call.path().and_then(|path| self.parse_path(path)) {
4948
if let Some(def) = self.resolve_path_as_macro(db, &path) {
50-
let call_id = db.intern_macro(MacroCallLoc { def, ast_id });
49+
let call_id = def.as_call_id(db, ast_id);
5150
let file_id = call_id.as_file(MacroFileKind::Expr);
5251
if let Some(node) = db.parse_or_expand(file_id) {
5352
if let Some(expr) = ast::Expr::cast(node) {

crates/ra_hir_def/src/nameres/collector.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use hir_expand::{
77
builtin_macro::find_builtin_macro,
88
name::{self, AsName, Name},
9-
HirFileId, MacroCallId, MacroCallLoc, MacroDefId, MacroDefKind, MacroFileKind,
9+
HirFileId, MacroCallId, MacroDefId, MacroDefKind, MacroFileKind,
1010
};
1111
use ra_cfg::CfgOptions;
1212
use ra_db::{CrateId, FileId};
@@ -480,7 +480,7 @@ where
480480
);
481481

482482
if let Some(def) = resolved_res.resolved_def.take_macros() {
483-
let call_id = self.db.intern_macro(MacroCallLoc { def, ast_id: *ast_id });
483+
let call_id = def.as_call_id(self.db, *ast_id);
484484
resolved.push((*module_id, call_id, def));
485485
res = ReachedFixedPoint::No;
486486
return false;
@@ -773,8 +773,7 @@ where
773773
if let Some(macro_def) = mac.path.as_ident().and_then(|name| {
774774
self.def_collector.def_map[self.module_id].scope.get_legacy_macro(&name)
775775
}) {
776-
let macro_call_id =
777-
self.def_collector.db.intern_macro(MacroCallLoc { def: macro_def, ast_id });
776+
let macro_call_id = macro_def.as_call_id(self.def_collector.db, ast_id);
778777

779778
self.def_collector.collect_macro_expansion(self.module_id, macro_call_id, macro_def);
780779
return;

crates/ra_hir_expand/src/lib.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,16 @@ pub struct MacroDefId {
135135
pub kind: MacroDefKind,
136136
}
137137

138+
impl MacroDefId {
139+
pub fn as_call_id(
140+
self,
141+
db: &dyn db::AstDatabase,
142+
ast_id: AstId<ast::MacroCall>,
143+
) -> MacroCallId {
144+
db.intern_macro(MacroCallLoc { def: self, ast_id })
145+
}
146+
}
147+
138148
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
139149
pub enum MacroDefKind {
140150
Declarative,
@@ -143,8 +153,8 @@ pub enum MacroDefKind {
143153

144154
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
145155
pub struct MacroCallLoc {
146-
pub def: MacroDefId,
147-
pub ast_id: AstId<ast::MacroCall>,
156+
pub(crate) def: MacroDefId,
157+
pub(crate) ast_id: AstId<ast::MacroCall>,
148158
}
149159

150160
impl MacroCallId {

0 commit comments

Comments
 (0)