public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Arthur Cohen <cohenarthur@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r14-8609] gccrs: ast: Infer static lifetime for const and static items
Date: Tue, 30 Jan 2024 12:01:32 +0000 (GMT)	[thread overview]
Message-ID: <20240130120132.DC55F385828A@sourceware.org> (raw)

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

commit r14-8609-gbc1a47f72de0b40a7f26372f8407c0492e10403e
Author: Jakub Dupak <dev@jakubdupak.com>
Date:   Sun Dec 3 12:30:08 2023 +0100

    gccrs: ast: Infer static lifetime for const and static items
    
    (probably incomplete propagation)
    
    gcc/rust/ChangeLog:
    
            * hir/rust-ast-lower-base.cc (ASTLoweringBase::lower_lifetime): Propagate static
            requirement.
            * hir/rust-ast-lower-base.h: Propagate static requirement.
            * hir/rust-ast-lower-implitem.h: Propagate static requirement.
            * hir/rust-ast-lower-item.cc (ASTLoweringItem::visit): Propagate static requirement.
            * hir/rust-ast-lower-type.cc (ASTLoweringType::translate): Propagate static requirement.
            (ASTLoweringType::visit): Propagate static requirement.
            * hir/rust-ast-lower-type.h: Propagate static requirement.
    
    Signed-off-by: Jakub Dupak <dev@jakubdupak.com>

Diff:
---
 gcc/rust/hir/rust-ast-lower-base.cc    | 14 +++++++++++---
 gcc/rust/hir/rust-ast-lower-base.h     |  3 ++-
 gcc/rust/hir/rust-ast-lower-implitem.h |  3 ++-
 gcc/rust/hir/rust-ast-lower-item.cc    |  5 +++--
 gcc/rust/hir/rust-ast-lower-type.cc    | 28 ++++++++++++++++++----------
 gcc/rust/hir/rust-ast-lower-type.h     | 12 ++++++++++--
 6 files changed, 46 insertions(+), 19 deletions(-)

diff --git a/gcc/rust/hir/rust-ast-lower-base.cc b/gcc/rust/hir/rust-ast-lower-base.cc
index f8438557707e..748cec74829c 100644
--- a/gcc/rust/hir/rust-ast-lower-base.cc
+++ b/gcc/rust/hir/rust-ast-lower-base.cc
@@ -530,16 +530,24 @@ ASTLoweringBase::visit (AST::SelfParam &param)
 {}
 
 HIR::Lifetime
-ASTLoweringBase::lower_lifetime (AST::Lifetime &lifetime)
+ASTLoweringBase::lower_lifetime (AST::Lifetime &lifetime,
+				 bool default_to_static_lifetime)
 {
+  auto lifetime_type = lifetime.get_lifetime_type ();
+  if (lifetime_type == AST::Lifetime::WILDCARD && default_to_static_lifetime)
+    {
+      // If compiling in a static context.
+      lifetime_type = AST::Lifetime::STATIC;
+    }
+
   auto crate_num = mappings->get_current_crate ();
   Analysis::NodeMapping mapping (crate_num, lifetime.get_node_id (),
 				 mappings->get_next_hir_id (crate_num),
 				 UNKNOWN_LOCAL_DEFID);
   mappings->insert_node_to_hir (mapping.get_nodeid (), mapping.get_hirid ());
 
-  return HIR::Lifetime (mapping, lifetime.get_lifetime_type (),
-			lifetime.get_lifetime_name (), lifetime.get_locus ());
+  return HIR::Lifetime (mapping, lifetime_type, lifetime.get_lifetime_name (),
+			lifetime.get_locus ());
 }
 
 HIR::LoopLabel
diff --git a/gcc/rust/hir/rust-ast-lower-base.h b/gcc/rust/hir/rust-ast-lower-base.h
index d52afd3421f8..8da11750d18b 100644
--- a/gcc/rust/hir/rust-ast-lower-base.h
+++ b/gcc/rust/hir/rust-ast-lower-base.h
@@ -264,7 +264,8 @@ protected:
   Analysis::Mappings *mappings;
   Analysis::BuiltinAttributeMappings *attr_mappings;
 
-  HIR::Lifetime lower_lifetime (AST::Lifetime &lifetime);
+  HIR::Lifetime lower_lifetime (AST::Lifetime &lifetime,
+				bool default_to_static_lifetime = false);
 
   HIR::LoopLabel lower_loop_label (AST::LoopLabel &loop_label);
 
diff --git a/gcc/rust/hir/rust-ast-lower-implitem.h b/gcc/rust/hir/rust-ast-lower-implitem.h
index 6f904dde19f0..81dae14cdcaa 100644
--- a/gcc/rust/hir/rust-ast-lower-implitem.h
+++ b/gcc/rust/hir/rust-ast-lower-implitem.h
@@ -88,7 +88,8 @@ public:
   {
     HIR::Visibility vis = translate_visibility (constant.get_visibility ());
 
-    HIR::Type *type = ASTLoweringType::translate (constant.get_type ().get ());
+    HIR::Type *type
+      = ASTLoweringType::translate (constant.get_type ().get (), true);
     HIR::Expr *expr = ASTLoweringExpr::translate (constant.get_expr ().get ());
 
     auto crate_num = mappings->get_current_crate ();
diff --git a/gcc/rust/hir/rust-ast-lower-item.cc b/gcc/rust/hir/rust-ast-lower-item.cc
index 2895872f336c..7ef556fee0d0 100644
--- a/gcc/rust/hir/rust-ast-lower-item.cc
+++ b/gcc/rust/hir/rust-ast-lower-item.cc
@@ -351,7 +351,7 @@ ASTLoweringItem::visit (AST::StaticItem &var)
 {
   HIR::Visibility vis = translate_visibility (var.get_visibility ());
 
-  HIR::Type *type = ASTLoweringType::translate (var.get_type ().get ());
+  HIR::Type *type = ASTLoweringType::translate (var.get_type ().get (), true);
   HIR::Expr *expr = ASTLoweringExpr::translate (var.get_expr ().get ());
 
   auto crate_num = mappings->get_current_crate ();
@@ -372,7 +372,8 @@ ASTLoweringItem::visit (AST::ConstantItem &constant)
 {
   HIR::Visibility vis = translate_visibility (constant.get_visibility ());
 
-  HIR::Type *type = ASTLoweringType::translate (constant.get_type ().get ());
+  HIR::Type *type
+    = ASTLoweringType::translate (constant.get_type ().get (), true);
   HIR::Expr *expr = ASTLoweringExpr::translate (constant.get_expr ().get ());
 
   auto crate_num = mappings->get_current_crate ();
diff --git a/gcc/rust/hir/rust-ast-lower-type.cc b/gcc/rust/hir/rust-ast-lower-type.cc
index 5388f2139ced..5cea71f83a23 100644
--- a/gcc/rust/hir/rust-ast-lower-type.cc
+++ b/gcc/rust/hir/rust-ast-lower-type.cc
@@ -180,9 +180,9 @@ ASTLowerQualifiedPathInType::visit (AST::QualifiedPathInType &path)
 }
 
 HIR::Type *
-ASTLoweringType::translate (AST::Type *type)
+ASTLoweringType::translate (AST::Type *type, bool default_to_static_lifetime)
 {
-  ASTLoweringType resolver;
+  ASTLoweringType resolver (default_to_static_lifetime);
   type->accept_vis (resolver);
 
   rust_assert (resolver.translated != nullptr);
@@ -222,7 +222,8 @@ ASTLoweringType::visit (AST::BareFunctionType &fntype)
 	}
 
       HIR::Type *param_type
-	= ASTLoweringType::translate (param.get_type ().get ());
+	= ASTLoweringType::translate (param.get_type ().get (),
+				      default_to_static_lifetime);
 
       HIR::MaybeNamedParam p (param.get_name (), kind,
 			      std::unique_ptr<HIR::Type> (param_type),
@@ -234,7 +235,8 @@ ASTLoweringType::visit (AST::BareFunctionType &fntype)
   if (fntype.has_return_type ())
     {
       return_type
-	= ASTLoweringType::translate (fntype.get_return_type ().get ());
+	= ASTLoweringType::translate (fntype.get_return_type ().get (),
+				      default_to_static_lifetime);
     }
 
   auto crate_num = mappings->get_current_crate ();
@@ -254,7 +256,8 @@ ASTLoweringType::visit (AST::TupleType &tuple)
   std::vector<std::unique_ptr<HIR::Type>> elems;
   for (auto &e : tuple.get_elems ())
     {
-      HIR::Type *t = ASTLoweringType::translate (e.get ());
+      HIR::Type *t
+	= ASTLoweringType::translate (e.get (), default_to_static_lifetime);
       elems.push_back (std::unique_ptr<HIR::Type> (t));
     }
 
@@ -283,7 +286,8 @@ void
 ASTLoweringType::visit (AST::ArrayType &type)
 {
   HIR::Type *translated_type
-    = ASTLoweringType::translate (type.get_elem_type ().get ());
+    = ASTLoweringType::translate (type.get_elem_type ().get (),
+				  default_to_static_lifetime);
   HIR::Expr *array_size
     = ASTLoweringExpr::translate (type.get_size_expr ().get ());
 
@@ -301,10 +305,12 @@ ASTLoweringType::visit (AST::ArrayType &type)
 void
 ASTLoweringType::visit (AST::ReferenceType &type)
 {
-  HIR::Lifetime lifetime = lower_lifetime (type.get_lifetime ());
+  HIR::Lifetime lifetime
+    = lower_lifetime (type.get_lifetime (), default_to_static_lifetime);
 
   HIR::Type *base_type
-    = ASTLoweringType::translate (type.get_base_type ().get ());
+    = ASTLoweringType::translate (type.get_base_type ().get (),
+				  default_to_static_lifetime);
 
   auto crate_num = mappings->get_current_crate ();
   Analysis::NodeMapping mapping (crate_num, type.get_node_id (),
@@ -322,7 +328,8 @@ void
 ASTLoweringType::visit (AST::RawPointerType &type)
 {
   HIR::Type *base_type
-    = ASTLoweringType::translate (type.get_type_pointed_to ().get ());
+    = ASTLoweringType::translate (type.get_type_pointed_to ().get (),
+				  default_to_static_lifetime);
 
   auto crate_num = mappings->get_current_crate ();
   Analysis::NodeMapping mapping (crate_num, type.get_node_id (),
@@ -343,7 +350,8 @@ void
 ASTLoweringType::visit (AST::SliceType &type)
 {
   HIR::Type *base_type
-    = ASTLoweringType::translate (type.get_elem_type ().get ());
+    = ASTLoweringType::translate (type.get_elem_type ().get (),
+				  default_to_static_lifetime);
 
   auto crate_num = mappings->get_current_crate ();
   Analysis::NodeMapping mapping (crate_num, type.get_node_id (),
diff --git a/gcc/rust/hir/rust-ast-lower-type.h b/gcc/rust/hir/rust-ast-lower-type.h
index 8194a9ace5e9..ed90c17bb0f8 100644
--- a/gcc/rust/hir/rust-ast-lower-type.h
+++ b/gcc/rust/hir/rust-ast-lower-type.h
@@ -63,7 +63,8 @@ class ASTLoweringType : public ASTLoweringBase
   using Rust::HIR::ASTLoweringBase::visit;
 
 public:
-  static HIR::Type *translate (AST::Type *type);
+  static HIR::Type *translate (AST::Type *type,
+			       bool default_to_static_lifetime = false);
 
   void visit (AST::BareFunctionType &fntype) override;
   void visit (AST::TupleType &tuple) override;
@@ -79,7 +80,14 @@ public:
   void visit (AST::TraitObjectType &type) override;
 
 private:
-  ASTLoweringType () : ASTLoweringBase (), translated (nullptr) {}
+  ASTLoweringType (bool default_to_static_lifetime)
+    : ASTLoweringBase (),
+      default_to_static_lifetime (default_to_static_lifetime),
+      translated (nullptr)
+  {}
+
+  /** Used when compiling const and static items. */
+  bool default_to_static_lifetime;
 
   HIR::Type *translated;
 };

                 reply	other threads:[~2024-01-30 12:01 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240130120132.DC55F385828A@sourceware.org \
    --to=cohenarthur@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).