mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-29 13:25:09 +00:00
Auto merge of #12691 - Veykril:proc-macro-diag, r=Veykril
fix: Fix unresolved proc macro diagnostics pointing to macro expansions Fixes https://github.com/rust-lang/rust-analyzer/issues/12657
This commit is contained in:
commit
fbba1d7acb
5 changed files with 63 additions and 41 deletions
|
@ -778,36 +778,19 @@ impl<'a> InFile<&'a SyntaxNode> {
|
|||
/// For attributes and derives, this will point back to the attribute only.
|
||||
/// For the entire item `InFile::use original_file_range_full`.
|
||||
pub fn original_file_range(self, db: &dyn db::AstDatabase) -> FileRange {
|
||||
if let Some(res) = self.original_file_range_opt(db) {
|
||||
return res;
|
||||
}
|
||||
|
||||
// Fall back to whole macro call.
|
||||
match self.file_id.0 {
|
||||
HirFileIdRepr::FileId(file_id) => FileRange { file_id, range: self.value.text_range() },
|
||||
HirFileIdRepr::MacroFile(mac_file) => {
|
||||
if let Some(res) = self.original_file_range_opt(db) {
|
||||
return res;
|
||||
}
|
||||
// Fall back to whole macro call.
|
||||
let loc = db.lookup_intern_macro_call(mac_file.macro_call_id);
|
||||
loc.kind.original_call_range(db)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Falls back to the macro call range if the node cannot be mapped up fully.
|
||||
pub fn original_file_range_full(self, db: &dyn db::AstDatabase) -> FileRange {
|
||||
if let Some(res) = self.original_file_range_opt(db) {
|
||||
return res;
|
||||
}
|
||||
|
||||
// Fall back to whole macro call.
|
||||
match self.file_id.0 {
|
||||
HirFileIdRepr::FileId(file_id) => FileRange { file_id, range: self.value.text_range() },
|
||||
HirFileIdRepr::MacroFile(mac_file) => {
|
||||
let loc = db.lookup_intern_macro_call(mac_file.macro_call_id);
|
||||
loc.kind.original_call_range_with_body(db)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Attempts to map the syntax node back up its macro calls.
|
||||
pub fn original_file_range_opt(self, db: &dyn db::AstDatabase) -> Option<FileRange> {
|
||||
match ascend_node_border_tokens(db, self) {
|
||||
|
@ -834,6 +817,49 @@ impl InFile<SyntaxToken> {
|
|||
let expansion = self.file_id.expansion_info(db)?;
|
||||
expansion.map_token_up(db, self.as_ref()).map(|(it, _)| it)
|
||||
}
|
||||
|
||||
/// Falls back to the macro call range if the node cannot be mapped up fully.
|
||||
pub fn original_file_range(self, db: &dyn db::AstDatabase) -> FileRange {
|
||||
match self.file_id.0 {
|
||||
HirFileIdRepr::FileId(file_id) => FileRange { file_id, range: self.value.text_range() },
|
||||
HirFileIdRepr::MacroFile(mac_file) => {
|
||||
if let Some(res) = self.original_file_range_opt(db) {
|
||||
return res;
|
||||
}
|
||||
// Fall back to whole macro call.
|
||||
let loc = db.lookup_intern_macro_call(mac_file.macro_call_id);
|
||||
loc.kind.original_call_range(db)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Attempts to map the syntax node back up its macro calls.
|
||||
pub fn original_file_range_opt(self, db: &dyn db::AstDatabase) -> Option<FileRange> {
|
||||
match self.file_id.0 {
|
||||
HirFileIdRepr::FileId(file_id) => {
|
||||
Some(FileRange { file_id, range: self.value.text_range() })
|
||||
}
|
||||
HirFileIdRepr::MacroFile(_) => {
|
||||
let expansion = self.file_id.expansion_info(db)?;
|
||||
let InFile { file_id, value } = ascend_call_token(db, &expansion, self)?;
|
||||
let original_file = file_id.original_file(db);
|
||||
if file_id != original_file.into() {
|
||||
return None;
|
||||
}
|
||||
Some(FileRange { file_id: original_file, range: value.text_range() })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn ancestors_with_macros(
|
||||
self,
|
||||
db: &dyn db::AstDatabase,
|
||||
) -> impl Iterator<Item = InFile<SyntaxNode>> + '_ {
|
||||
self.value.parent().into_iter().flat_map({
|
||||
let file_id = self.file_id;
|
||||
move |parent| InFile::new(file_id, &parent).ancestors_with_macros(db)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn ascend_node_border_tokens(
|
||||
|
@ -867,18 +893,6 @@ fn ascend_call_token(
|
|||
None
|
||||
}
|
||||
|
||||
impl InFile<SyntaxToken> {
|
||||
pub fn ancestors_with_macros(
|
||||
self,
|
||||
db: &dyn db::AstDatabase,
|
||||
) -> impl Iterator<Item = InFile<SyntaxNode>> + '_ {
|
||||
self.value.parent().into_iter().flat_map({
|
||||
let file_id = self.file_id;
|
||||
move |parent| InFile::new(file_id, &parent).ancestors_with_macros(db)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<N: AstNode> InFile<N> {
|
||||
pub fn descendants<T: AstNode>(self) -> impl Iterator<Item = InFile<T>> {
|
||||
self.value.syntax().descendants().filter_map(T::cast).map(move |n| self.with_value(n))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue