Skip to content

Commit d3e6fea

Browse files
committed
Auto merge of rust-lang#16789 - wyatt-herkamp:cfg_attr, r=Veykril
cfg Attribute Stripping for Proc Macro Expansion This will attempt to process cfg attributes and cfg_attr attributes for proc macro expansion. ![image](https://github.com/rust-lang/rust-analyzer/assets/11785959/b85ef203-14a5-44c9-9b67-59a65a5f2d96) Closes rust-lang#8434 , rust-lang#11657, and rust-lang#13904
2 parents 03d2d90 + 447de3d commit d3e6fea

File tree

8 files changed

+486
-23
lines changed

8 files changed

+486
-23
lines changed

crates/cfg/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,4 @@ mbe.workspace = true
3131
syntax.workspace = true
3232

3333
[lints]
34-
workspace = true
34+
workspace = true

crates/cfg/src/cfg_expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ impl CfgExpr {
4747
pub fn parse<S>(tt: &tt::Subtree<S>) -> CfgExpr {
4848
next_cfg_expr(&mut tt.token_trees.iter()).unwrap_or(CfgExpr::Invalid)
4949
}
50+
5051
/// Fold the cfg by querying all basic `Atom` and `KeyValue` predicates.
5152
pub fn fold(&self, query: &dyn Fn(&CfgAtom) -> bool) -> Option<bool> {
5253
match self {
@@ -62,7 +63,6 @@ impl CfgExpr {
6263
}
6364
}
6465
}
65-
6666
fn next_cfg_expr<S>(it: &mut SliceIter<'_, tt::TokenTree<S>>) -> Option<CfgExpr> {
6767
let name = match it.next() {
6868
None => return None,

crates/hir-def/src/macro_expansion_tests/builtin_derive_macro.rs

+118
Original file line numberDiff line numberDiff line change
@@ -528,3 +528,121 @@ impl < > $crate::fmt::Debug for Command< > where {
528528
}"#]],
529529
);
530530
}
531+
#[test]
532+
fn test_debug_expand_with_cfg() {
533+
check(
534+
r#"
535+
//- minicore: derive, fmt
536+
use core::fmt::Debug;
537+
538+
#[derive(Debug)]
539+
struct HideAndShow {
540+
#[cfg(never)]
541+
always_hide: u32,
542+
#[cfg(not(never))]
543+
always_show: u32,
544+
}
545+
#[derive(Debug)]
546+
enum HideAndShowEnum {
547+
#[cfg(never)]
548+
AlwaysHide,
549+
#[cfg(not(never))]
550+
AlwaysShow{
551+
#[cfg(never)]
552+
always_hide: u32,
553+
#[cfg(not(never))]
554+
always_show: u32,
555+
}
556+
}
557+
"#,
558+
expect![[r#"
559+
use core::fmt::Debug;
560+
561+
#[derive(Debug)]
562+
struct HideAndShow {
563+
#[cfg(never)]
564+
always_hide: u32,
565+
#[cfg(not(never))]
566+
always_show: u32,
567+
}
568+
#[derive(Debug)]
569+
enum HideAndShowEnum {
570+
#[cfg(never)]
571+
AlwaysHide,
572+
#[cfg(not(never))]
573+
AlwaysShow{
574+
#[cfg(never)]
575+
always_hide: u32,
576+
#[cfg(not(never))]
577+
always_show: u32,
578+
}
579+
}
580+
581+
impl < > $crate::fmt::Debug for HideAndShow< > where {
582+
fn fmt(&self , f: &mut $crate::fmt::Formatter) -> $crate::fmt::Result {
583+
match self {
584+
HideAndShow {
585+
always_show: always_show,
586+
}
587+
=>f.debug_struct("HideAndShow").field("always_show", &always_show).finish()
588+
}
589+
}
590+
}
591+
impl < > $crate::fmt::Debug for HideAndShowEnum< > where {
592+
fn fmt(&self , f: &mut $crate::fmt::Formatter) -> $crate::fmt::Result {
593+
match self {
594+
HideAndShowEnum::AlwaysShow {
595+
always_show: always_show,
596+
}
597+
=>f.debug_struct("AlwaysShow").field("always_show", &always_show).finish(),
598+
}
599+
}
600+
}"#]],
601+
);
602+
}
603+
#[test]
604+
fn test_default_expand_with_cfg() {
605+
check(
606+
r#"
607+
//- minicore: derive, default
608+
#[derive(Default)]
609+
struct Foo {
610+
field1: i32,
611+
#[cfg(never)]
612+
field2: (),
613+
}
614+
#[derive(Default)]
615+
enum Bar {
616+
Foo,
617+
#[cfg_attr(not(never), default)]
618+
Bar,
619+
}
620+
"#,
621+
expect![[r#"
622+
#[derive(Default)]
623+
struct Foo {
624+
field1: i32,
625+
#[cfg(never)]
626+
field2: (),
627+
}
628+
#[derive(Default)]
629+
enum Bar {
630+
Foo,
631+
#[cfg_attr(not(never), default)]
632+
Bar,
633+
}
634+
635+
impl < > $crate::default::Default for Foo< > where {
636+
fn default() -> Self {
637+
Foo {
638+
field1: $crate::default::Default::default(),
639+
}
640+
}
641+
}
642+
impl < > $crate::default::Default for Bar< > where {
643+
fn default() -> Self {
644+
Bar::Bar
645+
}
646+
}"#]],
647+
);
648+
}

0 commit comments

Comments
 (0)