[gcc r17-2253] gccrs: Fix crash when resolving invalid enum item

Arthur Cohen via Gcc-cvs <[email protected]>
Newsgroups gmane.comp.gcc.cvs
Message-ID <[email protected]>
https://gcc.gnu.org/g:79e5f790e70a3a090cf0b43d1bac4a42c18dc4f0

commit r17-2253-g79e5f790e70a3a090cf0b43d1bac4a42c18dc4f0
Author: Philip Herron <[email protected]>
Date:   Mon Jun 29 12:01:59 2026 +0100

    gccrs: Fix crash when resolving invalid enum item
    
    Fixes Rust-GCC#1617
    
    gcc/rust/ChangeLog:
    
            * typecheck/rust-hir-type-check-item.cc (TypeCheckItem::visit): check valid
    
    gcc/testsuite/ChangeLog:
    
            * rust/compile/issue-4617.rs: New test.
    
    Signed-off-by: Philip Herron <[email protected]>

Diff:
---
 gcc/rust/typecheck/rust-hir-type-check-item.cc | 8 +++++---
 gcc/testsuite/rust/compile/issue-4617.rs       | 8 ++++++++
 2 files changed, 13 insertions(+), 3 deletions(-)

diff --git a/gcc/rust/typecheck/rust-hir-type-check-item.cc b/gcc/rust/typecheck/rust-hir-type-check-item.cc
index 055ce2b2832b..065615a23b0a 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-item.cc
+++ b/gcc/rust/typecheck/rust-hir-type-check-item.cc
@@ -421,9 +421,11 @@ TypeCheckItem::visit (HIR::Enum &enum_decl)
     {
       TyTy::VariantDef *field_type
 	= TypeCheckEnumItem::Resolve (*variant, discriminant_value);
-
-      discriminant_value++;
-      variants.push_back (field_type);
+      if (field_type)
+	{
+	  discriminant_value++;
+	  variants.push_back (field_type);
+	}
     }
 
   // Check for zero-variant enum compatibility
diff --git a/gcc/testsuite/rust/compile/issue-4617.rs b/gcc/testsuite/rust/compile/issue-4617.rs
new file mode 100644
index 000000000000..202310071d1b
--- /dev/null
+++ b/gcc/testsuite/rust/compile/issue-4617.rs
@@ -0,0 +1,8 @@
+#![feature(no_core)]
+#![no_core]
+
+struct Apple;
+
+enum Delicious {
+    ApplePie = Apple::PIE, // { dg-error "failed to resolve path segment using an impl Probe" }
+}
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.