Skip to content

Commit b963d59

Browse files
committed
gccrs: fix a compiler crash when path expression contains nothing
gcc/rust/ChangeLog: * ast/rust-path.h: allows error nodes to specify the source location, so that it could be used in diagnostics later. * parse/rust-parse-impl.h: creates the PathInExpression error node with source location and prints proper diagnostics if the path is empty. gcc/testsuite/ChangeLog: * rust/compile/paamayim-nekudotayim.rs: add a test for testing proper error recovery logic when there is no path names.
1 parent 9e07940 commit b963d59

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

gcc/rust/ast/rust-path.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -727,9 +727,9 @@ class PathInExpression : public Pattern, public ExprWithoutBlock
727727
}
728728

729729
// Creates an error state path in expression.
730-
static PathInExpression create_error ()
730+
static PathInExpression create_error (location_t locus = UNDEF_LOCATION)
731731
{
732-
return PathInExpression ({}, {}, UNDEF_LOCATION);
732+
return PathInExpression ({}, {}, locus);
733733
}
734734

735735
// Returns whether path in expression is in an error state.

gcc/rust/parse/rust-parse-impl.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6713,9 +6713,9 @@ Parser<ManagedTokenSource>::parse_path_in_expression ()
67136713
AST::PathExprSegment initial_segment = parse_path_expr_segment ();
67146714
if (initial_segment.is_error ())
67156715
{
6716-
// skip after somewhere?
6717-
// don't necessarily throw error but yeah
6718-
return AST::PathInExpression::create_error ();
6716+
// we can not throw an error here because if entered from macro expansion
6717+
// the macro expander can throw another error which might confuse the user
6718+
return AST::PathInExpression::create_error (locus);
67196719
}
67206720
segments.push_back (std::move (initial_segment));
67216721

@@ -11637,6 +11637,11 @@ Parser<ManagedTokenSource>::parse_stmt_or_expr ()
1163711637
AST::PathInExpression path = parse_path_in_expression ();
1163811638
std::unique_ptr<AST::Expr> null_denotation;
1163911639

11640+
if (path.is_error ())
11641+
{
11642+
rust_error_at (path.get_locus (), "expected identifier");
11643+
}
11644+
1164011645
if (lexer.peek_token ()->get_id () == EXCLAM)
1164111646
{
1164211647
std::unique_ptr<AST::MacroInvocation> invoc
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// http://phpsadness.com/sad/1
2+
fn main() {
3+
::;
4+
// { dg-error "expected identifier" "" { target *-*-* } .-1 }
5+
}

0 commit comments

Comments
 (0)