Merge pull request #18313 from github/redsun82/rust-mute-warnings-in-uncompiled-blocks

Rust: exclude extraction of code excluded by `cfg`
This commit is contained in:
Paolo Tranquilli
2025-01-08 17:03:29 +01:00
committed by GitHub
6 changed files with 910 additions and 823 deletions

View File

@@ -474,10 +474,10 @@ use ra_ap_syntax::ast::{{
use ra_ap_syntax::{{ast, AstNode}};
impl Translator<'_> {{
fn emit_else_branch(&mut self, node: ast::ElseBranch) -> Label<generated::Expr> {{
fn emit_else_branch(&mut self, node: ast::ElseBranch) -> Option<Label<generated::Expr>> {{
match node {{
ast::ElseBranch::IfExpr(inner) => self.emit_if_expr(inner).into(),
ast::ElseBranch::Block(inner) => self.emit_block_expr(inner).into(),
ast::ElseBranch::IfExpr(inner) => self.emit_if_expr(inner).map(Into::into),
ast::ElseBranch::Block(inner) => self.emit_block_expr(inner).map(Into::into),
}}
}}\n"
)?;
@@ -487,7 +487,7 @@ impl Translator<'_> {{
writeln!(
buf,
" pub(crate) fn emit_{}(&mut self, node: ast::{}) -> Label<generated::{}> {{",
" pub(crate) fn emit_{}(&mut self, node: ast::{}) -> Option<Label<generated::{}>> {{",
to_lower_snake_case(type_name),
type_name,
class_name
@@ -496,7 +496,7 @@ impl Translator<'_> {{
for variant in &node.variants {
writeln!(
buf,
" ast::{}::{}(inner) => self.emit_{}(inner).into(),",
" ast::{}::{}(inner) => self.emit_{}(inner).map(Into::into),",
type_name,
variant,
to_lower_snake_case(variant)
@@ -512,7 +512,7 @@ impl Translator<'_> {{
writeln!(
buf,
" pub(crate) fn emit_{}(&mut self, node: ast::{}) -> Label<generated::{}> {{",
" pub(crate) fn emit_{}(&mut self, node: ast::{}) -> Option<Label<generated::{}>> {{",
to_lower_snake_case(type_name),
type_name,
class_name
@@ -522,6 +522,15 @@ impl Translator<'_> {{
continue;
}
if field.name == "attrs" {
// special case: this means the node type implements `HasAttrs`, and we want to
// check whether it was not excluded by a `cfg` attribute
writeln!(
buf,
" if self.should_be_excluded(&node) {{ return None; }}"
)?;
}
let type_name = &field.tp;
let struct_field_name = &field.name;
let class_field_name = property_name(&node.name, &field.name);
@@ -541,7 +550,7 @@ impl Translator<'_> {{
} else if field.is_many {
writeln!(
buf,
" let {} = node.{}().map(|x| self.emit_{}(x)).collect();",
" let {} = node.{}().filter_map(|x| self.emit_{}(x)).collect();",
class_field_name,
struct_field_name,
to_lower_snake_case(type_name)
@@ -549,7 +558,7 @@ impl Translator<'_> {{
} else {
writeln!(
buf,
" let {} = node.{}().map(|x| self.emit_{}(x));",
" let {} = node.{}().and_then(|x| self.emit_{}(x));",
class_field_name,
struct_field_name,
to_lower_snake_case(type_name)
@@ -581,7 +590,7 @@ impl Translator<'_> {{
buf,
" self.emit_tokens(&node, label.into(), node.syntax().children_with_tokens());"
)?;
writeln!(buf, " label")?;
writeln!(buf, " Some(label)")?;
writeln!(buf, " }}\n")?;
}

View File

@@ -1,5 +1,4 @@
use super::mappings::{AddressableAst, AddressableHir, PathAst};
use crate::generated::MacroCall;
use crate::generated::{self};
use crate::rust_analyzer::FileSemanticInformation;
use crate::trap::{DiagnosticSeverity, TrapFile, TrapId};
@@ -267,22 +266,22 @@ impl<'a> Translator<'a> {
expanded: SyntaxNode,
) -> Option<Label<generated::AstNode>> {
match expand_to {
ra_ap_hir_expand::ExpandTo::Statements => {
ast::MacroStmts::cast(expanded).map(|x| self.emit_macro_stmts(x).into())
}
ra_ap_hir_expand::ExpandTo::Items => {
ast::MacroItems::cast(expanded).map(|x| self.emit_macro_items(x).into())
}
ra_ap_hir_expand::ExpandTo::Statements => ast::MacroStmts::cast(expanded)
.and_then(|x| self.emit_macro_stmts(x))
.map(Into::into),
ra_ap_hir_expand::ExpandTo::Items => ast::MacroItems::cast(expanded)
.and_then(|x| self.emit_macro_items(x))
.map(Into::into),
ra_ap_hir_expand::ExpandTo::Pattern => {
ast::Pat::cast(expanded).map(|x| self.emit_pat(x).into())
}
ra_ap_hir_expand::ExpandTo::Type => {
ast::Type::cast(expanded).map(|x| self.emit_type(x).into())
}
ra_ap_hir_expand::ExpandTo::Expr => {
ast::Expr::cast(expanded).map(|x| self.emit_expr(x).into())
}
ra_ap_hir_expand::ExpandTo::Pattern => ast::Pat::cast(expanded)
.and_then(|x| self.emit_pat(x))
.map(Into::into),
ra_ap_hir_expand::ExpandTo::Type => ast::Type::cast(expanded)
.and_then(|x| self.emit_type(x))
.map(Into::into),
ra_ap_hir_expand::ExpandTo::Expr => ast::Expr::cast(expanded)
.and_then(|x| self.emit_expr(x))
.map(Into::into),
}
}
pub(crate) fn extract_macro_call_expanded(
@@ -295,7 +294,7 @@ impl<'a> Translator<'a> {
let expand_to = ra_ap_hir_expand::ExpandTo::from_call_site(mcall);
let kind = expanded.kind();
if let Some(value) = self.emit_expanded_as(expand_to, expanded) {
MacroCall::emit_expanded(label, value, &mut self.trap.writer);
generated::MacroCall::emit_expanded(label, value, &mut self.trap.writer);
} else {
let range = self.text_range_for_node(mcall);
self.emit_parse_error(mcall, &SyntaxError::new(
@@ -559,4 +558,14 @@ impl<'a> Translator<'a> {
Some(())
})();
}
pub(crate) fn should_be_excluded(&self, item: &impl ast::HasAttrs) -> bool {
self.semantics.is_some_and(|sema| {
item.attrs().any(|attr| {
attr.as_simple_call().is_some_and(|(name, tokens)| {
name == "cfg" && sema.check_cfg_attr(&tokens) == Some(false)
})
})
})
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -1,5 +1,4 @@
import rust
from Function f
where f.hasExtendedCanonicalPath()
select f

View File

@@ -1,5 +1,4 @@
import rust
from Function f
where f.hasExtendedCanonicalPath()
select f

View File

@@ -1,5 +1,4 @@
import rust
from Function f
where f.hasExtendedCanonicalPath()
select f