Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ use_repo(
"vendor_ts__ra_ap_span-0.0.328",
"vendor_ts__ra_ap_stdx-0.0.328",
"vendor_ts__ra_ap_syntax-0.0.328",
"vendor_ts__ra_ap_syntax-bridge-0.0.328",
"vendor_ts__ra_ap_vfs-0.0.328",
"vendor_ts__rand-0.10.1",
"vendor_ts__rayon-1.12.0",
Expand Down
12 changes: 12 additions & 0 deletions misc/bazel/3rdparty/tree_sitter_extractors_deps/BUILD.bazel

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions misc/bazel/3rdparty/tree_sitter_extractors_deps/defs.bzl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions rust/extractor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ ra_ap_load-cargo = "0.0.328"
ra_ap_paths = "0.0.328"
ra_ap_project_model = "0.0.328"
ra_ap_syntax = "0.0.328"
ra_ap_syntax-bridge = "0.0.328"
ra_ap_vfs = "0.0.328"
ra_ap_parser = "0.0.328"
ra_ap_span = "0.0.328"
Expand Down
105 changes: 94 additions & 11 deletions rust/extractor/src/translate/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@ use crate::trap::{Label, TrapClass};
use ra_ap_base_db::EditionedFileId;
use ra_ap_hir::Semantics;
use ra_ap_hir::db::ExpandDatabase;
use ra_ap_hir_expand::{ExpandResult, ExpandTo, InFile};
use ra_ap_hir_expand::builtin::{BuiltinDeriveExpander, find_builtin_derive};
use ra_ap_hir_expand::span_map::ExpansionSpanMap;
use ra_ap_hir_expand::{ExpandResult, ExpandTo, InFile, map_node_range_up_rooted};
use ra_ap_ide_db::RootDatabase;
use ra_ap_ide_db::line_index::{LineCol, LineIndex};
use ra_ap_parser::SyntaxKind;
use ra_ap_parser::{SyntaxKind, TopEntryPoint};
use ra_ap_span::TextSize;
use ra_ap_syntax::ast::HasAttrs;
use ra_ap_syntax::{
AstNode, NodeOrToken, SyntaxElementChildren, SyntaxError, SyntaxNode, SyntaxToken, TextRange,
ast,
};
use ra_ap_syntax_bridge::{DocCommentDesugarMode, syntax_node_to_token_tree, token_tree_to_syntax_node};

impl Emission<ast::Item> for Translator<'_> {
fn pre_emit(&mut self, node: &ast::Item) -> Option<Label<generated::Item>> {
Expand Down Expand Up @@ -128,6 +131,10 @@ pub struct Translator<'a> {
source_kind: SourceKind,
pub(crate) macro_context_depth: usize,
diagnostic_count: usize,
/// When emitting a reconstructed built-in derive expansion, holds the span map of the
/// synthesized syntax tree. Those nodes are not registered in the semantics cache, so
/// locations are resolved through this map instead of `Semantics::original_range`.
builtin_derive_span_map: Option<ExpansionSpanMap>,
}

const UNKNOWN_LOCATION: (LineCol, LineCol) =
Expand All @@ -154,6 +161,7 @@ impl<'a> Translator<'a> {
source_kind,
macro_context_depth: 0,
diagnostic_count: 0,
builtin_derive_span_map: None,
}
}
fn location(&self, range: TextRange) -> Option<(LineCol, LineCol)> {
Expand All @@ -177,6 +185,15 @@ impl<'a> Translator<'a> {
}

pub fn text_range_for_node(&mut self, node: &impl ast::AstNode) -> Option<TextRange> {
if let Some(span_map) = self.builtin_derive_span_map.as_ref() {
// Nodes synthesized by a reconstructed built-in derive expansion are not in the
// semantics cache; resolve their original source range through the expansion span map.
let semantics = self.semantics.as_ref()?;
let file_id = self.file_id?;
let file_range =
map_node_range_up_rooted(semantics.db, span_map, node.syntax().text_range())?;
return (file_id == file_range.file_id).then_some(file_range.range);
}
if let Some(semantics) = self.semantics.as_ref() {
let file_range = semantics.original_range(node.syntax());
let file_id = self.file_id?;
Expand Down Expand Up @@ -411,6 +428,11 @@ impl<'a> Translator<'a> {
// way as from version 0.0.274 rust-analyser only expands in the context of an expansion
return;
}
if self.builtin_derive_span_map.is_some() {
// inside a reconstructed built-in derive expansion the macro call is not registered in
// the semantics cache, so it cannot (and need not) be expanded further
return;
}
if let Some(expanded) = self
.semantics
.as_ref()
Expand Down Expand Up @@ -582,7 +604,8 @@ impl<'a> Translator<'a> {
fn is_attribute_macro_target(&self, node: &ast::Item) -> bool {
// rust-analyzer considers as an `attr_macro_call` also a plain macro call, but we want to
// process that differently (in `extract_macro_call_expanded`)
!matches!(node, ast::Item::MacroCall(_))
self.builtin_derive_span_map.is_none()
&& !matches!(node, ast::Item::MacroCall(_))
&& self.semantics.is_some_and(|semantics| {
let file = semantics.hir_file_for(node.syntax());
let node = InFile::new(file, node);
Expand Down Expand Up @@ -703,6 +726,51 @@ impl<'a> Translator<'a> {
}
}

/// Reconstructs the expansion of a built-in derive macro (e.g. `Debug`, `PartialEq`).
///
/// Since rust-analyzer 0.0.317 built-in derives are modeled as synthetic impls rather than
/// syntactic macro expansions, `Semantics::expand_derive_macro` no longer returns anything for
/// them. We re-run the still-public built-in derive expander over the ADT and emit the resulting
/// `impl` items ourselves. The synthesized nodes are not registered in the semantics
/// cache, so `builtin_derive_span_map` is set for the duration of the emission to route their
/// locations through the expansion span map.
fn emit_builtin_derive_expansion(
&mut self,
adt: &ast::Adt,
expander: BuiltinDeriveExpander,
) -> Option<Label<generated::MacroItems>> {
let semantics = self.semantics?;
let db = semantics.db;
let file_id = semantics.hir_file_for(adt.syntax());
let span_map = db.span_map(file_id);
let call_site = span_map.span_for_range(adt.syntax().text_range());
let input = syntax_node_to_token_tree(
adt.syntax(),
span_map.as_ref(),
call_site,
DocCommentDesugarMode::ProcMacro,
Comment on lines +747 to +751
);
let ExpandResult { value: output, err } = expander.expander()(db, call_site, &input);
let edition = self.file_id?.edition(db);
let (parsed, output_span_map) =
token_tree_to_syntax_node(&output, TopEntryPoint::MacroItems, &mut |_| edition);
let items = ast::MacroItems::cast(parsed.syntax_node())?;
if let Some(err) = err {
let rendered = err.render_to_string(db);
self.emit_diagnostic_for_node(
adt,
DiagnosticSeverity::Warning,
"item_expansion".to_owned(),
format!("built-in derive expansion failed ({})", rendered.kind),
rendered.message,
);
}
let previous = self.builtin_derive_span_map.replace(output_span_map);
let result = self.emit_macro_items(&items);
self.builtin_derive_span_map = previous;
result
}

pub(crate) fn emit_derive_expansion(
&mut self,
node: &(impl Into<ast::Adt> + Clone),
Expand All @@ -712,14 +780,29 @@ impl<'a> Translator<'a> {
return;
};
let node: ast::Adt = node.clone().into();
let expansions = node
.attrs()
.filter_map(|attr| attr.meta())
.filter_map(|meta| semantics.expand_derive_macro(&meta))
.flatten()
.flatten()
.filter_map(|expanded| self.process_item_macro_expansion(&node, expanded))
.collect::<Vec<_>>();
let mut expansions = Vec::new();
for meta in node.attrs().filter_map(|attr| attr.meta()) {
let Some(expanded) = semantics.expand_derive_macro(&meta) else {
continue;
};
// `resolve_derive_macro` yields one entry per derive, including built-in ones for which
// `expand_derive_macro` returns `None`; we use it to recover the built-in expander.
let resolved = semantics.resolve_derive_macro(&meta).unwrap_or_default();
for (i, expanded) in expanded.into_iter().enumerate() {
let label = if let Some(expanded) = expanded {
self.process_item_macro_expansion(&node, expanded)
} else if let Some(expander) = resolved
.get(i)
.and_then(|m| m.as_ref())
.and_then(|m| find_builtin_derive(&m.name(semantics.db)))
{
self.emit_builtin_derive_expansion(&node, expander)
} else {
None
};
expansions.extend(label);
}
}
generated::TypeItem::emit_derive_macro_expansions(
label.into(),
expansions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
| {EXTERNAL LOCATION} | fn trim | <core::str>::trim |
| lib.rs:1:1:1:18 | mod anonymous | test::anonymous |
| lib.rs:2:1:2:16 | mod regular | test::regular |
| regular.rs:1:1:2:18 | fn eq | <test::regular::Struct as core::cmp::PartialEq>::eq |
| regular.rs:1:1:2:18 | impl ...::Eq for Struct::<...> { ... } | <test::regular::Struct as core::cmp::Eq> |
| regular.rs:1:1:2:18 | impl ...::PartialEq for Struct::<...> { ... } | <test::regular::Struct as core::cmp::PartialEq> |
| regular.rs:1:1:2:18 | struct Struct | test::regular::Struct |
| regular.rs:4:1:6:1 | trait Trait | test::regular::Trait |
| regular.rs:5:5:5:16 | fn f | <_ as test::regular::Trait>::f |
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
instances
| gen_macro_items.rs:5:5:5:12 | MacroItems |
| gen_macro_items.rs:12:5:13:15 | MacroItems |
getItem
| gen_macro_items.rs:5:5:5:12 | MacroItems | 0 | gen_macro_items.rs:5:5:5:38 | use ...::Path |
| gen_macro_items.rs:5:5:5:12 | MacroItems | 1 | gen_macro_items.rs:5:5:5:38 | fn get_parent |
| gen_macro_items.rs:12:5:13:15 | MacroItems | 0 | gen_macro_items.rs:12:5:13:15 | impl ...::Debug for Bar::<...> { ... } |
Loading
Loading