From: Utkarsh Bahuguna <[email protected]>

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]>
---
This change was merged into the gccrs repository and is posted here for
upstream visibility and potential drive-by review, as requested by GCC
release managers.
Each commit email contains a link to its details on github from where you can
find the Pull-Request and associated discussions.


Commit on github: 
https://github.com/Rust-GCC/gccrs/commit/9e6f1072efcf73d741471025b2ae74666af5eb4b

The commit has been mentioned in the following issue(s):
 - Rust-GCC/gccrs#4790: https://github.com/Rust-GCC/gccrs/issues/4790

The commit has been mentioned in the following pull-request(s):
 - https://github.com/Rust-GCC/gccrs/pull/4799

 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 4ca12e988..0cc517e28 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 49a5a45b9..b5f417685 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 000000000..55357db04
--- /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 000000000..551d0fefd
--- /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 }
+}

base-commit: e56b411c5faef5f596efc6c5553c154cbc98f0ef
-- 
2.55.0

Reply via email to