Skip to content

Commit 6996255

Browse files
VillosseP-E-P
authored andcommitted
gccrs: fix parser error on parenthesis types
Do not cast parenthesised types to TraitBound types. Fixes #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]>
1 parent d432809 commit 6996255

File tree

4 files changed

+16
-3
lines changed

4 files changed

+16
-3
lines changed

gcc/rust/ast/rust-path.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,11 @@ TypePath::make_debug_string () const
290290
TraitBound *
291291
TypePath::to_trait_bound (bool in_parens) const
292292
{
293+
// If already in parentheses, don't convert to trait bound
294+
// This ensures (TypePath) stays as ParenthesisedType in the parser
295+
if (in_parens)
296+
return nullptr;
297+
293298
return new TraitBound (TypePath (*this), get_locus (), in_parens);
294299
}
295300

gcc/rust/hir/tree/rust-hir-type.cc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,13 @@ ParenthesisedType::operator= (ParenthesisedType const &other)
101101
}
102102

103103
std::unique_ptr<TraitBound>
104-
ParenthesisedType::to_trait_bound (bool in_parens ATTRIBUTE_UNUSED) const
104+
ParenthesisedType::to_trait_bound (bool in_parens) const
105105
{
106+
/* If already in parentheses, don't convert - should stay as
107+
* ParenthesisedType */
108+
if (in_parens)
109+
return nullptr;
110+
106111
/* NOTE: obviously it is unknown whether the internal type is a trait bound
107112
* due to polymorphism, so just let the internal type handle it. As
108113
* parenthesised type, it must be in parentheses. */

gcc/rust/hir/tree/rust-hir.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2798,6 +2798,11 @@ Expr::as_string () const
27982798
std::unique_ptr<TraitBound>
27992799
TypePath::to_trait_bound (bool in_parens) const
28002800
{
2801+
// If already in parentheses, don't convert to trait bound
2802+
// This ensures (TypePath) stays as ParenthesisedType in the parser
2803+
if (in_parens)
2804+
return nullptr;
2805+
28012806
// create clone FIXME is this required? or is copy constructor automatically
28022807
// called?
28032808
TypePath copy (*this);

gcc/testsuite/rust/compile/issue-4148.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// { dg-excess-errors "warnings" }
2-
31
// TODO: all `xfail` conditions should be changed to `target` once the ICE in #4148 is resolved
42

53
pub fn ret_parens(x: i32) -> i32 {

0 commit comments

Comments
 (0)