Skip to content

Commit 3bf2416

Browse files
authored
Merge pull request #18313 from github/redsun82/rust-mute-warnings-in-uncompiled-blocks
Rust: exclude extraction of code excluded by `cfg`
2 parents 7a589c4 + b481190 commit 3bf2416

File tree

6 files changed

+910
-823
lines changed

6 files changed

+910
-823
lines changed

rust/ast-generator/src/main.rs

+18-9
Original file line numberDiff line numberDiff line change
@@ -474,10 +474,10 @@ use ra_ap_syntax::ast::{{
474474
use ra_ap_syntax::{{ast, AstNode}};
475475
476476
impl Translator<'_> {{
477-
fn emit_else_branch(&mut self, node: ast::ElseBranch) -> Label<generated::Expr> {{
477+
fn emit_else_branch(&mut self, node: ast::ElseBranch) -> Option<Label<generated::Expr>> {{
478478
match node {{
479-
ast::ElseBranch::IfExpr(inner) => self.emit_if_expr(inner).into(),
480-
ast::ElseBranch::Block(inner) => self.emit_block_expr(inner).into(),
479+
ast::ElseBranch::IfExpr(inner) => self.emit_if_expr(inner).map(Into::into),
480+
ast::ElseBranch::Block(inner) => self.emit_block_expr(inner).map(Into::into),
481481
}}
482482
}}\n"
483483
)?;
@@ -487,7 +487,7 @@ impl Translator<'_> {{
487487

488488
writeln!(
489489
buf,
490-
" pub(crate) fn emit_{}(&mut self, node: ast::{}) -> Label<generated::{}> {{",
490+
" pub(crate) fn emit_{}(&mut self, node: ast::{}) -> Option<Label<generated::{}>> {{",
491491
to_lower_snake_case(type_name),
492492
type_name,
493493
class_name
@@ -496,7 +496,7 @@ impl Translator<'_> {{
496496
for variant in &node.variants {
497497
writeln!(
498498
buf,
499-
" ast::{}::{}(inner) => self.emit_{}(inner).into(),",
499+
" ast::{}::{}(inner) => self.emit_{}(inner).map(Into::into),",
500500
type_name,
501501
variant,
502502
to_lower_snake_case(variant)
@@ -512,7 +512,7 @@ impl Translator<'_> {{
512512

513513
writeln!(
514514
buf,
515-
" pub(crate) fn emit_{}(&mut self, node: ast::{}) -> Label<generated::{}> {{",
515+
" pub(crate) fn emit_{}(&mut self, node: ast::{}) -> Option<Label<generated::{}>> {{",
516516
to_lower_snake_case(type_name),
517517
type_name,
518518
class_name
@@ -522,6 +522,15 @@ impl Translator<'_> {{
522522
continue;
523523
}
524524

525+
if field.name == "attrs" {
526+
// special case: this means the node type implements `HasAttrs`, and we want to
527+
// check whether it was not excluded by a `cfg` attribute
528+
writeln!(
529+
buf,
530+
" if self.should_be_excluded(&node) {{ return None; }}"
531+
)?;
532+
}
533+
525534
let type_name = &field.tp;
526535
let struct_field_name = &field.name;
527536
let class_field_name = property_name(&node.name, &field.name);
@@ -541,15 +550,15 @@ impl Translator<'_> {{
541550
} else if field.is_many {
542551
writeln!(
543552
buf,
544-
" let {} = node.{}().map(|x| self.emit_{}(x)).collect();",
553+
" let {} = node.{}().filter_map(|x| self.emit_{}(x)).collect();",
545554
class_field_name,
546555
struct_field_name,
547556
to_lower_snake_case(type_name)
548557
)?;
549558
} else {
550559
writeln!(
551560
buf,
552-
" let {} = node.{}().map(|x| self.emit_{}(x));",
561+
" let {} = node.{}().and_then(|x| self.emit_{}(x));",
553562
class_field_name,
554563
struct_field_name,
555564
to_lower_snake_case(type_name)
@@ -581,7 +590,7 @@ impl Translator<'_> {{
581590
buf,
582591
" self.emit_tokens(&node, label.into(), node.syntax().children_with_tokens());"
583592
)?;
584-
writeln!(buf, " label")?;
593+
writeln!(buf, " Some(label)")?;
585594

586595
writeln!(buf, " }}\n")?;
587596
}

rust/extractor/src/translate/base.rs

+26-17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use super::mappings::{AddressableAst, AddressableHir, PathAst};
2-
use crate::generated::MacroCall;
32
use crate::generated::{self};
43
use crate::rust_analyzer::FileSemanticInformation;
54
use crate::trap::{DiagnosticSeverity, TrapFile, TrapId};
@@ -267,22 +266,22 @@ impl<'a> Translator<'a> {
267266
expanded: SyntaxNode,
268267
) -> Option<Label<generated::AstNode>> {
269268
match expand_to {
270-
ra_ap_hir_expand::ExpandTo::Statements => {
271-
ast::MacroStmts::cast(expanded).map(|x| self.emit_macro_stmts(x).into())
272-
}
273-
ra_ap_hir_expand::ExpandTo::Items => {
274-
ast::MacroItems::cast(expanded).map(|x| self.emit_macro_items(x).into())
275-
}
269+
ra_ap_hir_expand::ExpandTo::Statements => ast::MacroStmts::cast(expanded)
270+
.and_then(|x| self.emit_macro_stmts(x))
271+
.map(Into::into),
272+
ra_ap_hir_expand::ExpandTo::Items => ast::MacroItems::cast(expanded)
273+
.and_then(|x| self.emit_macro_items(x))
274+
.map(Into::into),
276275

277-
ra_ap_hir_expand::ExpandTo::Pattern => {
278-
ast::Pat::cast(expanded).map(|x| self.emit_pat(x).into())
279-
}
280-
ra_ap_hir_expand::ExpandTo::Type => {
281-
ast::Type::cast(expanded).map(|x| self.emit_type(x).into())
282-
}
283-
ra_ap_hir_expand::ExpandTo::Expr => {
284-
ast::Expr::cast(expanded).map(|x| self.emit_expr(x).into())
285-
}
276+
ra_ap_hir_expand::ExpandTo::Pattern => ast::Pat::cast(expanded)
277+
.and_then(|x| self.emit_pat(x))
278+
.map(Into::into),
279+
ra_ap_hir_expand::ExpandTo::Type => ast::Type::cast(expanded)
280+
.and_then(|x| self.emit_type(x))
281+
.map(Into::into),
282+
ra_ap_hir_expand::ExpandTo::Expr => ast::Expr::cast(expanded)
283+
.and_then(|x| self.emit_expr(x))
284+
.map(Into::into),
286285
}
287286
}
288287
pub(crate) fn extract_macro_call_expanded(
@@ -295,7 +294,7 @@ impl<'a> Translator<'a> {
295294
let expand_to = ra_ap_hir_expand::ExpandTo::from_call_site(mcall);
296295
let kind = expanded.kind();
297296
if let Some(value) = self.emit_expanded_as(expand_to, expanded) {
298-
MacroCall::emit_expanded(label, value, &mut self.trap.writer);
297+
generated::MacroCall::emit_expanded(label, value, &mut self.trap.writer);
299298
} else {
300299
let range = self.text_range_for_node(mcall);
301300
self.emit_parse_error(mcall, &SyntaxError::new(
@@ -559,4 +558,14 @@ impl<'a> Translator<'a> {
559558
Some(())
560559
})();
561560
}
561+
562+
pub(crate) fn should_be_excluded(&self, item: &impl ast::HasAttrs) -> bool {
563+
self.semantics.is_some_and(|sema| {
564+
item.attrs().any(|attr| {
565+
attr.as_simple_call().is_some_and(|(name, tokens)| {
566+
name == "cfg" && sema.check_cfg_attr(&tokens) == Some(false)
567+
})
568+
})
569+
})
570+
}
562571
}

0 commit comments

Comments
 (0)