https://gcc.gnu.org/g:c38cc8c51cf6445105bb10df165dcd9379062429
commit r16-4778-gc38cc8c51cf6445105bb10df165dcd9379062429 Author: Owen Avery <[email protected]> Date: Mon Aug 11 22:06:02 2025 -0400 gccrs: CfgStrip AST nodes marked with #[test] gcc/rust/ChangeLog: * expand/rust-cfg-strip.cc: Include "rust-macro-expand.h". (fails_cfg): Rename to... (CfgStrip::fails_cfg): ...here and handle test attributes. (fails_cfg_with_expand): Rename to... (CfgStrip::fails_cfg_with_expand): ...here and handle test attributes. * expand/rust-cfg-strip.h (struct ExpansionCfg): Forward declare. (CfgStrip::fails_cfg): New member function. (CfgStrip::fails_cfg_with_expand): Likewise. (CfgStrip::CfgStrip): Accept reference to ExpansionCfg. (CfgStrip::expansion_cfg): New member variable. * rust-session-manager.cc (Session::expansion): Pass ExpansionCfg instance to CfgStrip constructor. gcc/testsuite/ChangeLog: * rust/compile/cfg-test.rs: New test. Signed-off-by: Owen Avery <[email protected]> Diff: --- gcc/rust/expand/rust-cfg-strip.cc | 11 +++++++++-- gcc/rust/expand/rust-cfg-strip.h | 14 +++++++++++++- gcc/rust/rust-session-manager.cc | 2 +- gcc/testsuite/rust/compile/cfg-test.rs | 4 ++++ 4 files changed, 27 insertions(+), 4 deletions(-) diff --git a/gcc/rust/expand/rust-cfg-strip.cc b/gcc/rust/expand/rust-cfg-strip.cc index 58d807160094..8a1cf787a0a6 100644 --- a/gcc/rust/expand/rust-cfg-strip.cc +++ b/gcc/rust/expand/rust-cfg-strip.cc @@ -22,6 +22,7 @@ #include "rust-path.h" #include "rust-session-manager.h" #include "rust-attribute-values.h" +#include "rust-macro-expand.h" namespace Rust { @@ -30,7 +31,7 @@ namespace Rust { * should be stripped. Note that attributes must be expanded before calling. */ bool -fails_cfg (const AST::AttrVec &attrs) +CfgStrip::fails_cfg (const AST::AttrVec &attrs) const { auto &session = Session::get_instance (); @@ -39,6 +40,9 @@ fails_cfg (const AST::AttrVec &attrs) if (attr.get_path () == Values::Attributes::CFG && !attr.check_cfg_predicate (session)) return true; + else if (!expansion_cfg.should_test + && attr.get_path () == Values::Attributes::TEST) + return true; } return false; } @@ -48,7 +52,7 @@ fails_cfg (const AST::AttrVec &attrs) * should be stripped. Will expand attributes as well. */ bool -fails_cfg_with_expand (AST::AttrVec &attrs) +CfgStrip::fails_cfg_with_expand (AST::AttrVec &attrs) const { auto &session = Session::get_instance (); @@ -85,6 +89,9 @@ fails_cfg_with_expand (AST::AttrVec &attrs) attr.as_string ().c_str ()); } } + else if (!expansion_cfg.should_test + && attr.get_path () == Values::Attributes::TEST) + return true; } return false; } diff --git a/gcc/rust/expand/rust-cfg-strip.h b/gcc/rust/expand/rust-cfg-strip.h index 767cf28a7123..75c504719249 100644 --- a/gcc/rust/expand/rust-cfg-strip.h +++ b/gcc/rust/expand/rust-cfg-strip.h @@ -23,14 +23,23 @@ #include "rust-item.h" namespace Rust { + +// forward declare +struct ExpansionCfg; + // Visitor used to maybe_strip attributes. class CfgStrip : public AST::DefaultASTVisitor { private: + bool fails_cfg (const AST::AttrVec &attrs) const; + + bool fails_cfg_with_expand (AST::AttrVec &attrs) const; + public: using DefaultASTVisitor::visit; - CfgStrip () {} + CfgStrip (const ExpansionCfg &expansion_cfg) : expansion_cfg (expansion_cfg) + {} /* Run the AttrVisitor on an entire crate */ void go (AST::Crate &crate); @@ -194,6 +203,9 @@ public: { DefaultASTVisitor::visit (item); } + +private: + const ExpansionCfg &expansion_cfg; }; } // namespace Rust diff --git a/gcc/rust/rust-session-manager.cc b/gcc/rust/rust-session-manager.cc index c88f46757535..5b8ba5759bfa 100644 --- a/gcc/rust/rust-session-manager.cc +++ b/gcc/rust/rust-session-manager.cc @@ -938,7 +938,7 @@ Session::expansion (AST::Crate &crate, Resolver2_0::NameResolutionContext &ctx) while (!fixed_point_reached && iterations < cfg.recursion_limit) { - CfgStrip ().go (crate); + CfgStrip (cfg).go (crate); // Errors might happen during cfg strip pass bool visitor_dirty = false; diff --git a/gcc/testsuite/rust/compile/cfg-test.rs b/gcc/testsuite/rust/compile/cfg-test.rs new file mode 100644 index 000000000000..a2e870c80dcf --- /dev/null +++ b/gcc/testsuite/rust/compile/cfg-test.rs @@ -0,0 +1,4 @@ +#[test] +fn foo() { + some_function_which_doesnt_exist(); +}
