Skip to content

Commit

Permalink
gccrs: fix a compiler crash when path expression contains nothing
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
liushuyu committed Dec 4, 2024
1 parent 9e07940 commit b963d59
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
4 changes: 2 additions & 2 deletions gcc/rust/ast/rust-path.h
Original file line number Diff line number Diff line change
Expand Up @@ -727,9 +727,9 @@ class PathInExpression : public Pattern, public ExprWithoutBlock
}

// Creates an error state path in expression.
static PathInExpression create_error ()
static PathInExpression create_error (location_t locus = UNDEF_LOCATION)
{
return PathInExpression ({}, {}, UNDEF_LOCATION);
return PathInExpression ({}, {}, locus);
}

// Returns whether path in expression is in an error state.
Expand Down
11 changes: 8 additions & 3 deletions gcc/rust/parse/rust-parse-impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -6713,9 +6713,9 @@ Parser<ManagedTokenSource>::parse_path_in_expression ()
AST::PathExprSegment initial_segment = parse_path_expr_segment ();
if (initial_segment.is_error ())
{
// skip after somewhere?
// don't necessarily throw error but yeah
return AST::PathInExpression::create_error ();
// we can not throw an error here because if entered from macro expansion
// the macro expander can throw another error which might confuse the user
return AST::PathInExpression::create_error (locus);
}
segments.push_back (std::move (initial_segment));

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

if (path.is_error ())
{
rust_error_at (path.get_locus (), "expected identifier");
}

if (lexer.peek_token ()->get_id () == EXCLAM)
{
std::unique_ptr<AST::MacroInvocation> invoc
Expand Down
5 changes: 5 additions & 0 deletions gcc/testsuite/rust/compile/paamayim-nekudotayim.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// http://phpsadness.com/sad/1
fn main() {
::;
// { dg-error "expected identifier" "" { target *-*-* } .-1 }
}

0 comments on commit b963d59

Please sign in to comment.