From: hriztam <[email protected]>

gcc/rust/ChangeLog:

        * parse/rust-parse-error.h (StructExprField): Add
        STRUCT_BASE_ATTRIBUTES.
        * parse/rust-parse-impl-expr.hxx
        (Parser<ManagedTokenSource>::parse_struct_expr_field):
        Reject attributes before struct-base `..`.
        (Parser<ManagedTokenSource>::parse_struct_expr_struct_partial):
        Handle struct-base parse results without dereferencing an errored
        expected.

gcc/testsuite/ChangeLog:

        * rust/compile/issue-4476.rs: New test.

Signed-off-by: Hritam Shrivastava <[email protected]>
---
This change was merged into the gccrs repository and is posted here for
upstream visibility and potential drive-by review, as requested by GCC
release managers.
Each commit email contains a link to its details on github from where you can
find the Pull-Request and associated discussions.


Commit on github: 
https://github.com/Rust-GCC/gccrs/commit/0144bafd9a9da4c220cc38c5f87b8a25d4c2c12d

The commit has NOT been mentioned in any issue.

The commit has been mentioned in the following pull-request(s):
 - https://github.com/Rust-GCC/gccrs/pull/4514

 gcc/rust/parse/rust-parse-error.h        |  1 +
 gcc/rust/parse/rust-parse-impl-expr.hxx  | 20 ++++++++++++++++++--
 gcc/testsuite/rust/compile/issue-4476.rs | 10 ++++++++++
 3 files changed, 29 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/rust/compile/issue-4476.rs

diff --git a/gcc/rust/parse/rust-parse-error.h 
b/gcc/rust/parse/rust-parse-error.h
index 46ac46245..a4eb34c12 100644
--- a/gcc/rust/parse/rust-parse-error.h
+++ b/gcc/rust/parse/rust-parse-error.h
@@ -358,6 +358,7 @@ enum class StructExprField
 {
   MALFORMED,
   CHILD_ERROR,
+  STRUCT_BASE_ATTRIBUTES,
   // Not a hard error
   STRUCT_BASE,
 };
diff --git a/gcc/rust/parse/rust-parse-impl-expr.hxx 
b/gcc/rust/parse/rust-parse-impl-expr.hxx
index 887991541..aaffa3dcb 100644
--- a/gcc/rust/parse/rust-parse-impl-expr.hxx
+++ b/gcc/rust/parse/rust-parse-impl-expr.hxx
@@ -1752,6 +1752,16 @@ Parser<ManagedTokenSource>::parse_struct_expr_field ()
     case DOT_DOT:
       /* this is a struct base and can't be parsed here, so just return
        * nothing without erroring */
+      if (!outer_attrs.empty ())
+       {
+         add_error (
+           Error (t->get_locus (),
+                  "attributes are not allowed before %<..%> in a struct "
+                  "expression"));
+
+         return tl::unexpected<Parse::Error::StructExprField> (
+           Parse::Error::StructExprField::STRUCT_BASE_ATTRIBUTES);
+       }
 
       return tl::unexpected<Parse::Error::StructExprField> (
        Parse::Error::StructExprField::STRUCT_BASE);
@@ -4068,9 +4078,15 @@ 
Parser<ManagedTokenSource>::parse_struct_expr_struct_partial (
        while (t->get_id () != RIGHT_CURLY && t->get_id () != DOT_DOT)
          {
            auto field = parse_struct_expr_field ();
-           if (!field
-               && field.error () != Parse::Error::StructExprField::STRUCT_BASE)
+           if (!field)
              {
+               if (field.error () == 
Parse::Error::StructExprField::STRUCT_BASE)
+                 break;
+               if (field.error ()
+                   == Parse::Error::StructExprField::STRUCT_BASE_ATTRIBUTES)
+                 return tl::unexpected<Parse::Error::Expr> (
+                   Parse::Error::Expr::CHILD_ERROR);
+
                Error error (t->get_locus (),
                             "failed to parse struct (or enum) expr field");
                add_error (std::move (error));
diff --git a/gcc/testsuite/rust/compile/issue-4476.rs 
b/gcc/testsuite/rust/compile/issue-4476.rs
new file mode 100644
index 000000000..2427a67eb
--- /dev/null
+++ b/gcc/testsuite/rust/compile/issue-4476.rs
@@ -0,0 +1,10 @@
+#![feature(no_core)]
+#![no_core]
+
+struct Foo {}
+struct S {}
+
+fn main() {
+    Foo { #[cfg(feature = -1)] .. } = S {};
+    // { dg-error "attributes are not allowed before .* in a struct 
expression" "" { target *-*-* } .-1 }
+}

base-commit: 2c7b222b96cb3dc63d104a16b65065fe21834829
-- 
2.54.0

Reply via email to