[COMMITTED 03/83] gccrs: Report an error on an empty path expression
arthur.cohen@opensrcsec.com
arthur.cohen@opensrcsec.com
Wed Sep 16 12:29:22 GMT 2026
From: Utkarsh Bahuguna <utkarshbahuguna10@gmail.com>
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 <utkarshbahuguna10@gmail.com>
---
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(-)
create mode 100644 gcc/testsuite/rust/compile/empty_path2.rs
create mode 100644 gcc/testsuite/rust/compile/empty_path3.rs
diff --git a/gcc/rust/parse/rust-parse-impl-path.hxx b/gcc/rust/parse/rust-parse-impl-path.hxx
index 4ca12e98893..0cc517e2815 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 49a5a45b9f4..b5f41768541 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 00000000000..55357db0461
--- /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 00000000000..551d0fefdff
--- /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 }
+}
--
2.50.1
More information about the Gcc-rust
mailing list