]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: unify: Always coerce `!` to the target type.
authorArthur Cohen <arthur.cohen@embecosm.com>
Wed, 6 Mar 2024 15:19:46 +0000 (16:19 +0100)
committerArthur Cohen <arthur.cohen@embecosm.com>
Thu, 1 Aug 2024 14:52:29 +0000 (16:52 +0200)
Never can... never... exist, so it should always be coerced to the type
it is being matched against. This is useful for breaking off of a loop
from inside a match, or an if condition, for example.

gcc/rust/ChangeLog:

* typecheck/rust-unify.cc (UnifyRules::go): Always unify to `ltype` if
we are matching against a `Never` in `rtype`.
(UnifyRules::expect_never): Always unify to the expected type.

gcc/testsuite/ChangeLog:

* rust/compile/match-never-ltype.rs: New test.
* rust/compile/match-never-rtype.rs: New test.

gcc/rust/typecheck/rust-unify.cc
gcc/testsuite/rust/compile/match-never-ltype.rs [new file with mode: 0644]
gcc/testsuite/rust/compile/match-never-rtype.rs [new file with mode: 0644]

index 8b43380cc59271d8f9eca7c3642b382b6e214af7..7d1042d8e63f6f757d8b610750e9c675525fffac 100644 (file)
@@ -17,6 +17,7 @@
 // <http://www.gnu.org/licenses/>.
 
 #include "rust-unify.h"
+#include "rust-tyty.h"
 
 namespace Rust {
 namespace Resolver {
@@ -237,6 +238,15 @@ UnifyRules::go ()
        }
     }
 
+  // The never type should always get coerced to the type it's being matched
+  // against, so in that case, ltype. This avoids doing the same check in all
+  // the `expect_*` functions.
+  // However, this does not work if we have an annoying ltype - like INFER.
+  // TODO: Is ltype == Infer the only special case here? What about projections?
+  // references?
+  if (rtype->get_kind () == TyTy::NEVER && ltype->get_kind () != TyTy::INFER)
+    return ltype->clone ();
+
   switch (ltype->get_kind ())
     {
     case TyTy::INFER:
@@ -1536,32 +1546,8 @@ UnifyRules::expect_never (TyTy::NeverType *ltype, TyTy::BaseType *rtype)
       }
       break;
 
-    case TyTy::NEVER:
+    default:
       return rtype->clone ();
-
-    case TyTy::PLACEHOLDER:
-    case TyTy::PROJECTION:
-    case TyTy::DYNAMIC:
-    case TyTy::CLOSURE:
-    case TyTy::SLICE:
-    case TyTy::PARAM:
-    case TyTy::POINTER:
-    case TyTy::STR:
-    case TyTy::ADT:
-    case TyTy::REF:
-    case TyTy::ARRAY:
-    case TyTy::FNDEF:
-    case TyTy::FNPTR:
-    case TyTy::TUPLE:
-    case TyTy::BOOL:
-    case TyTy::CHAR:
-    case TyTy::INT:
-    case TyTy::UINT:
-    case TyTy::FLOAT:
-    case TyTy::USIZE:
-    case TyTy::ISIZE:
-    case TyTy::ERROR:
-      return new TyTy::ErrorType (0);
     }
   return new TyTy::ErrorType (0);
 }
diff --git a/gcc/testsuite/rust/compile/match-never-ltype.rs b/gcc/testsuite/rust/compile/match-never-ltype.rs
new file mode 100644 (file)
index 0000000..6516ab3
--- /dev/null
@@ -0,0 +1,17 @@
+fn foo() {}
+
+enum Foo {
+    A,
+    B,
+}
+
+fn main() {
+    let a = Foo::A;
+
+    loop {
+        match a {
+            Foo::A => break,
+            Foo::B => foo(),
+        }
+    }
+}
diff --git a/gcc/testsuite/rust/compile/match-never-rtype.rs b/gcc/testsuite/rust/compile/match-never-rtype.rs
new file mode 100644 (file)
index 0000000..6e4e763
--- /dev/null
@@ -0,0 +1,17 @@
+fn foo() {}
+
+enum Foo {
+    A,
+    B,
+}
+
+fn main() {
+    let a = Foo::A;
+
+    loop {
+        match a {
+            Foo::B => foo(),
+            Foo::A => break,
+        }
+    }
+}