]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: fix parser error on parenthesis types
authorlenny.chiadmi-delage <lenny.chiadmi-delage@epita.fr>
Mon, 15 Dec 2025 14:27:17 +0000 (14:27 +0000)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 23 Dec 2025 11:16:47 +0000 (12:16 +0100)
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 <lenny.chiadmi-delage@epita.fr>
gcc/rust/ast/rust-path.cc
gcc/rust/hir/tree/rust-hir-type.cc
gcc/rust/hir/tree/rust-hir.cc
gcc/testsuite/rust/compile/issue-4148.rs

index 068e364d1848946976f0210eb86f7aa975774dbc..6d6810ec518bd09ead6d24443d8222511cf2313e 100644 (file)
@@ -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);
 }
 
index ec484254138bcf83d6410d87cd4a4669f63174ed..9ecc440c2d0b41d31939e2ed449c92b6db45aab3 100644 (file)
@@ -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. */
index 614fec7076e15217af2abdf8d3e4ee2836b902a3..4a67651c27446519cd472904e7c9441cfbffbd7a 100644 (file)
@@ -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);
index 599d73955acf314ec2ea5313e96ef5eae16f6b93..f46c5ca3e8e9f0b46fd2e4c28887c203acf87ca6 100644 (file)
@@ -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 {