diff options
author | Jonas Schievink <[email protected]> | 2020-12-03 17:38:05 +0000 |
---|---|---|
committer | Jonas Schievink <[email protected]> | 2020-12-03 17:38:05 +0000 |
commit | 3e6ffa5124c620ed1e8d47b676d5fb3300176d24 (patch) | |
tree | e891fb7e51b71893418c1eea48b6c619f07717df /crates/hir_expand/src | |
parent | d46fce88f5af1a97888edf91df2cb51ff5bfd61c (diff) |
Fix proc macro token mapping
Diffstat (limited to 'crates/hir_expand/src')
-rw-r--r-- | crates/hir_expand/src/lib.rs | 22 |
1 files changed, 16 insertions, 6 deletions
diff --git a/crates/hir_expand/src/lib.rs b/crates/hir_expand/src/lib.rs index 6dad2507b..2633fd8f7 100644 --- a/crates/hir_expand/src/lib.rs +++ b/crates/hir_expand/src/lib.rs | |||
@@ -143,7 +143,11 @@ impl HirFileId { | |||
143 | let loc: MacroCallLoc = db.lookup_intern_macro(lazy_id); | 143 | let loc: MacroCallLoc = db.lookup_intern_macro(lazy_id); |
144 | 144 | ||
145 | let arg_tt = loc.kind.arg(db)?; | 145 | let arg_tt = loc.kind.arg(db)?; |
146 | let def_tt = loc.def.ast_id?.to_node(db).token_tree()?; | 146 | |
147 | let def = loc.def.ast_id.and_then(|id| { | ||
148 | let def_tt = id.to_node(db).token_tree()?; | ||
149 | Some(InFile::new(id.file_id, def_tt)) | ||
150 | }); | ||
147 | 151 | ||
148 | let macro_def = db.macro_def(loc.def)?; | 152 | let macro_def = db.macro_def(loc.def)?; |
149 | let (parse, exp_map) = db.parse_macro_expansion(macro_file).value?; | 153 | let (parse, exp_map) = db.parse_macro_expansion(macro_file).value?; |
@@ -152,7 +156,7 @@ impl HirFileId { | |||
152 | Some(ExpansionInfo { | 156 | Some(ExpansionInfo { |
153 | expanded: InFile::new(self, parse.syntax_node()), | 157 | expanded: InFile::new(self, parse.syntax_node()), |
154 | arg: InFile::new(loc.kind.file_id(), arg_tt), | 158 | arg: InFile::new(loc.kind.file_id(), arg_tt), |
155 | def: InFile::new(loc.def.ast_id?.file_id, def_tt), | 159 | def, |
156 | macro_arg, | 160 | macro_arg, |
157 | macro_def, | 161 | macro_def, |
158 | exp_map, | 162 | exp_map, |
@@ -311,7 +315,8 @@ pub struct EagerCallLoc { | |||
311 | pub struct ExpansionInfo { | 315 | pub struct ExpansionInfo { |
312 | expanded: InFile<SyntaxNode>, | 316 | expanded: InFile<SyntaxNode>, |
313 | arg: InFile<SyntaxNode>, | 317 | arg: InFile<SyntaxNode>, |
314 | def: InFile<ast::TokenTree>, | 318 | /// The `macro_rules!` arguments. |
319 | def: Option<InFile<ast::TokenTree>>, | ||
315 | 320 | ||
316 | macro_def: Arc<(db::TokenExpander, mbe::TokenMap)>, | 321 | macro_def: Arc<(db::TokenExpander, mbe::TokenMap)>, |
317 | macro_arg: Arc<(tt::Subtree, mbe::TokenMap)>, | 322 | macro_arg: Arc<(tt::Subtree, mbe::TokenMap)>, |
@@ -348,9 +353,14 @@ impl ExpansionInfo { | |||
348 | let (token_id, origin) = self.macro_def.0.map_id_up(token_id); | 353 | let (token_id, origin) = self.macro_def.0.map_id_up(token_id); |
349 | let (token_map, tt) = match origin { | 354 | let (token_map, tt) = match origin { |
350 | mbe::Origin::Call => (&self.macro_arg.1, self.arg.clone()), | 355 | mbe::Origin::Call => (&self.macro_arg.1, self.arg.clone()), |
351 | mbe::Origin::Def => { | 356 | mbe::Origin::Def => ( |
352 | (&self.macro_def.1, self.def.as_ref().map(|tt| tt.syntax().clone())) | 357 | &self.macro_def.1, |
353 | } | 358 | self.def |
359 | .as_ref() | ||
360 | .expect("`Origin::Def` used with non-`macro_rules!` macro") | ||
361 | .as_ref() | ||
362 | .map(|tt| tt.syntax().clone()), | ||
363 | ), | ||
354 | }; | 364 | }; |
355 | 365 | ||
356 | let range = token_map.range_by_token(token_id)?.by_kind(token.value.kind())?; | 366 | let range = token_map.range_by_token(token_id)?.by_kind(token.value.kind())?; |