Skip to content

Commit 0144baf

Browse files
hriztamphilberty
authored andcommitted
rust: avoid ICE on attributed struct base parsing
gcc/rust/ChangeLog: * parse/rust-parse-error.h (StructExprField): Add STRUCT_BASE_ATTRIBUTES. * parse/rust-parse-impl-expr.hxx (Parser<ManagedTokenSource>::parse_struct_expr_field): Reject attributes before struct-base `..`. (Parser<ManagedTokenSource>::parse_struct_expr_struct_partial): Handle struct-base parse results without dereferencing an errored expected. gcc/testsuite/ChangeLog: * rust/compile/issue-4476.rs: New test. Signed-off-by: Hritam Shrivastava <hritamstark05@gmail.com>
1 parent 2c7b222 commit 0144baf

3 files changed

Lines changed: 29 additions & 2 deletions

File tree

gcc/rust/parse/rust-parse-error.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,7 @@ enum class StructExprField
358358
{
359359
MALFORMED,
360360
CHILD_ERROR,
361+
STRUCT_BASE_ATTRIBUTES,
361362
// Not a hard error
362363
STRUCT_BASE,
363364
};

gcc/rust/parse/rust-parse-impl-expr.hxx

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1752,6 +1752,16 @@ Parser<ManagedTokenSource>::parse_struct_expr_field ()
17521752
case DOT_DOT:
17531753
/* this is a struct base and can't be parsed here, so just return
17541754
* nothing without erroring */
1755+
if (!outer_attrs.empty ())
1756+
{
1757+
add_error (
1758+
Error (t->get_locus (),
1759+
"attributes are not allowed before %<..%> in a struct "
1760+
"expression"));
1761+
1762+
return tl::unexpected<Parse::Error::StructExprField> (
1763+
Parse::Error::StructExprField::STRUCT_BASE_ATTRIBUTES);
1764+
}
17551765

17561766
return tl::unexpected<Parse::Error::StructExprField> (
17571767
Parse::Error::StructExprField::STRUCT_BASE);
@@ -4068,9 +4078,15 @@ Parser<ManagedTokenSource>::parse_struct_expr_struct_partial (
40684078
while (t->get_id () != RIGHT_CURLY && t->get_id () != DOT_DOT)
40694079
{
40704080
auto field = parse_struct_expr_field ();
4071-
if (!field
4072-
&& field.error () != Parse::Error::StructExprField::STRUCT_BASE)
4081+
if (!field)
40734082
{
4083+
if (field.error () == Parse::Error::StructExprField::STRUCT_BASE)
4084+
break;
4085+
if (field.error ()
4086+
== Parse::Error::StructExprField::STRUCT_BASE_ATTRIBUTES)
4087+
return tl::unexpected<Parse::Error::Expr> (
4088+
Parse::Error::Expr::CHILD_ERROR);
4089+
40744090
Error error (t->get_locus (),
40754091
"failed to parse struct (or enum) expr field");
40764092
add_error (std::move (error));
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#![feature(no_core)]
2+
#![no_core]
3+
4+
struct Foo {}
5+
struct S {}
6+
7+
fn main() {
8+
Foo { #[cfg(feature = -1)] .. } = S {};
9+
// { dg-error "attributes are not allowed before .* in a struct expression" "" { target *-*-* } .-1 }
10+
}

0 commit comments

Comments
 (0)