From: "lenny.chiadmi-delage" <[email protected]>
Fix cfg_attr expansion and feature gate attribute handling
Fixes Rust-GCC#4245
gcc/rust/ChangeLog:
* checks/errors/feature/rust-feature-gate.cc (FeatureGate::visit): Added
handling for META_ITEM type attributes to properly process feature
gates.
* expand/rust-cfg-strip.cc (expand_cfg_attrs): Fixed a bug where
newly inserted cfg_attr attributes wheren't being reprocessed,
and cleaned up the loop increment logic.
Signed-off-by: lenny.chiadmi-delage <[email protected]>
---
.../checks/errors/feature/rust-feature-gate.cc | 16 ++++++++++++++++
gcc/rust/expand/rust-cfg-strip.cc | 10 ++++++----
2 files changed, 22 insertions(+), 4 deletions(-)
diff --git a/gcc/rust/checks/errors/feature/rust-feature-gate.cc
b/gcc/rust/checks/errors/feature/rust-feature-gate.cc
index 9df822b7e1c..e5170b77808 100644
--- a/gcc/rust/checks/errors/feature/rust-feature-gate.cc
+++ b/gcc/rust/checks/errors/feature/rust-feature-gate.cc
@@ -69,6 +69,22 @@ FeatureGate::visit (AST::Crate &crate)
valid_lib_features.emplace (name_str, item->get_locus ());
}
}
+ else if (type == AST::AttrInput::AttrInputType::META_ITEM)
+ {
+ const auto &meta_item
+ = static_cast<const AST::AttrInputMetaItemContainer &> (
+ attr_input);
+ for (const auto &item : meta_item.get_items ())
+ {
+ const auto &name_str = item->as_string ();
+
+ // TODO: detect duplicates
+ if (auto tname = Feature::as_name (name_str))
+ valid_lang_features.insert (tname.value ());
+ else
+ valid_lib_features.emplace (name_str, item->get_locus ());
+ }
+ }
}
}
diff --git a/gcc/rust/expand/rust-cfg-strip.cc
b/gcc/rust/expand/rust-cfg-strip.cc
index 3c5e74e7aae..e8c6c378942 100644
--- a/gcc/rust/expand/rust-cfg-strip.cc
+++ b/gcc/rust/expand/rust-cfg-strip.cc
@@ -124,6 +124,12 @@ expand_cfg_attrs (AST::AttrVec &attrs)
attrs.insert (attrs.begin () + i,
std::make_move_iterator (new_attrs.begin ()),
std::make_move_iterator (new_attrs.end ()));
+
+ /* Decrement i so that the for loop's i++ will bring us back to
+ * position i, allowing us to reprocess the newly inserted
+ * attribute (in case it's also a cfg_attr that needs expansion)
+ */
+ i--;
}
/* do something - if feature (first token in tree) is in fact enabled,
@@ -133,10 +139,6 @@ expand_cfg_attrs (AST::AttrVec &attrs)
* recursive, so check for expanded attributes being recursive and
* possibly recursively call the expand_attrs? */
}
- else
- {
- i++;
- }
}
attrs.shrink_to_fit ();
}
--
2.50.1