https://gcc.gnu.org/g:f96905887701cce16f80e701f3944e1476ae53ef

commit r16-4842-gf96905887701cce16f80e701f3944e1476ae53ef
Author: Philip Herron <[email protected]>
Date:   Tue Aug 19 19:34:38 2025 +0100

    gccrs: Add implicit infer support for unify on const types
    
    gcc/rust/ChangeLog:
    
            * typecheck/rust-unify.cc (UnifyRules::commit): commit hook update
            (UnifyRules::go): insert implicit infer const types
    
    Signed-off-by: Philip Herron <[email protected]>

Diff:
---
 gcc/rust/typecheck/rust-unify.cc | 39 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)

diff --git a/gcc/rust/typecheck/rust-unify.cc b/gcc/rust/typecheck/rust-unify.cc
index caecf2e1831e..10e4b6b3806e 100644
--- a/gcc/rust/typecheck/rust-unify.cc
+++ b/gcc/rust/typecheck/rust-unify.cc
@@ -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> ())

Reply via email to