From: Matty Kuhn <matty.kuh...@gmail.com>

This patch fixes an issue where an empty feature gate would segfault,
instead of reporting a syntax error to the user.

gcc/rust/ChangeLog:

        * ast/rust-ast.h: (AST::Attribute): add empty_input function
        * checks/errors/rust-feature-gate.cc: (FeatureGate::visit): check for 
empty feature gate

gcc/testsuite/ChangeLog:

        * rust/compile/feature.rs: add an invalid empty feature to produce an 
error

Signed-off-by: Matty Kuhn <matty.kuh...@gmail.com>
---
 gcc/rust/ast/rust-ast.h                     | 3 +++
 gcc/rust/checks/errors/rust-feature-gate.cc | 7 +++++++
 gcc/testsuite/rust/compile/feature.rs       | 2 ++
 3 files changed, 12 insertions(+)

diff --git a/gcc/rust/ast/rust-ast.h b/gcc/rust/ast/rust-ast.h
index 1091ba0593e..09e0fce4f19 100644
--- a/gcc/rust/ast/rust-ast.h
+++ b/gcc/rust/ast/rust-ast.h
@@ -657,6 +657,9 @@ public:
   // Returns whether the attribute is considered an "empty" attribute.
   bool is_empty () const { return attr_input == nullptr && path.is_empty (); }
 
+  // Returns whether the attribute has no input
+  bool empty_input () const { return !attr_input; }
+
   location_t get_locus () const { return locus; }
 
   AttrInput &get_attr_input () const { return *attr_input; }
diff --git a/gcc/rust/checks/errors/rust-feature-gate.cc 
b/gcc/rust/checks/errors/rust-feature-gate.cc
index f3daa61f170..44007f99e5c 100644
--- a/gcc/rust/checks/errors/rust-feature-gate.cc
+++ b/gcc/rust/checks/errors/rust-feature-gate.cc
@@ -40,6 +40,13 @@ FeatureGate::visit (AST::Crate &crate)
     {
       if (attr.get_path ().as_string () == "feature")
        {
+         // check for empty feature, such as `#![feature], this is an error
+         if (attr.empty_input ())
+           {
+             rust_error_at (attr.get_locus (), ErrorCode::E0556,
+                            "malformed %<feature%> attribute input");
+             continue;
+           }
          const auto &attr_input = attr.get_attr_input ();
          auto type = attr_input.get_attr_input_type ();
          if (type == AST::AttrInput::AttrInputType::TOKEN_TREE)
diff --git a/gcc/testsuite/rust/compile/feature.rs 
b/gcc/testsuite/rust/compile/feature.rs
index f743f9229b6..6f428f075a1 100644
--- a/gcc/testsuite/rust/compile/feature.rs
+++ b/gcc/testsuite/rust/compile/feature.rs
@@ -2,5 +2,7 @@
 #![feature(AA)] //{ dg-error "unknown feature .AA." }
 #![feature(iamcrabby)] // { dg-error "unknown feature .iamcrabby." }
 #![feature(nonexistent_gccrs_feature)] // { dg-error "unknown feature 
.nonexistent_gccrs_feature." }
+// ErrorCode - E0556
+#![feature] // { dg-error "malformed .feature. attribute input" }
 
 fn main() {}
-- 
2.49.0

Reply via email to