https://gcc.gnu.org/g:1ac674c7a743598d2d689c35092a2475c3cea947

commit r16-6353-g1ac674c7a743598d2d689c35092a2475c3cea947
Author: lenny.chiadmi-delage <[email protected]>
Date:   Mon Dec 15 14:27:17 2025 +0000

    gccrs: fix parser error on parenthesis types
    
    Do not cast parenthesised types to TraitBound types.
    
    Fixes Rust-GCC#4148
    
    gcc/rust/ChangeLog:
    
            * ast/rust-path.cc (TypePath::to_trait_bound): Check if in
            parenthesis.
            * hir/tree/rust-hir-type.cc (ParenthesisedType::to_trait_bound):
            Likewise.
            * hir/tree/rust-hir.cc (TypePath::to_trait_bound): Likewise.
    
    gcc/testsuite/ChangeLog:
    
            * rust/compile/issue-4148.rs: Test should produce errors.
    
    Signed-off-by: lenny.chiadmi-delage <[email protected]>

Diff:
---
 gcc/rust/ast/rust-path.cc                | 5 +++++
 gcc/rust/hir/tree/rust-hir-type.cc       | 7 ++++++-
 gcc/rust/hir/tree/rust-hir.cc            | 5 +++++
 gcc/testsuite/rust/compile/issue-4148.rs | 2 --
 4 files changed, 16 insertions(+), 3 deletions(-)

diff --git a/gcc/rust/ast/rust-path.cc b/gcc/rust/ast/rust-path.cc
index 068e364d1848..6d6810ec518b 100644
--- a/gcc/rust/ast/rust-path.cc
+++ b/gcc/rust/ast/rust-path.cc
@@ -290,6 +290,11 @@ TypePath::make_debug_string () const
 TraitBound *
 TypePath::to_trait_bound (bool in_parens) const
 {
+  // If already in parentheses, don't convert to trait bound
+  // This ensures (TypePath) stays as ParenthesisedType in the parser
+  if (in_parens)
+    return nullptr;
+
   return new TraitBound (TypePath (*this), get_locus (), in_parens);
 }
 
diff --git a/gcc/rust/hir/tree/rust-hir-type.cc 
b/gcc/rust/hir/tree/rust-hir-type.cc
index ec484254138b..9ecc440c2d0b 100644
--- a/gcc/rust/hir/tree/rust-hir-type.cc
+++ b/gcc/rust/hir/tree/rust-hir-type.cc
@@ -101,8 +101,13 @@ ParenthesisedType::operator= (ParenthesisedType const 
&other)
 }
 
 std::unique_ptr<TraitBound>
-ParenthesisedType::to_trait_bound (bool in_parens ATTRIBUTE_UNUSED) const
+ParenthesisedType::to_trait_bound (bool in_parens) const
 {
+  /* If already in parentheses, don't convert - should stay as
+   * ParenthesisedType */
+  if (in_parens)
+    return nullptr;
+
   /* NOTE: obviously it is unknown whether the internal type is a trait bound
    * due to polymorphism, so just let the internal type handle it. As
    * parenthesised type, it must be in parentheses. */
diff --git a/gcc/rust/hir/tree/rust-hir.cc b/gcc/rust/hir/tree/rust-hir.cc
index 614fec7076e1..4a67651c2744 100644
--- a/gcc/rust/hir/tree/rust-hir.cc
+++ b/gcc/rust/hir/tree/rust-hir.cc
@@ -2798,6 +2798,11 @@ Expr::as_string () const
 std::unique_ptr<TraitBound>
 TypePath::to_trait_bound (bool in_parens) const
 {
+  // If already in parentheses, don't convert to trait bound
+  // This ensures (TypePath) stays as ParenthesisedType in the parser
+  if (in_parens)
+    return nullptr;
+
   // create clone FIXME is this required? or is copy constructor automatically
   // called?
   TypePath copy (*this);
diff --git a/gcc/testsuite/rust/compile/issue-4148.rs 
b/gcc/testsuite/rust/compile/issue-4148.rs
index 599d73955acf..f46c5ca3e8e9 100644
--- a/gcc/testsuite/rust/compile/issue-4148.rs
+++ b/gcc/testsuite/rust/compile/issue-4148.rs
@@ -1,5 +1,3 @@
-// { dg-excess-errors "warnings" }
-
 // TODO: all `xfail` conditions should be changed to `target` once the ICE in 
#4148 is resolved
 
 pub fn ret_parens(x: i32) -> i32 {

Reply via email to