Skip to content
Merged
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
108 changes: 70 additions & 38 deletions rust/extractor/src/translate/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ use ra_ap_syntax::{
#[macro_export]
macro_rules! pre_emit {
(Item, $self:ident, $node:ident) => {
$self.setup_item_expansion($node);
if let Some(label) = $self.prepare_item_expansion($node) {
return Some(label);
}
};
($($_:tt)*) => {};
}
Expand Down Expand Up @@ -687,6 +689,9 @@ impl<'a> Translator<'a> {
{
return true;
}
if syntax.kind() == SyntaxKind::TOKEN_TREE {
return true;
}
}
false
}
Expand Down Expand Up @@ -723,48 +728,75 @@ impl<'a> Translator<'a> {
}
}

pub(crate) fn setup_item_expansion(&mut self, node: &ast::Item) {
if self.semantics.is_some_and(|s| {
let file = s.hir_file_for(node.syntax());
let node = InFile::new(file, node);
s.is_attr_macro_call(node)
}) {
pub(crate) fn prepare_item_expansion(
&mut self,
node: &ast::Item,
) -> Option<Label<generated::Item>> {
if self.source_kind == SourceKind::Library {
// if the item expands via an attribute macro, we want to only emit the expansion
if let Some(expanded) = self.emit_attribute_macro_expansion(node) {
Copy link

Copilot AI May 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

emit_attribute_macro_expansion is invoked both in prepare_item_expansion and again in emit_item_expansion, which may lead to duplicate work or inconsistent emission. Consider caching the result or ensuring it's only called once.

Suggested change
if let Some(expanded) = self.emit_attribute_macro_expansion(node) {
let node_id = node.syntax().text_range().start().into(); // Use a unique identifier for the node
if let Some(expanded) = self.macro_expansion_cache.get(&node_id).cloned()
.or_else(|| {
let result = self.emit_attribute_macro_expansion(node);
if let Some(ref expansion) = result {
self.macro_expansion_cache.insert(node_id, expansion.clone());
}
result
})
{

Copilot uses AI. Check for mistakes.
// we wrap it in a dummy MacroCall to get a single Item label that can replace
// the original Item
let label = self.trap.emit(generated::MacroCall {
id: TrapId::Star,
attrs: vec![],
path: None,
token_tree: None,
});
generated::MacroCall::emit_macro_call_expansion(
label,
expanded.into(),
&mut self.trap.writer,
);
return Some(label.into());
}
}
let semantics = self.semantics.as_ref()?;
let file = semantics.hir_file_for(node.syntax());
let node = InFile::new(file, node);
if semantics.is_attr_macro_call(node) {
self.macro_context_depth += 1;
}
None
}

fn emit_attribute_macro_expansion(
&mut self,
node: &ast::Item,
) -> Option<Label<generated::MacroItems>> {
let semantics = self.semantics?;
let file = semantics.hir_file_for(node.syntax());
let infile_node = InFile::new(file, node);
if !semantics.is_attr_macro_call(infile_node) {
return None;
}
self.macro_context_depth -= 1;
if self.macro_context_depth > 0 {
// only expand the outermost attribute macro
return None;
}
let ExpandResult {
value: expanded, ..
} = semantics.expand_attr_macro(node)?;
self.emit_macro_expansion_parse_errors(node, &expanded);
let macro_items = ast::MacroItems::cast(expanded).or_else(|| {
let message = "attribute macro expansion cannot be cast to MacroItems".to_owned();
let location = self.location_for_node(node);
self.emit_diagnostic(
DiagnosticSeverity::Warning,
"item_expansion".to_owned(),
message.clone(),
message,
location.unwrap_or(UNKNOWN_LOCATION),
);
None
})?;
self.emit_macro_items(&macro_items)
}

pub(crate) fn emit_item_expansion(&mut self, node: &ast::Item, label: Label<generated::Item>) {
(|| {
let semantics = self.semantics?;
let file = semantics.hir_file_for(node.syntax());
let infile_node = InFile::new(file, node);
if !semantics.is_attr_macro_call(infile_node) {
return None;
}
self.macro_context_depth -= 1;
if self.macro_context_depth > 0 {
// only expand the outermost attribute macro
return None;
}
let ExpandResult {
value: expanded, ..
} = semantics.expand_attr_macro(node)?;
self.emit_macro_expansion_parse_errors(node, &expanded);
let macro_items = ast::MacroItems::cast(expanded).or_else(|| {
let message = "attribute macro expansion cannot be cast to MacroItems".to_owned();
let location = self.location_for_node(node);
self.emit_diagnostic(
DiagnosticSeverity::Warning,
"item_expansion".to_owned(),
message.clone(),
message,
location.unwrap_or(UNKNOWN_LOCATION),
);
None
})?;
let expanded = self.emit_macro_items(&macro_items)?;
if let Some(expanded) = self.emit_attribute_macro_expansion(node) {
generated::Item::emit_attribute_macro_expansion(label, expanded, &mut self.trap.writer);
Some(())
})();
}
}
}