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.
	* typecheck/rust-hir-type-check-path.cc: properly checks if the
	PathInExpression is an error node and prints proper diagnostics if
	so.

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 3, 2024
1 parent 9e07940 commit d71e030
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 3 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
2 changes: 1 addition & 1 deletion gcc/rust/parse/rust-parse-impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -6715,7 +6715,7 @@ Parser<ManagedTokenSource>::parse_path_in_expression ()
{
// skip after somewhere?
// don't necessarily throw error but yeah
return AST::PathInExpression::create_error ();
return AST::PathInExpression::create_error (locus);
}
segments.push_back (std::move (initial_segment));

Expand Down
7 changes: 7 additions & 0 deletions gcc/rust/typecheck/rust-hir-type-check-path.cc
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,13 @@ TypeCheckExpr::resolve_root_path (HIR::PathInExpression &expr, size_t *offset,
root_tyty = lookup;
}

if (!root_tyty)
{
// then it must be an empty `::` path
rust_error_at (expr.get_locus (), "expected identifier");
return new TyTy::ErrorType (expr.get_mappings ().get_hirid ());
}

return root_tyty;
}

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 d71e030

Please sign in to comment.