https://gcc.gnu.org/g:4c052de3d7a37c0cb5ec6f6261c27a1658a7af74
commit r17-4280-g4c052de3d7a37c0cb5ec6f6261c27a1658a7af74 Author: Utkarsh Bahuguna <[email protected]> Date: Wed Aug 19 02:11:50 2026 +0530 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 Rust-GCC/gccrs#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 <[email protected]> Diff: --- gcc/rust/parse/rust-parse-impl-path.hxx | 7 +++++-- gcc/rust/parse/rust-parse-impl.hxx | 9 --------- gcc/testsuite/rust/compile/empty_path2.rs | 9 +++++++++ gcc/testsuite/rust/compile/empty_path3.rs | 12 ++++++++++++ 4 files changed, 26 insertions(+), 11 deletions(-) diff --git a/gcc/rust/parse/rust-parse-impl-path.hxx b/gcc/rust/parse/rust-parse-impl-path.hxx index 4ca12e98893a..0cc517e28156 100644 --- a/gcc/rust/parse/rust-parse-impl-path.hxx +++ b/gcc/rust/parse/rust-parse-impl-path.hxx @@ -389,8 +389,11 @@ 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 + if (has_opening_scope_resolution) + { + Error error (locus, "expected identifier"); + add_error (std::move (error)); + } return AST::PathInExpression::create_error (); } segments.push_back (std::move (initial_segment)); diff --git a/gcc/rust/parse/rust-parse-impl.hxx b/gcc/rust/parse/rust-parse-impl.hxx index 49a5a45b9f41..b5f41768541c 100644 --- a/gcc/rust/parse/rust-parse-impl.hxx +++ b/gcc/rust/parse/rust-parse-impl.hxx @@ -7294,15 +7294,6 @@ Parser<ManagedTokenSource>::parse_stmt_or_expr () case DOLLAR_SIGN: { AST::PathInExpression path = parse_path_in_expression (); - if (path.is_error ()) - { - Error error (t->get_locus (), "expected identifier"); - add_error (std::move (error)); - skip_after_semicolon (); - return tl::unexpected<Parse::Error::Node> ( - Parse::Error::Node::CHILD_ERROR); - } - tl::expected<std::unique_ptr<AST::Expr>, Parse::Error::Expr> null_denotation; diff --git a/gcc/testsuite/rust/compile/empty_path2.rs b/gcc/testsuite/rust/compile/empty_path2.rs new file mode 100644 index 000000000000..55357db04614 --- /dev/null +++ b/gcc/testsuite/rust/compile/empty_path2.rs @@ -0,0 +1,9 @@ +#![feature(no_core)] +#![no_core] + +// A path expression with no segments in a let initialiser segfaulted +// during type checking, see Rust-GCC/gccrs#4790. +fn main() { + let x = ::; + // { dg-error "expected identifier" "" { target *-*-* } .-1 } +} diff --git a/gcc/testsuite/rust/compile/empty_path3.rs b/gcc/testsuite/rust/compile/empty_path3.rs new file mode 100644 index 000000000000..551d0fefdff3 --- /dev/null +++ b/gcc/testsuite/rust/compile/empty_path3.rs @@ -0,0 +1,12 @@ +#![feature(no_core)] +#![no_core] + +// An empty path `::` in pattern position. The parser returned a path with no +// segments without reporting anything, so type checking dereferenced a null +// root type and the compiler crashed instead of diagnosing. Same defect as +// empty_path2.rs, which covers the let initialiser instead of the pattern. +// See Rust-GCC/gccrs#4790. +fn main() { + let :: = 1; + // { dg-error "expected identifier" "" { target *-*-* } .-1 } +}
