From: Yap Zhi Heng <[email protected]>

Fixes Rust-GCC/gccrs#3550. Previously parse_repr_options assumes that all attrs 
in its
params are token trees, but it is possible that already-parsed meta items can 
be passed
as params as well due to expansion of cfg_attr.

gcc/rust/ChangeLog:

        * typecheck/rust-hir-type-check-base.cc 
(TypeCheckBase::parse_repr_options):
        Allow parsing of AttrInputMetaItemContainer.

Signed-off-by: Yap Zhi Heng <[email protected]>
---
 .../typecheck/rust-hir-type-check-base.cc     | 21 +++++++++++++++----
 gcc/testsuite/rust/compile/issue-3550.rs      |  7 +++++++
 2 files changed, 24 insertions(+), 4 deletions(-)
 create mode 100644 gcc/testsuite/rust/compile/issue-3550.rs

diff --git a/gcc/rust/typecheck/rust-hir-type-check-base.cc 
b/gcc/rust/typecheck/rust-hir-type-check-base.cc
index 835e8ca790c..6c3cb4054b1 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-base.cc
+++ b/gcc/rust/typecheck/rust-hir-type-check-base.cc
@@ -421,14 +421,27 @@ TypeCheckBase::parse_repr_options (const AST::AttrVec 
&attrs, location_t locus)
          const AST::AttrInput &input = attr.get_attr_input ();
          bool is_token_tree = input.get_attr_input_type ()
                               == AST::AttrInput::AttrInputType::TOKEN_TREE;
-         if (!is_token_tree)
+         bool is_meta_item = input.get_attr_input_type ()
+                             == AST::AttrInput::AttrInputType::META_ITEM;
+         if (!is_token_tree && !is_meta_item)
            {
              rust_error_at (attr.get_locus (), "malformed %<repr%> attribute");
              continue;
            }
-         const auto &option = static_cast<const AST::DelimTokenTree &> (input);
-         AST::AttrInputMetaItemContainer *meta_items
-           = option.parse_to_meta_item ();
+
+         const AST::AttrInputMetaItemContainer *meta_items = nullptr;
+         if (is_token_tree)
+           {
+             const auto &option
+               = static_cast<const AST::DelimTokenTree &> (input);
+             meta_items = option.parse_to_meta_item ();
+           }
+         else
+           { // is_meta_item is true
+             const auto &option
+               = static_cast<const AST::AttrInputMetaItemContainer &> (input);
+             meta_items = new AST::AttrInputMetaItemContainer (option);
+           }
 
          if (meta_items == nullptr)
            {
diff --git a/gcc/testsuite/rust/compile/issue-3550.rs 
b/gcc/testsuite/rust/compile/issue-3550.rs
new file mode 100644
index 00000000000..50b8bbd4105
--- /dev/null
+++ b/gcc/testsuite/rust/compile/issue-3550.rs
@@ -0,0 +1,7 @@
+#![feature(no_core)]
+#![no_core]
+
+#[cfg_attr(not(wrong = "32"), repr(i32))]
+enum Eu64 {
+    Au64 = 0,
+}
\ No newline at end of file
-- 
2.50.1

Reply via email to