https://gcc.gnu.org/g:1461fa714b54621b6b0c0f9258dd5e15ccf4c5f9

commit r16-4886-g1461fa714b54621b6b0c0f9258dd5e15ccf4c5f9
Author: Pierre-Emmanuel Patry <[email protected]>
Date:   Wed Oct 1 16:35:37 2025 +0200

    gccrs: Avoid malformed attribute conversion
    
    Add location for malformed attribute errors and avoid processing of
    malformed attributes.
    
    gcc/rust/ChangeLog:
    
            * ast/rust-ast.cc (AttrInputMetaItemContainer::separate_cfg_attrs):
            Avoid malformed attributes.
            * util/rust-attributes.cc (AttributeChecker::visit): Change 
location.
    
    gcc/testsuite/ChangeLog:
    
            * rust/compile/attr_malformed_path.rs: New test.
    
    Signed-off-by: Pierre-Emmanuel Patry <[email protected]>

Diff:
---
 gcc/rust/ast/rust-ast.cc                          | 6 ++++++
 gcc/rust/util/rust-attributes.cc                  | 4 ++--
 gcc/testsuite/rust/compile/attr_malformed_path.rs | 3 +++
 3 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/gcc/rust/ast/rust-ast.cc b/gcc/rust/ast/rust-ast.cc
index 003a6edbc106..a4b256c9e67f 100644
--- a/gcc/rust/ast/rust-ast.cc
+++ b/gcc/rust/ast/rust-ast.cc
@@ -4128,6 +4128,12 @@ AttrInputMetaItemContainer::separate_cfg_attrs () const
 
   for (auto it = items.begin () + 1; it != items.end (); ++it)
     {
+      if ((*it)->get_kind () == MetaItemInner::Kind::MetaItem
+         && static_cast<MetaItem &> (**it).get_item_kind ()
+              == MetaItem::ItemKind::PathExpr
+         && !static_cast<MetaItemPathExpr &> (**it).get_expr ().is_literal ())
+       continue;
+
       Attribute attr = (*it)->to_attribute ();
       if (attr.is_empty ())
        {
diff --git a/gcc/rust/util/rust-attributes.cc b/gcc/rust/util/rust-attributes.cc
index 49c4282755cc..ce29e59b06da 100644
--- a/gcc/rust/util/rust-attributes.cc
+++ b/gcc/rust/util/rust-attributes.cc
@@ -435,9 +435,9 @@ AttributeChecker::visit (AST::MetaItemPathExpr &attribute)
 {
   if (!attribute.get_expr ().is_literal ())
     {
-      rust_error_at (attribute.get_locus (),
+      rust_error_at (attribute.get_expr ().get_locus (),
                     "malformed %<path%> attribute input");
-      rust_inform (attribute.get_locus (),
+      rust_inform (attribute.get_expr ().get_locus (),
                   "must be of the form: %<#[path = \"file\"]%>");
     }
 }
diff --git a/gcc/testsuite/rust/compile/attr_malformed_path.rs 
b/gcc/testsuite/rust/compile/attr_malformed_path.rs
new file mode 100644
index 000000000000..2bccf37bd793
--- /dev/null
+++ b/gcc/testsuite/rust/compile/attr_malformed_path.rs
@@ -0,0 +1,3 @@
+#[cfg_attr(target_arch = "x86_64", path = (target_arch = "x86",    path = 
"x86.rs"))]
+mod imp {}
+// { dg-error "malformed .path. attribute input" "" { target *-*-* } .-2 }

Reply via email to