public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r14-7863] gccrs: Track trait bound polarity properly
@ 2024-01-16 18:05 Arthur Cohen
  0 siblings, 0 replies; only message in thread
From: Arthur Cohen @ 2024-01-16 18:05 UTC (permalink / raw)
  To: gcc-cvs

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

commit r14-7863-gc533a11ae05bae53e4421810f7108aac697ded4e
Author: Philip Herron <herron.philip@googlemail.com>
Date:   Tue Jul 18 12:12:22 2023 +0100

    gccrs: Track trait bound polarity properly
    
    Trait bounds can have three forms in Rust the regular trait bound, '!' the
    negative trait bound to enforice that this trait must not be implmented and
    '?' the anti trait bound to remove this bound. This patch extends our
    Polarity enum to include the Anti trait bound and updates the HIR lowering
    code to track this properly.
    
    Addresses #2443
    
    gcc/rust/ChangeLog:
    
            * hir/rust-ast-lower-item.cc (ASTLoweringItem::visit): use new BoundPolarity enum
            * hir/rust-ast-lower-type.cc (ASTLoweringTypeBounds::visit): likewise
            * hir/rust-hir-dump.cc (BoundPolarityString): new helper
            (Dump::visit): update hir dump
            * hir/tree/rust-hir-item.h (class ImplBlock): likewise
            * hir/tree/rust-hir-type.h (class TraitBound): likewise
            * hir/tree/rust-hir.cc (TraitBound::as_string): fix as string
            * util/rust-common.h (enum Polarity): add new anti bound
            (enum BoundPolarity): likewise
            * util/rust-hir-map.cc (Mappings::Mappings): update naming
    
    Signed-off-by: Philip Herron <herron.philip@googlemail.com>

Diff:
---
 gcc/rust/hir/rust-ast-lower-item.cc |  6 ++++--
 gcc/rust/hir/rust-ast-lower-type.cc |  6 ++++--
 gcc/rust/hir/rust-hir-dump.cc       | 18 ++++++++++++++++--
 gcc/rust/hir/tree/rust-hir-item.h   |  6 +++---
 gcc/rust/hir/tree/rust-hir-type.h   |  8 ++++----
 gcc/rust/hir/tree/rust-hir.cc       | 16 +++++++++-------
 gcc/rust/util/rust-common.h         |  7 ++++---
 gcc/rust/util/rust-hir-map.cc       |  2 +-
 8 files changed, 45 insertions(+), 24 deletions(-)

diff --git a/gcc/rust/hir/rust-ast-lower-item.cc b/gcc/rust/hir/rust-ast-lower-item.cc
index 93659c2867a..6eaf75ffd0b 100644
--- a/gcc/rust/hir/rust-ast-lower-item.cc
+++ b/gcc/rust/hir/rust-ast-lower-item.cc
@@ -535,7 +535,7 @@ ASTLoweringItem::visit (AST::InherentImpl &impl_block)
       impl_item_ids.push_back (lowered->get_impl_mappings ().get_hirid ());
     }
 
-  Polarity polarity = Positive;
+  BoundPolarity polarity = BoundPolarity::RegularBound;
   HIR::ImplBlock *hir_impl_block = new HIR::ImplBlock (
     mapping, std::move (impl_items), std::move (generic_params),
     std::unique_ptr<HIR::Type> (impl_type), nullptr, where_clause, polarity,
@@ -683,7 +683,9 @@ ASTLoweringItem::visit (AST::TraitImpl &impl_block)
       impl_item_ids.push_back (lowered->get_impl_mappings ().get_hirid ());
     }
 
-  Polarity polarity = impl_block.is_exclam () ? Positive : Negative;
+  BoundPolarity polarity = impl_block.is_exclam ()
+			     ? BoundPolarity::RegularBound
+			     : BoundPolarity::NegativeBound;
   HIR::ImplBlock *hir_impl_block = new HIR::ImplBlock (
     mapping, std::move (impl_items), std::move (generic_params),
     std::unique_ptr<HIR::Type> (impl_type),
diff --git a/gcc/rust/hir/rust-ast-lower-type.cc b/gcc/rust/hir/rust-ast-lower-type.cc
index 768817b8fc3..5388f2139ce 100644
--- a/gcc/rust/hir/rust-ast-lower-type.cc
+++ b/gcc/rust/hir/rust-ast-lower-type.cc
@@ -525,9 +525,11 @@ ASTLoweringTypeBounds::visit (AST::TraitBound &bound)
 				 mappings->get_next_hir_id (crate_num),
 				 UNKNOWN_LOCAL_DEFID);
 
+  BoundPolarity polarity = bound.has_opening_question_mark ()
+			     ? BoundPolarity::AntiBound
+			     : BoundPolarity::RegularBound;
   translated = new HIR::TraitBound (mapping, *trait_path, bound.get_locus (),
-				    bound.is_in_parens (),
-				    bound.has_opening_question_mark ());
+				    bound.is_in_parens (), polarity);
 }
 
 void
diff --git a/gcc/rust/hir/rust-hir-dump.cc b/gcc/rust/hir/rust-hir-dump.cc
index bb1fea920bf..012e3cd90cf 100644
--- a/gcc/rust/hir/rust-hir-dump.cc
+++ b/gcc/rust/hir/rust-hir-dump.cc
@@ -52,6 +52,21 @@ std::string Dump::delims[2][2] = {
   {std::string ("["), std::string ("]")},
 };
 
+static std::string
+BoundPolarityString (BoundPolarity polarity)
+{
+  switch (polarity)
+    {
+    case RegularBound:
+      return "regular";
+    case NegativeBound:
+      return "negative";
+    case AntiBound:
+      return "anti";
+    }
+  return "unknown";
+}
+
 void
 Dump::go (HIR::Crate &e)
 {
@@ -2276,8 +2291,7 @@ Dump::visit (TraitBound &e)
   begin ("TraitBound");
   do_mappings (e.get_mappings ());
   put_field ("in_parens", std::to_string (e.get_in_parens ()));
-  put_field ("opening_question_mark",
-	     std::to_string (e.get_opening_question_mark ()));
+  put_field ("polarity", BoundPolarityString (e.get_polarity ()));
 
   visit_collection ("for_lifetime", e.get_for_lifetimes ());
   visit_field ("type_path", e.get_path ());
diff --git a/gcc/rust/hir/tree/rust-hir-item.h b/gcc/rust/hir/tree/rust-hir-item.h
index 75e02e98c5c..b174acd3186 100644
--- a/gcc/rust/hir/tree/rust-hir-item.h
+++ b/gcc/rust/hir/tree/rust-hir-item.h
@@ -2733,7 +2733,7 @@ class ImplBlock : public VisItem, public WithInnerAttrs
   std::unique_ptr<Type> impl_type;
   std::unique_ptr<TypePath> trait_ref;
   WhereClause where_clause;
-  Polarity polarity;
+  BoundPolarity polarity;
   location_t locus;
   std::vector<std::unique_ptr<ImplItem>> impl_items;
 
@@ -2743,7 +2743,7 @@ public:
 	     std::vector<std::unique_ptr<GenericParam>> generic_params,
 	     std::unique_ptr<Type> impl_type,
 	     std::unique_ptr<TypePath> trait_ref, WhereClause where_clause,
-	     Polarity polarity, Visibility vis, AST::AttrVec inner_attrs,
+	     BoundPolarity polarity, Visibility vis, AST::AttrVec inner_attrs,
 	     AST::AttrVec outer_attrs, location_t locus)
     : VisItem (std::move (mappings), std::move (vis), std::move (outer_attrs)),
       WithInnerAttrs (std::move (inner_attrs)),
@@ -2817,7 +2817,7 @@ public:
   bool has_where_clause () const { return !where_clause.is_empty (); }
 
   // Returns the polarity of the impl.
-  Polarity get_polarity () const { return polarity; }
+  BoundPolarity get_polarity () const { return polarity; }
 
   location_t get_locus () const override final { return locus; }
 
diff --git a/gcc/rust/hir/tree/rust-hir-type.h b/gcc/rust/hir/tree/rust-hir-type.h
index 37d108e2d3f..8f068ef230e 100644
--- a/gcc/rust/hir/tree/rust-hir-type.h
+++ b/gcc/rust/hir/tree/rust-hir-type.h
@@ -33,7 +33,7 @@ class Lifetime;
 class TraitBound : public TypeParamBound
 {
   bool in_parens;
-  bool opening_question_mark;
+  BoundPolarity polarity;
   std::vector<LifetimeParam> for_lifetimes;
   TypePath type_path;
   location_t locus;
@@ -46,10 +46,10 @@ public:
 
   TraitBound (Analysis::NodeMapping mapping, TypePath type_path,
 	      location_t locus, bool in_parens = false,
-	      bool opening_question_mark = false,
+	      BoundPolarity polarity = BoundPolarity::RegularBound,
 	      std::vector<LifetimeParam> for_lifetimes
 	      = std::vector<LifetimeParam> ())
-    : in_parens (in_parens), opening_question_mark (opening_question_mark),
+    : in_parens (in_parens), polarity (polarity),
       for_lifetimes (std::move (for_lifetimes)),
       type_path (std::move (type_path)), locus (locus), mappings (mapping)
   {}
@@ -67,7 +67,7 @@ public:
 
   std::vector<LifetimeParam> &get_for_lifetimes () { return for_lifetimes; }
   bool get_in_parens () { return in_parens; }
-  bool get_opening_question_mark () { return opening_question_mark; }
+  BoundPolarity get_polarity () { return polarity; }
 
   BoundType get_bound_type () const final override { return TRAITBOUND; }
 
diff --git a/gcc/rust/hir/tree/rust-hir.cc b/gcc/rust/hir/tree/rust-hir.cc
index 6e626c6f5d1..ac1ae63bf96 100644
--- a/gcc/rust/hir/tree/rust-hir.cc
+++ b/gcc/rust/hir/tree/rust-hir.cc
@@ -1986,14 +1986,16 @@ TraitBound::as_string () const
 {
   std::string str ("TraitBound:");
 
-  str += "\n Has opening question mark: ";
-  if (opening_question_mark)
+  switch (polarity)
     {
-      str += "true";
-    }
-  else
-    {
-      str += "false";
+    case RegularBound:
+      break;
+    case NegativeBound:
+      str += "!";
+      break;
+    case AntiBound:
+      str += "?";
+      break;
     }
 
   str += "\n For lifetimes: ";
diff --git a/gcc/rust/util/rust-common.h b/gcc/rust/util/rust-common.h
index ed1935d6f60..763771dceb5 100644
--- a/gcc/rust/util/rust-common.h
+++ b/gcc/rust/util/rust-common.h
@@ -37,10 +37,11 @@ enum Unsafety
   Normal
 };
 
-enum Polarity
+enum BoundPolarity
 {
-  Positive,
-  Negative
+  RegularBound,
+  NegativeBound,
+  AntiBound,
 };
 
 enum AsyncConstStatus
diff --git a/gcc/rust/util/rust-hir-map.cc b/gcc/rust/util/rust-hir-map.cc
index 9df7fe9e63a..c50836cfd72 100644
--- a/gcc/rust/util/rust-hir-map.cc
+++ b/gcc/rust/util/rust-hir-map.cc
@@ -100,7 +100,7 @@ Mappings::Mappings ()
   Analysis::NodeMapping node (0, 0, 0, 0);
   builtinMarker
     = new HIR::ImplBlock (node, {}, {}, nullptr, nullptr, HIR::WhereClause ({}),
-			  Positive,
+			  BoundPolarity::RegularBound,
 			  HIR::Visibility (HIR::Visibility::VisType::PUBLIC),
 			  {}, {}, UNDEF_LOCATION);
 }

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2024-01-16 18:05 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-01-16 18:05 [gcc r14-7863] gccrs: Track trait bound polarity properly Arthur Cohen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).