Skip to content

Commit 9e6f107

Browse files
u7k4rs6powerboat9
authored andcommitted
gccrs: Report an error on an empty path expression
The path parser previously returned an error node silently when the initial path segment failed to parse. Report a diagnostic for paths beginning with ::, where the resulting error node could otherwise lead to an ICE downstream. Do not apply the guard to bare $, since undefined metavariables already receive more specific diagnostics downstream. Remove the redundant parse_stmt_or_expr guard, which otherwise reports the same error twice. Fixes #4790 gcc/rust/ChangeLog: * parse/rust-parse-impl-path.hxx (Parser::parse_path_in_expression): Report an error when the initial segment of a scoped path fails to parse. * parse/rust-parse-impl.hxx (Parser::parse_stmt_or_expr): Remove the redundant expected-identifier error guard. gcc/testsuite/ChangeLog: * rust/compile/empty_path2.rs: New test. * rust/compile/empty_path3.rs: New test. Signed-off-by: Utkarsh Bahuguna <utkarshbahuguna10@gmail.com>
1 parent e56b411 commit 9e6f107

4 files changed

Lines changed: 26 additions & 11 deletions

File tree

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -389,8 +389,11 @@ Parser<ManagedTokenSource>::parse_path_in_expression ()
389389
AST::PathExprSegment initial_segment = parse_path_expr_segment ();
390390
if (initial_segment.is_error ())
391391
{
392-
// skip after somewhere?
393-
// don't necessarily throw error but yeah
392+
if (has_opening_scope_resolution)
393+
{
394+
Error error (locus, "expected identifier");
395+
add_error (std::move (error));
396+
}
394397
return AST::PathInExpression::create_error ();
395398
}
396399
segments.push_back (std::move (initial_segment));

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

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7294,15 +7294,6 @@ Parser<ManagedTokenSource>::parse_stmt_or_expr ()
72947294
case DOLLAR_SIGN:
72957295
{
72967296
AST::PathInExpression path = parse_path_in_expression ();
7297-
if (path.is_error ())
7298-
{
7299-
Error error (t->get_locus (), "expected identifier");
7300-
add_error (std::move (error));
7301-
skip_after_semicolon ();
7302-
return tl::unexpected<Parse::Error::Node> (
7303-
Parse::Error::Node::CHILD_ERROR);
7304-
}
7305-
73067297
tl::expected<std::unique_ptr<AST::Expr>, Parse::Error::Expr>
73077298
null_denotation;
73087299

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#![feature(no_core)]
2+
#![no_core]
3+
4+
// A path expression with no segments in a let initialiser segfaulted
5+
// during type checking, see Rust-GCC/gccrs#4790.
6+
fn main() {
7+
let x = ::;
8+
// { dg-error "expected identifier" "" { target *-*-* } .-1 }
9+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#![feature(no_core)]
2+
#![no_core]
3+
4+
// An empty path `::` in pattern position. The parser returned a path with no
5+
// segments without reporting anything, so type checking dereferenced a null
6+
// root type and the compiler crashed instead of diagnosing. Same defect as
7+
// empty_path2.rs, which covers the let initialiser instead of the pattern.
8+
// See Rust-GCC/gccrs#4790.
9+
fn main() {
10+
let :: = 1;
11+
// { dg-error "expected identifier" "" { target *-*-* } .-1 }
12+
}

0 commit comments

Comments
 (0)