public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/rust/master] Support forward declared items within blocks
@ 2022-06-08 11:48 Thomas Schwinge
  0 siblings, 0 replies; only message in thread
From: Thomas Schwinge @ 2022-06-08 11:48 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:17dc14cb4b0819532376a22b592b26dd6ffc364f

commit 17dc14cb4b0819532376a22b592b26dd6ffc364f
Author: Philip Herron <philip.herron@embecosm.com>
Date:   Fri Nov 5 12:32:44 2021 +0000

    Support forward declared items within blocks
    
    This changes the BlockExpr resolution to iterate Items first, then the
    Stmts. This could be handled in HIR by desugaring the BlockExpr by lowering
    the block into { <items>; <stmts>; } would also work. But the HIR lowering
    of blocks is a little messy right now and we need to clean up the
    unreachable lint.
    
    Fixes #531

Diff:
---
 gcc/rust/hir/tree/rust-hir-expr.h                  |  9 -----
 gcc/rust/lint/rust-lint-marklive.h                 |  8 ++--
 gcc/rust/resolve/rust-ast-resolve.cc               | 11 +++++-
 gcc/rust/typecheck/rust-hir-type-check.cc          | 43 +++++++++++++++-------
 gcc/rust/typecheck/rust-tycheck-dump.h             | 12 +++---
 .../rust/compile/torture/forward_decl_5.rs         | 19 ++++++++++
 6 files changed, 68 insertions(+), 34 deletions(-)

diff --git a/gcc/rust/hir/tree/rust-hir-expr.h b/gcc/rust/hir/tree/rust-hir-expr.h
index c472cab8a3f..575d1f66a86 100644
--- a/gcc/rust/hir/tree/rust-hir-expr.h
+++ b/gcc/rust/hir/tree/rust-hir-expr.h
@@ -2007,15 +2007,6 @@ public:
 
   void accept_vis (HIRVisitor &vis) override;
 
-  void iterate_stmts (std::function<bool (Stmt *)> cb)
-  {
-    for (auto it = statements.begin (); it != statements.end (); it++)
-      {
-	if (!cb (it->get ()))
-	  return;
-      }
-  }
-
   bool is_final_stmt (Stmt *stmt) { return statements.back ().get () == stmt; }
 
   Location get_closing_locus ()
diff --git a/gcc/rust/lint/rust-lint-marklive.h b/gcc/rust/lint/rust-lint-marklive.h
index 7b7b68f3f97..ca5d8945c29 100644
--- a/gcc/rust/lint/rust-lint-marklive.h
+++ b/gcc/rust/lint/rust-lint-marklive.h
@@ -97,10 +97,10 @@ public:
 
   void visit (HIR::BlockExpr &expr) override
   {
-    expr.iterate_stmts ([&] (HIR::Stmt *s) mutable -> bool {
-      s->accept_vis (*this);
-      return true;
-    });
+    for (auto &s : expr.get_statements ())
+      {
+	s->accept_vis (*this);
+      }
     if (expr.has_expr ())
       {
 	expr.get_final_expr ()->accept_vis (*this);
diff --git a/gcc/rust/resolve/rust-ast-resolve.cc b/gcc/rust/resolve/rust-ast-resolve.cc
index 83a15a8f08d..2758c83595d 100644
--- a/gcc/rust/resolve/rust-ast-resolve.cc
+++ b/gcc/rust/resolve/rust-ast-resolve.cc
@@ -349,7 +349,16 @@ ResolveExpr::visit (AST::BlockExpr &expr)
   resolver->push_new_label_rib (resolver->get_type_scope ().peek ());
 
   for (auto &s : expr.get_statements ())
-    ResolveStmt::go (s.get (), s->get_node_id ());
+    {
+      if (s->is_item ())
+	ResolveStmt::go (s.get (), s->get_node_id ());
+    }
+
+  for (auto &s : expr.get_statements ())
+    {
+      if (!s->is_item ())
+	ResolveStmt::go (s.get (), s->get_node_id ());
+    }
 
   if (expr.has_tail_expr ())
     ResolveExpr::go (expr.get_tail_expr ().get (), expr.get_node_id ());
diff --git a/gcc/rust/typecheck/rust-hir-type-check.cc b/gcc/rust/typecheck/rust-hir-type-check.cc
index 5237082461d..ca4842a1630 100644
--- a/gcc/rust/typecheck/rust-hir-type-check.cc
+++ b/gcc/rust/typecheck/rust-hir-type-check.cc
@@ -93,22 +93,37 @@ TypeResolution::Resolve (HIR::Crate &crate)
 void
 TypeCheckExpr::visit (HIR::BlockExpr &expr)
 {
-  expr.iterate_stmts ([&] (HIR::Stmt *s) mutable -> bool {
-    auto resolved = TypeCheckStmt::Resolve (s, inside_loop);
-    if (resolved == nullptr)
-      {
-	rust_error_at (s->get_locus (), "failure to resolve type");
-	return false;
-      }
+  for (auto &s : expr.get_statements ())
+    {
+      if (!s->is_item ())
+	continue;
 
-    if (s->is_unit_check_needed () && !resolved->is_unit ())
-      {
-	auto unit = new TyTy::TupleType (s->get_mappings ().get_hirid ());
-	resolved = unit->unify (resolved);
-      }
+      auto resolved = TypeCheckStmt::Resolve (s.get (), inside_loop);
+      if (resolved == nullptr)
+	{
+	  rust_error_at (s->get_locus (), "failure to resolve type");
+	  return;
+	}
+    }
 
-    return true;
-  });
+  for (auto &s : expr.get_statements ())
+    {
+      if (s->is_item ())
+	continue;
+
+      auto resolved = TypeCheckStmt::Resolve (s.get (), inside_loop);
+      if (resolved == nullptr)
+	{
+	  rust_error_at (s->get_locus (), "failure to resolve type");
+	  return;
+	}
+
+      if (s->is_unit_check_needed () && !resolved->is_unit ())
+	{
+	  auto unit = new TyTy::TupleType (s->get_mappings ().get_hirid ());
+	  resolved = unit->unify (resolved);
+	}
+    }
 
   if (expr.has_expr ())
     infered
diff --git a/gcc/rust/typecheck/rust-tycheck-dump.h b/gcc/rust/typecheck/rust-tycheck-dump.h
index c0681b891b2..c305d48bec3 100644
--- a/gcc/rust/typecheck/rust-tycheck-dump.h
+++ b/gcc/rust/typecheck/rust-tycheck-dump.h
@@ -94,12 +94,12 @@ public:
   {
     indentation_level++;
 
-    expr.iterate_stmts ([&] (HIR::Stmt *s) mutable -> bool {
-      dump += indent ();
-      s->accept_vis (*this);
-      dump += ";\n";
-      return true;
-    });
+    for (auto &s : expr.get_statements ())
+      {
+	dump += indent ();
+	s->accept_vis (*this);
+	dump += ";\n";
+      }
 
     if (expr.has_expr ())
       {
diff --git a/gcc/testsuite/rust/compile/torture/forward_decl_5.rs b/gcc/testsuite/rust/compile/torture/forward_decl_5.rs
new file mode 100644
index 00000000000..73a47fe061b
--- /dev/null
+++ b/gcc/testsuite/rust/compile/torture/forward_decl_5.rs
@@ -0,0 +1,19 @@
+pub fn main() {
+    let a;
+    a = foo { a: 123, b: 456f32 };
+
+    let mut a = 123;
+    a = bar(a);
+
+    let mut b = 456f32;
+    b = bar(b);
+
+    fn bar<T>(x: T) -> T {
+        x
+    }
+
+    struct foo {
+        a: i32,
+        b: f32,
+    };
+}


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

only message in thread, other threads:[~2022-06-08 11:48 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-08 11:48 [gcc/devel/rust/master] Support forward declared items within blocks Thomas Schwinge

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).