https://gcc.gnu.org/g:cb23e7f1a781ed2307e975d084fbd4804d3c0a63

commit r17-4119-gcb23e7f1a781ed2307e975d084fbd4804d3c0a63
Author: Philip Herron <[email protected]>
Date:   Sat Aug 15 22:45:47 2026 +0100

    gccrs: Fix missing error diag on const parser and crash in typecheck
    
    We need to force braces on the default expression in the parser but also
    the typecheck cant const eval when the typecheck fails otherwise it will
    crash because invalid error mark node.
    
    Fixes Rust-GCC/gccrs#4173
    
    gcc/rust/ChangeLog:
    
            * parse/rust-parse-impl.hxx:
            * typecheck/rust-hir-type-check-base.cc:
    
    gcc/testsuite/ChangeLog:
    
            * rust/compile/issue-4173-1.rs: New test.
            * rust/compile/issue-4173-2.rs: New test.
            * rust/compile/issue-4173-3.rs: New test.
    
    Signed-off-by: Philip Herron <[email protected]>

Diff:
---
 gcc/rust/parse/rust-parse-impl.hxx             | 10 ++++++++++
 gcc/rust/typecheck/rust-hir-type-check-base.cc |  4 ++++
 gcc/testsuite/rust/compile/issue-4173-1.rs     |  8 ++++++++
 gcc/testsuite/rust/compile/issue-4173-2.rs     |  9 +++++++++
 gcc/testsuite/rust/compile/issue-4173-3.rs     |  9 +++++++++
 5 files changed, 40 insertions(+)

diff --git a/gcc/rust/parse/rust-parse-impl.hxx 
b/gcc/rust/parse/rust-parse-impl.hxx
index 9d7d529d4ee2..49a5a45b9f41 100644
--- a/gcc/rust/parse/rust-parse-impl.hxx
+++ b/gcc/rust/parse/rust-parse-impl.hxx
@@ -2021,6 +2021,16 @@ Parser<ManagedTokenSource>::parse_generic_param 
(EndTokenPred is_end_token)
            if (default_expr.value ().get_kind ()
                == AST::GenericArg::Kind::Either)
              default_expr = default_expr.value ().disambiguate_to_const ();
+           else if (default_expr.value ().get_kind ()
+                    != AST::GenericArg::Kind::Const)
+             {
+               Error error (
+                 default_expr.value ().get_locus (),
+                 "expressions must be enclosed in braces to be used as const "
+                 "generic arguments");
+               add_error (std::move (error));
+               default_expr = tl::nullopt;
+             }
          }
 
        param = std::unique_ptr<AST::ConstGenericParam> (
diff --git a/gcc/rust/typecheck/rust-hir-type-check-base.cc 
b/gcc/rust/typecheck/rust-hir-type-check-base.cc
index 4c276ce83d0a..399b9594609a 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-base.cc
+++ b/gcc/rust/typecheck/rust-hir-type-check-base.cc
@@ -683,6 +683,10 @@ TypeCheckBase::resolve_generic_params (
                auto expr_type
                  = TypeCheckExpr::Resolve (param.get_default_expression ());
 
+               if (specified_type->is<TyTy::ErrorType> ()
+                   || expr_type->is<TyTy::ErrorType> ())
+                 break;
+
                coercion_site (param.get_mappings ().get_hirid (),
                               TyTy::TyWithLocation (specified_type),
                               TyTy::TyWithLocation (
diff --git a/gcc/testsuite/rust/compile/issue-4173-1.rs 
b/gcc/testsuite/rust/compile/issue-4173-1.rs
new file mode 100644
index 000000000000..f5ec82bbc70a
--- /dev/null
+++ b/gcc/testsuite/rust/compile/issue-4173-1.rs
@@ -0,0 +1,8 @@
+#![feature(no_core)]
+#![no_core]
+#![feature(lang_items)]
+
+#[lang = "sized"]
+trait Sized {}
+
+pub struct S<const N: u32 = { 1 }>;
diff --git a/gcc/testsuite/rust/compile/issue-4173-2.rs 
b/gcc/testsuite/rust/compile/issue-4173-2.rs
new file mode 100644
index 000000000000..d6e26bb54c6a
--- /dev/null
+++ b/gcc/testsuite/rust/compile/issue-4173-2.rs
@@ -0,0 +1,9 @@
+#![feature(no_core)]
+#![no_core]
+#![feature(lang_items)]
+
+#[lang = "sized"]
+trait Sized {}
+
+pub struct S<const N: u32 = { u32::MAX }>;
+// { dg-error "failed to resolve path segment using an impl Probe" "" { target 
*-*-* } .-1 }
diff --git a/gcc/testsuite/rust/compile/issue-4173-3.rs 
b/gcc/testsuite/rust/compile/issue-4173-3.rs
new file mode 100644
index 000000000000..c4933c4ff97b
--- /dev/null
+++ b/gcc/testsuite/rust/compile/issue-4173-3.rs
@@ -0,0 +1,9 @@
+#![feature(no_core)]
+#![no_core]
+#![feature(lang_items)]
+
+#[lang = "sized"]
+trait Sized {}
+
+pub struct S<const N: u32 = u32::MAX>;
+// { dg-error "expressions must be enclosed in braces to be used as const 
generic arguments" "" { target *-*-* } .-1 }

Reply via email to