]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: Add name resolution and HIR lowering for ImplTraitType's
authorPhilip Herron <herron.philip@googlemail.com>
Sat, 15 Feb 2025 21:22:16 +0000 (21:22 +0000)
committerArthur Cohen <arthur.cohen@embecosm.com>
Mon, 24 Mar 2025 12:07:06 +0000 (13:07 +0100)
Our AST has ImplTraitType for multiple bounds and a singular
ImplTraitTypeOneBound, this patch desugars these into a simple
HIR::ImplTraitType. It also does the name resolution for this.

gcc/rust/ChangeLog:

* checks/errors/borrowck/rust-bir-builder-struct.h: remove HIR::ImplTraitTypeOneBound
* checks/errors/borrowck/rust-function-collector.h: likewise
* checks/errors/rust-const-checker.cc (ConstChecker::visit): likewise
* checks/errors/rust-const-checker.h: likewise
* checks/errors/rust-hir-pattern-analysis.cc (PatternChecker::visit): likewise
* checks/errors/rust-hir-pattern-analysis.h: likewise
* checks/errors/rust-unsafe-checker.cc (UnsafeChecker::visit): likewise
* checks/errors/rust-unsafe-checker.h: likewise
* hir/rust-ast-lower-type.cc (ASTLoweringType::translate): likewise
(ASTLoweringType::visit): likewise
* hir/rust-ast-lower-type.h: cleanup
* hir/rust-hir-dump.cc (Dump::visit): remove ImplTraitTypeOneBound
* hir/rust-hir-dump.h: likewise
* hir/tree/rust-hir-full-decls.h (class ImplTraitTypeOneBound): likewise
* hir/tree/rust-hir-type.h (class ImplTraitTypeOneBound): likewise
* hir/tree/rust-hir-visitor.h: likewise
* hir/tree/rust-hir.cc (ImplTraitTypeOneBound::as_string):  likewise
(ImplTraitTypeOneBound::accept_vis):  likewise
* resolve/rust-ast-resolve-type.cc (ResolveType::go): likewise
(ResolveType::visit): likewise
* resolve/rust-ast-resolve-type.h: add name resolution
* typecheck/rust-hir-type-check-type.h: likewise

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
19 files changed:
gcc/rust/checks/errors/borrowck/rust-bir-builder-struct.h
gcc/rust/checks/errors/borrowck/rust-function-collector.h
gcc/rust/checks/errors/rust-const-checker.cc
gcc/rust/checks/errors/rust-const-checker.h
gcc/rust/checks/errors/rust-hir-pattern-analysis.cc
gcc/rust/checks/errors/rust-hir-pattern-analysis.h
gcc/rust/checks/errors/rust-unsafe-checker.cc
gcc/rust/checks/errors/rust-unsafe-checker.h
gcc/rust/hir/rust-ast-lower-type.cc
gcc/rust/hir/rust-ast-lower-type.h
gcc/rust/hir/rust-hir-dump.cc
gcc/rust/hir/rust-hir-dump.h
gcc/rust/hir/tree/rust-hir-full-decls.h
gcc/rust/hir/tree/rust-hir-type.h
gcc/rust/hir/tree/rust-hir-visitor.h
gcc/rust/hir/tree/rust-hir.cc
gcc/rust/resolve/rust-ast-resolve-type.cc
gcc/rust/resolve/rust-ast-resolve-type.h
gcc/rust/typecheck/rust-hir-type-check-type.h

index 18ddc19db1ddbe3933fc9f06cb0cbf97b96c2ecc..94fcecd40edb547b2801bff329c15a6ff56e4940 100644 (file)
@@ -250,10 +250,6 @@ protected:
   void visit (HIR::ImplTraitType &type) override { rust_unreachable (); }
   void visit (HIR::TraitObjectType &type) override { rust_unreachable (); }
   void visit (HIR::ParenthesisedType &type) override { rust_unreachable (); }
-  void visit (HIR::ImplTraitTypeOneBound &type) override
-  {
-    rust_unreachable ();
-  }
   void visit (HIR::TupleType &type) override { rust_unreachable (); }
   void visit (HIR::NeverType &type) override { rust_unreachable (); }
   void visit (HIR::RawPointerType &type) override { rust_unreachable (); }
index 5de503d441ccabc2d19816f7e540703e265ea192..cdb20e8504a43690ecb98f4a2dd98fb5fea054b2 100644 (file)
@@ -180,7 +180,6 @@ public:
   void visit (HIR::ImplTraitType &type) override {}
   void visit (HIR::TraitObjectType &type) override {}
   void visit (HIR::ParenthesisedType &type) override {}
-  void visit (HIR::ImplTraitTypeOneBound &type) override {}
   void visit (HIR::TupleType &type) override {}
   void visit (HIR::NeverType &type) override {}
   void visit (HIR::RawPointerType &type) override {}
index 97b35288baedbffe3b3790cdb879186aabbd2b23..4904322b9382c9a76135478c94a59c011de30fd1 100644 (file)
@@ -846,10 +846,6 @@ void
 ConstChecker::visit (ParenthesisedType &)
 {}
 
-void
-ConstChecker::visit (ImplTraitTypeOneBound &)
-{}
-
 void
 ConstChecker::visit (TupleType &)
 {}
index 9a618b8f96d03f0e93ad672a3d8b1ca0634c75d9..00f5798865a77534fe8a05bf49b3256b64865134 100644 (file)
@@ -191,7 +191,6 @@ private:
   virtual void visit (ImplTraitType &type) override;
   virtual void visit (TraitObjectType &type) override;
   virtual void visit (ParenthesisedType &type) override;
-  virtual void visit (ImplTraitTypeOneBound &type) override;
   virtual void visit (TupleType &type) override;
   virtual void visit (NeverType &type) override;
   virtual void visit (RawPointerType &type) override;
index 79416b5d50a5bfdb35f608fd6eda538e772edc78..257f4cd0c08cbdac30861c4e47afd5209634569a 100644 (file)
@@ -684,10 +684,6 @@ void
 PatternChecker::visit (ParenthesisedType &)
 {}
 
-void
-PatternChecker::visit (ImplTraitTypeOneBound &)
-{}
-
 void
 PatternChecker::visit (TupleType &)
 {}
index 9c43d4143d2ae7efbd0bd7cbc97a74249079a879..2171340d4ad7dabd22d4f202889bf1c5420b05d3 100644 (file)
@@ -164,7 +164,6 @@ private:
   virtual void visit (ImplTraitType &type) override;
   virtual void visit (TraitObjectType &type) override;
   virtual void visit (ParenthesisedType &type) override;
-  virtual void visit (ImplTraitTypeOneBound &type) override;
   virtual void visit (TupleType &type) override;
   virtual void visit (NeverType &type) override;
   virtual void visit (RawPointerType &type) override;
index fadfd9dea83ae2054c9cf9998108bab33f2e5913..8aa59ee9e2843061d2ec0c6c1f74119f36b74de4 100644 (file)
@@ -956,10 +956,6 @@ void
 UnsafeChecker::visit (ParenthesisedType &)
 {}
 
-void
-UnsafeChecker::visit (ImplTraitTypeOneBound &)
-{}
-
 void
 UnsafeChecker::visit (TupleType &)
 {}
index 8dc6ab790716f435298a1850982b07ef287ba268..63098fe5a7dbfd5361992d2d23bced5318206f1f 100644 (file)
@@ -172,7 +172,6 @@ private:
   virtual void visit (ImplTraitType &type) override;
   virtual void visit (TraitObjectType &type) override;
   virtual void visit (ParenthesisedType &type) override;
-  virtual void visit (ImplTraitTypeOneBound &type) override;
   virtual void visit (TupleType &type) override;
   virtual void visit (NeverType &type) override;
   virtual void visit (RawPointerType &type) override;
index 8df418b272d4d330e866228e3a9bb89d67203b71..d3e528dc925a095ab8f29d0c31982159ab4f8859 100644 (file)
@@ -477,6 +477,42 @@ ASTLoweringType::visit (AST::ParenthesisedType &type)
                                  type.get_locus ());
 }
 
+void
+ASTLoweringType::visit (AST::ImplTraitType &type)
+{
+  std::vector<std::unique_ptr<HIR::TypeParamBound>> bounds;
+  for (auto &bound : type.get_type_param_bounds ())
+    {
+      auto b = ASTLoweringTypeBounds::translate (*bound.get ());
+      bounds.push_back (std::unique_ptr<HIR::TypeParamBound> (b));
+    }
+
+  auto crate_num = mappings.get_current_crate ();
+  Analysis::NodeMapping mapping (crate_num, type.get_node_id (),
+                                mappings.get_next_hir_id (crate_num),
+                                mappings.get_next_localdef_id (crate_num));
+
+  translated
+    = new HIR::ImplTraitType (mapping, std::move (bounds), type.get_locus ());
+}
+
+void
+ASTLoweringType::visit (AST::ImplTraitTypeOneBound &type)
+{
+  std::vector<std::unique_ptr<HIR::TypeParamBound>> bounds;
+
+  auto b = ASTLoweringTypeBounds::translate (type.get_trait_bound ());
+  bounds.push_back (std::unique_ptr<HIR::TypeParamBound> (b));
+
+  auto crate_num = mappings.get_current_crate ();
+  Analysis::NodeMapping mapping (crate_num, type.get_node_id (),
+                                mappings.get_next_hir_id (crate_num),
+                                mappings.get_next_localdef_id (crate_num));
+
+  translated
+    = new HIR::ImplTraitType (mapping, std::move (bounds), type.get_locus ());
+}
+
 HIR::GenericParam *
 ASTLowerGenericParam::translate (AST::GenericParam &param)
 {
index af0b4bec91945897d3cf827d21dfffb2829a9851..4efaeee13149238f3dbe47d2c6affa7ba9d6a5df 100644 (file)
@@ -82,6 +82,9 @@ public:
   void visit (AST::TraitObjectType &type) override;
   void visit (AST::ParenthesisedType &type) override;
 
+  void visit (AST::ImplTraitType &type) override;
+  void visit (AST::ImplTraitTypeOneBound &type) override;
+
 private:
   ASTLoweringType (bool default_to_static_lifetime)
     : ASTLoweringBase (),
index 0bfcc97afe7b963e5f6f1f8932334110529c1890..686127750b472dcd59405ce9e8b71bb124676ca8 100644 (file)
@@ -2332,15 +2332,6 @@ Dump::visit (ParenthesisedType &e)
   end ("ParenthesisedType");
 }
 
-void
-Dump::visit (ImplTraitTypeOneBound &e)
-{
-  begin ("ImplTraitTypeOneBound");
-  do_type (e);
-  visit_field ("trait_bound", e.get_trait_bound ());
-  end ("ImplTraitTypeOneBound");
-}
-
 void
 Dump::visit (TupleType &e)
 {
index 8683dee9d6c03de56aadf6aa7a74ee7f1b0e5746..afcd668372810cbd6b0bb785861f7c424c109fa3 100644 (file)
@@ -237,7 +237,6 @@ private:
   virtual void visit (ImplTraitType &) override;
   virtual void visit (TraitObjectType &) override;
   virtual void visit (ParenthesisedType &) override;
-  virtual void visit (ImplTraitTypeOneBound &) override;
   virtual void visit (TupleType &) override;
   virtual void visit (NeverType &) override;
   virtual void visit (RawPointerType &) override;
index c64c8bf65136e33aea8bbf1211aff430c9bca99b..6c19f24e0ad03a0bde2707d224a138131edb0888 100644 (file)
@@ -211,7 +211,6 @@ class TraitBound;
 class ImplTraitType;
 class TraitObjectType;
 class ParenthesisedType;
-class ImplTraitTypeOneBound;
 class TupleType;
 class NeverType;
 class RawPointerType;
index e231d78b2ceddeac29cefe963fbc1a2bdd42c275..bd0f2b6213e5ee368cd14ac719659032a6dbf080 100644 (file)
@@ -164,38 +164,6 @@ public:
   void accept_vis (HIRTypeVisitor &vis) override;
 };
 
-// Impl trait with a single bound? Poor reference material here.
-class ImplTraitTypeOneBound : public TypeNoBounds
-{
-  TraitBound trait_bound;
-
-protected:
-  /* Use covariance to implement clone function as returning this object rather
-   * than base */
-  ImplTraitTypeOneBound *clone_type_impl () const override
-  {
-    return new ImplTraitTypeOneBound (*this);
-  }
-
-  /* Use covariance to implement clone function as returning this object rather
-   * than base */
-  ImplTraitTypeOneBound *clone_type_no_bounds_impl () const override
-  {
-    return new ImplTraitTypeOneBound (*this);
-  }
-
-public:
-  ImplTraitTypeOneBound (Analysis::NodeMapping mappings, TraitBound trait_bound,
-                        location_t locus)
-    : TypeNoBounds (mappings, locus), trait_bound (std::move (trait_bound))
-  {}
-
-  std::string as_string () const override;
-  TraitBound &get_trait_bound () { return trait_bound; }
-  void accept_vis (HIRFullVisitor &vis) override;
-  void accept_vis (HIRTypeVisitor &vis) override;
-};
-
 /* A type consisting of the "product" of others (the tuple's elements) in a
  * specific order */
 class TupleType : public TypeNoBounds
index 33e6b7bc70853b7d647b4016de64558b2763cb99..800e64767a418f8eb1373e07ae9b3325cbcc248e 100644 (file)
@@ -142,7 +142,6 @@ public:
   virtual void visit (ImplTraitType &type) = 0;
   virtual void visit (TraitObjectType &type) = 0;
   virtual void visit (ParenthesisedType &type) = 0;
-  virtual void visit (ImplTraitTypeOneBound &type) = 0;
   virtual void visit (TupleType &type) = 0;
   virtual void visit (NeverType &type) = 0;
   virtual void visit (RawPointerType &type) = 0;
@@ -290,7 +289,6 @@ public:
   virtual void visit (ImplTraitType &) override {}
   virtual void visit (TraitObjectType &) override {}
   virtual void visit (ParenthesisedType &) override {}
-  virtual void visit (ImplTraitTypeOneBound &) override {}
   virtual void visit (TupleType &) override {}
   virtual void visit (NeverType &) override {}
   virtual void visit (RawPointerType &) override {}
@@ -354,7 +352,6 @@ public:
   virtual void visit (ImplTraitType &type) = 0;
   virtual void visit (TraitObjectType &type) = 0;
   virtual void visit (ParenthesisedType &type) = 0;
-  virtual void visit (ImplTraitTypeOneBound &type) = 0;
   virtual void visit (TupleType &type) = 0;
   virtual void visit (NeverType &type) = 0;
   virtual void visit (RawPointerType &type) = 0;
index 22b0867305c99d4d42b78d32642147b5a453564b..822eaffcb7c18d6fa9b17eddfb8a31dc52f4272d 100644 (file)
@@ -2865,14 +2865,6 @@ BareFunctionType::as_string () const
   return str;
 }
 
-std::string
-ImplTraitTypeOneBound::as_string () const
-{
-  std::string str ("ImplTraitTypeOneBound: \n TraitBound: ");
-
-  return str + trait_bound.as_string ();
-}
-
 std::string
 TypePathSegmentGeneric::as_string () const
 {
@@ -4525,12 +4517,6 @@ ParenthesisedType::accept_vis (HIRFullVisitor &vis)
   vis.visit (*this);
 }
 
-void
-ImplTraitTypeOneBound::accept_vis (HIRFullVisitor &vis)
-{
-  vis.visit (*this);
-}
-
 void
 TupleType::accept_vis (HIRFullVisitor &vis)
 {
@@ -4723,12 +4709,6 @@ ArrayType::accept_vis (HIRTypeVisitor &vis)
   vis.visit (*this);
 }
 
-void
-ImplTraitTypeOneBound::accept_vis (HIRTypeVisitor &vis)
-{
-  vis.visit (*this);
-}
-
 void
 BareFunctionType::accept_vis (HIRTypeVisitor &vis)
 {
index 63c9daca565fcf860e713e1ee5930a9310671afe..f0e5468a2451e8e2c55a787b0ce4a64e791d22b1 100644 (file)
@@ -27,6 +27,49 @@ namespace Resolver {
 
 // rust-ast-resolve-type.h
 
+NodeId
+ResolveType::go (AST::Type &type)
+{
+  ResolveType resolver;
+  type.accept_vis (resolver);
+  return resolver.resolved_node;
+}
+
+void
+ResolveType::visit (AST::BareFunctionType &fntype)
+{
+  for (auto &param : fntype.get_function_params ())
+    ResolveType::go (param.get_type ());
+
+  if (fntype.has_return_type ())
+    ResolveType::go (fntype.get_return_type ());
+}
+
+void
+ResolveType::visit (AST::TupleType &tuple)
+{
+  if (tuple.is_unit_type ())
+    {
+      resolved_node = resolver->get_unit_type_node_id ();
+      return;
+    }
+
+  for (auto &elem : tuple.get_elems ())
+    ResolveType::go (*elem);
+}
+
+void
+ResolveType::visit (AST::TypePath &path)
+{
+  ResolveRelativeTypePath::go (path, resolved_node);
+}
+
+void
+ResolveType::visit (AST::QualifiedPathInType &path)
+{
+  ResolveRelativeQualTypePath::go (path);
+}
+
 void
 ResolveType::visit (AST::ArrayType &type)
 {
@@ -72,7 +115,7 @@ ResolveType::visit (AST::RawPointerType &type)
 void
 ResolveType::visit (AST::InferredType &)
 {
-  // FIXME
+  // nothing to do
 }
 
 void
@@ -87,6 +130,19 @@ ResolveType::visit (AST::SliceType &type)
   resolved_node = ResolveType::go (type.get_elem_type ());
 }
 
+void
+ResolveType::visit (AST::ImplTraitType &type)
+{
+  for (auto &bound : type.get_type_param_bounds ())
+    ResolveTypeBound::go (*bound);
+}
+
+void
+ResolveType::visit (AST::ImplTraitTypeOneBound &type)
+{
+  ResolveTypeBound::go (type.get_trait_bound ());
+}
+
 // resolve relative type-paths
 
 bool
index d3b60b6ce78d97660a4df814d678eeaee246720c..7edcec9e1f65c016e44717ae7458cd66e47da158 100644 (file)
@@ -61,61 +61,23 @@ class ResolveType : public ResolverBase
   using Rust::Resolver::ResolverBase::visit;
 
 public:
-  static NodeId go (AST::Type &type)
-  {
-    ResolveType resolver;
-    type.accept_vis (resolver);
-    return resolver.resolved_node;
-  }
-
-  void visit (AST::BareFunctionType &fntype) override
-  {
-    for (auto &param : fntype.get_function_params ())
-      ResolveType::go (param.get_type ());
-
-    if (fntype.has_return_type ())
-      ResolveType::go (fntype.get_return_type ());
-  }
-
-  void visit (AST::TupleType &tuple) override
-  {
-    if (tuple.is_unit_type ())
-      {
-       resolved_node = resolver->get_unit_type_node_id ();
-       return;
-      }
-
-    for (auto &elem : tuple.get_elems ())
-      ResolveType::go (*elem);
-  }
-
-  void visit (AST::TypePath &path) override
-  {
-    ResolveRelativeTypePath::go (path, resolved_node);
-  }
-
-  void visit (AST::QualifiedPathInType &path) override
-  {
-    ResolveRelativeQualTypePath::go (path);
-  }
+  static NodeId go (AST::Type &type);
 
+  void visit (AST::BareFunctionType &fntype) override;
+  void visit (AST::TupleType &tuple) override;
+  void visit (AST::TypePath &path) override;
+  void visit (AST::QualifiedPathInType &path) override;
   void visit (AST::ArrayType &type) override;
-
   void visit (AST::ReferenceType &type) override;
-
   void visit (AST::InferredType &type) override;
-
   void visit (AST::NeverType &type) override;
-
   void visit (AST::RawPointerType &type) override;
-
   void visit (AST::TraitObjectTypeOneBound &type) override;
-
   void visit (AST::TraitObjectType &type) override;
-
   void visit (AST::ParenthesisedType &type) override;
-
   void visit (AST::SliceType &type) override;
+  void visit (AST::ImplTraitType &type) override;
+  void visit (AST::ImplTraitTypeOneBound &type) override;
 
 private:
   ResolveType () : ResolverBase () {}
index 4c2e6a2f72a446d64763ab08fce7a1517843e8a0..558dc5cd0b62f949e19cb2104b28379ebe4c0b15 100644 (file)
@@ -70,9 +70,6 @@ public:
   void visit (HIR::ImplTraitType &type) override
   { /* TODO */
   }
-  void visit (HIR::ImplTraitTypeOneBound &type) override
-  { /* TODO */
-  }
 
 private:
   TypeCheckType (HirId id)