Skip to content

Commit ec2bdd3

Browse files
Merge #6712
6712: Fix proc macro token mapping r=jonas-schievink a=jonas-schievink Diagnostics inside proc macros are currently incorrectly placed at their original offset, but inside the containing file. This fixes that, by allowing the creation of `ExpansionInfo` from non-`macro_rules!` macro invocations. bors r+ Co-authored-by: Jonas Schievink <[email protected]>
2 parents d46fce8 + 3e6ffa5 commit ec2bdd3

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

crates/hir_def/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ impl AsMacroCall for AstIdWithPath<ast::MacroCall> {
521521
error_sink: &mut dyn FnMut(mbe::ExpandError),
522522
) -> Option<MacroCallId> {
523523
let def: MacroDefId = resolver(self.path.clone()).or_else(|| {
524-
error_sink(mbe::ExpandError::Other("could not resolve macro".into()));
524+
error_sink(mbe::ExpandError::Other(format!("could not resolve macro `{}`", self.path)));
525525
None
526526
})?;
527527

@@ -556,7 +556,7 @@ impl AsMacroCall for AstIdWithPath<ast::Item> {
556556
error_sink: &mut dyn FnMut(mbe::ExpandError),
557557
) -> Option<MacroCallId> {
558558
let def: MacroDefId = resolver(self.path.clone()).or_else(|| {
559-
error_sink(mbe::ExpandError::Other("could not resolve macro".into()));
559+
error_sink(mbe::ExpandError::Other(format!("could not resolve macro `{}`", self.path)));
560560
None
561561
})?;
562562

crates/hir_expand/src/lib.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,11 @@ impl HirFileId {
143143
let loc: MacroCallLoc = db.lookup_intern_macro(lazy_id);
144144

145145
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+
});
147151

148152
let macro_def = db.macro_def(loc.def)?;
149153
let (parse, exp_map) = db.parse_macro_expansion(macro_file).value?;
@@ -152,7 +156,7 @@ impl HirFileId {
152156
Some(ExpansionInfo {
153157
expanded: InFile::new(self, parse.syntax_node()),
154158
arg: InFile::new(loc.kind.file_id(), arg_tt),
155-
def: InFile::new(loc.def.ast_id?.file_id, def_tt),
159+
def,
156160
macro_arg,
157161
macro_def,
158162
exp_map,
@@ -311,7 +315,8 @@ pub struct EagerCallLoc {
311315
pub struct ExpansionInfo {
312316
expanded: InFile<SyntaxNode>,
313317
arg: InFile<SyntaxNode>,
314-
def: InFile<ast::TokenTree>,
318+
/// The `macro_rules!` arguments.
319+
def: Option<InFile<ast::TokenTree>>,
315320

316321
macro_def: Arc<(db::TokenExpander, mbe::TokenMap)>,
317322
macro_arg: Arc<(tt::Subtree, mbe::TokenMap)>,
@@ -348,9 +353,14 @@ impl ExpansionInfo {
348353
let (token_id, origin) = self.macro_def.0.map_id_up(token_id);
349354
let (token_map, tt) = match origin {
350355
mbe::Origin::Call => (&self.macro_arg.1, self.arg.clone()),
351-
mbe::Origin::Def => {
352-
(&self.macro_def.1, self.def.as_ref().map(|tt| tt.syntax().clone()))
353-
}
356+
mbe::Origin::Def => (
357+
&self.macro_def.1,
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+
),
354364
};
355365

356366
let range = token_map.range_by_token(token_id)?.by_kind(token.value.kind())?;

0 commit comments

Comments
 (0)