]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: parser: Fix ICE on invalid arithmetic expression
authorHarishankar <harishankarpp7@gmail.com>
Sat, 31 Jan 2026 12:15:51 +0000 (17:45 +0530)
committerArthur Cohen <arthur.cohen@embecosm.com>
Fri, 27 Feb 2026 14:57:11 +0000 (15:57 +0100)
The compiler previously crashed with a segmentation fault when parsing
an arithmetic expression where the left-hand side was invalid (e.g.,
after a failed macro expansion). This occurred because the parser
attempted to access the location of the null 'left' pointer. This
patch adds a check to ensure the left operand is valid before proceeding.

Fixes Rust-GCC/gccrs#4414

gcc/rust/ChangeLog:

* parse/rust-parse-impl-expr.hxx (parse_arithmetic_or_logical_expr):
Check for null left operand to prevent segmentation fault.

gcc/testsuite/ChangeLog:

* rust/compile/issue-4414.rs: New test.

Signed-off-by: Harishankar <harishankarpp7@gmail.com>
gcc/rust/parse/rust-parse-impl-expr.hxx
gcc/testsuite/rust/compile/issue-4414.rs [new file with mode: 0644]

index 2a2d844b7fada5dd1f17fc2ca31166e472d531a1..713bcc8d00b14730499b281eb66e9ab957223abe 100644 (file)
@@ -1794,7 +1794,9 @@ Parser<ManagedTokenSource>::parse_expr (int right_binding_power,
     = null_denotation ({}, null_denotation_restrictions);
   if (!expr)
     return tl::unexpected<Parse::Error::Expr> (Parse::Error::Expr::CHILD_ERROR);
-
+  if (expr.value () == nullptr)
+    return tl::unexpected<Parse::Error::Expr> (Parse::Error::Expr::CHILD_ERROR);
+  
   return left_denotations (std::move (expr), right_binding_power,
                           std::move (outer_attrs), restrictions);
 }
diff --git a/gcc/testsuite/rust/compile/issue-4414.rs b/gcc/testsuite/rust/compile/issue-4414.rs
new file mode 100644 (file)
index 0000000..49f0ba8
--- /dev/null
@@ -0,0 +1,30 @@
+#![feature(no_core)]
+#![no_core]
+
+trait Speak : Sized {
+    fn say(&self, s:&str) -> String;
+    fn hi(&self) -> String { hello(self) }
+}
+
+fn hello<S:Speak>(s:&S) -> String{
+    s.say("hello")
+}
+
+impl Speak for assert_eq!(Some(3).hi(), "something!hello: 3".to_string()) { 
+    fn say(&self, s:&str) -> String {
+        format!("{}: {}", s, *self)
+    }
+}
+
+impl<T: Speak> Speak for Option<T> {
+    fn say(&self, s:&str) -> String {
+        match something!hello - none { // { dg-error "unexpected token 'identifier'|failed to parse scrutinee" }
+            None => format!("{} - none", s),
+            Some(ref x) => { format!("something!{}", x.say(s)) }
+        }
+    } 
+} // { dg-error "could not parse definition" }
+
+fn hello<S:Speak>(s:&S) -> String{ // { dg-error "failed to parse trait impl item" }
+    s.say("hello")
+}
\ No newline at end of file