Skip to content

CfgStrip AST nodes marked with #[test] #4067

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions gcc/rust/expand/rust-cfg-strip.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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 ();

Expand All @@ -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;
}
Expand All @@ -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 ();

Expand Down Expand Up @@ -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;
}
Expand Down
14 changes: 13 additions & 1 deletion gcc/rust/expand/rust-cfg-strip.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -194,6 +203,9 @@ class CfgStrip : public AST::DefaultASTVisitor
{
DefaultASTVisitor::visit (item);
}

private:
const ExpansionCfg &expansion_cfg;
};
} // namespace Rust

Expand Down
2 changes: 1 addition & 1 deletion gcc/rust/rust-session-manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
4 changes: 4 additions & 0 deletions gcc/testsuite/rust/compile/cfg-test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#[test]
fn foo() {
some_function_which_doesnt_exist();
}
Loading