mirror of
https://github.com/rust-lang/rust.git
synced 2024-12-09 23:24:12 +00:00
Shrink diagnostic spans for errors inside macros
This commit is contained in:
parent
67f548d8e9
commit
3dd5e273b6
@ -350,6 +350,53 @@ impl MacroCallKind {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns the original file range that best describes the location of this macro call.
|
||||||
|
///
|
||||||
|
/// Here we try to roughly match what rustc does to improve diagnostics: fn-like macros
|
||||||
|
/// get the whole `ast::MacroCall`, attribute macros get the attribute's range, and derives
|
||||||
|
/// get only the specific derive that is being referred to.
|
||||||
|
pub fn original_call_range(self, db: &dyn db::AstDatabase) -> FileRange {
|
||||||
|
let mut kind = self;
|
||||||
|
loop {
|
||||||
|
match kind.file_id().0 {
|
||||||
|
HirFileIdRepr::MacroFile(file) => {
|
||||||
|
kind = db.lookup_intern_macro_call(file.macro_call_id).kind;
|
||||||
|
}
|
||||||
|
_ => break,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// `call_id` is now the outermost macro call, so its location is in a real file.
|
||||||
|
let file_id = match kind.file_id().0 {
|
||||||
|
HirFileIdRepr::FileId(it) => it,
|
||||||
|
HirFileIdRepr::MacroFile(_) => unreachable!("encountered unexpected macro file"),
|
||||||
|
};
|
||||||
|
let range = match kind {
|
||||||
|
MacroCallKind::FnLike { ast_id, .. } => ast_id.to_node(db).syntax().text_range(),
|
||||||
|
MacroCallKind::Derive { ast_id, derive_attr_index, .. } => {
|
||||||
|
// FIXME: should be the range of the macro name, not the whole derive
|
||||||
|
ast_id
|
||||||
|
.to_node(db)
|
||||||
|
.doc_comments_and_attrs()
|
||||||
|
.nth(derive_attr_index as usize)
|
||||||
|
.expect("missing derive")
|
||||||
|
.expect_right("derive is a doc comment?")
|
||||||
|
.syntax()
|
||||||
|
.text_range()
|
||||||
|
}
|
||||||
|
MacroCallKind::Attr { ast_id, invoc_attr_index, .. } => ast_id
|
||||||
|
.to_node(db)
|
||||||
|
.doc_comments_and_attrs()
|
||||||
|
.nth(invoc_attr_index as usize)
|
||||||
|
.expect("missing attribute")
|
||||||
|
.expect_right("attribute macro is a doc comment?")
|
||||||
|
.syntax()
|
||||||
|
.text_range(),
|
||||||
|
};
|
||||||
|
|
||||||
|
FileRange { range, file_id }
|
||||||
|
}
|
||||||
|
|
||||||
fn arg(&self, db: &dyn db::AstDatabase) -> Option<SyntaxNode> {
|
fn arg(&self, db: &dyn db::AstDatabase) -> Option<SyntaxNode> {
|
||||||
match self {
|
match self {
|
||||||
MacroCallKind::FnLike { ast_id, .. } => {
|
MacroCallKind::FnLike { ast_id, .. } => {
|
||||||
@ -623,15 +670,13 @@ impl<'a> InFile<&'a SyntaxNode> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Fall back to whole macro call.
|
// Fall back to whole macro call.
|
||||||
let mut node = self.cloned();
|
match self.file_id.0 {
|
||||||
while let Some(call_node) = node.file_id.call_node(db) {
|
HirFileIdRepr::FileId(file_id) => FileRange { file_id, range: self.value.text_range() },
|
||||||
node = call_node;
|
HirFileIdRepr::MacroFile(mac_file) => {
|
||||||
|
let loc = db.lookup_intern_macro_call(mac_file.macro_call_id);
|
||||||
|
loc.kind.original_call_range(db)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let orig_file = node.file_id.original_file(db);
|
|
||||||
assert_eq!(node.file_id, orig_file.into());
|
|
||||||
|
|
||||||
FileRange { file_id: orig_file, range: node.value.text_range() }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Attempts to map the syntax node back up its macro calls.
|
/// Attempts to map the syntax node back up its macro calls.
|
||||||
|
@ -51,7 +51,7 @@ impl Abi {
|
|||||||
&proc_macro::bridge::server::SameThread,
|
&proc_macro::bridge::server::SameThread,
|
||||||
rustc_server::Rustc::default(),
|
rustc_server::Rustc::default(),
|
||||||
parsed_body,
|
parsed_body,
|
||||||
false,
|
true,
|
||||||
);
|
);
|
||||||
return res.map(|it| it.into_subtree()).map_err(PanicMessage::from);
|
return res.map(|it| it.into_subtree()).map_err(PanicMessage::from);
|
||||||
}
|
}
|
||||||
@ -62,7 +62,7 @@ impl Abi {
|
|||||||
&proc_macro::bridge::server::SameThread,
|
&proc_macro::bridge::server::SameThread,
|
||||||
rustc_server::Rustc::default(),
|
rustc_server::Rustc::default(),
|
||||||
parsed_body,
|
parsed_body,
|
||||||
false,
|
true,
|
||||||
);
|
);
|
||||||
return res.map(|it| it.into_subtree()).map_err(PanicMessage::from);
|
return res.map(|it| it.into_subtree()).map_err(PanicMessage::from);
|
||||||
}
|
}
|
||||||
@ -74,7 +74,7 @@ impl Abi {
|
|||||||
rustc_server::Rustc::default(),
|
rustc_server::Rustc::default(),
|
||||||
parsed_attributes,
|
parsed_attributes,
|
||||||
parsed_body,
|
parsed_body,
|
||||||
false,
|
true,
|
||||||
);
|
);
|
||||||
return res.map(|it| it.into_subtree()).map_err(PanicMessage::from);
|
return res.map(|it| it.into_subtree()).map_err(PanicMessage::from);
|
||||||
}
|
}
|
||||||
|
@ -51,7 +51,7 @@ impl Abi {
|
|||||||
&proc_macro::bridge::server::SameThread,
|
&proc_macro::bridge::server::SameThread,
|
||||||
rustc_server::Rustc::default(),
|
rustc_server::Rustc::default(),
|
||||||
parsed_body,
|
parsed_body,
|
||||||
false,
|
true,
|
||||||
);
|
);
|
||||||
return res.map(|it| it.into_subtree()).map_err(PanicMessage::from);
|
return res.map(|it| it.into_subtree()).map_err(PanicMessage::from);
|
||||||
}
|
}
|
||||||
@ -62,7 +62,7 @@ impl Abi {
|
|||||||
&proc_macro::bridge::server::SameThread,
|
&proc_macro::bridge::server::SameThread,
|
||||||
rustc_server::Rustc::default(),
|
rustc_server::Rustc::default(),
|
||||||
parsed_body,
|
parsed_body,
|
||||||
false,
|
true,
|
||||||
);
|
);
|
||||||
return res.map(|it| it.into_subtree()).map_err(PanicMessage::from);
|
return res.map(|it| it.into_subtree()).map_err(PanicMessage::from);
|
||||||
}
|
}
|
||||||
@ -74,7 +74,7 @@ impl Abi {
|
|||||||
rustc_server::Rustc::default(),
|
rustc_server::Rustc::default(),
|
||||||
parsed_attributes,
|
parsed_attributes,
|
||||||
parsed_body,
|
parsed_body,
|
||||||
false,
|
true,
|
||||||
);
|
);
|
||||||
return res.map(|it| it.into_subtree()).map_err(PanicMessage::from);
|
return res.map(|it| it.into_subtree()).map_err(PanicMessage::from);
|
||||||
}
|
}
|
||||||
|
@ -772,7 +772,6 @@ impl ast::HasLoopBody for ast::ForExpr {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl ast::HasAttrs for ast::AnyHasDocComments {}
|
impl ast::HasAttrs for ast::AnyHasDocComments {}
|
||||||
impl ast::HasDocComments for ast::Item {}
|
|
||||||
|
|
||||||
impl From<ast::Adt> for ast::Item {
|
impl From<ast::Adt> for ast::Item {
|
||||||
fn from(it: ast::Adt) -> Self {
|
fn from(it: ast::Adt) -> Self {
|
||||||
|
Loading…
Reference in New Issue
Block a user