]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: Remove more calls to the old TyTy::BaseType::can_eq interface
authorPhilip Herron <herron.philip@googlemail.com>
Fri, 1 Aug 2025 10:54:52 +0000 (11:54 +0100)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 5 Aug 2025 14:37:02 +0000 (16:37 +0200)
This old can_eq interface was an old method to try and check if types can match up
by taking into account generics but its not maintained properly and we already have
a new wrapper Resolver::types_compatable (type, type, locus, emit_errors) which
just calls unify_site_and infer with commit false. There are only a few places left
to update.

One minor downside is that i need to remove const in some places because the unify
interface is non const. Ideally we would want to avoid writing a seperate const unify
anyway so i think this is not ideal but fine for us.

gcc/rust/ChangeLog:

* typecheck/rust-autoderef.cc: remove useless assertion
* typecheck/rust-coercion.cc (TypeCoercionRules::coerce_unsafe_ptr): refactor
(TypeCoercionRules::coerce_borrowed_pointer): remove FIXME this is fine
* typecheck/rust-hir-inherent-impl-overlap.h: use types_compatable
* typecheck/rust-hir-path-probe.cc (PathProbeType::PathProbeType): remove const
(PathProbeType::Probe): likewise
(PathProbeImplTrait::PathProbeImplTrait): likewise
(PathProbeImplTrait::Probe): likewise
* typecheck/rust-hir-path-probe.h: likewise
* typecheck/rust-hir-type-check-base.cc (walk_types_to_constrain): likewise
* typecheck/rust-hir-type-check-base.h: likewise
* typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::visit): use types_compatable
* typecheck/rust-hir-type-check.h: remove const
* typecheck/rust-typecheck-context.cc (TypeCheckContext::insert_associated_impl_mapping):
likewise
(TypeCheckContext::lookup_associated_impl_mapping_for_self): remove can_Eq
* typecheck/rust-tyty-bounds.cc (TypeBoundPredicate::is_equal): likewise
* typecheck/rust-tyty-subst.cc (SubstitutionArg::get_tyty): remove const version
* typecheck/rust-tyty-subst.h: likewise
* typecheck/rust-tyty.cc (BaseType::get_root): likewise
* typecheck/rust-tyty.h: likewise

gcc/testsuite/ChangeLog:

* rust/compile/generics8.rs: extra error message

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
16 files changed:
gcc/rust/typecheck/rust-autoderef.cc
gcc/rust/typecheck/rust-coercion.cc
gcc/rust/typecheck/rust-hir-inherent-impl-overlap.h
gcc/rust/typecheck/rust-hir-path-probe.cc
gcc/rust/typecheck/rust-hir-path-probe.h
gcc/rust/typecheck/rust-hir-type-check-base.cc
gcc/rust/typecheck/rust-hir-type-check-base.h
gcc/rust/typecheck/rust-hir-type-check-expr.cc
gcc/rust/typecheck/rust-hir-type-check.h
gcc/rust/typecheck/rust-typecheck-context.cc
gcc/rust/typecheck/rust-tyty-bounds.cc
gcc/rust/typecheck/rust-tyty-subst.cc
gcc/rust/typecheck/rust-tyty-subst.h
gcc/rust/typecheck/rust-tyty.cc
gcc/rust/typecheck/rust-tyty.h
gcc/testsuite/rust/compile/generics8.rs

index 825607431f07df8d4c5f8100f6e4d890e057d51f..10a59bd24f74f92a73d2fecd6b6891002a715c52 100644 (file)
@@ -247,7 +247,6 @@ resolve_operator_overload_fn (
          const TyTy::ADTType *adt = static_cast<const TyTy::ADTType *> (lhs);
 
          auto s = fn->get_self_type ()->get_root ();
-         rust_assert (s->can_eq (adt, false));
          rust_assert (s->get_kind () == TyTy::TypeKind::ADT);
          const TyTy::ADTType *self_adt
            = static_cast<const TyTy::ADTType *> (s);
index 50c74ed689d3d7a06646c38294e87252c642154c..fd12839c99bbde926cd7cd9ff537fb24b0dfa55e 100644 (file)
@@ -204,8 +204,9 @@ TypeCoercionRules::coerce_unsafe_ptr (TyTy::BaseType *receiver,
 
     default:
       {
-       // FIXME this can probably turn into a unify_and
-       if (receiver->can_eq (expected, false))
+       if (types_compatable (TyTy::TyWithLocation (receiver),
+                             TyTy::TyWithLocation (expected), UNKNOWN_LOCATION,
+                             false))
          return CoercionResult{{}, expected->clone ()};
 
        return CoercionResult::get_error ();
@@ -280,9 +281,6 @@ TypeCoercionRules::coerce_borrowed_pointer (TyTy::BaseType *receiver,
 
     default:
       {
-       // FIXME
-       // we might be able to replace this with a can_eq because we default
-       // back to a final unity anyway
        rust_debug ("coerce_borrowed_pointer -- unify");
        TyTy::BaseType *result
          = unify_site_and (receiver->get_ref (),
index 5537b14fbb261ddd1ea2c8e48aadc41568e3ba62..a66396f9f6f15c40d87c8a53555bdf9ddc87cbcd 100644 (file)
@@ -79,27 +79,29 @@ public:
            if (query == candidate)
              continue;
 
-           if (query->can_eq (candidate, false))
+           if (!types_compatable (TyTy::TyWithLocation (query),
+                                  TyTy::TyWithLocation (candidate),
+                                  UNKNOWN_LOCATION, false))
+             continue;
+
+           // we might be in the case that we have:
+           //
+           // *const T vs *const [T]
+           //
+           // so lets use an equality check when the
+           // candidates are both generic to be sure we dont emit a false
+           // positive
+
+           bool a = query->is_concrete ();
+           bool b = candidate->is_concrete ();
+           bool both_generic = !a && !b;
+           if (both_generic)
              {
-               // we might be in the case that we have:
-               //
-               // *const T vs *const [T]
-               //
-               // so lets use an equality check when the
-               // candidates are both generic to be sure we dont emit a false
-               // positive
-
-               bool a = query->is_concrete ();
-               bool b = candidate->is_concrete ();
-               bool both_generic = !a && !b;
-               if (both_generic)
-                 {
-                   if (!query->is_equal (*candidate))
-                     continue;
-                 }
-
-               possible_collision (it->second, iy->second);
+               if (!query->is_equal (*candidate))
+                 continue;
              }
+
+           possible_collision (it->second, iy->second);
          }
       }
   }
index 32e23997cb77a40f37d5863d5d6fc01fc3688ae7..c02702fbc250d2dc6bc83d0e051186d56fefd803 100644 (file)
@@ -137,7 +137,7 @@ PathProbeCandidate::operator< (const PathProbeCandidate &c) const
 
 // PathProbeType
 
-PathProbeType::PathProbeType (const TyTy::BaseType *receiver,
+PathProbeType::PathProbeType (TyTy::BaseType *receiver,
                              const HIR::PathIdentSegment &query,
                              DefId specific_trait_id)
   : TypeCheckBase (), receiver (receiver), search (query),
@@ -145,7 +145,7 @@ PathProbeType::PathProbeType (const TyTy::BaseType *receiver,
 {}
 
 std::set<PathProbeCandidate>
-PathProbeType::Probe (const TyTy::BaseType *receiver,
+PathProbeType::Probe (TyTy::BaseType *receiver,
                      const HIR::PathIdentSegment &segment_name,
                      bool probe_impls, bool probe_bounds,
                      bool ignore_mandatory_trait_items,
@@ -443,7 +443,7 @@ PathProbeType::is_receiver_generic () const
 
 // PathProbImplTrait
 
-PathProbeImplTrait::PathProbeImplTrait (const TyTy::BaseType *receiver,
+PathProbeImplTrait::PathProbeImplTrait (TyTy::BaseType *receiver,
                                        const HIR::PathIdentSegment &query,
                                        const TraitReference *trait_reference)
   : PathProbeType (receiver, query, UNKNOWN_DEFID),
@@ -451,7 +451,7 @@ PathProbeImplTrait::PathProbeImplTrait (const TyTy::BaseType *receiver,
 {}
 
 std::set<PathProbeCandidate>
-PathProbeImplTrait::Probe (const TyTy::BaseType *receiver,
+PathProbeImplTrait::Probe (TyTy::BaseType *receiver,
                           const HIR::PathIdentSegment &segment_name,
                           const TraitReference *trait_reference)
 {
index 59ffeb114e0aeed66344e1bb30dce77b5cdccf38..936bcb978cc0af00e937756a9420c4ba4697df2a 100644 (file)
@@ -108,9 +108,8 @@ class PathProbeType : public TypeCheckBase, public HIR::HIRImplVisitor
 {
 public:
   static std::set<PathProbeCandidate>
-  Probe (const TyTy::BaseType *receiver,
-        const HIR::PathIdentSegment &segment_name, bool probe_impls,
-        bool probe_bounds, bool ignore_mandatory_trait_items,
+  Probe (TyTy::BaseType *receiver, const HIR::PathIdentSegment &segment_name,
+        bool probe_impls, bool probe_bounds, bool ignore_mandatory_trait_items,
         DefId specific_trait_id = UNKNOWN_DEFID);
 
   void visit (HIR::TypeAlias &alias) override;
@@ -135,8 +134,8 @@ protected:
                                    bool ignore_mandatory_trait_items);
 
 protected:
-  PathProbeType (const TyTy::BaseType *receiver,
-                const HIR::PathIdentSegment &query, DefId specific_trait_id);
+  PathProbeType (TyTy::BaseType *receiver, const HIR::PathIdentSegment &query,
+                DefId specific_trait_id);
 
   std::vector<std::pair<const TraitReference *, HIR::ImplBlock *>>
   union_bounds (
@@ -147,7 +146,7 @@ protected:
 
   bool is_receiver_generic () const;
 
-  const TyTy::BaseType *receiver;
+  TyTy::BaseType *receiver;
   const HIR::PathIdentSegment &search;
   std::set<PathProbeCandidate> candidates;
   HIR::ImplBlock *current_impl;
@@ -178,12 +177,11 @@ class PathProbeImplTrait : public PathProbeType
 {
 public:
   static std::set<PathProbeCandidate>
-  Probe (const TyTy::BaseType *receiver,
-        const HIR::PathIdentSegment &segment_name,
+  Probe (TyTy::BaseType *receiver, const HIR::PathIdentSegment &segment_name,
         const TraitReference *trait_reference);
 
 private:
-  PathProbeImplTrait (const TyTy::BaseType *receiver,
+  PathProbeImplTrait (TyTy::BaseType *receiver,
                      const HIR::PathIdentSegment &query,
                      const TraitReference *trait_reference);
 
index 5c7690a6e76dd0384565ec804c97e35d9f7ad86e..68001bf17bed4b2a06490abb33a0a2cf0a281d68 100644 (file)
@@ -52,10 +52,10 @@ walk_types_to_constrain (std::set<HirId> &constrained_symbols,
 {
   for (const auto &c : constraints.get_mappings ())
     {
-      const TyTy::BaseType *arg = c.get_tyty ();
+      auto arg = c.get_tyty ();
       if (arg != nullptr)
        {
-         const TyTy::BaseType *p = arg->get_root ();
+         const auto p = arg->get_root ();
          constrained_symbols.insert (p->get_ty_ref ());
          if (p->has_substitutions_defined ())
            {
@@ -71,7 +71,7 @@ TypeCheckBase::check_for_unconstrained (
   const std::vector<TyTy::SubstitutionParamMapping> &params_to_constrain,
   const TyTy::SubstitutionArgumentMappings &constraint_a,
   const TyTy::SubstitutionArgumentMappings &constraint_b,
-  const TyTy::BaseType *reference)
+  TyTy::BaseType *reference)
 {
   bool check_result = false;
   bool check_completed
index 5934e57727f7385325e527f3e432b2155d03bc54..64300890af4cdd5f3e041b5baa6143358d8ce0a3 100644 (file)
@@ -53,7 +53,7 @@ protected:
     const std::vector<TyTy::SubstitutionParamMapping> &params_to_constrain,
     const TyTy::SubstitutionArgumentMappings &constraint_a,
     const TyTy::SubstitutionArgumentMappings &constraint_b,
-    const TyTy::BaseType *reference);
+    TyTy::BaseType *reference);
 
   TyTy::BaseType *resolve_literal (const Analysis::NodeMapping &mappings,
                                   HIR::Literal &literal, location_t locus);
index 2dc0e296e79baefe5f5ebb496b5abb248c69007a..438200ba4214c41399d0c5197f02099272c8ba91 100644 (file)
@@ -1025,7 +1025,10 @@ TypeCheckExpr::visit (HIR::ArrayIndexExpr &expr)
   bool ok = context->lookup_builtin ("usize", &size_ty);
   rust_assert (ok);
 
-  bool maybe_simple_array_access = index_expr_ty->can_eq (size_ty, false);
+  bool maybe_simple_array_access
+    = types_compatable (TyTy::TyWithLocation (index_expr_ty),
+                       TyTy::TyWithLocation (size_ty), expr.get_locus (),
+                       false);
   if (maybe_simple_array_access
       && direct_array_expr_ty->get_kind () == TyTy::TypeKind::ARRAY)
     {
index 80e403448359127203eb50f64f088db3f44f13a8..e5a6e9ee2f399ac51f39d195bbd41c269e9e37af 100644 (file)
@@ -249,10 +249,10 @@ public:
   bool lookup_associated_type_mapping (HirId id, HirId *mapping);
 
   void insert_associated_impl_mapping (HirId trait_id,
-                                      const TyTy::BaseType *impl_type,
+                                      TyTy::BaseType *impl_type,
                                       HirId impl_id);
   bool lookup_associated_impl_mapping_for_self (HirId trait_id,
-                                               const TyTy::BaseType *self,
+                                               TyTy::BaseType *self,
                                                HirId *mapping);
 
   void insert_autoderef_mappings (HirId id,
@@ -325,7 +325,7 @@ private:
   std::map<HirId, AssociatedImplTrait> associated_impl_traits;
 
   // trait-id -> list of < self-tyty:impl-id>
-  std::map<HirId, std::vector<std::pair<const TyTy::BaseType *, HirId>>>
+  std::map<HirId, std::vector<std::pair<TyTy::BaseType *, HirId>>>
     associated_traits_to_impls;
 
   std::map<HirId, HirId> associated_type_mappings;
index 83b17612d5e36d5aef7af775d3c0f05bca2df7c4..c74a92075d595ccc944b9f6b8e5151e6151f85a2 100644 (file)
@@ -300,8 +300,9 @@ TypeCheckContext::lookup_associated_type_mapping (HirId id, HirId *mapping)
 }
 
 void
-TypeCheckContext::insert_associated_impl_mapping (
-  HirId trait_id, const TyTy::BaseType *impl_type, HirId impl_id)
+TypeCheckContext::insert_associated_impl_mapping (HirId trait_id,
+                                                 TyTy::BaseType *impl_type,
+                                                 HirId impl_id)
 {
   auto it = associated_traits_to_impls.find (trait_id);
   if (it == associated_traits_to_impls.end ())
@@ -313,8 +314,9 @@ TypeCheckContext::insert_associated_impl_mapping (
 }
 
 bool
-TypeCheckContext::lookup_associated_impl_mapping_for_self (
-  HirId trait_id, const TyTy::BaseType *self, HirId *mapping)
+TypeCheckContext::lookup_associated_impl_mapping_for_self (HirId trait_id,
+                                                          TyTy::BaseType *self,
+                                                          HirId *mapping)
 {
   auto it = associated_traits_to_impls.find (trait_id);
   if (it == associated_traits_to_impls.end ())
@@ -322,7 +324,9 @@ TypeCheckContext::lookup_associated_impl_mapping_for_self (
 
   for (auto &item : it->second)
     {
-      if (item.first->can_eq (self, false))
+      if (types_compatable (TyTy::TyWithLocation (item.first),
+                           TyTy::TyWithLocation (self), UNKNOWN_LOCATION,
+                           false))
        {
          *mapping = item.second;
          return true;
index dd15e735f7317131221c1ef5107c95abb8fdca2e..6cf9b04b9c664fe2e6d634149bca913b1a78c4ba 100644 (file)
@@ -849,21 +849,19 @@ TypeBoundPredicate::is_equal (const TypeBoundPredicate &other) const
   // then match the generics applied
   for (size_t i = 0; i < get_num_substitutions (); i++)
     {
-      const SubstitutionParamMapping &a = substitutions.at (i);
-      const SubstitutionParamMapping &b = other.substitutions.at (i);
+      SubstitutionParamMapping a = substitutions.at (i);
+      SubstitutionParamMapping b = other.substitutions.at (i);
 
-      const auto ap = a.get_param_ty ();
-      const auto bp = b.get_param_ty ();
+      auto ap = a.get_param_ty ();
+      auto bp = b.get_param_ty ();
 
-      const BaseType *apd = ap->destructure ();
-      const BaseType *bpd = bp->destructure ();
+      BaseType *apd = ap->destructure ();
+      BaseType *bpd = bp->destructure ();
 
-      // FIXME use the unify_and infer inteface or try coerce
-      if (!apd->can_eq (bpd, false /*emit_errors*/))
-       {
-         if (!bpd->can_eq (apd, false /*emit_errors*/))
-           return false;
-       }
+      if (!Resolver::types_compatable (TyTy::TyWithLocation (apd),
+                                      TyTy::TyWithLocation (bpd),
+                                      UNKNOWN_LOCATION, false))
+       return false;
     }
 
   return true;
index 64791097b18d8b8c257e6e72c9a9d7a85b00c2f1..ead162a1898d2b87e6daea52ce3194fab3716d32 100644 (file)
@@ -234,12 +234,6 @@ SubstitutionArg::operator= (const SubstitutionArg &other)
 }
 
 BaseType *
-SubstitutionArg::get_tyty ()
-{
-  return argument;
-}
-
-const BaseType *
 SubstitutionArg::get_tyty () const
 {
   return argument;
index 69ef098969728bc48ba0ea058c546e3fc646b2c7..c1bc96a7f8a5198759fec124ddb5ce35dbe6034a 100644 (file)
@@ -151,9 +151,7 @@ public:
 
   SubstitutionArg &operator= (const SubstitutionArg &other);
 
-  BaseType *get_tyty ();
-
-  const BaseType *get_tyty () const;
+  BaseType *get_tyty () const;
 
   const SubstitutionParamMapping *get_param_mapping () const;
 
index d25f524a6cc9e3eccf683e8f524519f38310dab4..db967737b709300c1a40e11d3a6260a77109cd96 100644 (file)
@@ -450,11 +450,10 @@ BaseType::inherit_bounds (
     }
 }
 
-const BaseType *
-BaseType::get_root () const
+BaseType *
+BaseType::get_root ()
 {
-  // FIXME this needs to be it its own visitor class with a vector adjustments
-  const TyTy::BaseType *root = this;
+  TyTy::BaseType *root = this;
 
   if (const auto r = root->try_as<const ReferenceType> ())
     {
index 8f37645b9eb0b1ecc8a38610a0ae904daa50aa6b..49415ea0b31ae0e71a35d98a038d18fd6ad41020 100644 (file)
@@ -167,8 +167,7 @@ public:
 
   void debug () const;
 
-  // FIXME this will eventually go away
-  const BaseType *get_root () const;
+  BaseType *get_root ();
 
   // This will get the monomorphized type from Params, Placeholders or
   // Projections if available or error
index 88c4bac1b076a73adaae2becdd5d7289dc881331..2d30a9ec6025d08aabdae86fd6120ff51b8af3a7 100644 (file)
@@ -4,7 +4,7 @@ pub trait Sized {}
 struct Foo<A, B>(A, B);
 
 impl<T> Foo<i32, T> {
-    fn test(a: T) -> T {
+    fn test(a: T) -> T { // { dg-error "duplicate definitions with name .test." }
         a
     }
 }