diff options
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ra_hir_def/src/body.rs | 37 | ||||
-rw-r--r-- | crates/ra_hir_def/src/body/lower.rs | 9 |
2 files changed, 38 insertions, 8 deletions
diff --git a/crates/ra_hir_def/src/body.rs b/crates/ra_hir_def/src/body.rs index 622c836d1..3b262e3bd 100644 --- a/crates/ra_hir_def/src/body.rs +++ b/crates/ra_hir_def/src/body.rs | |||
@@ -3,7 +3,7 @@ mod lower; | |||
3 | 3 | ||
4 | use std::{ops::Index, sync::Arc}; | 4 | use std::{ops::Index, sync::Arc}; |
5 | 5 | ||
6 | use hir_expand::{either::Either, HirFileId, MacroDefId, Source}; | 6 | use hir_expand::{either::Either, hygiene::Hygiene, HirFileId, MacroDefId, Source}; |
7 | use ra_arena::{map::ArenaMap, Arena}; | 7 | use ra_arena::{map::ArenaMap, Arena}; |
8 | use ra_syntax::{ast, AstPtr}; | 8 | use ra_syntax::{ast, AstPtr}; |
9 | use rustc_hash::FxHashMap; | 9 | use rustc_hash::FxHashMap; |
@@ -20,13 +20,34 @@ pub struct Expander { | |||
20 | crate_def_map: Arc<CrateDefMap>, | 20 | crate_def_map: Arc<CrateDefMap>, |
21 | original_file_id: HirFileId, | 21 | original_file_id: HirFileId, |
22 | current_file_id: HirFileId, | 22 | current_file_id: HirFileId, |
23 | hygiene: Hygiene, | ||
23 | module: ModuleId, | 24 | module: ModuleId, |
24 | } | 25 | } |
25 | 26 | ||
26 | impl Expander { | 27 | impl Expander { |
27 | pub fn new(db: &impl DefDatabase2, current_file_id: HirFileId, module: ModuleId) -> Expander { | 28 | pub fn new(db: &impl DefDatabase2, current_file_id: HirFileId, module: ModuleId) -> Expander { |
28 | let crate_def_map = db.crate_def_map(module.krate); | 29 | let crate_def_map = db.crate_def_map(module.krate); |
29 | Expander { crate_def_map, original_file_id: current_file_id, current_file_id, module } | 30 | let hygiene = Hygiene::new(db, current_file_id); |
31 | Expander { | ||
32 | crate_def_map, | ||
33 | original_file_id: current_file_id, | ||
34 | current_file_id, | ||
35 | hygiene, | ||
36 | module, | ||
37 | } | ||
38 | } | ||
39 | |||
40 | fn enter(&mut self, db: &impl DefDatabase2, file_id: HirFileId) -> Mark { | ||
41 | let mark = Mark { file_id: self.current_file_id }; | ||
42 | self.hygiene = Hygiene::new(db, file_id); | ||
43 | self.current_file_id = file_id; | ||
44 | mark | ||
45 | } | ||
46 | |||
47 | fn exit(&mut self, db: &impl DefDatabase2, mark: Mark) { | ||
48 | self.hygiene = Hygiene::new(db, mark.file_id); | ||
49 | self.current_file_id = mark.file_id; | ||
50 | std::mem::forget(mark); | ||
30 | } | 51 | } |
31 | 52 | ||
32 | // FIXME: remove this. | 53 | // FIXME: remove this. |
@@ -43,6 +64,18 @@ impl Expander { | |||
43 | } | 64 | } |
44 | } | 65 | } |
45 | 66 | ||
67 | struct Mark { | ||
68 | file_id: HirFileId, | ||
69 | } | ||
70 | |||
71 | impl Drop for Mark { | ||
72 | fn drop(&mut self) { | ||
73 | if !std::thread::panicking() { | ||
74 | panic!("dropped mark") | ||
75 | } | ||
76 | } | ||
77 | } | ||
78 | |||
46 | /// The body of an item (function, const etc.). | 79 | /// The body of an item (function, const etc.). |
47 | #[derive(Debug, Eq, PartialEq)] | 80 | #[derive(Debug, Eq, PartialEq)] |
48 | pub struct Body { | 81 | pub struct Body { |
diff --git a/crates/ra_hir_def/src/body/lower.rs b/crates/ra_hir_def/src/body/lower.rs index 1ea8ce249..5c291421a 100644 --- a/crates/ra_hir_def/src/body/lower.rs +++ b/crates/ra_hir_def/src/body/lower.rs | |||
@@ -2,7 +2,6 @@ | |||
2 | 2 | ||
3 | use hir_expand::{ | 3 | use hir_expand::{ |
4 | either::Either, | 4 | either::Either, |
5 | hygiene::Hygiene, | ||
6 | name::{self, AsName, Name}, | 5 | name::{self, AsName, Name}, |
7 | AstId, MacroCallLoc, MacroFileKind, | 6 | AstId, MacroCallLoc, MacroFileKind, |
8 | }; | 7 | }; |
@@ -447,10 +446,9 @@ where | |||
447 | if let Some(node) = self.db.parse_or_expand(file_id) { | 446 | if let Some(node) = self.db.parse_or_expand(file_id) { |
448 | if let Some(expr) = ast::Expr::cast(node) { | 447 | if let Some(expr) = ast::Expr::cast(node) { |
449 | log::debug!("macro expansion {:#?}", expr.syntax()); | 448 | log::debug!("macro expansion {:#?}", expr.syntax()); |
450 | let old_file_id = | 449 | let mark = self.expander.enter(self.db, file_id); |
451 | std::mem::replace(&mut self.expander.current_file_id, file_id); | ||
452 | let id = self.collect_expr(expr); | 450 | let id = self.collect_expr(expr); |
453 | self.expander.current_file_id = old_file_id; | 451 | self.expander.exit(self.db, mark); |
454 | return id; | 452 | return id; |
455 | } | 453 | } |
456 | } | 454 | } |
@@ -572,8 +570,7 @@ where | |||
572 | } | 570 | } |
573 | 571 | ||
574 | fn parse_path(&mut self, path: ast::Path) -> Option<Path> { | 572 | fn parse_path(&mut self, path: ast::Path) -> Option<Path> { |
575 | let hygiene = Hygiene::new(self.db, self.expander.current_file_id); | 573 | Path::from_src(path, &self.expander.hygiene) |
576 | Path::from_src(path, &hygiene) | ||
577 | } | 574 | } |
578 | } | 575 | } |
579 | 576 | ||