https://gcc.gnu.org/g:2c5d1113e3352992a3e497dc092e14741aaca334

commit 2c5d1113e3352992a3e497dc092e14741aaca334
Author: Pierre-Emmanuel Patry <[email protected]>
Date:   Tue Oct 24 16:01:52 2023 +0200

    Add more checks for expr value in early visitors
    
    Early passes visitors may encounter constant item without a value, this
    is expected as the pass rejecting a constant without an expression is
    done later during the ast validation.
    
    gcc/rust/ChangeLog:
    
            * expand/rust-cfg-strip.cc (CfgStrip::visit): Add expr value check.
            * expand/rust-expand-visitor.cc (ExpandVisitor::visit): Likewise.
            * resolve/rust-early-name-resolver.cc (EarlyNameResolver::visit):
            Likewise.
    
    Signed-off-by: Pierre-Emmanuel Patry <[email protected]>

Diff:
---
 gcc/rust/expand/rust-cfg-strip.cc            | 15 +++++++++------
 gcc/rust/expand/rust-expand-visitor.cc       |  3 ++-
 gcc/rust/resolve/rust-early-name-resolver.cc |  3 ++-
 3 files changed, 13 insertions(+), 8 deletions(-)

diff --git a/gcc/rust/expand/rust-cfg-strip.cc 
b/gcc/rust/expand/rust-cfg-strip.cc
index 21fb2f539518..50c4a1c07239 100644
--- a/gcc/rust/expand/rust-cfg-strip.cc
+++ b/gcc/rust/expand/rust-cfg-strip.cc
@@ -2293,12 +2293,15 @@ CfgStrip::visit (AST::ConstantItem &const_item)
   /* strip any internal sub-expressions - expression itself isn't
    * allowed to have external attributes in this position so can't be
    * stripped. */
-  auto &expr = const_item.get_expr ();
-  expr->accept_vis (*this);
-  if (expr->is_marked_for_strip ())
-    rust_error_at (expr->get_locus (),
-                  "cannot strip expression in this position - outer "
-                  "attributes not allowed");
+  if (const_item.has_expr ())
+    {
+      auto &expr = const_item.get_expr ();
+      expr->accept_vis (*this);
+      if (expr->is_marked_for_strip ())
+       rust_error_at (expr->get_locus (),
+                      "cannot strip expression in this position - outer "
+                      "attributes not allowed");
+    }
 }
 void
 CfgStrip::visit (AST::StaticItem &static_item)
diff --git a/gcc/rust/expand/rust-expand-visitor.cc 
b/gcc/rust/expand/rust-expand-visitor.cc
index 55d5de0a04ec..b0fa004fdc34 100644
--- a/gcc/rust/expand/rust-expand-visitor.cc
+++ b/gcc/rust/expand/rust-expand-visitor.cc
@@ -1127,7 +1127,8 @@ ExpandVisitor::visit (AST::ConstantItem &const_item)
 {
   maybe_expand_type (const_item.get_type ());
 
-  maybe_expand_expr (const_item.get_expr ());
+  if (const_item.has_expr ())
+    maybe_expand_expr (const_item.get_expr ());
 }
 
 void
diff --git a/gcc/rust/resolve/rust-early-name-resolver.cc 
b/gcc/rust/resolve/rust-early-name-resolver.cc
index 32522d52cac0..d19c384afd3c 100644
--- a/gcc/rust/resolve/rust-early-name-resolver.cc
+++ b/gcc/rust/resolve/rust-early-name-resolver.cc
@@ -752,7 +752,8 @@ void
 EarlyNameResolver::visit (AST::ConstantItem &const_item)
 {
   const_item.get_type ()->accept_vis (*this);
-  const_item.get_expr ()->accept_vis (*this);
+  if (const_item.has_expr ())
+    const_item.get_expr ()->accept_vis (*this);
 }
 
 void

Reply via email to