Skip to content

Commit 240bd28

Browse files
Merge pull request #21205 from eihqnh/fix/cfg-attr-index-mismatch
fix: Skip cfg attributes in macro input attribute stripping
2 parents 2d36bea + 013250c commit 240bd28

File tree

2 files changed

+28
-15
lines changed

2 files changed

+28
-15
lines changed

src/tools/rust-analyzer/crates/hir-def/src/macro_expansion_tests/proc_macros.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,3 +341,22 @@ struct Foo;
341341
#[helper_should_be_ignored] struct Foo;"#]],
342342
);
343343
}
344+
345+
#[test]
346+
fn attribute_macro_stripping_with_cfg() {
347+
check(
348+
r#"
349+
//- proc_macros: generate_suffixed_type
350+
#[cfg(all())]
351+
#[proc_macros::generate_suffixed_type]
352+
struct S;
353+
"#,
354+
expect![[r#"
355+
#[cfg(all())]
356+
#[proc_macros::generate_suffixed_type]
357+
struct S;
358+
359+
struct S;
360+
struct SSuffix;"#]],
361+
);
362+
}

src/tools/rust-analyzer/crates/hir-expand/src/cfg_process.rs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -162,25 +162,19 @@ fn macro_input_callback(
162162
}
163163
}
164164
Meta::TokenTree { path, tt } => {
165-
if path.segments.len() != 1
165+
if path.is1("cfg") {
166+
let cfg_expr = CfgExpr::parse_from_ast(
167+
&mut TokenTreeChildren::new(&tt).peekable(),
168+
);
169+
if cfg_options().check(&cfg_expr) == Some(false) {
170+
return ControlFlow::Break(ItemIsCfgedOut);
171+
}
172+
strip_current_attr = true;
173+
} else if path.segments.len() != 1
166174
|| !is_item_tree_filtered_attr(path.segments[0].text())
167175
{
168176
strip_current_attr = should_strip_attr();
169177
}
170-
171-
if path.segments.len() == 1 {
172-
let name = path.segments[0].text();
173-
174-
if name == "cfg" {
175-
let cfg_expr = CfgExpr::parse_from_ast(
176-
&mut TokenTreeChildren::new(&tt).peekable(),
177-
);
178-
if cfg_options().check(&cfg_expr) == Some(false) {
179-
return ControlFlow::Break(ItemIsCfgedOut);
180-
}
181-
strip_current_attr = true;
182-
}
183-
}
184178
}
185179
Meta::Path { path } => {
186180
if path.segments.len() != 1

0 commit comments

Comments
 (0)