]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: fix ICE in borrows to invalid expressions
authorPhilip Herron <herron.philip@googlemail.com>
Tue, 7 Jan 2025 12:32:43 +0000 (12:32 +0000)
committerArthur Cohen <arthur.cohen@embecosm.com>
Fri, 21 Mar 2025 11:56:55 +0000 (12:56 +0100)
We need to check if the borrowed value is valid before creating the
reference type. Otherwise this will lead to an ICE.

Fixes Rust-GCC#3140

gcc/rust/ChangeLog:

* typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::visit): check for error
* typecheck/rust-tyty-call.cc (TypeCheckCallExpr::visit): likewise and remove debug error

gcc/testsuite/ChangeLog:

* rust/compile/issue-3046.rs: remove old error message
* rust/compile/nr2/exclude: nr2 cant handle this
* rust/compile/issue-3140.rs: New test.

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
gcc/rust/typecheck/rust-hir-type-check-expr.cc
gcc/rust/typecheck/rust-tyty-call.cc
gcc/testsuite/rust/compile/issue-3046.rs
gcc/testsuite/rust/compile/issue-3140.rs [new file with mode: 0644]
gcc/testsuite/rust/compile/nr2/exclude

index 5a96c359d7c509aeb7e190e99158650dce591dcb..2ea8b4127e69626a43529f7e50d0f51b44b0a36f 100644 (file)
@@ -1366,6 +1366,8 @@ void
 TypeCheckExpr::visit (HIR::BorrowExpr &expr)
 {
   TyTy::BaseType *resolved_base = TypeCheckExpr::Resolve (expr.get_expr ());
+  if (resolved_base->is<TyTy::ErrorType> ())
+    return;
 
   // In Rust this is valid because of DST's
   //
index 5fea34de40a21d66e22dd095fa84df199aba627d..2e0830e4adad959a58f20325eece83844f206b10 100644 (file)
@@ -140,13 +140,8 @@ TypeCheckCallExpr::visit (FnType &type)
     {
       location_t arg_locus = argument->get_locus ();
       auto argument_expr_tyty = Resolver::TypeCheckExpr::Resolve (*argument);
-      if (argument_expr_tyty->get_kind () == TyTy::TypeKind::ERROR)
-       {
-         rust_error_at (
-           argument->get_locus (),
-           "failed to resolve type for argument expr in CallExpr");
-         return;
-       }
+      if (argument_expr_tyty->is<TyTy::ErrorType> ())
+       return;
 
       // it might be a variadic function
       if (i < type.num_params ())
index c982cc98f58285fee0e8c48cb74339cba426a644..f0c72a3cef4b11f7ea02e0b7e199af5741999a5e 100644 (file)
@@ -12,12 +12,10 @@ fn test(v: LOption) -> Res {
     return Res::BAD;
 }
 
-
 fn main() {
     // Should be:
     // test(LOption::Some(2));
-    // 
+    //
     test(LOption(2));
     // { dg-error "expected function, tuple struct or tuple variant, found enum" "" { target *-*-* } .-1 }
-    // { dg-error "failed to resolve type for argument expr in CallExpr" "" { target *-*-* } .-2 }
 }
diff --git a/gcc/testsuite/rust/compile/issue-3140.rs b/gcc/testsuite/rust/compile/issue-3140.rs
new file mode 100644 (file)
index 0000000..dcf86db
--- /dev/null
@@ -0,0 +1,27 @@
+enum State {
+    Succeeded,
+    Failed,
+}
+
+fn print_on_failure(state: &State) {
+    let mut num = 0;
+    match *state {
+        // error: expected unit struct, unit variant or constant, found tuple
+        //        variant `State::Failed`
+        State::Failed => {
+            num = 1;
+        }
+        State::Succeeded => {
+            num = 2;
+        }
+        _ => (),
+    }
+}
+
+fn main() {
+    let b = State::Failed(1);
+    // { dg-error "expected function, tuple struct or tuple variant, found struct .State." "" { target *-*-* } .-1 }
+
+    print_on_failure(&b);
+    // { dg-error "cannot find value .b. in this scope" "" { target *-*-* } .-1 }
+}
index 945a697f677c85ee2fcd9bc5c2b5ea8565192234..e7344ed0d5970007be8ea2b94f5ab5172afd8da2 100644 (file)
@@ -195,4 +195,5 @@ issue-266.rs
 additional-trait-bounds2.rs
 auto_traits2.rs
 auto_traits3.rs
+issue-3140.rs
 # please don't delete the trailing newline