]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: Add implicit infer support for unify on const types
authorPhilip Herron <herron.philip@googlemail.com>
Tue, 19 Aug 2025 18:34:38 +0000 (19:34 +0100)
committerArthur Cohen <arthur.cohen@embecosm.com>
Thu, 30 Oct 2025 19:59:04 +0000 (20:59 +0100)
gcc/rust/ChangeLog:

* typecheck/rust-unify.cc (UnifyRules::commit): commit hook update
(UnifyRules::go): insert implicit infer const types

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
gcc/rust/typecheck/rust-unify.cc

index caecf2e1831eb2e3ed9520204c3cac6bdcfa3452..10e4b6b3806e87e79c658ccde47fe1d6de5a3cec 100644 (file)
@@ -137,6 +137,16 @@ UnifyRules::commit (TyTy::BaseType *base, TyTy::BaseType *other,
          // if any of the types are inference variables lets fix them
          if (ref_tyty->is<TyTy::InferType> ())
            context.insert_implicit_type (ref, resolved);
+         else if (resolved->is<TyTy::ConstType> ()
+                  && ref_tyty->is<TyTy::ConstType> ())
+           {
+             auto &const_expr = *static_cast<TyTy::ConstType *> (resolved);
+             if (const_expr.get_const_kind ()
+                 == TyTy::ConstType::ConstKind::Value)
+               {
+                 context.insert_implicit_type (ref, resolved);
+               }
+           }
        }
     }
 }
@@ -264,6 +274,35 @@ UnifyRules::go ()
          // set the rtype now to the new inference var
          ltype = i;
        }
+      else if (ltype->is<TyTy::ConstType> () && rtype->is<TyTy::ConstType> ())
+       {
+         const auto &lhs = *static_cast<TyTy::ConstType *> (ltype);
+         const auto &rhs = *static_cast<TyTy::ConstType *> (rtype);
+
+         bool both_are_decls
+           = lhs.get_const_kind () == TyTy::ConstType::ConstKind::Decl
+             && rhs.get_const_kind () == TyTy::ConstType::ConstKind::Decl;
+         bool have_decls
+           = lhs.get_const_kind () == TyTy::ConstType::ConstKind::Decl
+             || rhs.get_const_kind () == TyTy::ConstType::ConstKind::Decl;
+
+         if (have_decls && !both_are_decls)
+           {
+             if (lhs.get_const_kind () == TyTy::ConstType::ConstKind::Decl)
+               {
+                 TyTy::TyVar iv = TyTy::TyVar::get_implicit_const_infer_var (
+                   lhs, lhs.get_locus ());
+                 ltype = iv.get_tyty ();
+               }
+             else if (rhs.get_const_kind ()
+                      == TyTy::ConstType::ConstKind::Decl)
+               {
+                 TyTy::TyVar iv = TyTy::TyVar::get_implicit_const_infer_var (
+                   rhs, rhs.get_locus ());
+                 rtype = iv.get_tyty ();
+               }
+           }
+       }
     }
 
   if ((ltype->is<TyTy::ConstType> () || rtype->is<TyTy::ConstType> ())