Skip to content

Commit 82736ea

Browse files
committed
Rust: add diagnostics about item expansion not working properly
1 parent 6ecaf65 commit 82736ea

File tree

1 file changed

+24
-11
lines changed
  • rust/extractor/src/translate

1 file changed

+24
-11
lines changed

rust/extractor/src/translate/base.rs

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -159,21 +159,24 @@ impl<'a> Translator<'a> {
159159
Some(node.syntax().text_range())
160160
}
161161
}
162-
pub fn emit_location<T: TrapClass>(&mut self, label: Label<T>, node: &impl ast::AstNode) {
163-
if let Some((start, end)) = self
164-
.text_range_for_node(node)
162+
163+
fn location_for_node(&mut self, node: &impl ast::AstNode) -> (LineCol, LineCol) {
164+
self.text_range_for_node(node)
165165
.and_then(|r| self.location(r))
166-
{
167-
self.trap.emit_location(self.label, label, start, end)
168-
} else {
169-
self.emit_diagnostic(
166+
.unwrap_or(UNKNOWN_LOCATION)
167+
}
168+
169+
pub fn emit_location<T: TrapClass>(&mut self, label: Label<T>, node: &impl ast::AstNode) {
170+
match self.location_for_node(node) {
171+
UNKNOWN_LOCATION => self.emit_diagnostic(
170172
DiagnosticSeverity::Debug,
171173
"locations".to_owned(),
172174
"missing location for AstNode".to_owned(),
173175
"missing location for AstNode".to_owned(),
174176
UNKNOWN_LOCATION,
175-
);
176-
}
177+
),
178+
(start, end) => self.trap.emit_location(self.label, label, start, end),
179+
};
177180
}
178181
pub fn emit_location_token(
179182
&mut self,
@@ -657,9 +660,19 @@ impl<'a> Translator<'a> {
657660
let ExpandResult {
658661
value: expanded, ..
659662
} = semantics.expand_attr_macro(node)?;
660-
// TODO emit err?
661663
self.emit_macro_expansion_parse_errors(node, &expanded);
662-
let macro_items = ast::MacroItems::cast(expanded)?;
664+
let macro_items = ast::MacroItems::cast(expanded).or_else(|| {
665+
let message = "attribute macro expansion cannot be cast to MacroItems".to_owned();
666+
let location = self.location_for_node(node);
667+
self.emit_diagnostic(
668+
DiagnosticSeverity::Warning,
669+
"item_expansion".to_owned(),
670+
message.clone(),
671+
message,
672+
location,
673+
);
674+
None
675+
})?;
663676
let expanded = self.emit_macro_items(&macro_items)?;
664677
generated::Item::emit_attribute_macro_expansion(label, expanded, &mut self.trap.writer);
665678
Some(())

0 commit comments

Comments
 (0)