From: "lenny.chiadmi-delage" <[email protected]>

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]>
---
This change was merged into the gccrs repository and is posted here for
upstream visibility and potential drive-by review, as requested by GCC
release managers.
Each commit email contains a link to its details on github from where you can
find the Pull-Request and associated discussions.


Commit on github: 
https://github.com/Rust-GCC/gccrs/commit/699625596f329afee81ddf240821d38eabf58b57

The commit has been mentioned in the following pull-request(s):
 - https://github.com/Rust-GCC/gccrs/pull/4214

 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 068e364d1..6d6810ec5 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 ec4842541..9ecc440c2 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 614fec707..4a67651c2 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 599d73955..f46c5ca3e 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 {

base-commit: d432809950bc0a23d7d5a24290fe556ac4fccd85
-- 
2.52.0

Reply via email to