[COMMITTED 019/145] gccrs: Change lookup_hir_to_node return type to optional

2025-03-17 Thread arthur . cohen
From: Pierre-Emmanuel Patry 

Optional are more convenient to use and avoid uninitialized data.

gcc/rust/ChangeLog:

* backend/rust-compile-expr.cc (CompileExpr::generate_closure_function):
Adapt code for new optional return type.
* checks/errors/privacy/rust-privacy-reporter.cc 
(PrivacyReporter::check_base_type_privacy):
Likewise.
* util/rust-hir-map.cc (Mappings::lookup_hir_to_node): Change return
type to an optional.
* util/rust-hir-map.h: Adapt function prototype with the new return
type.

Signed-off-by: Pierre-Emmanuel Patry 
---
 gcc/rust/backend/rust-compile-expr.cc  | 10 ++
 .../checks/errors/privacy/rust-privacy-reporter.cc |  9 +++--
 gcc/rust/util/rust-hir-map.cc  |  9 -
 gcc/rust/util/rust-hir-map.h   |  2 +-
 4 files changed, 14 insertions(+), 16 deletions(-)

diff --git a/gcc/rust/backend/rust-compile-expr.cc 
b/gcc/rust/backend/rust-compile-expr.cc
index 28b901e69de..e2fa6dda09e 100644
--- a/gcc/rust/backend/rust-compile-expr.cc
+++ b/gcc/rust/backend/rust-compile-expr.cc
@@ -2216,10 +2216,12 @@ CompileExpr::generate_closure_function 
(HIR::ClosureExpr &expr,
 
   const Resolver::CanonicalPath &parent_canonical_path
 = closure_tyty.get_ident ().path;
-  NodeId node_id;
-  bool ok = ctx->get_mappings ().lookup_hir_to_node (
-expr.get_mappings ().get_hirid (), &node_id);
-  rust_assert (ok);
+
+  tl::optional nid = ctx->get_mappings ().lookup_hir_to_node (
+expr.get_mappings ().get_hirid ());
+  rust_assert (nid.has_value ());
+  auto node_id = nid.value ();
+
   Resolver::CanonicalPath path = parent_canonical_path.append (
 Resolver::CanonicalPath::new_seg (node_id, "{{closure}}"));
 
diff --git a/gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc 
b/gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc
index 869f99c5b80..3a304e881d5 100644
--- a/gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc
+++ b/gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc
@@ -201,12 +201,9 @@ PrivacyReporter::check_base_type_privacy 
(Analysis::NodeMapping &node_mappings,
 case TyTy::ADT:
   case TyTy::STR: {
auto ref_id = ty->get_ref ();
-   NodeId lookup_id;
-
-   bool ok = mappings.lookup_hir_to_node (ref_id, &lookup_id);
-   rust_assert (ok);
-
-   return check_for_privacy_violation (lookup_id, locus);
+   if (auto lookup_id = mappings.lookup_hir_to_node (ref_id))
+ return check_for_privacy_violation (*lookup_id, locus);
+   rust_unreachable ();
   }
 case TyTy::REF:
   return recursive_check (
diff --git a/gcc/rust/util/rust-hir-map.cc b/gcc/rust/util/rust-hir-map.cc
index 78ce17db138..b8d393759d6 100644
--- a/gcc/rust/util/rust-hir-map.cc
+++ b/gcc/rust/util/rust-hir-map.cc
@@ -779,15 +779,14 @@ Mappings::lookup_node_to_hir (NodeId id)
   return {it->second};
 }
 
-bool
-Mappings::lookup_hir_to_node (HirId id, NodeId *ref)
+tl::optional
+Mappings::lookup_hir_to_node (HirId id)
 {
   auto it = hirIdToNodeMappings.find (id);
   if (it == hirIdToNodeMappings.end ())
-return false;
+return tl::nullopt;
 
-  *ref = it->second;
-  return true;
+  return {it->second};
 }
 
 void
diff --git a/gcc/rust/util/rust-hir-map.h b/gcc/rust/util/rust-hir-map.h
index c2b3e27fd9f..6fd0e7a52f4 100644
--- a/gcc/rust/util/rust-hir-map.h
+++ b/gcc/rust/util/rust-hir-map.h
@@ -171,7 +171,7 @@ public:
 
   void insert_node_to_hir (NodeId id, HirId ref);
   tl::optional lookup_node_to_hir (NodeId id);
-  bool lookup_hir_to_node (HirId id, NodeId *ref);
+  tl::optional lookup_hir_to_node (HirId id);
 
   void insert_location (HirId id, location_t locus);
   location_t lookup_location (HirId id);
-- 
2.45.2



[COMMITTED 018/145] gccrs: Change lookup_node_to_hir return type to optional

2025-03-17 Thread arthur . cohen
From: Pierre-Emmanuel Patry 

Previous API was using a boolean and a pointer, this was not practical
and could be replaced with an optional.

gcc/rust/ChangeLog:

* backend/rust-compile-expr.cc (CompileExpr::visit): Change call to use
the returned optional.
(CompileExpr::generate_closure_function): Likewise.
* backend/rust-compile-resolve-path.cc (ResolvePathRef::resolve):
Likewise.
* backend/rust-compile-type.cc (TyTyResolveCompile::visit): Likewise.
* backend/rust-mangle-v0.cc (v0_path): Likewise.
* checks/errors/privacy/rust-visibility-resolver.cc:
Likewise.
* checks/errors/rust-const-checker.cc (ConstChecker::visit): Likewise.
* checks/errors/rust-unsafe-checker.cc (UnsafeChecker::visit):
Likewise.
* checks/lints/rust-lint-marklive.cc (MarkLive::visit): Likewise.
(MarkLive::visit_path_segment): Likewise.
* typecheck/rust-hir-trait-resolve.cc 
(TraitResolver::resolve_path_to_trait):
Likewise.
* typecheck/rust-hir-type-check-path.cc 
(TypeCheckExpr::resolve_root_path):
Likewise.
* typecheck/rust-hir-type-check-type.cc 
(TypeCheckType::resolve_root_path):
Likewise.
(ResolveWhereClauseItem::visit): Likewise.
* util/rust-hir-map.cc (Mappings::lookup_node_to_hir): Return an
optional instead of a boolean.
* util/rust-hir-map.h: Change function prototype to match the new
return type.

Signed-off-by: Pierre-Emmanuel Patry 
---
 gcc/rust/backend/rust-compile-expr.cc | 68 ++-
 gcc/rust/backend/rust-compile-resolve-path.cc |  6 +-
 gcc/rust/backend/rust-compile-type.cc |  6 +-
 gcc/rust/backend/rust-mangle-v0.cc|  7 +-
 .../privacy/rust-visibility-resolver.cc   |  5 +-
 gcc/rust/checks/errors/rust-const-checker.cc  | 17 +++--
 gcc/rust/checks/errors/rust-unsafe-checker.cc | 45 +++-
 gcc/rust/checks/lints/rust-lint-marklive.cc   | 34 +-
 gcc/rust/typecheck/rust-hir-trait-resolve.cc  | 21 +++---
 .../typecheck/rust-hir-type-check-path.cc |  5 +-
 .../typecheck/rust-hir-type-check-type.cc | 36 +-
 gcc/rust/util/rust-hir-map.cc |  9 ++-
 gcc/rust/util/rust-hir-map.h  |  2 +-
 13 files changed, 142 insertions(+), 119 deletions(-)

diff --git a/gcc/rust/backend/rust-compile-expr.cc 
b/gcc/rust/backend/rust-compile-expr.cc
index 52318a973aa..28b901e69de 100644
--- a/gcc/rust/backend/rust-compile-expr.cc
+++ b/gcc/rust/backend/rust-compile-expr.cc
@@ -30,6 +30,7 @@
 #include "realmpfr.h"
 #include "convert.h"
 #include "print-tree.h"
+#include "rust-system.h"
 
 namespace Rust {
 namespace Compile {
@@ -734,12 +735,14 @@ CompileExpr::visit (HIR::BreakExpr &expr)
  return;
}
 
-  HirId ref = UNKNOWN_HIRID;
-  if (!ctx->get_mappings ().lookup_node_to_hir (resolved_node_id, &ref))
+  tl::optional hid
+   = ctx->get_mappings ().lookup_node_to_hir (resolved_node_id);
+  if (!hid.has_value ())
{
  rust_fatal_error (expr.get_locus (), "reverse lookup label failure");
  return;
}
+  auto ref = hid.value ();
 
   tree label = NULL_TREE;
   if (!ctx->lookup_label_decl (ref, &label))
@@ -778,12 +781,14 @@ CompileExpr::visit (HIR::ContinueExpr &expr)
  return;
}
 
-  HirId ref = UNKNOWN_HIRID;
-  if (!ctx->get_mappings ().lookup_node_to_hir (resolved_node_id, &ref))
+  tl::optional hid
+   = ctx->get_mappings ().lookup_node_to_hir (resolved_node_id);
+  if (!hid.has_value ())
{
  rust_fatal_error (expr.get_locus (), "reverse lookup label failure");
  return;
}
+  auto ref = hid.value ();
 
   if (!ctx->lookup_label_decl (ref, &label))
{
@@ -2176,20 +2181,21 @@ CompileExpr::visit (HIR::ClosureExpr &expr)
   for (const auto &capture : closure_tyty->get_captures ())
 {
   // lookup the HirId
-  HirId ref = UNKNOWN_HIRID;
-  bool ok = ctx->get_mappings ().lookup_node_to_hir (capture, &ref);
-  rust_assert (ok);
-
-  // lookup the var decl
-  Bvariable *var = nullptr;
-  bool found = ctx->lookup_var_decl (ref, &var);
-  rust_assert (found);
-
-  // FIXME
-  // this should bes based on the closure move-ability
-  tree var_expr = var->get_tree (expr.get_locus ());
-  tree val = address_expression (var_expr, expr.get_locus ());
-  vals.push_back (val);
+  if (auto hid = ctx->get_mappings ().lookup_node_to_hir (capture))
+   {
+ // lookup the var decl
+ Bvariable *var = nullptr;
+ bool found = ctx->lookup_var_decl (*hid, &var);
+ rust_assert (found);
+
+ // FIXME
+ // this should bes based on the closure move-ability
+ tree var_expr = var->get_tree (expr.get_locus ());
+ tree val = address_expression (var_expr, expr.get_locus ());
+ vals.push

[COMMITTED 022/145] gccrs: Change return type of lookup_hir_trait_item

2025-03-17 Thread arthur . cohen
From: Pierre-Emmanuel Patry 

Change the return type to an optional instead of returning a null
pointer. This allows easier tracking of rogue null pointers in the
map. This commit also fixes a bug in trait associated function mangling,
the function was using an already invalidated pointer.

gcc/rust/ChangeLog:

* backend/rust-compile-resolve-path.cc (HIRCompileBase::query_compile):
Adapt return type to new optional.
* backend/rust-mangle-v0.cc (v0_function_path): Change prototype to use
the generic arguments vector instead of the whole HIR function.
(v0_path): Fix a bug with a null pointer being used to create the
trait function mangling.
* util/rust-hir-map.cc (Mappings::insert_hir_trait_item): Adapt code
to new return type.
(Mappings::lookup_hir_trait_item): Change the return type of the
function to an optional.
* util/rust-hir-map.h: Update the function's prototype.

Signed-off-by: Pierre-Emmanuel Patry 
---
 gcc/rust/backend/rust-compile-resolve-path.cc |  7 +++--
 gcc/rust/backend/rust-mangle-v0.cc| 30 +++
 gcc/rust/util/rust-hir-map.cc |  6 ++--
 gcc/rust/util/rust-hir-map.h  |  2 +-
 4 files changed, 25 insertions(+), 20 deletions(-)

diff --git a/gcc/rust/backend/rust-compile-resolve-path.cc 
b/gcc/rust/backend/rust-compile-resolve-path.cc
index 1f7dc7d83c1..bf294e4c879 100644
--- a/gcc/rust/backend/rust-compile-resolve-path.cc
+++ b/gcc/rust/backend/rust-compile-resolve-path.cc
@@ -256,10 +256,11 @@ HIRCompileBase::query_compile (HirId ref, TyTy::BaseType 
*lookup,
   else
{
  // it might be resolved to a trait item
- HIR::TraitItem *trait_item
+ tl::optional trait_item
= ctx->get_mappings ().lookup_hir_trait_item (ref);
+
  HIR::Trait *trait = ctx->get_mappings ().lookup_trait_item_mapping (
-   trait_item->get_mappings ().get_hirid ());
+   trait_item.value ()->get_mappings ().get_hirid ());
 
  Resolver::TraitReference *trait_ref
= &Resolver::TraitReference::error_node ();
@@ -285,7 +286,7 @@ HIRCompileBase::query_compile (HirId ref, TyTy::BaseType 
*lookup,
  // this means we are defaulting back to the trait_item if
  // possible
  Resolver::TraitItemReference *trait_item_ref = nullptr;
- bool ok = trait_ref->lookup_hir_trait_item (*trait_item,
+ bool ok = trait_ref->lookup_hir_trait_item (*trait_item.value (),
  &trait_item_ref);
  rust_assert (ok); // found
  rust_assert (trait_item_ref->is_optional ()); // has definition
diff --git a/gcc/rust/backend/rust-mangle-v0.cc 
b/gcc/rust/backend/rust-mangle-v0.cc
index 9be7b2d2bf4..0b6d9455242 100644
--- a/gcc/rust/backend/rust-mangle-v0.cc
+++ b/gcc/rust/backend/rust-mangle-v0.cc
@@ -279,16 +279,17 @@ v0_type_path (V0Path path, std::string ident)
 }
 
 static V0Path
-v0_function_path (V0Path path, Rust::Compile::Context *ctx,
- const TyTy::BaseType *ty, HIR::Function *fn,
- std::string ident)
+v0_function_path (
+  V0Path path, Rust::Compile::Context *ctx, const TyTy::BaseType *ty,
+  const std::vector> &generic_params,
+  std::string ident)
 {
   V0Path v0path;
   v0path.prefix = "N";
   v0path.ns = "v";
   v0path.path = path.as_string ();
   v0path.ident = ident;
-  if (!fn->get_generic_params ().empty ())
+  if (!generic_params.empty ())
 {
   v0path.generic_prefix = "I";
   v0path.generic_postfix = v0_generic_args (ctx, ty) + "E";
@@ -386,7 +387,6 @@ v0_path (Rust::Compile::Context *ctx, const TyTy::BaseType 
*ty,
 HirId parent_impl_id = UNKNOWN_HIRID;
 HIR::ImplItem *impl_item
   = mappings.lookup_hir_implitem (hir_id, &parent_impl_id);
-HIR::TraitItem *trait_item = mappings.lookup_hir_trait_item (hir_id);
 HIR::Expr *expr = mappings.lookup_hir_expr (hir_id);
 
 if (impl_item != nullptr)
@@ -395,8 +395,9 @@ v0_path (Rust::Compile::Context *ctx, const TyTy::BaseType 
*ty,
  {
case HIR::ImplItem::FUNCTION: {
  HIR::Function *fn = static_cast (impl_item);
- v0path = v0_function_path (v0path, ctx, ty, fn,
-v0_identifier (seg.get ()));
+ v0path
+   = v0_function_path (v0path, ctx, ty, fn->get_generic_params (),
+   v0_identifier (seg.get ()));
}
break;
  case HIR::ImplItem::CONSTANT:
@@ -408,13 +409,15 @@ v0_path (Rust::Compile::Context *ctx, const 
TyTy::BaseType *ty,
break;
  }
   }
-else if (trait_item != nullptr)
+else if (auto trait_item = mappings.lookup_hir_trait_item (hir_id))
   {
-   switch (trait_item->get_item_kind ())
+   switch (trait_item.value ()->get_i

[COMMITTED 015/145] gccrs: Add testcases for handling struct as scrutinee for match expr

2025-03-17 Thread arthur . cohen
From: Nobel Singh 

gcc/testsuite/ChangeLog:

* rust/compile/issue-2906.rs: New test.
* rust/execute/torture/issue-2906.rs: New test.

Signed-off-by: Nobel Singh 
---
 gcc/testsuite/rust/compile/issue-2906.rs  | 10 ++
 .../rust/execute/torture/issue-2906.rs| 34 +++
 2 files changed, 44 insertions(+)
 create mode 100644 gcc/testsuite/rust/compile/issue-2906.rs
 create mode 100644 gcc/testsuite/rust/execute/torture/issue-2906.rs

diff --git a/gcc/testsuite/rust/compile/issue-2906.rs 
b/gcc/testsuite/rust/compile/issue-2906.rs
new file mode 100644
index 000..20abcb095b8
--- /dev/null
+++ b/gcc/testsuite/rust/compile/issue-2906.rs
@@ -0,0 +1,10 @@
+// { dg-warning "field is never read: .a." "" { target *-*-* } .-1 }
+struct Foo { a: i32 }
+
+fn main() {
+let a = Foo { a: 15 };
+
+match a {
+b => { }
+}
+}
\ No newline at end of file
diff --git a/gcc/testsuite/rust/execute/torture/issue-2906.rs 
b/gcc/testsuite/rust/execute/torture/issue-2906.rs
new file mode 100644
index 000..d3ca8ae1c5d
--- /dev/null
+++ b/gcc/testsuite/rust/execute/torture/issue-2906.rs
@@ -0,0 +1,34 @@
+// { dg-warning "field is never read: .x." "" { target *-*-* } .-1 }
+// { dg-warning "field is never read: .y." "" { target *-*-* } .-2 }
+struct Point {
+x: u32,
+y: u32,
+}
+
+fn is_origin(p: Point) -> bool {
+match p {
+Point { x, y } => {
+if x == 0 && y == 0 {
+return true;
+}
+false
+}
+_ => false,
+}
+}
+
+fn main() -> i32 {
+let p = Point { x: 0, y: 0 };
+let q = Point { x: 0, y: 1 };
+let mut retval = 2;
+
+if is_origin(p) {
+retval -= 1;
+}
+
+if !is_origin(q) {
+retval -= 1;
+}
+
+retval
+}
-- 
2.45.2



[COMMITTED 026/145] gccrs: Change return type of lookup_crate_name

2025-03-17 Thread arthur . cohen
From: Pierre-Emmanuel Patry 

Change the function's return type to use an optional.

gcc/rust/ChangeLog:

* resolve/rust-ast-resolve-toplevel.h: Adapt the code to the new
return type.
* resolve/rust-toplevel-name-resolver-2.0.cc (TopLevel::visit):
Likewise.
* rust-session-manager.cc (Session::load_extern_crate): Likewise.
* util/rust-hir-map.cc (Mappings::lookup_crate_name): Change the return
type to an optional.
* util/rust-hir-map.h: Update the function's prototype.

Signed-off-by: Pierre-Emmanuel Patry 
---
 gcc/rust/resolve/rust-ast-resolve-toplevel.h|  9 -
 gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc |  3 +--
 gcc/rust/rust-session-manager.cc|  6 ++
 gcc/rust/util/rust-hir-map.cc   | 12 
 gcc/rust/util/rust-hir-map.h|  4 ++--
 5 files changed, 13 insertions(+), 21 deletions(-)

diff --git a/gcc/rust/resolve/rust-ast-resolve-toplevel.h 
b/gcc/rust/resolve/rust-ast-resolve-toplevel.h
index 1cadf9829fe..99fc4f6bfcc 100644
--- a/gcc/rust/resolve/rust-ast-resolve-toplevel.h
+++ b/gcc/rust/resolve/rust-ast-resolve-toplevel.h
@@ -434,16 +434,15 @@ public:
   }
 else
   {
-   CrateNum found_crate_num = UNKNOWN_CRATENUM;
-   bool found
- = mappings.lookup_crate_name (extern_crate.get_referenced_crate (),
-   found_crate_num);
-   if (!found)
+   auto cnum
+ = mappings.lookup_crate_name (extern_crate.get_referenced_crate ());
+   if (!cnum)
  {
rust_error_at (extern_crate.get_locus (), "unknown crate %qs",
   extern_crate.get_referenced_crate ().c_str ());
return;
  }
+   auto found_crate_num = cnum.value ();
 
bool ok
  = mappings.crate_num_to_nodeid (found_crate_num, resolved_crate);
diff --git a/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc 
b/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc
index 281d4752fe1..1d8b1c5a65c 100644
--- a/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc
+++ b/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc
@@ -231,8 +231,7 @@ void
 TopLevel::visit (AST::ExternCrate &crate)
 {
   auto &mappings = Analysis::Mappings::get ();
-  CrateNum num;
-  rust_assert (mappings.lookup_crate_name (crate.get_referenced_crate (), 
num));
+  CrateNum num = *mappings.lookup_crate_name (crate.get_referenced_crate ());
 
   auto attribute_macros = mappings.lookup_attribute_proc_macros (num);
 
diff --git a/gcc/rust/rust-session-manager.cc b/gcc/rust/rust-session-manager.cc
index 0dd17b2b43a..da66479d608 100644
--- a/gcc/rust/rust-session-manager.cc
+++ b/gcc/rust/rust-session-manager.cc
@@ -1048,13 +1048,11 @@ NodeId
 Session::load_extern_crate (const std::string &crate_name, location_t locus)
 {
   // has it already been loaded?
-  CrateNum found_crate_num = UNKNOWN_CRATENUM;
-  bool found = mappings.lookup_crate_name (crate_name, found_crate_num);
-  if (found)
+  if (auto crate_num = mappings.lookup_crate_name (crate_name))
 {
   NodeId resolved_node_id = UNKNOWN_NODEID;
   bool resolved
-   = mappings.crate_num_to_nodeid (found_crate_num, resolved_node_id);
+   = mappings.crate_num_to_nodeid (*crate_num, resolved_node_id);
   rust_assert (resolved);
 
   return resolved_node_id;
diff --git a/gcc/rust/util/rust-hir-map.cc b/gcc/rust/util/rust-hir-map.cc
index 99c2493da14..95d3b3a4d61 100644
--- a/gcc/rust/util/rust-hir-map.cc
+++ b/gcc/rust/util/rust-hir-map.cc
@@ -164,19 +164,15 @@ Mappings::get_current_crate_name () const
   return name;
 }
 
-bool
-Mappings::lookup_crate_name (const std::string &crate_name,
-CrateNum &resolved_crate_num) const
+tl::optional
+Mappings::lookup_crate_name (const std::string &crate_name) const
 {
   for (const auto &it : crate_names)
 {
   if (it.second.compare (crate_name) == 0)
-   {
- resolved_crate_num = it.first;
- return true;
-   }
+   return it.first;
 }
-  return false;
+  return tl::nullopt;
 }
 
 bool
diff --git a/gcc/rust/util/rust-hir-map.h b/gcc/rust/util/rust-hir-map.h
index 912d42a4b97..a68d81f34ef 100644
--- a/gcc/rust/util/rust-hir-map.h
+++ b/gcc/rust/util/rust-hir-map.h
@@ -83,8 +83,8 @@ public:
   bool get_crate_name (CrateNum crate_num, std::string &name) const;
   void set_crate_name (CrateNum crate_num, const std::string &name);
   std::string get_current_crate_name () const;
-  bool lookup_crate_name (const std::string &crate_name,
- CrateNum &resolved_crate_num) const;
+  tl::optional
+  lookup_crate_name (const std::string &crate_name) const;
   bool crate_num_to_nodeid (const CrateNum &crate_num, NodeId &node_id) const;
   bool node_is_crate (NodeId node_id) const;
 
-- 
2.45.2



[COMMITTED 106/145] gccrs: Scaffolding HIRFullVisitor for inline asm

2025-03-17 Thread arthur . cohen
From: jjasmine 

gcc/rust/ChangeLog:

* ast/rust-ast-visitor.h:
Scaffolding HIRFullVisitor for inline asm
* ast/rust-ast.cc (InlineAsm::accept_vis): Likewise.
* hir/tree/rust-hir-visitor.h (RUST_HIR_VISITOR_H): Likewise.
* hir/tree/rust-hir.cc (InlineAsm::accept_vis): Likewise.
---
 gcc/rust/ast/rust-ast-visitor.h  | 2 +-
 gcc/rust/ast/rust-ast.cc | 1 +
 gcc/rust/hir/tree/rust-hir-visitor.h | 2 ++
 gcc/rust/hir/tree/rust-hir.cc| 9 +++--
 4 files changed, 11 insertions(+), 3 deletions(-)

diff --git a/gcc/rust/ast/rust-ast-visitor.h b/gcc/rust/ast/rust-ast-visitor.h
index 6c64d719446..d91ef3da93e 100644
--- a/gcc/rust/ast/rust-ast-visitor.h
+++ b/gcc/rust/ast/rust-ast-visitor.h
@@ -129,7 +129,7 @@ public:
   virtual void visit (MatchExpr &expr) = 0;
   virtual void visit (AwaitExpr &expr) = 0;
   virtual void visit (AsyncBlockExpr &expr) = 0;
-  virtual void visit (InlineAsm &expr){};
+  virtual void visit (InlineAsm &expr) { rust_unreachable (); }
 
   // rust-item.h
   virtual void visit (TypeParam ¶m) = 0;
diff --git a/gcc/rust/ast/rust-ast.cc b/gcc/rust/ast/rust-ast.cc
index 2ff2e133037..9da908f3d7e 100644
--- a/gcc/rust/ast/rust-ast.cc
+++ b/gcc/rust/ast/rust-ast.cc
@@ -4659,6 +4659,7 @@ AsyncBlockExpr::accept_vis (ASTVisitor &vis)
 void
 InlineAsm::accept_vis (ASTVisitor &vis)
 {
+  rust_unreachable ();
   vis.visit (*this);
 }
 
diff --git a/gcc/rust/hir/tree/rust-hir-visitor.h 
b/gcc/rust/hir/tree/rust-hir-visitor.h
index 9adee3190ac..be1414905fa 100644
--- a/gcc/rust/hir/tree/rust-hir-visitor.h
+++ b/gcc/rust/hir/tree/rust-hir-visitor.h
@@ -19,6 +19,7 @@
 #ifndef RUST_HIR_VISITOR_H
 #define RUST_HIR_VISITOR_H
 
+#include "rust-hir-expr.h"
 #include "rust-hir-full-decls.h"
 
 namespace Rust {
@@ -85,6 +86,7 @@ public:
   virtual void visit (MatchExpr &expr) = 0;
   virtual void visit (AwaitExpr &expr) = 0;
   virtual void visit (AsyncBlockExpr &expr) = 0;
+  virtual void visit (InlineAsm &expr) {}
   virtual void visit (TypeParam ¶m) = 0;
   virtual void visit (ConstGenericParam ¶m) = 0;
   virtual void visit (LifetimeWhereClauseItem &item) = 0;
diff --git a/gcc/rust/hir/tree/rust-hir.cc b/gcc/rust/hir/tree/rust-hir.cc
index 66c6240bfa5..bcaf66edfb8 100644
--- a/gcc/rust/hir/tree/rust-hir.cc
+++ b/gcc/rust/hir/tree/rust-hir.cc
@@ -3772,11 +3772,16 @@ BorrowExpr::accept_vis (HIRFullVisitor &vis)
 
 void
 InlineAsm::accept_vis (HIRExpressionVisitor &vis)
-{}
+{
+  rust_unreachable ();
+}
 
 void
 InlineAsm::accept_vis (HIRFullVisitor &vis)
-{}
+{
+  rust_unreachable ();
+  vis.visit (*this);
+}
 
 void
 BorrowExpr::accept_vis (HIRExpressionVisitor &vis)
-- 
2.45.2



[COMMITTED 100/145] gccrs: Refactoring for inline asm pr

2025-03-17 Thread arthur . cohen
From: jjasmine 

gcc/rust/ChangeLog:

* ast/rust-expr.h (struct AnonConst):
major refactoring of inline asm, mostly concerns
naming convention, trinary conditionals, warnings,
adding rust_unreachables in not-yet supported errors.
(struct InlineAsmRegOrRegClass): Likewise.
(struct InlineAsmOperand): Likewise.
* expand/rust-macro-builtins-asm.cc (parse_clobber_abi): Likewise.
(parse_reg): Likewise.
(parse_operand): Likewise.
(parse_reg_operand): Likewise.
(check_and_set): Likewise.
(parse_options): Likewise.
(parse_format_string): Likewise.
(parse_asm_arg): Likewise.
(parse_asm): Likewise.
* expand/rust-macro-builtins-asm.h (parse_asm_arg): Likewise.
(check_identifier): Likewise.
(check_and_set): Likewise.
(parse_operand): Likewise.
(parse_reg_operand): Likewise.
(parse_options): Likewise.
(parse_reg): Likewise.
(parse_clobber_abi): Likewise.
* expand/rust-macro-builtins.cc (enum class): Likewise.
(inline_asm_maker): Likewise.
* checks/errors/borrowck/ffi-polonius/Cargo.lock: Removed. Likewise.

gcc/testsuite/ChangeLog:
* rust/compile/inline_asm_faulty_clobber.rs: Likewise.
* rust/compile/inline_asm_faulty_clobber_1.rs: Likewise.
* rust/compile/inline_asm_faulty_clobber_2.rs: Likewise.
* rust/compile/inline_asm_illegal_options.rs: Likewise.
---
 gcc/rust/ast/rust-expr.h  |  88 ++-
 .../errors/borrowck/ffi-polonius/Cargo.lock   |  39 -
 gcc/rust/expand/rust-macro-builtins-asm.cc| 139 ++
 gcc/rust/expand/rust-macro-builtins-asm.h |  28 ++--
 gcc/rust/expand/rust-macro-builtins.cc|  10 +-
 .../rust/compile/inline_asm_faulty_clobber.rs |   2 +-
 .../compile/inline_asm_faulty_clobber_1.rs|   2 +-
 .../compile/inline_asm_faulty_clobber_2.rs|   2 +-
 .../compile/inline_asm_illegal_options.rs |   4 +-
 9 files changed, 142 insertions(+), 172 deletions(-)
 delete mode 100644 gcc/rust/checks/errors/borrowck/ffi-polonius/Cargo.lock

diff --git a/gcc/rust/ast/rust-expr.h b/gcc/rust/ast/rust-expr.h
index 1284c7367fe..ad742bfe85d 100644
--- a/gcc/rust/ast/rust-expr.h
+++ b/gcc/rust/ast/rust-expr.h
@@ -6,7 +6,7 @@
 #include "rust-path.h"
 #include "rust-macro.h"
 #include "rust-operators.h"
-#include 
+#include "rust-system.h"
 
 namespace Rust {
 namespace AST {
@@ -4723,22 +4723,20 @@ enum class InlineAsmOption
 struct AnonConst
 {
   NodeId id;
-  std::unique_ptr value;
+  std::unique_ptr expr;
   AnonConst () {}
   AnonConst (const AnonConst &other)
   {
 id = other.id;
-value = other.value == nullptr
- ? nullptr
- : std::unique_ptr (other.value->clone_expr ());
+if (other.expr)
+  expr = other.expr->clone_expr ();
   }
 
   AnonConst operator= (const AnonConst &other)
   {
 id = other.id;
-value = other.value == nullptr
- ? nullptr
- : std::unique_ptr (other.value->clone_expr ());
+if (other.expr)
+  expr = other.expr->clone_expr ();
 return *this;
   }
 };
@@ -4763,7 +4761,7 @@ struct InlineAsmRegOrRegClass
 
   Type type;
   struct Reg reg;
-  struct RegClass regClass;
+  struct RegClass reg_class;
 
   Identifier name;
   location_t locus;
@@ -4790,17 +4788,15 @@ struct InlineAsmOperand
 In (const struct In &other)
 {
   reg = other.reg;
-  expr = other.expr == nullptr
-  ? nullptr
-  : std::unique_ptr (other.expr->clone_expr ());
+  if (other.expr)
+   expr = other.expr->clone_expr ();
 }
 
 In operator= (const struct In &other)
 {
   reg = other.reg;
-  expr = other.expr == nullptr
-  ? nullptr
-  : std::unique_ptr (other.expr->clone_expr ());
+  if (other.expr)
+   expr = other.expr->clone_expr ();
 
   return *this;
 }
@@ -4817,18 +4813,16 @@ struct InlineAsmOperand
 {
   reg = other.reg;
   late = other.late;
-  expr = other.expr == nullptr
-  ? nullptr
-  : std::unique_ptr (other.expr->clone_expr ());
+  if (other.expr)
+   expr = other.expr->clone_expr ();
 }
 
 Out operator= (const struct Out &other)
 {
   reg = other.reg;
   late = other.late;
-  expr = other.expr == nullptr
-  ? nullptr
-  : std::unique_ptr (other.expr->clone_expr ());
+  if (other.expr)
+   expr = other.expr->clone_expr ();
   return *this;
 }
   };
@@ -4844,18 +4838,17 @@ struct InlineAsmOperand
 {
   reg = other.reg;
   late = other.late;
-  expr = other.expr == nullptr
-  ? nullptr
-  : std::unique_ptr (other.expr->clone_expr ());
+  if (other.expr)
+   expr = other.expr->clone_expr ();
 }
 
 InOut operator= (const struct InOut &other)
 {
   reg = other.reg;
 

[COMMITTED 117/145] gccrs: Fix optional trait parsing

2025-03-17 Thread arthur . cohen
From: dave 

gcc/rust/ChangeLog:

* typecheck/rust-hir-type-check-item.cc (TypeCheckItem::visit): Check 
for ?Trait in visitor

gcc/testsuite/ChangeLog:

* rust/compile/issue-2725.rs: New test.

Signed-off-by: Dave Evans 
---
 gcc/rust/typecheck/rust-hir-type-check-item.cc | 18 ++
 gcc/testsuite/rust/compile/issue-2725.rs   |  3 +++
 2 files changed, 21 insertions(+)
 create mode 100644 gcc/testsuite/rust/compile/issue-2725.rs

diff --git a/gcc/rust/typecheck/rust-hir-type-check-item.cc 
b/gcc/rust/typecheck/rust-hir-type-check-item.cc
index 317d16700aa..68e206924bb 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-item.cc
+++ b/gcc/rust/typecheck/rust-hir-type-check-item.cc
@@ -609,6 +609,24 @@ TypeCheckItem::visit (HIR::Module &module)
 void
 TypeCheckItem::visit (HIR::Trait &trait)
 {
+  if (trait.has_type_param_bounds ())
+{
+  for (auto &tp_bound : trait.get_type_param_bounds ())
+   {
+ if (tp_bound.get ()->get_bound_type ()
+ == HIR::TypeParamBound::BoundType::TRAITBOUND)
+   {
+ HIR::TraitBound &tb
+   = static_cast (*tp_bound.get ());
+ if (tb.get_polarity () == BoundPolarity::AntiBound)
+   {
+ rust_error_at (tb.get_locus (),
+"% is not permitted in supertraits");
+   }
+   }
+   }
+}
+
   TraitReference *trait_ref = TraitResolver::Resolve (trait);
   if (trait_ref->is_error ())
 {
diff --git a/gcc/testsuite/rust/compile/issue-2725.rs 
b/gcc/testsuite/rust/compile/issue-2725.rs
new file mode 100644
index 000..a344bc8d464
--- /dev/null
+++ b/gcc/testsuite/rust/compile/issue-2725.rs
@@ -0,0 +1,3 @@
+#[lang = "sized"]
+pub trait Sized {}
+trait Trait: ?Sized {} // { dg-error ".?Trait. is not permitted in 
supertraits" }
-- 
2.45.2



[COMMITTED 096/145] gccrs: Big Refactor after meeting

2025-03-17 Thread arthur . cohen
From: jjasmine 

gcc/rust/ChangeLog:

* ast/rust-expr.h (struct InlineAsmRegOrRegClass):
Remove union
(struct InlineAsmOperand): Make instances of inside struct.
* expand/rust-macro-builtins-asm.cc (parse_clobber_abi):
Change signature with inlineAsmCtx.
(parse_reg): Likewise.
(parse_operand): Likewise.
(parse_reg_operand): Likewise.
(check_and_set): Likewise.
(parse_options): Likewise.
(parse_format_string): Likewise.
(parseAsmArg): Likewise.
(parse_asm): Likewise.
* expand/rust-macro-builtins-asm.h (class InlineAsmContext): Likewise.
(parseAsmArg): Likewise.
(check_and_set): Likewise.
(parse_reg_operand): Likewise.
(parse_options): Likewise.
(parse_reg): Likewise.
(parse_clobber_abi): Likewise.
---
 gcc/rust/ast/rust-expr.h   |  17 ++-
 gcc/rust/expand/rust-macro-builtins-asm.cc | 152 +++--
 gcc/rust/expand/rust-macro-builtins-asm.h  |  44 --
 3 files changed, 157 insertions(+), 56 deletions(-)

diff --git a/gcc/rust/ast/rust-expr.h b/gcc/rust/ast/rust-expr.h
index 1af72965a93..76e5fa7c447 100644
--- a/gcc/rust/ast/rust-expr.h
+++ b/gcc/rust/ast/rust-expr.h
@@ -4744,11 +4744,9 @@ struct InlineAsmRegOrRegClass
   };
 
   Type type;
-  union
-  {
-struct Reg reg;
-struct RegClass regClass;
-  };
+  struct Reg reg;
+  struct RegClass regClass;
+
   Identifier name;
   location_t locus;
 };
@@ -4802,6 +4800,15 @@ struct InlineAsmOperand
   {
 std::unique_ptr sym;
   };
+  RegisterType registerType;
+
+  struct In in;
+  struct Out out;
+  struct InOut inOut;
+  struct SplitInOut splitInOut;
+  struct Const cnst;
+  struct Sym sym;
+
   location_t locus;
 };
 
diff --git a/gcc/rust/expand/rust-macro-builtins-asm.cc 
b/gcc/rust/expand/rust-macro-builtins-asm.cc
index 8c2b88c997d..f7703ddd730 100644
--- a/gcc/rust/expand/rust-macro-builtins-asm.cc
+++ b/gcc/rust/expand/rust-macro-builtins-asm.cc
@@ -39,11 +39,12 @@ parseDirSpec (Parser &parser, TokenId 
last_token_id)
 
 int
 parse_clobber_abi (Parser &parser, TokenId last_token_id,
-  AST::InlineAsm &inlineAsm)
+  InlineAsmContext &inlineAsmCtx)
 {
   // clobber_abi := "clobber_abi("  *("," ) [","] ")"
   // PARSE EVERYTHING COMMITTEDLY IN THIS FUNCTION, WE CONFIRMED VIA 
clobber_abi
   // identifier keyword
+  auto &inlineAsm = inlineAsmCtx.inlineAsm;
   auto token = parser.peek_current_token ();
   if (!parser.skip_token (LEFT_PAREN))
 {
@@ -59,7 +60,6 @@ parse_clobber_abi (Parser &parser, TokenId 
last_token_id,
 "expected `(`, found end of macro arguments");
  return -1;
}
-
   else
{
  rust_error_at (
@@ -125,15 +125,16 @@ parse_clobber_abi (Parser &parser, 
TokenId last_token_id,
   return 0;
 }
 
-int
+tl::optional
 parse_reg (Parser &parser, TokenId last_token_id,
-  AST::InlineAsm &inlineAsm, bool is_explicit)
+  InlineAsmContext &inlineAsmCtx)
 {
+  using RegType = AST::InlineAsmRegOrRegClass::Type;
   if (!parser.skip_token (LEFT_PAREN))
 {
   // TODO: we expect a left parenthesis here, please return the correct
   // error.
-  return 0;
+  return tl::nullopt;
 }
 
   // after successful left parenthesis parsing, we should return ast of
@@ -144,6 +145,9 @@ parse_reg (Parser &parser, TokenId 
last_token_id,
   if (tok_id == IDENTIFIER)
 {
   // construct a InlineAsmRegOrRegClass
+  AST::InlineAsmRegOrRegClass regClass;
+  regClass.type = RegType::RegClass;
+  regClass.regClass.Symbol = token->as_string ();
 }
   else if (tok_id == STRING_LITERAL)
 {
@@ -151,6 +155,7 @@ parse_reg (Parser &parser, TokenId 
last_token_id,
   // for both?
 
   // construct a InlineAsmRegOrRegClass
+  // parse_format_string
 }
   else
 {
@@ -159,23 +164,86 @@ parse_reg (Parser &parser, TokenId 
last_token_id,
   if (!parser.skip_token (RIGHT_PAREN))
 {
   // we expect a left parenthesis here, please return the correct error.
-  return 0;
+  return tl::nullopt;
 }
 
-  return 0;
+  return tl::nullopt;
 }
 
 int
 parse_operand (Parser &parser, TokenId last_token_id,
-  AST::InlineAsm &inlineAsm)
+  InlineAsmContext &inlineAsmCtx)
 {
   return 0;
 }
 
+// From rustc
+tl::optional
+parse_reg_operand (Parser &parser, TokenId last_token_id,
+  InlineAsmContext &inlineAsmCtx)
+{
+  // let name = if p.token.is_ident() && p.look_ahead(1, |t| *t == token::Eq) {
+  //   let (ident, _) = p.token.ident().unwrap();
+  //   p.bump();
+  //   p.expect(&token::Eq)?;
+  //   allow_templates = false;
+  //   Some(ident.name)
+  //   } else {
+  //   None
+  //   };
+
+  using RegisterType = AST::InlineAsmOperand::RegisterType;
+
+  auto token = parser.peek_current_token ();
+  auto iden_token

[COMMITTED 097/145] gccrs: Renamed parseAsmArg to conform to other function names

2025-03-17 Thread arthur . cohen
From: jjasmine 

gcc/rust/ChangeLog:

* expand/rust-macro-builtins-asm.cc (parseAsmArg):
Renamed parseAsmArg to conform to other function names
(parse_asm_arg): Likewise.
(parse_asm): Likewise.
* expand/rust-macro-builtins-asm.h (parseAsmArg): Likewise.
(parse_asm_arg): Likewise.
---
 gcc/rust/expand/rust-macro-builtins-asm.cc | 6 +++---
 gcc/rust/expand/rust-macro-builtins-asm.h  | 4 ++--
 2 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/gcc/rust/expand/rust-macro-builtins-asm.cc 
b/gcc/rust/expand/rust-macro-builtins-asm.cc
index f7703ddd730..3ce9bf9d78b 100644
--- a/gcc/rust/expand/rust-macro-builtins-asm.cc
+++ b/gcc/rust/expand/rust-macro-builtins-asm.cc
@@ -385,8 +385,8 @@ MacroBuiltin::asm_handler (location_t invoc_locus, 
AST::MacroInvocData &invoc,
 }
 
 int
-parseAsmArg (Parser &parser, TokenId last_token_id,
-InlineAsmContext &inlineAsmCtx)
+parse_asm_arg (Parser &parser, TokenId last_token_id,
+  InlineAsmContext &inlineAsmCtx)
 {
   auto token = parser.peek_current_token ();
   tl::optional fm_string;
@@ -505,7 +505,7 @@ parse_asm (location_t invoc_locus, AST::MacroInvocData 
&invoc,
 }
 
   // operands stream, also handles the optional ","
-  parseAsmArg (parser, last_token_id, inlineAsmCtx);
+  parse_asm_arg (parser, last_token_id, inlineAsmCtx);
 
   return tl::nullopt;
 }
diff --git a/gcc/rust/expand/rust-macro-builtins-asm.h 
b/gcc/rust/expand/rust-macro-builtins-asm.h
index 65763780979..a35d7707e6c 100644
--- a/gcc/rust/expand/rust-macro-builtins-asm.h
+++ b/gcc/rust/expand/rust-macro-builtins-asm.h
@@ -31,8 +31,8 @@ public:
 };
 
 int
-parseAsmArg (Parser &p, TokenId last_token_id,
-InlineAsmContext &inlineAsmCtx);
+parse_asm_arg (Parser &p, TokenId last_token_id,
+  InlineAsmContext &inlineAsmCtx);
 
 tl::optional
 parse_global_asm (location_t invoc_locus, AST::MacroInvocData &invoc);
-- 
2.45.2



[COMMITTED 103/145] gccrs: Partial unsafe support for inline asm

2025-03-17 Thread arthur . cohen
From: jjasmine 

gcc/rust/ChangeLog:

* checks/errors/rust-unsafe-checker.cc (UnsafeChecker::visit):
Partial unsafe support for inline asm
* checks/errors/rust-unsafe-checker.h: Likewise.
* hir/tree/rust-hir-expr.h: Likewise.
* hir/tree/rust-hir.cc (InlineAsm::accept_vis): Likewise.
---
 gcc/rust/checks/errors/rust-unsafe-checker.cc | 11 +++
 gcc/rust/checks/errors/rust-unsafe-checker.h  |  1 +
 gcc/rust/hir/tree/rust-hir-expr.h |  4 ++--
 gcc/rust/hir/tree/rust-hir.cc |  9 +
 4 files changed, 23 insertions(+), 2 deletions(-)

diff --git a/gcc/rust/checks/errors/rust-unsafe-checker.cc 
b/gcc/rust/checks/errors/rust-unsafe-checker.cc
index 8d986c72eda..c6ed9221565 100644
--- a/gcc/rust/checks/errors/rust-unsafe-checker.cc
+++ b/gcc/rust/checks/errors/rust-unsafe-checker.cc
@@ -636,6 +636,17 @@ UnsafeChecker::visit (AsyncBlockExpr &)
   // TODO: Visit block expression
 }
 
+void
+UnsafeChecker::visit (InlineAsm &expr)
+{
+  if (unsafe_context.is_in_context ())
+return;
+
+  rust_error_at (
+expr.get_locus (), ErrorCode::E0133,
+"use of inline assembly is unsafe and requires unsafe function or block");
+}
+
 void
 UnsafeChecker::visit (TypeParam &)
 {}
diff --git a/gcc/rust/checks/errors/rust-unsafe-checker.h 
b/gcc/rust/checks/errors/rust-unsafe-checker.h
index ba926729c70..1fa1fe072da 100644
--- a/gcc/rust/checks/errors/rust-unsafe-checker.h
+++ b/gcc/rust/checks/errors/rust-unsafe-checker.h
@@ -115,6 +115,7 @@ private:
   virtual void visit (MatchExpr &expr) override;
   virtual void visit (AwaitExpr &expr) override;
   virtual void visit (AsyncBlockExpr &expr) override;
+  virtual void visit (InlineAsm &expr);
   virtual void visit (TypeParam ¶m) override;
   virtual void visit (ConstGenericParam ¶m) override;
   virtual void visit (LifetimeWhereClauseItem &item) override;
diff --git a/gcc/rust/hir/tree/rust-hir-expr.h 
b/gcc/rust/hir/tree/rust-hir-expr.h
index a0858a36f04..9c66f3e79d5 100644
--- a/gcc/rust/hir/tree/rust-hir-expr.h
+++ b/gcc/rust/hir/tree/rust-hir-expr.h
@@ -3878,9 +3878,9 @@ public:
 
   std::vector line_spans;
 
-  void accept_vis (HIRExpressionVisitor &vis) override{};
+  void accept_vis (HIRExpressionVisitor &vis) override;
 
-  void accept_vis (HIRFullVisitor &vis) override{};
+  void accept_vis (HIRFullVisitor &vis) override;
 
   std::string as_string () const override { return "InlineAsm HIR Node"; }
 
diff --git a/gcc/rust/hir/tree/rust-hir.cc b/gcc/rust/hir/tree/rust-hir.cc
index 5cb22450672..66c6240bfa5 100644
--- a/gcc/rust/hir/tree/rust-hir.cc
+++ b/gcc/rust/hir/tree/rust-hir.cc
@@ -17,6 +17,7 @@
 // .
 
 #include "rust-ast-full.h"
+#include "rust-hir-expr.h"
 #include "rust-hir-full.h"
 #include "rust-hir-visitor.h"
 #include "rust-diagnostics.h"
@@ -3769,6 +3770,14 @@ BorrowExpr::accept_vis (HIRFullVisitor &vis)
   vis.visit (*this);
 }
 
+void
+InlineAsm::accept_vis (HIRExpressionVisitor &vis)
+{}
+
+void
+InlineAsm::accept_vis (HIRFullVisitor &vis)
+{}
+
 void
 BorrowExpr::accept_vis (HIRExpressionVisitor &vis)
 {
-- 
2.45.2



[COMMITTED 101/145] gccrs: Slim down the test cases

2025-03-17 Thread arthur . cohen
From: jjasmine 

gcc/testsuite/ChangeLog:

* rust/compile/inline_asm_faulty_clobber.rs: compress
the test
* rust/compile/inline_asm_nop.rs: compress
the test
* rust/compile/inline_asm_faulty_clobber_1.rs: Removed.
* rust/compile/inline_asm_faulty_clobber_2.rs: Removed.
* rust/compile/inline_asm_nop_2.rs: Removed.
---
 .../rust/compile/inline_asm_faulty_clobber.rs|  2 ++
 .../rust/compile/inline_asm_faulty_clobber_1.rs  | 12 
 .../rust/compile/inline_asm_faulty_clobber_2.rs  | 12 
 gcc/testsuite/rust/compile/inline_asm_nop.rs |  1 +
 gcc/testsuite/rust/compile/inline_asm_nop_2.rs   | 12 
 5 files changed, 3 insertions(+), 36 deletions(-)
 delete mode 100644 gcc/testsuite/rust/compile/inline_asm_faulty_clobber_1.rs
 delete mode 100644 gcc/testsuite/rust/compile/inline_asm_faulty_clobber_2.rs
 delete mode 100644 gcc/testsuite/rust/compile/inline_asm_nop_2.rs

diff --git a/gcc/testsuite/rust/compile/inline_asm_faulty_clobber.rs 
b/gcc/testsuite/rust/compile/inline_asm_faulty_clobber.rs
index 1358b5eb490..ea3dac7734e 100644
--- a/gcc/testsuite/rust/compile/inline_asm_faulty_clobber.rs
+++ b/gcc/testsuite/rust/compile/inline_asm_faulty_clobber.rs
@@ -8,5 +8,7 @@ macro_rules! asm {
 fn main() {
 unsafe {
 asm!("nop", clobber_abi());  // { dg-error "at least one abi must be 
provided as an argument to 'clobber_abi'" }
+asm!("nop", clobber_abi+);  // { dg-error "expected '\\(', found 
'\\+'" }
+asm!("nop", clobber_abi);  // { dg-error "expected '\\(', found end of 
macro arguments" }
 }
 }
\ No newline at end of file
diff --git a/gcc/testsuite/rust/compile/inline_asm_faulty_clobber_1.rs 
b/gcc/testsuite/rust/compile/inline_asm_faulty_clobber_1.rs
deleted file mode 100644
index 56889923939..000
--- a/gcc/testsuite/rust/compile/inline_asm_faulty_clobber_1.rs
+++ /dev/null
@@ -1,12 +0,0 @@
-#![feature(rustc_attrs)]
-
-#[rustc_builtin_macro]
-macro_rules! asm {
-() => {}
-}
-
-fn main() {
-unsafe {
-asm!("nop", clobber_abi);  // { dg-error "expected '\\(', found end of 
macro arguments" }
-}
-}
\ No newline at end of file
diff --git a/gcc/testsuite/rust/compile/inline_asm_faulty_clobber_2.rs 
b/gcc/testsuite/rust/compile/inline_asm_faulty_clobber_2.rs
deleted file mode 100644
index 98cd0525f55..000
--- a/gcc/testsuite/rust/compile/inline_asm_faulty_clobber_2.rs
+++ /dev/null
@@ -1,12 +0,0 @@
-#![feature(rustc_attrs)]
-
-#[rustc_builtin_macro]
-macro_rules! asm {
-() => {}
-}
-
-fn main() {
-unsafe {
-asm!("nop", clobber_abi+);  // { dg-error "expected '\\(', found 
'\\+'" }
-}
-}
\ No newline at end of file
diff --git a/gcc/testsuite/rust/compile/inline_asm_nop.rs 
b/gcc/testsuite/rust/compile/inline_asm_nop.rs
index 7da9bef3e56..ba21d024079 100644
--- a/gcc/testsuite/rust/compile/inline_asm_nop.rs
+++ b/gcc/testsuite/rust/compile/inline_asm_nop.rs
@@ -8,5 +8,6 @@ macro_rules! asm {
 fn main() {
 unsafe {
 asm!("nop");
+asm!("nop",);
 }
 }
diff --git a/gcc/testsuite/rust/compile/inline_asm_nop_2.rs 
b/gcc/testsuite/rust/compile/inline_asm_nop_2.rs
deleted file mode 100644
index 76f53fadbe3..000
--- a/gcc/testsuite/rust/compile/inline_asm_nop_2.rs
+++ /dev/null
@@ -1,12 +0,0 @@
-#![feature(rustc_attrs)]
-
-#[rustc_builtin_macro]
-macro_rules! asm {
-() => {}
-}
-
-fn main() {
-unsafe {
-asm!("nop",);
-}
-}
-- 
2.45.2



[PATCHSET] Update Rust frontend 17/03/2024 1/4

2025-03-17 Thread arthur . cohen
Hi everyone,

This patchset is the first of four similarly-sized patchsets aimed at
updating upstream with our most recent changes to the Rust frontend.

We plan on upstreaming small patchsets every week up to the release
of 15.1.

This first set's main change is the addition of the polonius
borrow-checker to the compiler, as well as all of the infrastructure
required for FFI and interacting with Rust crates in general. As
such, an installation of the Rust programming language is now required
for building the Rust frontend to GCC. As a reminder, this change is
temporary and we are working hard towards supporting enough of the
language that we can bootstrap our own requirements.

This patchset also adds some support for inline assembly by taking
inspiration from the C frontend. There are also numerous changes for
supporting the Rust core library, such as handling more attributes,
handling negative trait implementations, built-in derive macros, and
more.

I will be following up with the next three patchsets in the coming days.

Best,

Arthur


[COMMITTED 005/145] gccrs: borrowck: Polonius dump

2025-03-17 Thread arthur . cohen
From: Jakub Dupak 

gcc/rust/ChangeLog:

* checks/errors/borrowck/polonius/rust-polonius.h (struct FullPoint):
Polonius facts dump.
(struct Facts): Polonius facts dump.
* checks/errors/borrowck/rust-bir-dump.cc (Dump::go):
Polonius facts dump.
(Dump::visit): Polonius facts dump.
(Dump::visit_place): Polonius facts dump.
(Dump::visit_move_place): Polonius facts dump.
(Dump::visit_scope): Polonius facts dump.
* checks/errors/borrowck/rust-borrow-checker.cc (BorrowChecker::go): 
Polonius facts dump.

Signed-off-by: Jakub Dupak 
---
 .../errors/borrowck/polonius/rust-polonius.h  |  2 +-
 .../checks/errors/borrowck/rust-bir-dump.cc   | 25 +--
 .../errors/borrowck/rust-borrow-checker.cc| 69 ++-
 3 files changed, 88 insertions(+), 8 deletions(-)

diff --git a/gcc/rust/checks/errors/borrowck/polonius/rust-polonius.h 
b/gcc/rust/checks/errors/borrowck/polonius/rust-polonius.h
index fa73528ea85..b93906cdfc1 100644
--- a/gcc/rust/checks/errors/borrowck/polonius/rust-polonius.h
+++ b/gcc/rust/checks/errors/borrowck/polonius/rust-polonius.h
@@ -148,7 +148,7 @@ struct Facts
   void dump_var_used_at (std::ostream &os) const
   {
 for (auto &e : var_used_at)
-  os << e.first - 1 << " " << FullPoint (e.second) << "\n";
+  os << e.first << " " << FullPoint (e.second) << "\n";
   }
 
   void dump_var_defined_at (std::ostream &os) const
diff --git a/gcc/rust/checks/errors/borrowck/rust-bir-dump.cc 
b/gcc/rust/checks/errors/borrowck/rust-bir-dump.cc
index 45a071da565..a35f47b86d6 100644
--- a/gcc/rust/checks/errors/borrowck/rust-bir-dump.cc
+++ b/gcc/rust/checks/errors/borrowck/rust-bir-dump.cc
@@ -115,7 +115,7 @@ Dump::go (bool enable_simplify_cfg)
   if (enable_simplify_cfg)
 simplify_cfg (func, bb_fold_map);
 
-  renumber_places (func, place_map);
+  // renumber_places (func, place_map);
 
   stream << "fn " << name << "(";
   print_comma_separated (stream, func.arguments, [this] (PlaceId place_id) {
@@ -214,6 +214,8 @@ Dump::visit (const Statement &stmt)
   visit_place (stmt.get_place ());
   stream << ")";
   break;
+default:
+  rust_internal_error_at (UNKNOWN_LOCATION, "Unknown statement kind.");
 }
   statement_place = INVALID_PLACE;
 }
@@ -251,7 +253,8 @@ Dump::visit_place (PlaceId place_id)
   stream << "const " << get_tyty_name (place.tyty);
   break;
 case Place::INVALID:
-  stream << "_INVALID";
+  if (place_id == INVALID_PLACE)
+   stream << "_INVALID";
 }
 }
 
@@ -259,7 +262,7 @@ void
 Dump::visit_move_place (PlaceId place_id)
 {
   const Place &place = func.place_db[place_id];
-  if (!place.is_constant ())
+  if (place.should_be_moved ())
 stream << "move ";
   visit_place (place_id);
 }
@@ -267,7 +270,11 @@ Dump::visit_move_place (PlaceId place_id)
 void
 Dump::visit (const BorrowExpr &expr)
 {
-  stream << "&";
+  stream << "&"
+<< "'?" << expr.get_origin () << " ";
+  if (func.place_db.get_loans ()[expr.get_loan ()].mutability
+  == Mutability::Mut)
+stream << "mut ";
   visit_place (expr.get_place ());
 }
 
@@ -360,7 +367,15 @@ Dump::visit_scope (ScopeId id, size_t depth)
   indent (depth + 1) << "let _";
   stream << place_map[local] << ": "
 << get_tyty_name (func.place_db[local].tyty);
-  stream << ";\n";
+  stream << ";\t";
+
+  stream << "[";
+  print_comma_separated (stream,
+func.place_db[local].regions.get_regions (),
+[this] (FreeRegion region_id) {
+  stream << "'?" << region_id;
+});
+  stream << "]\n";
 }
   for (auto &child : scope.children)
 visit_scope (child, (id >= 1) ? depth + 1 : depth);
diff --git a/gcc/rust/checks/errors/borrowck/rust-borrow-checker.cc 
b/gcc/rust/checks/errors/borrowck/rust-borrow-checker.cc
index 8380f2c5cfa..e41cea33b3b 100644
--- a/gcc/rust/checks/errors/borrowck/rust-borrow-checker.cc
+++ b/gcc/rust/checks/errors/borrowck/rust-borrow-checker.cc
@@ -18,9 +18,10 @@
 
 #include "rust-borrow-checker.h"
 #include "rust-function-collector.h"
+#include "rust-bir-fact-collector.h"
 #include "rust-bir-builder.h"
 #include "rust-bir-dump.h"
-#include "rust-bir-fact-collector.h"
+#include "polonius/rust-polonius.h"
 
 namespace Rust {
 namespace HIR {
@@ -36,7 +37,7 @@ mkdir_wrapped (const std::string &dirname)
 #elif __APPLE__
   ret = mkdir (dirname.c_str (), 0775);
 #endif
-  (void) ret;
+  rust_assert (ret == 0 || errno == EEXIST);
 }
 
 void
@@ -68,6 +69,8 @@ BorrowChecker::go (HIR::Crate &crate)
= mappings->get_crate_name (crate.get_mappings ().get_crate_num (),
crate_name);
   rust_assert (ok);
+
+  mkdir_wrapped ("nll_facts_gccrs");
 }
 
   FunctionCollector collector;
@@ -75,6 +78,9 @@ BorrowChecker::go (HIR::Crate &crate)
 
   for (auto func : collector.get_functions

[COMMITTED 002/145] gccrs: git: Ignore libgrust build folders

2025-03-17 Thread arthur . cohen
From: Arthur Cohen 

ChangeLog:

* .gitignore: Add libgrust target folders to the ignore list.
---
 .gitignore | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/.gitignore b/.gitignore
index f044fe16b5f..7150fc3b29c 100644
--- a/.gitignore
+++ b/.gitignore
@@ -71,3 +71,6 @@ stamp-*
 /gmp*
 /isl*
 /gettext*
+
+# ADDITIONS from GCCRS front-end
+libgrust/*/target/
-- 
2.45.2



[COMMITTED 055/145] gccrs: Corrected access specifiers

2025-03-17 Thread arthur . cohen
From: Kushal Pal 

gcc/rust/ChangeLog:

* ast/rust-expr.h (class OperatorExpr):
Location should be private.
* hir/tree/rust-hir-expr.h (class OperatorExpr): Likewise.

Signed-off-by: Kushal Pal 
---
 gcc/rust/ast/rust-expr.h  | 2 +-
 gcc/rust/hir/tree/rust-hir-expr.h | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/gcc/rust/ast/rust-expr.h b/gcc/rust/ast/rust-expr.h
index b990358df29..015680b2af4 100644
--- a/gcc/rust/ast/rust-expr.h
+++ b/gcc/rust/ast/rust-expr.h
@@ -302,7 +302,7 @@ protected:
 class OperatorExpr : public ExprWithoutBlock
 {
   // TODO: create binary and unary operator subclasses?
-public:
+private:
   location_t locus;
 
 protected:
diff --git a/gcc/rust/hir/tree/rust-hir-expr.h 
b/gcc/rust/hir/tree/rust-hir-expr.h
index ad0cdd47dfc..11b1c4c295f 100644
--- a/gcc/rust/hir/tree/rust-hir-expr.h
+++ b/gcc/rust/hir/tree/rust-hir-expr.h
@@ -160,7 +160,7 @@ protected:
 class OperatorExpr : public ExprWithoutBlock
 {
   // TODO: create binary and unary operator subclasses?
-public:
+private:
   location_t locus;
 
 protected:
-- 
2.45.2



[COMMITTED 060/145] gccrs: Add new test for box syntax

2025-03-17 Thread arthur . cohen
From: Pierre-Emmanuel Patry 

Add a new test to prevent regressions on the box syntax as well as its
feature gate.

gcc/testsuite/ChangeLog:

* rust/compile/box_syntax.rs: New test.
* rust/compile/box_syntax_feature_gate.rs: New test.

Signed-off-by: Pierre-Emmanuel Patry 
---
 gcc/testsuite/rust/compile/box_syntax.rs  | 6 ++
 gcc/testsuite/rust/compile/box_syntax_feature_gate.rs | 5 +
 2 files changed, 11 insertions(+)
 create mode 100644 gcc/testsuite/rust/compile/box_syntax.rs
 create mode 100644 gcc/testsuite/rust/compile/box_syntax_feature_gate.rs

diff --git a/gcc/testsuite/rust/compile/box_syntax.rs 
b/gcc/testsuite/rust/compile/box_syntax.rs
new file mode 100644
index 000..c63284b5163
--- /dev/null
+++ b/gcc/testsuite/rust/compile/box_syntax.rs
@@ -0,0 +1,6 @@
+// { dg-options "-fsyntax-only" }
+#![feature(box_syntax)]
+
+fn main() {
+let x: Box<_> = box 1;
+}
diff --git a/gcc/testsuite/rust/compile/box_syntax_feature_gate.rs 
b/gcc/testsuite/rust/compile/box_syntax_feature_gate.rs
new file mode 100644
index 000..8eb5503dde6
--- /dev/null
+++ b/gcc/testsuite/rust/compile/box_syntax_feature_gate.rs
@@ -0,0 +1,5 @@
+// { dg-options "-frust-compile-until=lowering" }
+
+fn main() {
+let x: Box<_> = box 1; //{ dg-error "box expression syntax is 
experimental." "" { target *-*-* }  }
+}
-- 
2.45.2



[COMMITTED 044/145] gccrs: Change lookup_hir_pattern return type

2025-03-17 Thread arthur . cohen
From: Pierre-Emmanuel Patry 

Wrap the function's return type within an optional in order to
differentiate between a null pointer and a missing value.

gcc/rust/ChangeLog:

* util/rust-hir-map.cc (Mappings::insert_hir_pattern): Change call site
in order to accomodate new return type.
(Mappings::lookup_hir_pattern): Change the function's return type.
* util/rust-hir-map.h: Update the function's prototype.

Signed-off-by: Pierre-Emmanuel Patry 
---
 gcc/rust/util/rust-hir-map.cc | 6 +++---
 gcc/rust/util/rust-hir-map.h  | 2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/gcc/rust/util/rust-hir-map.cc b/gcc/rust/util/rust-hir-map.cc
index d1f55a372ac..2ac614141a2 100644
--- a/gcc/rust/util/rust-hir-map.cc
+++ b/gcc/rust/util/rust-hir-map.cc
@@ -692,18 +692,18 @@ void
 Mappings::insert_hir_pattern (HIR::Pattern *pattern)
 {
   auto id = pattern->get_mappings ().get_hirid ();
-  rust_assert (lookup_hir_pattern (id) == nullptr);
+  rust_assert (!lookup_hir_pattern (id));
 
   hirPatternMappings[id] = pattern;
   insert_node_to_hir (pattern->get_mappings ().get_nodeid (), id);
 }
 
-HIR::Pattern *
+tl::optional
 Mappings::lookup_hir_pattern (HirId id)
 {
   auto it = hirPatternMappings.find (id);
   if (it == hirPatternMappings.end ())
-return nullptr;
+return tl::nullopt;
 
   return it->second;
 }
diff --git a/gcc/rust/util/rust-hir-map.h b/gcc/rust/util/rust-hir-map.h
index 740a8e6d499..c556d766b35 100644
--- a/gcc/rust/util/rust-hir-map.h
+++ b/gcc/rust/util/rust-hir-map.h
@@ -170,7 +170,7 @@ public:
   tl::optional lookup_hir_struct_field (HirId id);
 
   void insert_hir_pattern (HIR::Pattern *pattern);
-  HIR::Pattern *lookup_hir_pattern (HirId id);
+  tl::optional lookup_hir_pattern (HirId id);
 
   void walk_local_defids_for_crate (CrateNum crateNum,
std::function cb);
-- 
2.45.2



[COMMITTED 065/145] gccrs: Add outer attributes to struct expr fields

2025-03-17 Thread arthur . cohen
From: Pierre-Emmanuel Patry 

Struct fields can have outer attributes on their field for various
purpose, this behavior should be reflected upon struct expr fields.

gcc/rust/ChangeLog:

* ast/rust-ast-collector.cc (TokenCollector::visit): Output field
attributes.
* ast/rust-expr.h (class StructExprField): Add outer attributes member.
* parse/rust-parse-impl.h (Parser::parse_struct_expr_field): Parse
outer attributes and store them in the appropriate AST node.

Signed-off-by: Pierre-Emmanuel Patry 
---
 gcc/rust/ast/rust-ast-collector.cc |  9 ++
 gcc/rust/ast/rust-expr.h   | 44 +++---
 gcc/rust/parse/rust-parse-impl.h   |  5 
 3 files changed, 42 insertions(+), 16 deletions(-)

diff --git a/gcc/rust/ast/rust-ast-collector.cc 
b/gcc/rust/ast/rust-ast-collector.cc
index a75722587f5..d43aef80760 100644
--- a/gcc/rust/ast/rust-ast-collector.cc
+++ b/gcc/rust/ast/rust-ast-collector.cc
@@ -1099,8 +1099,7 @@ TokenCollector::visit (StructExprStruct &expr)
 void
 TokenCollector::visit (StructExprFieldIdentifier &expr)
 {
-  // TODO: Add attributes
-  // visit_items_as_lines (expr.get_attrs ());
+  visit_items_as_lines (expr.get_outer_attrs ());
   auto id = expr.get_field_name ().as_string ();
   push (Rust::Token::make_identifier (expr.get_locus (), std::move (id)));
 }
@@ -1108,8 +1107,7 @@ TokenCollector::visit (StructExprFieldIdentifier &expr)
 void
 TokenCollector::visit (StructExprFieldIdentifierValue &expr)
 {
-  // TODO: Add attributes
-  // visit_items_as_lines (expr.get_attrs ());
+  visit_items_as_lines (expr.get_outer_attrs ());
   auto id = expr.get_field_name ();
   push (Rust::Token::make_identifier (expr.get_locus (), std::move (id)));
   push (Rust::Token::make (COLON, UNDEF_LOCATION));
@@ -1119,8 +1117,7 @@ TokenCollector::visit (StructExprFieldIdentifierValue 
&expr)
 void
 TokenCollector::visit (StructExprFieldIndexValue &expr)
 {
-  // TODO: Add attributes
-  // visit_items_as_lines (expr.get_attrs ());
+  visit_items_as_lines (expr.get_outer_attrs ());
   push (Rust::Token::make_int (expr.get_locus (),
   std::to_string (expr.get_index (;
   push (Rust::Token::make (COLON, UNDEF_LOCATION));
diff --git a/gcc/rust/ast/rust-expr.h b/gcc/rust/ast/rust-expr.h
index c3b93d4dc0d..50c0cb146ed 100644
--- a/gcc/rust/ast/rust-expr.h
+++ b/gcc/rust/ast/rust-expr.h
@@ -1758,6 +1758,13 @@ public:
 
   NodeId get_node_id () const { return node_id; }
 
+  const std::vector &get_outer_attrs () const
+  {
+return outer_attrs;
+  }
+
+  std::vector &get_outer_attrs () { return outer_attrs; }
+
 protected:
   // pure virtual clone implementation
   virtual StructExprField *clone_struct_expr_field_impl () const = 0;
@@ -1765,6 +1772,12 @@ protected:
   StructExprField () : node_id (Analysis::Mappings::get ().get_next_node_id ())
   {}
 
+  StructExprField (AST::AttrVec outer_attrs)
+: outer_attrs (std::move (outer_attrs)),
+  node_id (Analysis::Mappings::get ().get_next_node_id ())
+  {}
+
+  AST::AttrVec outer_attrs;
   NodeId node_id;
 };
 
@@ -1775,9 +1788,10 @@ class StructExprFieldIdentifier : public StructExprField
   location_t locus;
 
 public:
-  StructExprFieldIdentifier (Identifier field_identifier, location_t locus)
-: StructExprField (), field_name (std::move (field_identifier)),
-  locus (locus)
+  StructExprFieldIdentifier (Identifier field_identifier,
+AST::AttrVec outer_attrs, location_t locus)
+: StructExprField (std::move (outer_attrs)),
+  field_name (std::move (field_identifier)), locus (locus)
   {}
 
   std::string as_string () const override { return field_name.as_string (); }
@@ -1804,19 +1818,22 @@ class StructExprFieldWithVal : public StructExprField
   std::unique_ptr value;
 
 protected:
-  StructExprFieldWithVal (std::unique_ptr field_value)
-: StructExprField (), value (std::move (field_value))
+  StructExprFieldWithVal (std::unique_ptr field_value,
+ AST::AttrVec outer_attrs)
+: StructExprField (std::move (outer_attrs)), value (std::move 
(field_value))
   {}
 
   // Copy constructor requires clone
   StructExprFieldWithVal (StructExprFieldWithVal const &other)
-: value (other.value->clone_expr ())
+: StructExprField (other.get_outer_attrs ()),
+  value (other.value->clone_expr ())
   {}
 
   // Overload assignment operator to clone unique_ptr
   StructExprFieldWithVal &operator= (StructExprFieldWithVal const &other)
   {
 value = other.value->clone_expr ();
+outer_attrs = other.get_outer_attrs ();
 
 return *this;
   }
@@ -1843,10 +1860,17 @@ class StructExprFieldIdentifierValue : public 
StructExprFieldWithVal
   location_t locus;
 
 public:
+  StructExprFieldIdentifierValue (Identifier field_identifier,
+ std::unique_ptr field_value,
+ AST::AttrVec outer_attrs, location_t locus)
+: StructExprF

[COMMITTED 058/145] gccrs: Parse box expressions

2025-03-17 Thread arthur . cohen
From: Pierre-Emmanuel Patry 

Add support for old box expression syntax.

gcc/rust/ChangeLog:

* ast/rust-ast-collector.cc (TokenCollector::visit): Add visit member
function for BoxExpr nodes.
* ast/rust-ast-collector.h: Add visit function prototype.
* ast/rust-ast-visitor.cc (DefaultASTVisitor::visit): Add visit member
function to default ast visitor.
* ast/rust-ast-visitor.h: Add visit function's prototype.
* ast/rust-ast.cc (BoxExpr::as_string): Add as_string function
implementation for BoxExpr.
(BoxExpr::accept_vis): Add accept_vis implementation to BoxExpr.
* ast/rust-expr.h (class BoxExpr): Add BoxExpr class to represent boxed
expressions.
* expand/rust-derive.h: Add BoxExpr visit function prototype.
* hir/rust-ast-lower-base.cc (ASTLoweringBase::visit): Add BoxExpr
visitor implementation.
* hir/rust-ast-lower-base.h: Add visit function's prototype.
* hir/rust-ast-lower-expr.cc (ASTLoweringExpr::visit): Add BoxExpr
visitor implementation.
* hir/rust-ast-lower-expr.h: Add visit function's prototype.
* parse/rust-parse-impl.h (Parser::parse_box_expr): Add parse_box_expr
function's implementation.
* parse/rust-parse.h: Add parse_box_expr function's prototype.
* resolve/rust-ast-resolve-base.cc (ResolverBase::visit): Add resolver
visit implementation.
* resolve/rust-ast-resolve-base.h: Add resolver's visit function
prototype.

Signed-off-by: Pierre-Emmanuel Patry 
---
 gcc/rust/ast/rust-ast-collector.cc|  8 +++
 gcc/rust/ast/rust-ast-collector.h |  1 +
 gcc/rust/ast/rust-ast-visitor.cc  |  7 +++
 gcc/rust/ast/rust-ast-visitor.h   |  2 +
 gcc/rust/ast/rust-ast.cc  | 12 
 gcc/rust/ast/rust-expr.h  | 73 +++
 gcc/rust/expand/rust-derive.h |  1 +
 gcc/rust/hir/rust-ast-lower-base.cc   |  5 ++
 gcc/rust/hir/rust-ast-lower-base.h|  1 +
 gcc/rust/hir/rust-ast-lower-expr.cc   |  8 +++
 gcc/rust/hir/rust-ast-lower-expr.h|  1 +
 gcc/rust/parse/rust-parse-impl.h  | 23 +++
 gcc/rust/parse/rust-parse.h   |  3 +
 gcc/rust/resolve/rust-ast-resolve-base.cc |  4 ++
 gcc/rust/resolve/rust-ast-resolve-base.h  |  1 +
 15 files changed, 150 insertions(+)

diff --git a/gcc/rust/ast/rust-ast-collector.cc 
b/gcc/rust/ast/rust-ast-collector.cc
index 5c44d3dc1cb..a7f6ed57623 100644
--- a/gcc/rust/ast/rust-ast-collector.cc
+++ b/gcc/rust/ast/rust-ast-collector.cc
@@ -20,6 +20,7 @@
 #include "rust-diagnostics.h"
 #include "rust-item.h"
 #include "rust-keyword-values.h"
+#include "rust-token.h"
 
 namespace Rust {
 namespace AST {
@@ -1315,6 +1316,13 @@ TokenCollector::visit (RangeToInclExpr &expr)
   visit (expr.get_to_expr ());
 }
 
+void
+TokenCollector::visit (BoxExpr &expr)
+{
+  push (Rust::Token::make (BOX, expr.get_locus ()));
+  visit (expr.get_boxed_expr ());
+}
+
 void
 TokenCollector::visit (ReturnExpr &expr)
 {
diff --git a/gcc/rust/ast/rust-ast-collector.h 
b/gcc/rust/ast/rust-ast-collector.h
index def95516945..7b418bb6d31 100644
--- a/gcc/rust/ast/rust-ast-collector.h
+++ b/gcc/rust/ast/rust-ast-collector.h
@@ -288,6 +288,7 @@ public:
   void visit (RangeFromToInclExpr &expr);
   void visit (RangeToInclExpr &expr);
   void visit (ReturnExpr &expr);
+  void visit (BoxExpr &expr);
   void visit (UnsafeBlockExpr &expr);
   void visit (LoopExpr &expr);
   void visit (WhileLoopExpr &expr);
diff --git a/gcc/rust/ast/rust-ast-visitor.cc b/gcc/rust/ast/rust-ast-visitor.cc
index 2021012a693..ba3eb67cbe7 100644
--- a/gcc/rust/ast/rust-ast-visitor.cc
+++ b/gcc/rust/ast/rust-ast-visitor.cc
@@ -531,6 +531,13 @@ DefaultASTVisitor::visit (AST::ReturnExpr &expr)
 visit (expr.get_returned_expr ());
 }
 
+void
+DefaultASTVisitor::visit (AST::BoxExpr &expr)
+{
+  visit_outer_attrs (expr);
+  visit (expr.get_boxed_expr ());
+}
+
 void
 DefaultASTVisitor::visit (AST::UnsafeBlockExpr &expr)
 {
diff --git a/gcc/rust/ast/rust-ast-visitor.h b/gcc/rust/ast/rust-ast-visitor.h
index 5da8a7b27e5..1d96304f188 100644
--- a/gcc/rust/ast/rust-ast-visitor.h
+++ b/gcc/rust/ast/rust-ast-visitor.h
@@ -112,6 +112,7 @@ public:
   virtual void visit (RangeFromToInclExpr &expr) = 0;
   virtual void visit (RangeToInclExpr &expr) = 0;
   virtual void visit (ReturnExpr &expr) = 0;
+  virtual void visit (BoxExpr &expr) = 0;
   virtual void visit (UnsafeBlockExpr &expr) = 0;
   virtual void visit (LoopExpr &expr) = 0;
   virtual void visit (WhileLoopExpr &expr) = 0;
@@ -297,6 +298,7 @@ protected:
   virtual void visit (AST::RangeFromToInclExpr &expr) override;
   virtual void visit (AST::RangeToInclExpr &expr) override;
   virtual void visit (AST::ReturnExpr &expr) override;
+  virtual void visit (AST::BoxExpr &expr) override;
   virtual void visit (AST::UnsafeBlockExpr &expr) override;
   virtual void visit (AST::Loop

[COMMITTED 051/145] gccrs: Remove useless cstddef header from rust-fmt

2025-03-17 Thread arthur . cohen
From: jjasmine 

gcc/rust/ChangeLog:

* ast/rust-fmt.h:  Remove useless cstddef header from rust-fmt
---
 gcc/rust/ast/rust-fmt.h | 1 -
 1 file changed, 1 deletion(-)

diff --git a/gcc/rust/ast/rust-fmt.h b/gcc/rust/ast/rust-fmt.h
index fe933eabdd3..31100ea8f84 100644
--- a/gcc/rust/ast/rust-fmt.h
+++ b/gcc/rust/ast/rust-fmt.h
@@ -20,7 +20,6 @@
 #define RUST_FMT_H
 
 #include "rust-system.h"
-#include 
 
 // FIXME: How to encode Option?
 
-- 
2.45.2



[COMMITTED 054/145] gccrs: Make gccrs recognize negative_impls

2025-03-17 Thread arthur . cohen
From: jjasmine 

gcc/rust/ChangeLog:

* checks/errors/rust-feature-gate.cc (FeatureGate::visit): make
gccrs recognize negative_impls
* checks/errors/rust-feature-gate.h: likewise.
* checks/errors/rust-feature.cc (Feature::create): likewise.
* checks/errors/rust-feature.h: likewise.

gcc/testsuite/ChangeLog:

* rust/compile/negative_impls.rs: New test.
* rust/compile/negative_impls_2.rs: New test.
---
 gcc/rust/checks/errors/rust-feature-gate.cc|  9 +
 gcc/rust/checks/errors/rust-feature-gate.h |  1 +
 gcc/rust/checks/errors/rust-feature.cc |  4 
 gcc/rust/checks/errors/rust-feature.h  |  1 +
 gcc/testsuite/rust/compile/negative_impls.rs   |  8 
 gcc/testsuite/rust/compile/negative_impls_2.rs | 16 
 6 files changed, 39 insertions(+)
 create mode 100644 gcc/testsuite/rust/compile/negative_impls.rs
 create mode 100644 gcc/testsuite/rust/compile/negative_impls_2.rs

diff --git a/gcc/rust/checks/errors/rust-feature-gate.cc 
b/gcc/rust/checks/errors/rust-feature-gate.cc
index c20164455c2..6ef2d1f710f 100644
--- a/gcc/rust/checks/errors/rust-feature-gate.cc
+++ b/gcc/rust/checks/errors/rust-feature-gate.cc
@@ -19,6 +19,7 @@
 #include "rust-feature-gate.h"
 #include "rust-abi.h"
 #include "rust-ast-visitor.h"
+#include "rust-feature.h"
 
 namespace Rust {
 
@@ -144,4 +145,12 @@ FeatureGate::visit (AST::ExternalTypeItem &item)
"extern types are experimental");
 }
 
+void
+FeatureGate::visit (AST::TraitImpl &impl)
+{
+  if (impl.is_exclam ())
+gate (Feature::Name::NEGATIVE_IMPLS, impl.get_locus (),
+ "negative_impls are not yet implemented");
+};
+
 } // namespace Rust
diff --git a/gcc/rust/checks/errors/rust-feature-gate.h 
b/gcc/rust/checks/errors/rust-feature-gate.h
index 1782e6d66fb..de48f225598 100644
--- a/gcc/rust/checks/errors/rust-feature-gate.h
+++ b/gcc/rust/checks/errors/rust-feature-gate.h
@@ -127,6 +127,7 @@ public:
   void visit (AST::StaticItem &static_item) override {}
   void visit (AST::TraitItemConst &item) override {}
   void visit (AST::TraitItemType &item) override {}
+  void visit (AST::TraitImpl &impl) override;
   void visit (AST::Trait &trait) override {}
   void visit (AST::ExternalTypeItem &item) override;
   void visit (AST::ExternalStaticItem &item) override {}
diff --git a/gcc/rust/checks/errors/rust-feature.cc 
b/gcc/rust/checks/errors/rust-feature.cc
index 92d10650df7..1e11bd541a5 100644
--- a/gcc/rust/checks/errors/rust-feature.cc
+++ b/gcc/rust/checks/errors/rust-feature.cc
@@ -42,6 +42,9 @@ Feature::create (Feature::Name name)
 case Feature::Name::EXTERN_TYPES:
   return Feature (Feature::Name::EXTERN_TYPES, Feature::State::ACTIVE,
  "extern_types", "1.23.0", 43467, tl::nullopt, "");
+case Feature::Name::NEGATIVE_IMPLS:
+  return Feature (Feature::Name::NEGATIVE_IMPLS, Feature::State::ACTIVE,
+ "negative_impls", "1.0.0", 68318, tl::nullopt, "");
 default:
   rust_unreachable ();
 }
@@ -52,6 +55,7 @@ const std::map 
Feature::name_hash_map = {
   {"intrinsics", Feature::Name::INTRINSICS},
   {"rustc_attrs", Feature::Name::RUSTC_ATTRS},
   {"decl_macro", Feature::Name::DECL_MACRO},
+  {"negative_impls", Feature::Name::NEGATIVE_IMPLS},
   // TODO: Rename to "auto_traits" when supporting
   // later Rust versions
   {"optin_builtin_traits", Feature::Name::AUTO_TRAITS},
diff --git a/gcc/rust/checks/errors/rust-feature.h 
b/gcc/rust/checks/errors/rust-feature.h
index 8eefb0f9754..611dceaa2c9 100644
--- a/gcc/rust/checks/errors/rust-feature.h
+++ b/gcc/rust/checks/errors/rust-feature.h
@@ -39,6 +39,7 @@ public:
   {
 ASSOCIATED_TYPE_BOUNDS,
 INTRINSICS,
+NEGATIVE_IMPLS,
 RUSTC_ATTRS,
 DECL_MACRO,
 AUTO_TRAITS,
diff --git a/gcc/testsuite/rust/compile/negative_impls.rs 
b/gcc/testsuite/rust/compile/negative_impls.rs
new file mode 100644
index 000..949a3906cdc
--- /dev/null
+++ b/gcc/testsuite/rust/compile/negative_impls.rs
@@ -0,0 +1,8 @@
+#![feature(negative_impls)]
+
+trait ExampleTrait {}
+
+impl !ExampleTrait for i32 {}
+
+
+fn main() {}
diff --git a/gcc/testsuite/rust/compile/negative_impls_2.rs 
b/gcc/testsuite/rust/compile/negative_impls_2.rs
new file mode 100644
index 000..d66fbc9c36b
--- /dev/null
+++ b/gcc/testsuite/rust/compile/negative_impls_2.rs
@@ -0,0 +1,16 @@
+// This test case should error out since we haven't included the 
negative_impls feature
+// Output from  online rust compiler 2021 ver
+// Compiling playground v0.0.1 (/playground)
+// error[E0658]: negative trait bounds are not yet fully implemented; use 
marker types for now
+//  --> src/main.rs:8:6
+//   |
+// 8 | impl !ExampleTrait for i32 {}//
+//   |  ^
+//   |
+//   = note: see issue #68318  
for more information
+
+// For more information about this error, try `rustc --explain E0658`.
+// error: could not c

[COMMITTED 068/145] gccrs: Prevent raw reference from being lowered silently

2025-03-17 Thread arthur . cohen
From: Pierre-Emmanuel Patry 

We do not handle those kind of references yet, we shall not let them
pass as a regular reference.

gcc/rust/ChangeLog:

* ast/rust-expr.h: Add a getter for mutability.
* hir/rust-ast-lower-expr.cc (ASTLoweringExpr::visit): Panic when a
raw reference is met.

Signed-off-by: Pierre-Emmanuel Patry 
---
 gcc/rust/ast/rust-expr.h|  2 ++
 gcc/rust/hir/rust-ast-lower-expr.cc | 13 +++--
 2 files changed, 9 insertions(+), 6 deletions(-)

diff --git a/gcc/rust/ast/rust-expr.h b/gcc/rust/ast/rust-expr.h
index 6609ad80b37..a5afbffee99 100644
--- a/gcc/rust/ast/rust-expr.h
+++ b/gcc/rust/ast/rust-expr.h
@@ -398,6 +398,8 @@ public:
 
   bool get_is_mut () const { return mutability == Mutability::Mut; }
 
+  Mutability get_mutability () const { return mutability; }
+
   bool get_is_double_borrow () const { return double_borrow; }
   bool is_raw_borrow () const { return raw_borrow; }
 
diff --git a/gcc/rust/hir/rust-ast-lower-expr.cc 
b/gcc/rust/hir/rust-ast-lower-expr.cc
index 515d36a839f..a0eb5e32f25 100644
--- a/gcc/rust/hir/rust-ast-lower-expr.cc
+++ b/gcc/rust/hir/rust-ast-lower-expr.cc
@@ -628,6 +628,9 @@ ASTLoweringExpr::visit (AST::ContinueExpr &expr)
 void
 ASTLoweringExpr::visit (AST::BorrowExpr &expr)
 {
+  if (expr.is_raw_borrow ())
+rust_unreachable ();
+
   HIR::Expr *borrow_lvalue
 = ASTLoweringExpr::translate (expr.get_borrowed_expr ());
 
@@ -638,9 +641,8 @@ ASTLoweringExpr::visit (AST::BorrowExpr &expr)
 
   auto *borrow_expr
 = new HIR::BorrowExpr (mapping, std::unique_ptr (borrow_lvalue),
-  expr.get_is_mut () ? Mutability::Mut
- : Mutability::Imm,
-  expr.get_outer_attrs (), expr.get_locus ());
+  expr.get_mutability (), expr.get_outer_attrs (),
+  expr.get_locus ());
 
   if (expr.get_is_double_borrow ())
 {
@@ -652,9 +654,8 @@ ASTLoweringExpr::visit (AST::BorrowExpr &expr)
   borrow_expr
= new HIR::BorrowExpr (mapping,
   std::unique_ptr (borrow_expr),
-  expr.get_is_mut () ? Mutability::Mut
- : Mutability::Imm,
-  expr.get_outer_attrs (), expr.get_locus ());
+  expr.get_mutability (), expr.get_outer_attrs (),
+  expr.get_locus ());
 }
 
   translated = borrow_expr;
-- 
2.45.2



[COMMITTED 052/145] gccrs: Add call and method call default visitors

2025-03-17 Thread arthur . cohen
From: Owen Avery 

gcc/rust/ChangeLog:

* resolve/rust-default-resolver.cc
(DefaultResolver::visit):
New for AST::CallExpr and AST::MethodCallExpr.
* resolve/rust-default-resolver.h
(DefaultResolver::visit): Likewise.

Signed-off-by: Owen Avery 
---
 gcc/rust/resolve/rust-default-resolver.cc | 30 +++
 gcc/rust/resolve/rust-default-resolver.h  |  2 ++
 2 files changed, 32 insertions(+)

diff --git a/gcc/rust/resolve/rust-default-resolver.cc 
b/gcc/rust/resolve/rust-default-resolver.cc
index 50625bab956..757acfe8595 100644
--- a/gcc/rust/resolve/rust-default-resolver.cc
+++ b/gcc/rust/resolve/rust-default-resolver.cc
@@ -236,6 +236,36 @@ void
 DefaultResolver::visit (AST::ReturnExpr &expr)
 {}
 
+void
+DefaultResolver::visit (AST::CallExpr &expr)
+{
+  expr.get_function_expr ().accept_vis (*this);
+
+  for (auto ¶m : expr.get_params ())
+param->accept_vis (*this);
+}
+
+void
+DefaultResolver::visit (AST::MethodCallExpr &expr)
+{
+  expr.get_receiver_expr ().accept_vis (*this);
+
+  if (expr.get_method_name ().has_generic_args ())
+{
+  auto &args = expr.get_method_name ().get_generic_args ();
+  for (auto &arg : args.get_generic_args ())
+   arg.accept_vis (*this);
+  for (auto &arg : args.get_binding_args ())
+   if (!arg.is_error ())
+ arg.get_type ().accept_vis (*this);
+  for (auto &arg : args.get_lifetime_args ())
+   arg.accept_vis (*this);
+}
+
+  for (auto ¶m : expr.get_params ())
+param->accept_vis (*this);
+}
+
 void
 DefaultResolver::visit (AST::LoopExpr &expr)
 {}
diff --git a/gcc/rust/resolve/rust-default-resolver.h 
b/gcc/rust/resolve/rust-default-resolver.h
index 13ab5ee142c..6bca8b7b6a1 100644
--- a/gcc/rust/resolve/rust-default-resolver.h
+++ b/gcc/rust/resolve/rust-default-resolver.h
@@ -66,6 +66,8 @@ public:
   void visit (AST::RangeFromToInclExpr &);
   void visit (AST::RangeToInclExpr &);
   void visit (AST::ReturnExpr &);
+  void visit (AST::CallExpr &);
+  void visit (AST::MethodCallExpr &);
   void visit (AST::LoopExpr &);
   void visit (AST::WhileLoopExpr &);
   void visit (AST::WhileLetLoopExpr &);
-- 
2.45.2



[COMMITTED 048/145] gccrs: Change lookup_visibility's return type

2025-03-17 Thread arthur . cohen
From: Pierre-Emmanuel Patry 

Wrap the return type within an optional.

gcc/rust/ChangeLog:

* checks/errors/privacy/rust-privacy-reporter.cc: Change call site
to accomodate new return type.
* checks/errors/privacy/rust-pub-restricted-visitor.cc: Likewise.
* util/rust-hir-map.cc (Mappings::lookup_visibility): Change the
function's return type.
* util/rust-hir-map.h: Update the function's prototype.

Signed-off-by: Pierre-Emmanuel Patry 
---
 gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc  | 8 
 .../checks/errors/privacy/rust-pub-restricted-visitor.cc | 6 +++---
 gcc/rust/util/rust-hir-map.cc| 9 -
 gcc/rust/util/rust-hir-map.h | 2 +-
 4 files changed, 12 insertions(+), 13 deletions(-)

diff --git a/gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc 
b/gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc
index f8df9f795ef..d16d6ed454c 100644
--- a/gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc
+++ b/gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc
@@ -128,15 +128,15 @@ PrivacyReporter::check_for_privacy_violation (const 
NodeId &use_id,
   if (ref_node_id == UNKNOWN_NODEID)
 return;
 
-  ModuleVisibility vis;
+  auto vis = mappings.lookup_visibility (ref_node_id);
 
   // FIXME: Can we really return here if the item has no visibility?
-  if (!mappings.lookup_visibility (ref_node_id, vis))
+  if (!vis)
 return;
 
   auto valid = true;
 
-  switch (vis.get_kind ())
+  switch (vis->get_kind ())
 {
 case ModuleVisibility::Public:
   break;
@@ -146,7 +146,7 @@ PrivacyReporter::check_for_privacy_violation (const NodeId 
&use_id,
if (!current_module.has_value ())
  return;
 
-   auto module = mappings.lookup_defid (vis.get_module_id ()).value ();
+   auto module = mappings.lookup_defid (vis->get_module_id ()).value ();
 
auto mod_node_id = module->get_mappings ().get_nodeid ();
 
diff --git a/gcc/rust/checks/errors/privacy/rust-pub-restricted-visitor.cc 
b/gcc/rust/checks/errors/privacy/rust-pub-restricted-visitor.cc
index bd11f5003d1..7e6be1a9836 100644
--- a/gcc/rust/checks/errors/privacy/rust-pub-restricted-visitor.cc
+++ b/gcc/rust/checks/errors/privacy/rust-pub-restricted-visitor.cc
@@ -27,16 +27,16 @@ bool
 PubRestrictedVisitor::is_restriction_valid (NodeId item_id,
const location_t locus)
 {
-  ModuleVisibility visibility;
+  auto visibility = mappings.lookup_visibility (item_id);
 
   // If there is no visibility in the mappings, then the item is private and
   // does not contain any restriction
   // FIXME: Is that correct?
-  if (!mappings.lookup_visibility (item_id, visibility))
+  if (!visibility)
 return true;
 
   for (auto mod = module_stack.rbegin (); mod != module_stack.rend (); mod++)
-if (*mod == visibility.get_module_id ())
+if (*mod == visibility->get_module_id ())
   return true;
 
   rust_error_at (locus, "restricted path is not an ancestor of the "
diff --git a/gcc/rust/util/rust-hir-map.cc b/gcc/rust/util/rust-hir-map.cc
index 3b721198ecc..7418fa4fb0b 100644
--- a/gcc/rust/util/rust-hir-map.cc
+++ b/gcc/rust/util/rust-hir-map.cc
@@ -1102,15 +1102,14 @@ Mappings::insert_visibility (NodeId id, 
Privacy::ModuleVisibility visibility)
   visibility_map.insert ({id, visibility});
 }
 
-bool
-Mappings::lookup_visibility (NodeId id, Privacy::ModuleVisibility &def)
+tl::optional
+Mappings::lookup_visibility (NodeId id)
 {
   auto it = visibility_map.find (id);
   if (it == visibility_map.end ())
-return false;
+return tl::nullopt;
 
-  def = it->second;
-  return true;
+  return it->second;
 }
 
 void
diff --git a/gcc/rust/util/rust-hir-map.h b/gcc/rust/util/rust-hir-map.h
index d80a15ec00c..c70af5bb1bc 100644
--- a/gcc/rust/util/rust-hir-map.h
+++ b/gcc/rust/util/rust-hir-map.h
@@ -326,7 +326,7 @@ public:
   AttributeProcMacro def);
 
   void insert_visibility (NodeId id, Privacy::ModuleVisibility visibility);
-  bool lookup_visibility (NodeId id, Privacy::ModuleVisibility &def);
+  tl::optional lookup_visibility (NodeId id);
 
   void insert_ast_module (AST::Module *);
   tl::optional lookup_ast_module (NodeId id);
-- 
2.45.2



[COMMITTED 047/145] gccrs: Change lookup_macro_invocation's return type

2025-03-17 Thread arthur . cohen
From: Pierre-Emmanuel Patry 

Wrap the function's return type within an optional and remove the out
reference argument.

gcc/rust/ChangeLog:

* expand/rust-macro-expand.cc (MacroExpander::expand_invoc): Adapt
the function call to match its new prototype.
* resolve/rust-early-name-resolver-2.0.cc (Early::insert_once):
Likewise.
(Early::visit): Likewise.
* resolve/rust-early-name-resolver.cc (EarlyNameResolver::visit):
Likewise.
* util/rust-hir-map.cc (Mappings::lookup_macro_invocation): Change the
function's return type.
* util/rust-hir-map.h: Update the function's prototype.

Signed-off-by: Pierre-Emmanuel Patry 
---
 gcc/rust/expand/rust-macro-expand.cc   | 18 +-
 .../resolve/rust-early-name-resolver-2.0.cc|  8 ++--
 gcc/rust/resolve/rust-early-name-resolver.cc   |  3 +--
 gcc/rust/util/rust-hir-map.cc  | 10 --
 gcc/rust/util/rust-hir-map.h   |  4 ++--
 5 files changed, 18 insertions(+), 25 deletions(-)

diff --git a/gcc/rust/expand/rust-macro-expand.cc 
b/gcc/rust/expand/rust-macro-expand.cc
index 86c6695408b..e2ef4041eff 100644
--- a/gcc/rust/expand/rust-macro-expand.cc
+++ b/gcc/rust/expand/rust-macro-expand.cc
@@ -272,25 +272,25 @@ MacroExpander::expand_invoc (AST::MacroInvocation &invoc, 
bool has_semicolon)
   invoc_data.set_expander (this);
 
   // lookup the rules
-  AST::MacroRulesDefinition *rules_def = nullptr;
-  bool ok = mappings.lookup_macro_invocation (invoc, &rules_def);
+  auto rules_def = mappings.lookup_macro_invocation (invoc);
 
   // If there's no rule associated with the invocation, we can simply return
   // early. The early name resolver will have already emitted an error.
-  if (!ok)
+  if (!rules_def)
 return;
 
+  auto rdef = rules_def.value ();
+
   // We store the last expanded invocation and macro definition for error
   // reporting in case the recursion limit is reached
   last_invoc = *invoc.clone_macro_invocation_impl ();
-  last_def = *rules_def;
+  last_def = *rdef;
 
-  if (rules_def->is_builtin ())
-fragment
-  = rules_def->get_builtin_transcriber () (invoc.get_locus (), invoc_data)
- .value_or (AST::Fragment::create_empty ());
+  if (rdef->is_builtin ())
+fragment = rdef->get_builtin_transcriber () (invoc.get_locus (), 
invoc_data)
+.value_or (AST::Fragment::create_empty ());
   else
-fragment = expand_decl_macro (invoc.get_locus (), invoc_data, *rules_def,
+fragment = expand_decl_macro (invoc.get_locus (), invoc_data, *rdef,
  has_semicolon);
 
   set_expanded_fragment (std::move (fragment));
diff --git a/gcc/rust/resolve/rust-early-name-resolver-2.0.cc 
b/gcc/rust/resolve/rust-early-name-resolver-2.0.cc
index c0513931f85..1b21e115aee 100644
--- a/gcc/rust/resolve/rust-early-name-resolver-2.0.cc
+++ b/gcc/rust/resolve/rust-early-name-resolver-2.0.cc
@@ -32,10 +32,7 @@ Early::insert_once (AST::MacroInvocation &invocation, NodeId 
resolved)
   // TODO: Should we use `ctx.mark_resolved()`?
   auto definition = ctx.mappings.lookup_macro_def (resolved);
 
-  AST::MacroRulesDefinition *existing;
-  auto exists = ctx.mappings.lookup_macro_invocation (invocation, &existing);
-
-  if (!exists)
+  if (!ctx.mappings.lookup_macro_invocation (invocation))
 ctx.mappings.insert_macro_invocation (invocation, definition.value ());
 }
 
@@ -176,8 +173,7 @@ Early::visit (AST::MacroInvocation &invoc)
   if (!rules_def)
 return;
 
-  AST::MacroRulesDefinition *tmp_def = nullptr;
-  if (mappings.lookup_macro_invocation (invoc, &tmp_def))
+  if (mappings.lookup_macro_invocation (invoc))
 return;
 
   mappings.insert_macro_invocation (invoc, rules_def.value ());
diff --git a/gcc/rust/resolve/rust-early-name-resolver.cc 
b/gcc/rust/resolve/rust-early-name-resolver.cc
index 604b05da2b8..ce427ddb2c8 100644
--- a/gcc/rust/resolve/rust-early-name-resolver.cc
+++ b/gcc/rust/resolve/rust-early-name-resolver.cc
@@ -503,8 +503,7 @@ EarlyNameResolver::visit (AST::MacroInvocation &invoc)
* we could be inserting the same macro def over and over again until we
* implement some optimizations */
   // FIXME: ARTHUR: Remove that lookup and add proper optimizations instead
-  AST::MacroRulesDefinition *tmp_def = nullptr;
-  if (mappings.lookup_macro_invocation (invoc, &tmp_def))
+  if (mappings.lookup_macro_invocation (invoc))
 return;
 
   mappings.insert_macro_invocation (invoc, *rules_def);
diff --git a/gcc/rust/util/rust-hir-map.cc b/gcc/rust/util/rust-hir-map.cc
index f6df55fdc9f..3b721198ecc 100644
--- a/gcc/rust/util/rust-hir-map.cc
+++ b/gcc/rust/util/rust-hir-map.cc
@@ -897,16 +897,14 @@ Mappings::insert_macro_invocation (AST::MacroInvocation 
&invoc,
   macroInvocations[invoc.get_macro_node_id ()] = def;
 }
 
-bool
-Mappings::lookup_macro_invocation (AST::MacroInvocation &invoc,
-  AST::MacroRulesDefinition **de

[COMMITTED 049/145] gccrs: Change lookup_ast_item's return type

2025-03-17 Thread arthur . cohen
From: Pierre-Emmanuel Patry 

Wrap the function's return type within an optional.

gcc/rust/ChangeLog:

* metadata/rust-export-metadata.cc (ExportContext::emit_trait):
Adapt call site to the new return type.
(ExportContext::emit_function): Likewise.
(ExportContext::emit_macro): Likewise.
* util/rust-hir-map.cc (Mappings::lookup_ast_item): Change the
function's return type.
* util/rust-hir-map.h: Update the function's prototype.

Signed-off-by: Pierre-Emmanuel Patry 
---
 gcc/rust/metadata/rust-export-metadata.cc | 15 +--
 gcc/rust/util/rust-hir-map.cc |  9 -
 gcc/rust/util/rust-hir-map.h  |  2 +-
 3 files changed, 10 insertions(+), 16 deletions(-)

diff --git a/gcc/rust/metadata/rust-export-metadata.cc 
b/gcc/rust/metadata/rust-export-metadata.cc
index 46afe356425..79c5f30d755 100644
--- a/gcc/rust/metadata/rust-export-metadata.cc
+++ b/gcc/rust/metadata/rust-export-metadata.cc
@@ -56,10 +56,8 @@ void
 ExportContext::emit_trait (const HIR::Trait &trait)
 {
   // lookup the AST node for this
-  AST::Item *item = nullptr;
-  bool ok
-= mappings.lookup_ast_item (trait.get_mappings ().get_nodeid (), &item);
-  rust_assert (ok);
+  AST::Item *item
+= mappings.lookup_ast_item (trait.get_mappings ().get_nodeid ()).value ();
 
   std::stringstream oss;
   AST::Dump dumper (oss);
@@ -72,9 +70,8 @@ void
 ExportContext::emit_function (const HIR::Function &fn)
 {
   // lookup the AST node for this
-  AST::Item *item = nullptr;
-  bool ok = mappings.lookup_ast_item (fn.get_mappings ().get_nodeid (), &item);
-  rust_assert (ok);
+  AST::Item *item
+= mappings.lookup_ast_item (fn.get_mappings ().get_nodeid ()).value ();
 
   // is this a CFG macro or not
   if (item->is_marked_for_strip ())
@@ -119,9 +116,7 @@ ExportContext::emit_macro (NodeId macro)
   std::stringstream oss;
   AST::Dump dumper (oss);
 
-  AST::Item *item;
-  auto ok = mappings.lookup_ast_item (macro, &item);
-  rust_assert (ok);
+  AST::Item *item = mappings.lookup_ast_item (macro).value ();
 
   dumper.go (*item);
 
diff --git a/gcc/rust/util/rust-hir-map.cc b/gcc/rust/util/rust-hir-map.cc
index 7418fa4fb0b..03b3343eaa1 100644
--- a/gcc/rust/util/rust-hir-map.cc
+++ b/gcc/rust/util/rust-hir-map.cc
@@ -1225,15 +1225,14 @@ Mappings::insert_ast_item (AST::Item *item)
   ast_item_mappings[item->get_node_id ()] = item;
 }
 
-bool
-Mappings::lookup_ast_item (NodeId id, AST::Item **result)
+tl::optional
+Mappings::lookup_ast_item (NodeId id)
 {
   auto it = ast_item_mappings.find (id);
   if (it == ast_item_mappings.end ())
-return false;
+return tl::nullopt;
 
-  *result = it->second;
-  return true;
+  return it->second;
 }
 
 HIR::ImplBlock *
diff --git a/gcc/rust/util/rust-hir-map.h b/gcc/rust/util/rust-hir-map.h
index c70af5bb1bc..bb7318e32ec 100644
--- a/gcc/rust/util/rust-hir-map.h
+++ b/gcc/rust/util/rust-hir-map.h
@@ -345,7 +345,7 @@ public:
   bool node_is_module (NodeId query);
 
   void insert_ast_item (AST::Item *item);
-  bool lookup_ast_item (NodeId id, AST::Item **result);
+  tl::optional lookup_ast_item (NodeId id);
 
   HIR::ImplBlock *lookup_builtin_marker ();
 
-- 
2.45.2



[COMMITTED 056/145] gccrs: Allow rustc_const_stable and rustc_const_unstable

2025-03-17 Thread arthur . cohen
From: Owen Avery 

gcc/rust/ChangeLog:

* expand/rust-macro-builtins.cc
(MacroBuiltin::builtin_transcribers):
Add entries for "rustc_const_stable" and "rustc_const_unstable".
* util/rust-attributes.cc
(__definitions): Add entries for RUSTC_CONST_STABLE and
RUSTC_CONST_UNSTABLE.
* util/rust-attribute-values.h
(Attributes::RUSTC_CONST_STABLE): New.
(Attributes::RUSTC_CONST_UNSTABLE): New.

gcc/testsuite/ChangeLog:

* rust/compile/rustc_const_stable.rs:
Enable feature rustc_attrs, expect no errors.
* rust/compile/rustc_const_unstable.rs: New test.

Signed-off-by: Owen Avery 
---
 gcc/rust/expand/rust-macro-builtins.cc | 2 ++
 gcc/rust/util/rust-attribute-values.h  | 2 ++
 gcc/rust/util/rust-attributes.cc   | 5 -
 gcc/testsuite/rust/compile/rustc_const_stable.rs   | 4 +++-
 gcc/testsuite/rust/compile/rustc_const_unstable.rs | 4 
 5 files changed, 15 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/rust/compile/rustc_const_unstable.rs

diff --git a/gcc/rust/expand/rust-macro-builtins.cc 
b/gcc/rust/expand/rust-macro-builtins.cc
index ed6c0e9db97..61b677ac075 100644
--- a/gcc/rust/expand/rust-macro-builtins.cc
+++ b/gcc/rust/expand/rust-macro-builtins.cc
@@ -123,6 +123,8 @@ std::unordered_map
 {"test_case", MacroBuiltin::sorry},
 {"global_allocator", MacroBuiltin::sorry},
 {"cfg_accessible", MacroBuiltin::sorry},
+{"rustc_const_stable", MacroBuiltin::sorry},
+{"rustc_const_unstable", MacroBuiltin::sorry},
 /* Derive builtins do not need a real transcriber, but still need one. It
should however never be called since builtin derive macros get expanded
differently, and benefit from knowing on what kind of items they are
diff --git a/gcc/rust/util/rust-attribute-values.h 
b/gcc/rust/util/rust-attribute-values.h
index e51ec076670..fec73b17c3e 100644
--- a/gcc/rust/util/rust-attribute-values.h
+++ b/gcc/rust/util/rust-attribute-values.h
@@ -53,6 +53,8 @@ public:
 = "rustc_inherit_overflow_checks";
   static constexpr auto &STABLE = "stable";
   static constexpr auto &UNSTABLE = "unstable";
+  static constexpr auto &RUSTC_CONST_STABLE = "rustc_const_stable";
+  static constexpr auto &RUSTC_CONST_UNSTABLE = "rustc_const_unstable";
 };
 } // namespace Values
 } // namespace Rust
diff --git a/gcc/rust/util/rust-attributes.cc b/gcc/rust/util/rust-attributes.cc
index aef51b7f4b1..c9e376400fd 100644
--- a/gcc/rust/util/rust-attributes.cc
+++ b/gcc/rust/util/rust-attributes.cc
@@ -61,7 +61,10 @@ static const BuiltinAttrDefinition __definitions[]
  {Attrs::RUSTC_DEPRECATED, STATIC_ANALYSIS},
  {Attrs::RUSTC_INHERIT_OVERFLOW_CHECKS, CODE_GENERATION},
  {Attrs::STABLE, STATIC_ANALYSIS},
- {Attrs::UNSTABLE, STATIC_ANALYSIS}};
+ {Attrs::UNSTABLE, STATIC_ANALYSIS},
+ // assuming we keep these for static analysis
+ {Attrs::RUSTC_CONST_STABLE, STATIC_ANALYSIS},
+ {Attrs::RUSTC_CONST_UNSTABLE, STATIC_ANALYSIS}};
 
 BuiltinAttributeMappings *
 BuiltinAttributeMappings::get ()
diff --git a/gcc/testsuite/rust/compile/rustc_const_stable.rs 
b/gcc/testsuite/rust/compile/rustc_const_stable.rs
index 9208b1ab3b6..a45355de6b3 100644
--- a/gcc/testsuite/rust/compile/rustc_const_stable.rs
+++ b/gcc/testsuite/rust/compile/rustc_const_stable.rs
@@ -1,2 +1,4 @@
+#![feature(rustc_attrs)]
+
 #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = 
"1.47.0")]
-pub fn foo() {} // { dg-error "macro not found" "" { target *-*-* } .-1 }
+pub fn foo() {}
diff --git a/gcc/testsuite/rust/compile/rustc_const_unstable.rs 
b/gcc/testsuite/rust/compile/rustc_const_unstable.rs
new file mode 100644
index 000..6cedefade72
--- /dev/null
+++ b/gcc/testsuite/rust/compile/rustc_const_unstable.rs
@@ -0,0 +1,4 @@
+#![feature(rustc_attrs)]
+
+#[rustc_const_unstable(feature = "const_ascii_ctype_on_intrinsics", issue = 
"1234")]
+pub fn foo() {}
-- 
2.45.2



[COMMITTED 059/145] gccrs: Add feature gate for box syntax

2025-03-17 Thread arthur . cohen
From: Pierre-Emmanuel Patry 

The box syntax is experimental even though it is used in the standard
library. It should be feature gated to prevent anyone from using it in
stable rust.

gcc/rust/ChangeLog:

* checks/errors/rust-feature-gate.cc (FeatureGate::visit): Allow
visitor recursion in functions. Also add the gate for the box syntax.
* checks/errors/rust-feature-gate.h: Remove several recursion fences
in the feature gate visitor.
* checks/errors/rust-feature.cc (Feature::create): Add a new feature.
(Feature::as_name): Likewise.
* checks/errors/rust-feature.h: Add box_syntax gate.

Signed-off-by: Pierre-Emmanuel Patry 
---
 gcc/rust/checks/errors/rust-feature-gate.cc | 11 +++
 gcc/rust/checks/errors/rust-feature-gate.h  |  3 +--
 gcc/rust/checks/errors/rust-feature.cc  |  6 +-
 gcc/rust/checks/errors/rust-feature.h   |  3 ++-
 4 files changed, 19 insertions(+), 4 deletions(-)

diff --git a/gcc/rust/checks/errors/rust-feature-gate.cc 
b/gcc/rust/checks/errors/rust-feature-gate.cc
index 6ef2d1f710f..69348cb90e8 100644
--- a/gcc/rust/checks/errors/rust-feature-gate.cc
+++ b/gcc/rust/checks/errors/rust-feature-gate.cc
@@ -134,6 +134,8 @@ FeatureGate::visit (AST::Function &function)
 {
   if (!function.is_external ())
 check_rustc_attri (function.get_outer_attrs ());
+
+  AST::DefaultASTVisitor::visit (function);
 }
 
 void
@@ -153,4 +155,13 @@ FeatureGate::visit (AST::TraitImpl &impl)
  "negative_impls are not yet implemented");
 };
 
+void
+FeatureGate::visit (AST::BoxExpr &expr)
+{
+  gate (
+Feature::Name::BOX_SYNTAX, expr.get_locus (),
+"box expression syntax is experimental; you can call `Box::new` instead");
+  AST::DefaultASTVisitor::visit (expr);
+}
+
 } // namespace Rust
diff --git a/gcc/rust/checks/errors/rust-feature-gate.h 
b/gcc/rust/checks/errors/rust-feature-gate.h
index de48f225598..bef7cf1b449 100644
--- a/gcc/rust/checks/errors/rust-feature-gate.h
+++ b/gcc/rust/checks/errors/rust-feature-gate.h
@@ -81,7 +81,6 @@ public:
   void visit (AST::MethodCallExpr &expr) override {}
   void visit (AST::FieldAccessExpr &expr) override {}
   void visit (AST::ClosureExprInner &expr) override {}
-  void visit (AST::BlockExpr &expr) override {}
   void visit (AST::ClosureExprInnerTyped &expr) override {}
   void visit (AST::ContinueExpr &expr) override {}
   void visit (AST::BreakExpr &expr) override {}
@@ -92,6 +91,7 @@ public:
   void visit (AST::RangeFromToInclExpr &expr) override {}
   void visit (AST::RangeToInclExpr &expr) override {}
   void visit (AST::ReturnExpr &expr) override {}
+  void visit (AST::BoxExpr &expr) override;
   void visit (AST::UnsafeBlockExpr &expr) override {}
   void visit (AST::LoopExpr &expr) override {}
   void visit (AST::WhileLoopExpr &expr) override {}
@@ -166,7 +166,6 @@ public:
   void visit (AST::SlicePattern &pattern) override {}
   void visit (AST::AltPattern &pattern) override {}
   void visit (AST::EmptyStmt &stmt) override {}
-  void visit (AST::LetStmt &stmt) override {}
   void visit (AST::ExprStmt &stmt) override {}
   void visit (AST::TraitBound &bound) override {}
   void visit (AST::ImplTraitType &type) override {}
diff --git a/gcc/rust/checks/errors/rust-feature.cc 
b/gcc/rust/checks/errors/rust-feature.cc
index 1e11bd541a5..f993bbb7245 100644
--- a/gcc/rust/checks/errors/rust-feature.cc
+++ b/gcc/rust/checks/errors/rust-feature.cc
@@ -45,6 +45,9 @@ Feature::create (Feature::Name name)
 case Feature::Name::NEGATIVE_IMPLS:
   return Feature (Feature::Name::NEGATIVE_IMPLS, Feature::State::ACTIVE,
  "negative_impls", "1.0.0", 68318, tl::nullopt, "");
+case Feature::Name::BOX_SYNTAX:
+  return Feature (Feature::Name::BOX_SYNTAX, Feature::State::ACTIVE,
+ "box_syntax", "1.0.0", 49733, tl::nullopt, "");
 default:
   rust_unreachable ();
 }
@@ -62,6 +65,7 @@ const std::map 
Feature::name_hash_map = {
   {"extern_types", Feature::Name::EXTERN_TYPES},
   {"lang_items", Feature::Name::LANG_ITEMS},
   {"no_core", Feature::Name::NO_CORE},
+  {"box_syntax", Feature::Name::BOX_SYNTAX},
 }; // namespace Rust
 
 tl::optional
@@ -73,4 +77,4 @@ Feature::as_name (const std::string &name)
   return tl::nullopt;
 }
 
-} // namespace Rust
\ No newline at end of file
+} // namespace Rust
diff --git a/gcc/rust/checks/errors/rust-feature.h 
b/gcc/rust/checks/errors/rust-feature.h
index 611dceaa2c9..736d97e1cde 100644
--- a/gcc/rust/checks/errors/rust-feature.h
+++ b/gcc/rust/checks/errors/rust-feature.h
@@ -46,6 +46,7 @@ public:
 EXTERN_TYPES,
 LANG_ITEMS,
 NO_CORE,
+BOX_SYNTAX,
   };
 
   const std::string &as_string () { return m_name_str; }
@@ -79,4 +80,4 @@ private:
 };
 
 } // namespace Rust
-#endif
\ No newline at end of file
+#endif
-- 
2.45.2



[COMMITTED 057/145] gccrs: Improve handling of ConstantItem during name resolution 2.0

2025-03-17 Thread arthur . cohen
From: Owen Avery 

gcc/rust/ChangeLog:

* resolve/rust-default-resolver.cc
(DefaultResolver::visit):
Scope with Rib::Kind::ConstantItem instead
of Rib::Kind::Item.
* resolve/rust-toplevel-name-resolver-2.0.cc
(TopLevel::visit): Remove redundancy in override of
ConstantItem visitor.

Signed-off-by: Owen Avery 
---
 gcc/rust/resolve/rust-default-resolver.cc   | 2 +-
 gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc | 5 +
 2 files changed, 2 insertions(+), 5 deletions(-)

diff --git a/gcc/rust/resolve/rust-default-resolver.cc 
b/gcc/rust/resolve/rust-default-resolver.cc
index 757acfe8595..6de694f48f4 100644
--- a/gcc/rust/resolve/rust-default-resolver.cc
+++ b/gcc/rust/resolve/rust-default-resolver.cc
@@ -487,7 +487,7 @@ DefaultResolver::visit (AST::ConstantItem &item)
   };
 
   // FIXME: Why do we need a Rib here?
-  ctx.scoped (Rib::Kind::Item, item.get_node_id (), expr_vis);
+  ctx.scoped (Rib::Kind::ConstantItem, item.get_node_id (), expr_vis);
 }
 }
 
diff --git a/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc 
b/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc
index 926ed52036c..746b2240d4b 100644
--- a/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc
+++ b/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc
@@ -424,10 +424,7 @@ TopLevel::visit (AST::ConstantItem &const_item)
   insert_or_error_out (const_item.get_identifier (), const_item,
   Namespace::Values);
 
-  auto expr_vis
-= [this, &const_item] () { const_item.get_expr ().accept_vis (*this); };
-
-  ctx.scoped (Rib::Kind::ConstantItem, const_item.get_node_id (), expr_vis);
+  DefaultResolver::visit (const_item);
 }
 
 bool
-- 
2.45.2



[COMMITTED 062/145] gccrs: Allow multiple outer attributes on generic params

2025-03-17 Thread arthur . cohen
From: Pierre-Emmanuel Patry 

Previously generic params only allowed one outer attribute in front of
them.

gcc/rust/ChangeLog:

* ast/rust-ast-collector.cc (TokenCollector::visit): Visit outer
attributes.
* ast/rust-ast-visitor.cc (DefaultASTVisitor::visit): Change outer
attribute visit, we need to visit all of them.
* ast/rust-ast.cc (LifetimeParam::as_string): Change as_string
implementation to allow multiple outer attributes.
(TypeParam::as_string): Likewise.
* ast/rust-ast.h (class LifetimeParam): Allow multiple outer
attributes.
* ast/rust-item.h (class TypeParam): Likewise.
* ast/rust-path.h: Likewise.
* parse/rust-parse-impl.h (Parser::parse_generic_param): Change call
to outer attribute parsing to collect several attributes.
(Parser::parse_lifetime_param): Likewise.
(Parser::parse_type_param): Likewise.

Signed-off-by: Pierre-Emmanuel Patry 
---
 gcc/rust/ast/rust-ast-collector.cc |  3 +++
 gcc/rust/ast/rust-ast-visitor.cc   |  6 +++---
 gcc/rust/ast/rust-ast.cc   | 12 ++--
 gcc/rust/ast/rust-ast.h| 13 ++---
 gcc/rust/ast/rust-item.h   | 14 +++---
 gcc/rust/ast/rust-path.h   | 10 +-
 gcc/rust/parse/rust-parse-impl.h   | 14 +++---
 7 files changed, 37 insertions(+), 35 deletions(-)

diff --git a/gcc/rust/ast/rust-ast-collector.cc 
b/gcc/rust/ast/rust-ast-collector.cc
index a7f6ed57623..a75722587f5 100644
--- a/gcc/rust/ast/rust-ast-collector.cc
+++ b/gcc/rust/ast/rust-ast-collector.cc
@@ -476,6 +476,7 @@ TokenCollector::visit (LifetimeParam &lifetime_param)
 
   // TODO what to do with outer attr? They are not mentioned in the reference.
 
+  visit_items_as_lines (lifetime_param.get_outer_attrs ());
   auto lifetime = lifetime_param.get_lifetime ();
   visit (lifetime);
 
@@ -495,6 +496,7 @@ TokenCollector::visit (ConstGenericParam ¶m)
   // Syntax:
   // const IDENTIFIER : Type ( = Block | IDENTIFIER | -?LITERAL )?
 
+  visit_items_as_lines (param.get_outer_attrs ());
   push (Rust::Token::make (CONST, param.get_locus ()));
   auto id = param.get_name ().as_string ();
   push (Rust::Token::make_identifier (UNDEF_LOCATION, std::move (id)));
@@ -1509,6 +1511,7 @@ TokenCollector::visit (TypeParam ¶m)
   // TypeParamBounds :
   //TypeParamBound ( + TypeParamBound )* +?
 
+  visit_items_as_lines (param.get_outer_attrs ());
   auto id = param.get_type_representation ().as_string ();
   push (Rust::Token::make_identifier (param.get_locus (), std::move (id)));
   if (param.has_type_param_bounds ())
diff --git a/gcc/rust/ast/rust-ast-visitor.cc b/gcc/rust/ast/rust-ast-visitor.cc
index ba3eb67cbe7..2c1674e21ea 100644
--- a/gcc/rust/ast/rust-ast-visitor.cc
+++ b/gcc/rust/ast/rust-ast-visitor.cc
@@ -69,7 +69,7 @@ DefaultASTVisitor::visit (AST::Lifetime &lifetime)
 void
 DefaultASTVisitor::visit (AST::LifetimeParam &lifetime_param)
 {
-  visit (lifetime_param.get_outer_attribute ());
+  visit_outer_attrs (lifetime_param);
   visit (lifetime_param.get_lifetime ());
   for (auto &lifetime_bound : lifetime_param.get_lifetime_bounds ())
 visit (lifetime_bound);
@@ -78,7 +78,7 @@ DefaultASTVisitor::visit (AST::LifetimeParam &lifetime_param)
 void
 DefaultASTVisitor::visit (AST::ConstGenericParam &const_param)
 {
-  visit (const_param.get_outer_attribute ());
+  visit_outer_attrs (const_param);
   if (const_param.has_type ())
 visit (const_param.get_type ());
   if (const_param.has_default_value ())
@@ -665,7 +665,7 @@ DefaultASTVisitor::visit (AST::AsyncBlockExpr &expr)
 void
 DefaultASTVisitor::visit (AST::TypeParam ¶m)
 {
-  visit (param.get_outer_attribute ());
+  visit_outer_attrs (param);
   for (auto &bound : param.get_type_param_bounds ())
 visit (bound);
   if (param.has_type ())
diff --git a/gcc/rust/ast/rust-ast.cc b/gcc/rust/ast/rust-ast.cc
index bc896928ce6..8045a688510 100644
--- a/gcc/rust/ast/rust-ast.cc
+++ b/gcc/rust/ast/rust-ast.cc
@@ -2399,11 +2399,11 @@ LifetimeParam::as_string () const
 {
   std::string str ("LifetimeParam: ");
 
-  str += "\n Outer attribute: ";
+  str += "\n Outer attribute:";
   if (!has_outer_attribute ())
 str += "none";
-  else
-str += outer_attr.as_string ();
+  for (auto &attr : outer_attrs)
+str += " " + attr.as_string ();
 
   str += "\n Lifetime: " + lifetime.as_string ();
 
@@ -2495,11 +2495,11 @@ TypeParam::as_string () const
 {
   std::string str ("TypeParam: ");
 
-  str += "\n Outer attribute: ";
+  str += "\n Outer attribute:";
   if (!has_outer_attribute ())
 str += "none";
-  else
-str += outer_attr.as_string ();
+  for (auto &attr : outer_attrs)
+str += " " + attr.as_string ();
 
   str += "\n Identifier: " + type_representation.as_string ();
 
diff --git a/gcc/rust/ast/rust-ast.h b/gcc/rust/ast/rust-ast.h
index 0693b10bd2e..ee1c58c8fd6 100644
--- a/gcc/rust/ast/rust-ast.h
+++ b/gcc/rust/ast/rust-ast.h
@@ -1602,7 +1602,7 @@ cl

[COMMITTED 063/145] gccrs: Add dropck_eyepatch feature gate for may_dangle

2025-03-17 Thread arthur . cohen
From: Pierre-Emmanuel Patry 

Add a new feature gate for may_dangle generic param outer attributes.

gcc/rust/ChangeLog:

* checks/errors/rust-feature-gate.cc: Visit and gate may_dangle
attributes.
* checks/errors/rust-feature-gate.h: Update visit function prototype
and add a new member function to check on a set of attributes whether
one is may_dangle.
* checks/errors/rust-feature.cc (Feature::create): Add new
dropck_eyepatch feature.
* checks/errors/rust-feature.h: Likewise.
* util/rust-attribute-values.h: Add new may_dangle attribute value.

Signed-off-by: Pierre-Emmanuel Patry 
---
 gcc/rust/checks/errors/rust-feature-gate.cc | 37 +
 gcc/rust/checks/errors/rust-feature-gate.h  |  8 +++--
 gcc/rust/checks/errors/rust-feature.cc  |  4 +++
 gcc/rust/checks/errors/rust-feature.h   |  1 +
 gcc/rust/util/rust-attribute-values.h   |  1 +
 5 files changed, 48 insertions(+), 3 deletions(-)

diff --git a/gcc/rust/checks/errors/rust-feature-gate.cc 
b/gcc/rust/checks/errors/rust-feature-gate.cc
index 69348cb90e8..16c5ecee6cb 100644
--- a/gcc/rust/checks/errors/rust-feature-gate.cc
+++ b/gcc/rust/checks/errors/rust-feature-gate.cc
@@ -18,6 +18,7 @@
 
 #include "rust-feature-gate.h"
 #include "rust-abi.h"
+#include "rust-attribute-values.h"
 #include "rust-ast-visitor.h"
 #include "rust-feature.h"
 
@@ -123,6 +124,19 @@ FeatureGate::check_rustc_attri (const 
std::vector &attributes)
 }
 }
 
+void
+FeatureGate::check_may_dangle_attribute (
+  const std::vector &attributes)
+{
+  for (const AST::Attribute &attr : attributes)
+{
+  if (attr.get_path ().as_string () == Values::Attributes::MAY_DANGLE)
+   gate (Feature::Name::DROPCK_EYEPATCH, attr.get_locus (),
+ "`may_dangle` has unstable semantics and may be removed in the "
+ "future");
+}
+}
+
 void
 FeatureGate::visit (AST::MacroRulesDefinition &rules_def)
 {
@@ -153,6 +167,8 @@ FeatureGate::visit (AST::TraitImpl &impl)
   if (impl.is_exclam ())
 gate (Feature::Name::NEGATIVE_IMPLS, impl.get_locus (),
  "negative_impls are not yet implemented");
+
+  AST::DefaultASTVisitor::visit (impl);
 };
 
 void
@@ -164,4 +180,25 @@ FeatureGate::visit (AST::BoxExpr &expr)
   AST::DefaultASTVisitor::visit (expr);
 }
 
+void
+FeatureGate::visit (AST::LifetimeParam &lifetime_param)
+{
+  check_may_dangle_attribute (lifetime_param.get_outer_attrs ());
+  AST::DefaultASTVisitor::visit (lifetime_param);
+}
+
+void
+FeatureGate::visit (AST::ConstGenericParam &const_param)
+{
+  check_may_dangle_attribute (const_param.get_outer_attrs ());
+  AST::DefaultASTVisitor::visit (const_param);
+}
+
+void
+FeatureGate::visit (AST::TypeParam ¶m)
+{
+  check_may_dangle_attribute (param.get_outer_attrs ());
+  AST::DefaultASTVisitor::visit (param);
+}
+
 } // namespace Rust
diff --git a/gcc/rust/checks/errors/rust-feature-gate.h 
b/gcc/rust/checks/errors/rust-feature-gate.h
index bef7cf1b449..5ead1103086 100644
--- a/gcc/rust/checks/errors/rust-feature-gate.h
+++ b/gcc/rust/checks/errors/rust-feature-gate.h
@@ -40,8 +40,8 @@ public:
   void visit (AST::AttrInputMetaItemContainer &input) override {}
   void visit (AST::IdentifierExpr &ident_expr) override {}
   void visit (AST::Lifetime &lifetime) override {}
-  void visit (AST::LifetimeParam &lifetime_param) override {}
-  void visit (AST::ConstGenericParam &const_param) override {}
+  void visit (AST::LifetimeParam &lifetime_param) override;
+  void visit (AST::ConstGenericParam &const_param) override;
   void visit (AST::PathInExpression &path) override {}
   void visit (AST::TypePathSegment &segment) override {}
   void visit (AST::TypePathSegmentGeneric &segment) override {}
@@ -104,7 +104,7 @@ public:
   void visit (AST::MatchExpr &expr) override {}
   void visit (AST::AwaitExpr &expr) override {}
   void visit (AST::AsyncBlockExpr &expr) override {}
-  void visit (AST::TypeParam ¶m) override {}
+  void visit (AST::TypeParam ¶m) override;
   void visit (AST::LifetimeWhereClauseItem &item) override {}
   void visit (AST::TypeBoundWhereClauseItem &item) override {}
   void visit (AST::Module &module) override {}
@@ -188,6 +188,8 @@ public:
 private:
   void gate (Feature::Name name, location_t loc, const std::string &error_msg);
   void check_rustc_attri (const std::vector &attributes);
+  void
+  check_may_dangle_attribute (const std::vector &attributes);
   std::set valid_features;
 };
 } // namespace Rust
diff --git a/gcc/rust/checks/errors/rust-feature.cc 
b/gcc/rust/checks/errors/rust-feature.cc
index f993bbb7245..b9a648e62ef 100644
--- a/gcc/rust/checks/errors/rust-feature.cc
+++ b/gcc/rust/checks/errors/rust-feature.cc
@@ -48,6 +48,9 @@ Feature::create (Feature::Name name)
 case Feature::Name::BOX_SYNTAX:
   return Feature (Feature::Name::BOX_SYNTAX, Feature::State::ACTIVE,
  "box_syntax", "1.0.0", 49733, tl::nullopt, "");
+case Feature::Name::DROPC

[COMMITTED 061/145] gccrs: Fix generic parameter parsing

2025-03-17 Thread arthur . cohen
From: Pierre-Emmanuel Patry 

Generic parameter parsing failed when an outer attribute was used on it.

gcc/rust/ChangeLog:

* parse/rust-parse-impl.h (Parser::parse_generic_param): Change token
reference to be the last token after all outer attributes have been
parsed.

Signed-off-by: Pierre-Emmanuel Patry 
---
 gcc/rust/parse/rust-parse-impl.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/gcc/rust/parse/rust-parse-impl.h b/gcc/rust/parse/rust-parse-impl.h
index d1f5b6bbf0a..2c6b3d80cb2 100644
--- a/gcc/rust/parse/rust-parse-impl.h
+++ b/gcc/rust/parse/rust-parse-impl.h
@@ -3093,9 +3093,9 @@ template 
 std::unique_ptr
 Parser::parse_generic_param (EndTokenPred is_end_token)
 {
-  auto token = lexer.peek_token ();
   auto outer_attrs = parse_outer_attribute ();
   std::unique_ptr param;
+  auto token = lexer.peek_token ();
 
   switch (token->get_id ())
 {
-- 
2.45.2



[COMMITTED 064/145] gccrs: Add two new tests related to may_dangle attribute

2025-03-17 Thread arthur . cohen
From: Pierre-Emmanuel Patry 

First test checks the may_dangle outer atttribute on generic params can
be parsed. The second one tests whether may_dangle attributes are
correctly feature gated.

gcc/testsuite/ChangeLog:

* rust/compile/dropck_eyepatch_feature_gate.rs: New test.
* rust/compile/may_dangle.rs: New test.

Signed-off-by: Pierre-Emmanuel Patry 
---
 .../rust/compile/dropck_eyepatch_feature_gate.rs   |  8 
 gcc/testsuite/rust/compile/may_dangle.rs   | 10 ++
 2 files changed, 18 insertions(+)
 create mode 100644 gcc/testsuite/rust/compile/dropck_eyepatch_feature_gate.rs
 create mode 100644 gcc/testsuite/rust/compile/may_dangle.rs

diff --git a/gcc/testsuite/rust/compile/dropck_eyepatch_feature_gate.rs 
b/gcc/testsuite/rust/compile/dropck_eyepatch_feature_gate.rs
new file mode 100644
index 000..3c3174f93bb
--- /dev/null
+++ b/gcc/testsuite/rust/compile/dropck_eyepatch_feature_gate.rs
@@ -0,0 +1,8 @@
+// { dg-options "-frust-compile-until=lowering" }
+struct Test {
+_inner: T,
+}
+
+trait Action {}
+
+unsafe impl<#[may_dangle] T> Action for Test {} //{ dg-error ".may_dangle. 
has unstable semantics and may be removed in the future." "" { target *-*-* }  }
diff --git a/gcc/testsuite/rust/compile/may_dangle.rs 
b/gcc/testsuite/rust/compile/may_dangle.rs
new file mode 100644
index 000..6b81b53aa59
--- /dev/null
+++ b/gcc/testsuite/rust/compile/may_dangle.rs
@@ -0,0 +1,10 @@
+// { dg-options "-fsyntax-only" }
+
+#![feature(dropck_eyepatch)]
+struct Test {
+_inner: T,
+}
+
+trait Action {}
+
+unsafe impl<#[may_dangle] T> Action for Test {}
-- 
2.45.2



[COMMITTED 072/145] gccrs: Add exclusive_range_pattern feature gate

2025-03-17 Thread arthur . cohen
From: Pierre-Emmanuel Patry 

This syntax is experimental and shall be explicitely enabled in the crate
attributes as it cannot be used in stable rust.

gcc/rust/ChangeLog:

* checks/errors/rust-feature-gate.cc (FeatureGate::visit): Gate the
excluded pattern.
* checks/errors/rust-feature-gate.h: Update the function prototype
and delete two empty implementations in order to use default visitor
behavior.
* checks/errors/rust-feature.cc (Feature::create): Add the new
exclusive range pattern feature gate.
* checks/errors/rust-feature.h: Add new feature enum variant for
exclusive range patterns.
* parse/rust-parse-impl.h (Parser::parse_pattern_no_alt): Forward the
token location to the AST.
(Parser::parse_ident_leading_pattern): Likewise.

Signed-off-by: Pierre-Emmanuel Patry 
---
 gcc/rust/checks/errors/rust-feature-gate.cc | 8 
 gcc/rust/checks/errors/rust-feature-gate.h  | 4 +---
 gcc/rust/checks/errors/rust-feature.cc  | 5 +
 gcc/rust/checks/errors/rust-feature.h   | 1 +
 gcc/rust/parse/rust-parse-impl.h| 4 ++--
 5 files changed, 17 insertions(+), 5 deletions(-)

diff --git a/gcc/rust/checks/errors/rust-feature-gate.cc 
b/gcc/rust/checks/errors/rust-feature-gate.cc
index 8f9e991237b..580afc9e110 100644
--- a/gcc/rust/checks/errors/rust-feature-gate.cc
+++ b/gcc/rust/checks/errors/rust-feature-gate.cc
@@ -209,4 +209,12 @@ FeatureGate::visit (AST::BorrowExpr &expr)
  "raw address of syntax is experimental");
 }
 
+void
+FeatureGate::visit (AST::RangePattern &pattern)
+{
+  if (pattern.get_range_kind () == AST::RangeKind::EXCLUDED)
+gate (Feature::Name::EXCLUSIVE_RANGE_PATTERN, pattern.get_locus (),
+ "exclusive range pattern syntax is experimental");
+}
+
 } // namespace Rust
diff --git a/gcc/rust/checks/errors/rust-feature-gate.h 
b/gcc/rust/checks/errors/rust-feature-gate.h
index a31405e7d63..31c2ed65904 100644
--- a/gcc/rust/checks/errors/rust-feature-gate.h
+++ b/gcc/rust/checks/errors/rust-feature-gate.h
@@ -99,9 +99,7 @@ public:
   void visit (AST::ForLoopExpr &expr) override {}
   void visit (AST::IfExpr &expr) override {}
   void visit (AST::IfExprConseqElse &expr) override {}
-  void visit (AST::IfLetExpr &expr) override {}
   void visit (AST::IfLetExprConseqElse &expr) override {}
-  void visit (AST::MatchExpr &expr) override {}
   void visit (AST::AwaitExpr &expr) override {}
   void visit (AST::AsyncBlockExpr &expr) override {}
   void visit (AST::TypeParam ¶m) override;
@@ -150,7 +148,7 @@ public:
   void visit (AST::RangePatternBoundLiteral &bound) override {}
   void visit (AST::RangePatternBoundPath &bound) override {}
   void visit (AST::RangePatternBoundQualPath &bound) override {}
-  void visit (AST::RangePattern &pattern) override {}
+  void visit (AST::RangePattern &pattern) override;
   void visit (AST::ReferencePattern &pattern) override {}
   void visit (AST::StructPatternFieldTuplePat &field) override {}
   void visit (AST::StructPatternFieldIdentPat &field) override {}
diff --git a/gcc/rust/checks/errors/rust-feature.cc 
b/gcc/rust/checks/errors/rust-feature.cc
index bc8aa915064..917e3b2bdd0 100644
--- a/gcc/rust/checks/errors/rust-feature.cc
+++ b/gcc/rust/checks/errors/rust-feature.cc
@@ -54,6 +54,10 @@ Feature::create (Feature::Name name)
 case Feature::Name::RAW_REF_OP:
   return Feature (Feature::Name::RAW_REF_OP, Feature::State::ACTIVE,
  "raw_ref_op", "1.41.0", 64490, tl::nullopt, "");
+case Feature::Name::EXCLUSIVE_RANGE_PATTERN:
+  return Feature (Feature::Name::EXCLUSIVE_RANGE_PATTERN,
+ Feature::State::ACTIVE, "exclusive_range_pattern",
+ "1.11.0", 37854, tl::nullopt, "");
 default:
   rust_unreachable ();
 }
@@ -74,6 +78,7 @@ const std::map 
Feature::name_hash_map = {
   {"box_syntax", Feature::Name::BOX_SYNTAX},
   {"dropck_eyepatch", Feature::Name::DROPCK_EYEPATCH},
   {"raw_ref_op", Feature::Name::RAW_REF_OP},
+  {"exclusive_range_pattern", Feature::Name::EXCLUSIVE_RANGE_PATTERN},
 }; // namespace Rust
 
 tl::optional
diff --git a/gcc/rust/checks/errors/rust-feature.h 
b/gcc/rust/checks/errors/rust-feature.h
index 6661865aedf..698aac2dc63 100644
--- a/gcc/rust/checks/errors/rust-feature.h
+++ b/gcc/rust/checks/errors/rust-feature.h
@@ -49,6 +49,7 @@ public:
 BOX_SYNTAX,
 DROPCK_EYEPATCH,
 RAW_REF_OP,
+EXCLUSIVE_RANGE_PATTERN,
   };
 
   const std::string &as_string () { return m_name_str; }
diff --git a/gcc/rust/parse/rust-parse-impl.h b/gcc/rust/parse/rust-parse-impl.h
index cabfb5bf5e2..7a9c0b72869 100644
--- a/gcc/rust/parse/rust-parse-impl.h
+++ b/gcc/rust/parse/rust-parse-impl.h
@@ -10586,7 +10586,7 @@ Parser::parse_pattern_no_alt ()
  return std::unique_ptr (
new AST::RangePattern (std::move (lower_bound),
   std::move (upper_bound), kind,
-

[COMMITTED 076/145] gccrs: Introduce first implementation of parse_clobber_abi

2025-03-17 Thread arthur . cohen
From: jjasmine 

gcc/rust/ChangeLog:

* expand/rust-macro-builtins-asm.cc (parse_clobber_abi): title.
(parseAsmArg): title.
* expand/rust-macro-builtins-asm.h (parse_clobber_abi): title.
---
 gcc/rust/expand/rust-macro-builtins-asm.cc | 76 +-
 gcc/rust/expand/rust-macro-builtins-asm.h  |  7 +-
 2 files changed, 81 insertions(+), 2 deletions(-)

diff --git a/gcc/rust/expand/rust-macro-builtins-asm.cc 
b/gcc/rust/expand/rust-macro-builtins-asm.cc
index f26c4610230..9d904d6223e 100644
--- a/gcc/rust/expand/rust-macro-builtins-asm.cc
+++ b/gcc/rust/expand/rust-macro-builtins-asm.cc
@@ -26,6 +26,80 @@ parseDirSpec (Parser &parser, TokenId 
last_token_id)
   return tl::nullopt;
 }
 
+int
+parse_clobber_abi (Parser &parser, TokenId last_token_id,
+  AsmArg &args)
+{
+  // clobber_abi := "clobber_abi("  *("," ) [","] ")"
+
+  // PARSE EVERYTHING COMMITTEDLY IN THIS FUNCTION, WE CONFIRMED VIA 
clobber_abi
+  // identifier keyword
+
+  if (!parser.skip_token (LEFT_PAREN))
+{
+  // TODO: Raise error exactly like rustc if left parenthesis is not
+  // encountered.
+  return -1;
+}
+
+  if (parser.skip_token (RIGHT_PAREN))
+{
+  // TODO: We encountered a "clobber_abi()", which should be illegal?
+  // 
https://github.com/rust-lang/rust/blob/c00957a3e269219413041a4e3565f33b1f9d0779/compiler/rustc_builtin_macros/src/asm.rs#L381
+  return -1;
+}
+
+  ClobberAbis new_abis;
+
+  auto token = parser.peek_current_token ();
+
+  while (token->get_id () != last_token_id && token->get_id () != RIGHT_PAREN)
+{
+  // Check if it is a string literal or not, codename:  in ABNF
+  if (token->get_id () == STRING_LITERAL)
+   {
+ // TODO: Caring for span in here.
+ new_abis.push_back (token->as_string ());
+   }
+  else
+   {
+ // TODO: We encountered something that is not string literal, which
+ // should be illegal, please emit the correct error
+ // 
https://github.com/rust-lang/rust/blob/b92758a9aef1cef7b79e2b72c3d8ba113e547f89/compiler/rustc_builtin_macros/src/asm.rs#L387
+   }
+
+  if (parser.skip_token (RIGHT_PAREN))
+   {
+ break;
+   }
+
+  if (!parser.skip_token (COMMA))
+   {
+ // TODO: If the skip of comma is unsuccessful, which should be
+ // illegal, pleaes emit the correct error.
+ return -1;
+   }
+
+  // Done processing the local clobber abis, push that to the main Args in
+  // argument
+
+  for (auto abi : new_abis)
+   {
+ args.clobber_abis.push_back (abi);
+   }
+
+  return 0;
+}
+}
+
+int
+parse_options (Parser &parser, TokenId last_token_id,
+  AsmArg &args, bool is_global_asm)
+{
+  // Parse everything commitedly
+  if (!p.skip_token (LEFT_PAREN))
+{}
+}
 bool
 check_identifier (Parser &p, std::string ident)
 {
@@ -115,7 +189,7 @@ parseAsmArg (Parser &parser, TokenId 
last_token_id,
   // Ok after the left paren is good, we better be parsing correctly
   // everything in here, which is operand in ABNF
 
-  // TODO: Parse clobber abi
+  // TODO: Parse clobber abi, eat the identifier named "clobber_abi" if 
true
   if (check_identifier (parser, "clobber_abi"))
{
  std::cout << "Clobber abi tee hee" << std::endl;
diff --git a/gcc/rust/expand/rust-macro-builtins-asm.h 
b/gcc/rust/expand/rust-macro-builtins-asm.h
index 3a67c7e3152..ebb939a0548 100644
--- a/gcc/rust/expand/rust-macro-builtins-asm.h
+++ b/gcc/rust/expand/rust-macro-builtins-asm.h
@@ -37,7 +37,7 @@ typedef std::string symbol_name;
 typedef std::vector Templates;
 typedef std::vector Operands;
 typedef std::map RegisterArgs;
-typedef std::map ClobberAbis;
+typedef std::vector ClobberAbis;
 typedef std::map NamedValues;
 
 struct AsmArg
@@ -84,4 +84,9 @@ parse_options (Parser &parser, TokenId 
last_token_id,
 tl::optional
 parse_reg (Parser &parser, TokenId last_token_id, AsmArg 
&args,
   bool is_explicit);
+
+int
+parse_clobber_abi (Parser &parser, TokenId last_token_id,
+  AsmArg &args);
+
 } // namespace Rust
\ No newline at end of file
-- 
2.45.2



[COMMITTED 077/145] gccrs: Working on parse_options for a bit more

2025-03-17 Thread arthur . cohen
From: jjasmine 

gcc/rust/ChangeLog:

* expand/rust-macro-builtins-asm.cc (parse_options): title.
---
 gcc/rust/expand/rust-macro-builtins-asm.cc | 14 +-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/gcc/rust/expand/rust-macro-builtins-asm.cc 
b/gcc/rust/expand/rust-macro-builtins-asm.cc
index 9d904d6223e..d47d95c594f 100644
--- a/gcc/rust/expand/rust-macro-builtins-asm.cc
+++ b/gcc/rust/expand/rust-macro-builtins-asm.cc
@@ -98,7 +98,19 @@ parse_options (Parser &parser, TokenId 
last_token_id,
 {
   // Parse everything commitedly
   if (!p.skip_token (LEFT_PAREN))
-{}
+{
+  // We have shifted `options` to search for the left parenthesis next, we
+  // should error out if this is not possible.
+  // TODO: report some error.
+  return -1;
+}
+
+  auto token = parser.peek_current_token ();
+  while (token->get_id () != last_token_id && token->get_id () != RIGHT_PAREN)
+{
+  parser.skip_token ();
+  token = parser.peek_current_token ();
+}
 }
 bool
 check_identifier (Parser &p, std::string ident)
-- 
2.45.2



[COMMITTED 071/145] gccrs: Parse exclusive range pattern

2025-03-17 Thread arthur . cohen
From: Pierre-Emmanuel Patry 

Exclusive range pattern were not handled by the parser as this an
experimental feature.

gcc/rust/ChangeLog:

* ast/rust-pattern.cc (tokenid_to_rangekind): Add a new function to
get a range kind from the current token type.
(RangePattern::as_string): Change the string representation for range
pattern in order to handle excluded ranges.
* ast/rust-pattern.h (enum class): Add new enum class to differentiate
range kinds.
(tokenid_to_rangekind): New prototype for a function that converts a
token id to it's corresponding range kind.
(class RangePattern): Change the class to accept a range kind instead
of an ellipsis boolean.
* hir/rust-ast-lower-pattern.cc (ASTLoweringPattern::visit): Abort
when an excluded pattern has been found as we do not handle their
lowering yet.
* parse/rust-parse-impl.h (Parser::parse_literal_or_range_pattern):
Parse excluded range patterns.
(Parser::parse_pattern_no_alt): Likewise.
(Parser::parse_ident_leading_pattern): Likewise.

Signed-off-by: Pierre-Emmanuel Patry 
---
 gcc/rust/ast/rust-pattern.cc   | 31 
 gcc/rust/ast/rust-pattern.h| 32 ++---
 gcc/rust/hir/rust-ast-lower-pattern.cc |  3 +++
 gcc/rust/parse/rust-parse-impl.h   | 33 +++---
 4 files changed, 72 insertions(+), 27 deletions(-)

diff --git a/gcc/rust/ast/rust-pattern.cc b/gcc/rust/ast/rust-pattern.cc
index 85b3f5f3f51..98fd8e52f5f 100644
--- a/gcc/rust/ast/rust-pattern.cc
+++ b/gcc/rust/ast/rust-pattern.cc
@@ -30,6 +30,22 @@ along with GCC; see the file COPYING3.  If not see
 namespace Rust {
 namespace AST {
 
+RangeKind
+tokenid_to_rangekind (TokenId id)
+{
+  switch (id)
+{
+case DOT_DOT_EQ:
+  return RangeKind::INCLUDED;
+case ELLIPSIS:
+  return RangeKind::ELLIPSIS;
+case DOT_DOT:
+  return RangeKind::EXCLUDED;
+default:
+  rust_unreachable ();
+}
+}
+
 std::string
 LiteralPattern::as_string () const
 {
@@ -73,10 +89,17 @@ std::string
 RangePattern::as_string () const
 {
   // TODO: maybe rewrite to work with non-linearisable bounds
-  if (has_ellipsis_syntax)
-return lower->as_string () + "..." + upper->as_string ();
-  else
-return lower->as_string () + "..=" + upper->as_string ();
+  switch (range_kind)
+{
+case RangeKind::EXCLUDED:
+  return lower->as_string () + ".." + upper->as_string ();
+case RangeKind::INCLUDED:
+  return lower->as_string () + "..=" + upper->as_string ();
+case RangeKind::ELLIPSIS:
+  return lower->as_string () + "..." + upper->as_string ();
+default:
+  rust_unreachable ();
+}
 }
 
 std::string
diff --git a/gcc/rust/ast/rust-pattern.h b/gcc/rust/ast/rust-pattern.h
index 7cb09a5c2ab..383a5ee40a7 100644
--- a/gcc/rust/ast/rust-pattern.h
+++ b/gcc/rust/ast/rust-pattern.h
@@ -368,13 +368,22 @@ protected:
   }
 };
 
+enum class RangeKind
+{
+  INCLUDED,
+  ELLIPSIS,
+  EXCLUDED,
+};
+
+RangeKind
+tokenid_to_rangekind (TokenId id);
 // AST node for matching within a certain range (range pattern)
 class RangePattern : public Pattern
 {
   std::unique_ptr lower;
   std::unique_ptr upper;
 
-  bool has_ellipsis_syntax;
+  RangeKind range_kind;
 
   /* location only stored to avoid a dereference - lower pattern should give
* correct location so maybe change in future */
@@ -386,10 +395,10 @@ public:
 
   // Constructor
   RangePattern (std::unique_ptr lower,
-   std::unique_ptr upper, location_t locus,
-   bool has_ellipsis_syntax = false)
+   std::unique_ptr upper, RangeKind range_kind,
+   location_t locus)
 : lower (std::move (lower)), upper (std::move (upper)),
-  has_ellipsis_syntax (has_ellipsis_syntax), locus (locus),
+  range_kind (range_kind), locus (locus),
   node_id (Analysis::Mappings::get ().get_next_node_id ())
   {}
 
@@ -397,7 +406,7 @@ public:
   RangePattern (RangePattern const &other)
 : lower (other.lower->clone_range_pattern_bound ()),
   upper (other.upper->clone_range_pattern_bound ()),
-  has_ellipsis_syntax (other.has_ellipsis_syntax), locus (other.locus),
+  range_kind (other.range_kind), locus (other.locus),
   node_id (other.node_id)
   {}
 
@@ -406,7 +415,7 @@ public:
   {
 lower = other.lower->clone_range_pattern_bound ();
 upper = other.upper->clone_range_pattern_bound ();
-has_ellipsis_syntax = other.has_ellipsis_syntax;
+range_kind = other.range_kind;
 locus = other.locus;
 node_id = other.node_id;
 
@@ -419,11 +428,16 @@ public:
 
   location_t get_locus () const override final { return locus; }
 
-  bool get_has_ellipsis_syntax () { return has_ellipsis_syntax; }
+  bool get_has_ellipsis_syntax () const
+  {
+return range_kind == RangeKind::ELLIPSIS;
+  }
+
+  RangeKind get_range_kind () const { return range_kind; 

[PATCH 2/4] rust: Use error_operand_p in rust-gcc.cc

2025-03-17 Thread Andrew Pinski
Just a simple cleanupof the code to use error_operand_p
instead of directly comparing against error_mark_node.

This also moves some cdoe around when dealing with error_operand_p
just to be faster and/or slightly tighten up the code slightly.

gcc/rust/ChangeLog:

* rust-gcc.cc (Bvariable::get_tree): Use error_operand_p.
(pointer_type): Likewise.
(reference_type): Likewise.
(immutable_type): Likewise.
(function_type): Likewise.
(function_type_variadic): Likewise.
Cleanup the check for receiver.type first.
(function_ptr_type): Use error_operand_p.
(fill_in_fields): Likewise.
(fill_in_array): Likewise.
(named_type): Likewise.
(type_size): Likewise.
(type_alignment): Likewise.
(type_field_alignment): Likewise.
(type_field_offset): Likewise.
(zero_expression): Likewise.
(float_constant_expression): Likewise.
(convert_expression): Likewise.
(struct_field_expression): Likewise.
(compound_expression): Likewise.
(conditional_expression): Likewise.
(negation_expression): Likewise.
(arithmetic_or_logical_expression): Likewise.
(arithmetic_or_logical_expression_checked): Likewise.
(comparison_expression): Likewise.
(lazy_boolean_expression): Likewise.
(constructor_expression): Likewise.
(array_constructor_expression): Likewise.
(array_index_expression): Likewise.
(call_expression): Likewise.
(init_statement): Likewise.
(assignment_statement): Likewise.
(return_statement): Likewise.
(exception_handler_statement): Likewise.
(if_statement): Likewise.
(compound_statement): Likewise.
Tighten up the code, removing t variable.
(statement_list): Use error_operand_p.
(block): Likewise.
(block_add_statements): Likewise.
(convert_tree): Likewise.
(global_variable): Likewise.
(global_variable_set_init): Likewise.
(local_variable): Likewise.
(parameter_variable): Likewise.
(static_chain_variable): Likewise.
(temporary_variable): Likewise.
(function): Likewise. Tighten up the code.
(function_defer_statement): Use error_operand_p.
(function_set_parameters): Use error_operand_p.
(write_global_definitions): Use error_operand_p.
Tighten up the code around the loop.

Signed-off-by: Andrew Pinski 
---
 gcc/rust/rust-gcc.cc | 183 ---
 1 file changed, 85 insertions(+), 98 deletions(-)

diff --git a/gcc/rust/rust-gcc.cc b/gcc/rust/rust-gcc.cc
index e877c034452..f8b29bc6cf1 100644
--- a/gcc/rust/rust-gcc.cc
+++ b/gcc/rust/rust-gcc.cc
@@ -61,7 +61,7 @@
 tree
 Bvariable::get_tree (location_t location) const
 {
-  if (this->t_ == error_mark_node)
+  if (error_operand_p (this->t_))
 return error_mark_node;
 
   TREE_USED (this->t_) = 1;
@@ -431,7 +431,7 @@ float_type (int bits)
 tree
 pointer_type (tree to_type)
 {
-  if (to_type == error_mark_node)
+  if (error_operand_p (to_type))
 return error_mark_node;
   tree type = build_pointer_type (to_type);
   return type;
@@ -442,7 +442,7 @@ pointer_type (tree to_type)
 tree
 reference_type (tree to_type)
 {
-  if (to_type == error_mark_node)
+  if (error_operand_p (to_type))
 return error_mark_node;
   tree type = build_reference_type (to_type);
   return type;
@@ -453,7 +453,7 @@ reference_type (tree to_type)
 tree
 immutable_type (tree base)
 {
-  if (base == error_mark_node)
+  if (error_operand_p (base))
 return error_mark_node;
   tree constified = build_qualified_type (base, TYPE_QUAL_CONST);
   return constified;
@@ -472,7 +472,7 @@ function_type (const typed_identifier &receiver,
   if (receiver.type != NULL_TREE)
 {
   tree t = receiver.type;
-  if (t == error_mark_node)
+  if (error_operand_p (t))
return error_mark_node;
   *pp = tree_cons (NULL_TREE, t, NULL_TREE);
   pp = &TREE_CHAIN (*pp);
@@ -482,7 +482,7 @@ function_type (const typed_identifier &receiver,
p != parameters.end (); ++p)
 {
   tree t = p->type;
-  if (t == error_mark_node)
+  if (error_operand_p (t))
return error_mark_node;
   *pp = tree_cons (NULL_TREE, t, NULL_TREE);
   pp = &TREE_CHAIN (*pp);
@@ -502,11 +502,11 @@ function_type (const typed_identifier &receiver,
   gcc_assert (result_struct != NULL);
   result = result_struct;
 }
-  if (result == error_mark_node)
+  if (error_operand_p (result))
 return error_mark_node;
 
   tree fntype = build_function_type (result, args);
-  if (fntype == error_mark_node)
+  if (error_operand_p (fntype))
 return error_mark_node;
 
   return build_pointer_type (fntype);
@@ -521,21 +521,17 @@ function_type_variadic (const typed_identifier &receiver,
   size_t n = parameters.size () + (receiver.type != NULL_TREE ? 1 : 0);
   tree *a

[COMMITTED 119/145] gccrs: expand: Switch semicolon boolean to an enum instead.

2025-03-17 Thread arthur . cohen
From: Arthur Cohen 

gcc/rust/ChangeLog:

* ast/rust-ast-fragment.h (enum class): Add InvocKind and AsmKind enums.
* ast/rust-macro.h: Switch semicolon boolean to InvocKind enum.
* expand/rust-expand-visitor.cc (ExpandVisitor::visit): Likewise.
* expand/rust-macro-builtins-asm.cc (MacroBuiltin::asm_handler): 
Likewise.
(parse_asm): Likewise.
* expand/rust-macro-builtins-asm.h (parse_asm): Likewise.
* expand/rust-macro-builtins-format-args.cc 
(MacroBuiltin::format_args_handler): Likewise.
* expand/rust-macro-builtins-include.cc 
(MacroBuiltin::include_bytes_handler): Likewise.
(MacroBuiltin::include_str_handler): Likewise.
(MacroBuiltin::include_handler): Likewise.
* expand/rust-macro-builtins-location.cc (MacroBuiltin::file_handler): 
Likewise.
(MacroBuiltin::column_handler): Likewise.
(MacroBuiltin::line_handler): Likewise.
* expand/rust-macro-builtins-log-debug.cc 
(MacroBuiltin::assert_handler): Likewise.
* expand/rust-macro-builtins-utility.cc 
(MacroBuiltin::compile_error_handler): Likewise.
(MacroBuiltin::concat_handler): Likewise.
(MacroBuiltin::env_handler): Likewise.
(MacroBuiltin::cfg_handler): Likewise.
(MacroBuiltin::stringify_handler): Likewise.
* expand/rust-macro-builtins.cc (format_args_maker): Likewise.
(enum class): Likewise.
(inline_asm_maker): Likewise.
(MacroBuiltin::sorry): Likewise.
(MacroBuiltin::proc_macro_builtin): Likewise.
* expand/rust-macro-builtins.h: Likewise.
* expand/rust-macro-expand.cc (MacroExpander::expand_decl_macro): 
Likewise.
(MacroExpander::expand_eager_invocations): Likewise.
(MacroExpander::expand_invoc): Likewise.
* expand/rust-macro-expand.h (struct MacroExpander): Likewise.
---
 gcc/rust/ast/rust-ast-fragment.h  | 14 ++-
 gcc/rust/ast/rust-macro.h |  2 +-
 gcc/rust/expand/rust-expand-visitor.cc|  5 ++-
 gcc/rust/expand/rust-macro-builtins-asm.cc| 20 ++
 gcc/rust/expand/rust-macro-builtins-asm.h |  3 +-
 .../expand/rust-macro-builtins-format-args.cc |  4 +-
 .../expand/rust-macro-builtins-include.cc | 10 +++--
 .../expand/rust-macro-builtins-location.cc|  9 +++--
 .../expand/rust-macro-builtins-log-debug.cc   |  4 +-
 .../expand/rust-macro-builtins-utility.cc | 13 ---
 gcc/rust/expand/rust-macro-builtins.cc| 30 ++-
 gcc/rust/expand/rust-macro-builtins.h | 37 ++-
 gcc/rust/expand/rust-macro-expand.cc  | 20 +-
 gcc/rust/expand/rust-macro-expand.h   |  7 ++--
 14 files changed, 105 insertions(+), 73 deletions(-)

diff --git a/gcc/rust/ast/rust-ast-fragment.h b/gcc/rust/ast/rust-ast-fragment.h
index daa511226e3..7d01fd7904f 100644
--- a/gcc/rust/ast/rust-ast-fragment.h
+++ b/gcc/rust/ast/rust-ast-fragment.h
@@ -123,13 +123,25 @@ private:
   void assert_single_fragment (SingleASTNode::NodeType expected) const;
 };
 
+enum class InvocKind
+{
+  Expr,
+  Semicoloned,
+};
+
+enum class AsmKind
+{
+  Global,
+  Inline
+};
+
 /**
  * This is the type for transcriber functions found in
  * rust-macro-builtins.{h,cc}.
  */
 using MacroTranscriberFunc
   = std::function (location_t, MacroInvocData &,
- bool semicolon)>;
+ InvocKind semicolon)>;
 
 } // namespace AST
 } // namespace Rust
diff --git a/gcc/rust/ast/rust-macro.h b/gcc/rust/ast/rust-macro.h
index fbd9bd08634..76b3b2a3a8f 100644
--- a/gcc/rust/ast/rust-macro.h
+++ b/gcc/rust/ast/rust-macro.h
@@ -482,7 +482,7 @@ private:
* should make use of the actual rules. If the macro is builtin, then another
* associated transcriber should be used
*/
-  static Fragment dummy_builtin (location_t, MacroInvocData &, bool)
+  static Fragment dummy_builtin (location_t, MacroInvocData &, AST::InvocKind)
   {
 rust_unreachable ();
 return Fragment::create_error ();
diff --git a/gcc/rust/expand/rust-expand-visitor.cc 
b/gcc/rust/expand/rust-expand-visitor.cc
index 78b78bca449..61147e2d726 100644
--- a/gcc/rust/expand/rust-expand-visitor.cc
+++ b/gcc/rust/expand/rust-expand-visitor.cc
@@ -17,6 +17,7 @@
 // .
 
 #include "rust-expand-visitor.h"
+#include "rust-ast-fragment.h"
 #include "rust-proc-macro.h"
 #include "rust-attributes.h"
 #include "rust-ast.h"
@@ -467,7 +468,9 @@ void
 ExpandVisitor::visit (AST::MacroInvocation ¯o_invoc)
 {
   // TODO: Can we do the AST fragment replacing here? Probably not, right?
-  expander.expand_invoc (macro_invoc, macro_invoc.has_semicolon ());
+  expander.expand_invoc (macro_invoc, macro_invoc.has_semicolon ()
+   ? AST::InvocKind::Semicoloned
+   : AST::InvocKind::Expr);
 }
 
 void
diff --git a/gcc/rust/expand/rust-macro-builtin

[PATCH 3/4] rust: use range for inside rust-gcc.cc [PR119341]

2025-03-17 Thread Andrew Pinski
There are some places inside rust-gcc.cc which are candidates
to use range for instead of iterators directly. This changes
the locations I saw and makes the code slightly more readable.

gcc/rust/ChangeLog:

PR rust/119341
* rust-gcc.cc (function_type): Use range fors.
(function_type_variadic): Likewise.
(fill_in_fields): Likewise.
(statement_list): Likewise.
(block): Likewise.
(block_add_statements): Likewise.
(function_set_parameters): Likewise.
(write_global_definitions): Likewise.

Signed-off-by: Andrew Pinski 
---
 gcc/rust/rust-gcc.cc | 56 +---
 1 file changed, 21 insertions(+), 35 deletions(-)

diff --git a/gcc/rust/rust-gcc.cc b/gcc/rust/rust-gcc.cc
index f8b29bc6cf1..8a740f72513 100644
--- a/gcc/rust/rust-gcc.cc
+++ b/gcc/rust/rust-gcc.cc
@@ -478,10 +478,9 @@ function_type (const typed_identifier &receiver,
   pp = &TREE_CHAIN (*pp);
 }
 
-  for (std::vector::const_iterator p = parameters.begin ();
-   p != parameters.end (); ++p)
+  for (const auto &p : parameters)
 {
-  tree t = p->type;
+  tree t = p.type;
   if (error_operand_p (t))
return error_mark_node;
   *pp = tree_cons (NULL_TREE, t, NULL_TREE);
@@ -527,10 +526,9 @@ function_type_variadic (const typed_identifier &receiver,
   if (receiver.type != NULL_TREE)
 args[offs++] = receiver.type;
 
-  for (std::vector::const_iterator p = parameters.begin ();
-   p != parameters.end (); ++p)
+  for (const auto &p : parameters)
 {
-  tree t = p->type;
+  tree t = p.type;
   if (error_operand_p (t))
return error_mark_node;
   args[offs++] = t;
@@ -608,14 +606,13 @@ fill_in_fields (tree fill, const 
std::vector &fields)
 {
   tree field_trees = NULL_TREE;
   tree *pp = &field_trees;
-  for (std::vector::const_iterator p = fields.begin ();
-   p != fields.end (); ++p)
+  for (const auto &p : fields)
 {
-  tree name_tree = get_identifier_from_string (p->name);
-  tree type_tree = p->type;
+  tree name_tree = get_identifier_from_string (p.name);
+  tree type_tree = p.type;
   if (error_operand_p (type_tree))
return error_mark_node;
-  tree field = build_decl (p->location, FIELD_DECL, name_tree, type_tree);
+  tree field = build_decl (p.location, FIELD_DECL, name_tree, type_tree);
   DECL_CONTEXT (field) = fill;
   *pp = field;
   pp = &DECL_CHAIN (field);
@@ -1721,10 +1718,8 @@ tree
 statement_list (const std::vector &statements)
 {
   tree stmt_list = NULL_TREE;
-  for (std::vector::const_iterator p = statements.begin ();
-   p != statements.end (); ++p)
+  for (tree t : statements)
 {
-  tree t = (*p);
   if (error_operand_p (t))
return error_mark_node;
   append_to_statement_list (t, &stmt_list);
@@ -1778,10 +1773,9 @@ block (tree fndecl, tree enclosing, const 
std::vector &vars,
 }
 
   tree *pp = &BLOCK_VARS (block_tree);
-  for (std::vector::const_iterator pv = vars.begin ();
-   pv != vars.end (); ++pv)
+  for (Bvariable *bv : vars)
 {
-  *pp = (*pv)->get_decl ();
+  *pp = bv->get_decl ();
   if (!error_operand_p (*pp))
pp = &DECL_CHAIN (*pp);
 }
@@ -1801,10 +1795,8 @@ void
 block_add_statements (tree bind_tree, const std::vector &statements)
 {
   tree stmt_list = NULL_TREE;
-  for (std::vector::const_iterator p = statements.begin ();
-   p != statements.end (); ++p)
+  for (tree s : statements)
 {
-  tree s = (*p);
   if (!error_operand_p (s))
append_to_statement_list (s, &stmt_list);
 }
@@ -2248,10 +2240,9 @@ function_set_parameters (tree function,
 
   tree params = NULL_TREE;
   tree *pp = ¶ms;
-  for (std::vector::const_iterator pv = param_vars.begin ();
-   pv != param_vars.end (); ++pv)
+  for (Bvariable *bv : param_vars)
 {
-  *pp = (*pv)->get_decl ();
+  *pp = bv->get_decl ();
   gcc_assert (!error_operand_p (*pp));
   pp = &DECL_CHAIN (*pp);
 }
@@ -2277,10 +2268,9 @@ write_global_definitions (const std::vector 
&type_decls,
 
   // Convert all non-erroneous declarations into Gimple form.
   size_t i = 0;
-  for (std::vector::const_iterator p = variable_decls.begin ();
-   p != variable_decls.end (); ++p)
+  for (Bvariable *bv : variable_decls)
 {
-  tree v = (*p)->get_decl ();
+  tree v = bv->get_decl ();
   if (error_operand_p (v))
continue;
defs[i] = v;
@@ -2288,8 +2278,7 @@ write_global_definitions (const std::vector 
&type_decls,
++i;
 }
 
-  for (std::vector::const_iterator p = type_decls.begin ();
-   p != type_decls.end (); ++p)
+  for (tree type_tree : type_decls)
 {
   tree type_tree = (*p);
   if (type_tree != error_mark_node && IS_TYPE_OR_DECL_P (type_tree))
@@ -2300,20 +2289,17 @@ write_global_definitions (const std::vector 
&type_decls,
  ++i;
}
 }
-  for (std::vector::const_iterator

[PATCH 1/4] rust: Use REAL_TYPE_P instead of manual checking

2025-03-17 Thread Andrew Pinski
This moves is_floating_point over to using REAL_TYPE_P instead
of manually checking. Note before it would return true for all
COMPLEX_TYPE but complex types' inner type could be integral.

Also fixes up the comment to be in more of the GNU style.

Bootstrapped and tested on x86_64-linux-gnu.

gcc/rust/ChangeLog:

* rust-gcc.cc (is_floating_point): Use REAL_TYPE_P
instead of manually checking the type.

Signed-off-by: Andrew Pinski 
---
 gcc/rust/rust-gcc.cc | 8 
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/gcc/rust/rust-gcc.cc b/gcc/rust/rust-gcc.cc
index 273ab7889b0..e877c034452 100644
--- a/gcc/rust/rust-gcc.cc
+++ b/gcc/rust/rust-gcc.cc
@@ -1018,12 +1018,12 @@ operator_to_tree_code (LazyBooleanOperator op)
 }
 }
 
-/* Helper function for deciding if a tree is a floating point node. */
+/* Returns true if the type of EXP is a floating point type.
+   False otherwise.  */
 bool
-is_floating_point (tree t)
+is_floating_point (tree exp)
 {
-  auto tree_type = TREE_CODE (TREE_TYPE (t));
-  return tree_type == REAL_TYPE || tree_type == COMPLEX_TYPE;
+  return REAL_TYPE_P (TREE_TYPE (exp));
 }
 
 // Return an expression for the negation operation OP EXPR.
-- 
2.43.0



[PATCH 4/4] rust: Add comment inside block [PR119342]

2025-03-17 Thread Andrew Pinski
Inside a BLOCK node, all of the variables of the scope/block
are chained together and that connects them to the block.
This just adds a comment to that effect as reading the code
it is not so obvious why they need to be chained together.

gcc/rust/ChangeLog:

PR rust/119342
* rust-gcc.cc (block): Add comment on why chaining
the variables of the scope toether.

Signed-off-by: Andrew Pinski 
---
 gcc/rust/rust-gcc.cc | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/gcc/rust/rust-gcc.cc b/gcc/rust/rust-gcc.cc
index 8a740f72513..53950a35fdc 100644
--- a/gcc/rust/rust-gcc.cc
+++ b/gcc/rust/rust-gcc.cc
@@ -1772,6 +1772,8 @@ block (tree fndecl, tree enclosing, const 
std::vector &vars,
   *pp = block_tree;
 }
 
+  // Chain the variables of the scope together so they are all connected
+  // to the block.
   tree *pp = &BLOCK_VARS (block_tree);
   for (Bvariable *bv : vars)
 {
-- 
2.43.0



[Bug rust/119341] New: statement_list in rust-gcc.cc can use range for

2025-03-17 Thread pinskia at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=119341

Bug ID: 119341
   Summary: statement_list in rust-gcc.cc can use range for
   Product: gcc
   Version: 15.0
Status: UNCONFIRMED
  Keywords: internal-improvement
  Severity: normal
  Priority: P3
 Component: rust
  Assignee: pinskia at gcc dot gnu.org
  Reporter: pinskia at gcc dot gnu.org
CC: dkm at gcc dot gnu.org, gcc-rust at gcc dot gnu.org,
pierre-emmanuel.patry at embecosm dot com
  Target Milestone: ---

Right now statement_list does:
```
  for (std::vector::const_iterator p = statements.begin ();
   p != statements.end (); ++p)
{
  tree t = (*p);

```

But this can be rewritten as:
```
  for (tree t : statements)
```


I am writing up this bug report to remind me to write a patch for this after
finishing up using error_operand_p more inside the rust fe.

-- 
You are receiving this mail because:
You are on the CC list for the bug.

[COMMITTED 122/145] gccrs: Add RAW_STRING_LITERAL

2025-03-17 Thread arthur . cohen
From: ansh 

gcc/rust/ChangeLog:

* ast/rust-ast-collector.cc (TokenCollector::visit):
Handle case for RAW_STRING_LITERAL.
* ast/rust-ast.cc (AttributeParser::parse_meta_item_inner):
Likewise.
(AttributeParser::parse_literal): Likewise.
* ast/rust-ast.h: Likewise.
* hir/rust-ast-lower-base.cc (ASTLoweringBase::lower_literal):
Likewise.
* lex/rust-lex.cc (Lexer::parse_raw_string): Likewise.
* lex/rust-token.cc (Token::as_string): Likewise.
* lex/rust-token.h (enum PrimitiveCoreType): Likewise.
* parse/rust-parse-impl.h (Parser::parse_attr_input): Likewise.
(Parser::parse_literal_expr): Likewise.
(Parser::parse_pattern_no_alt): Likewise.

Signed-off-by: ansh 
---
 gcc/rust/ast/rust-ast-collector.cc  |  6 ++
 gcc/rust/ast/rust-ast.cc|  5 +
 gcc/rust/ast/rust-ast.h |  2 ++
 gcc/rust/hir/rust-ast-lower-base.cc |  3 +++
 gcc/rust/lex/rust-lex.cc|  2 +-
 gcc/rust/lex/rust-token.cc  |  3 +++
 gcc/rust/lex/rust-token.h   |  8 
 gcc/rust/parse/rust-parse-impl.h| 17 +
 8 files changed, 45 insertions(+), 1 deletion(-)

diff --git a/gcc/rust/ast/rust-ast-collector.cc 
b/gcc/rust/ast/rust-ast-collector.cc
index f1d5c8c6c32..6980fef6c51 100644
--- a/gcc/rust/ast/rust-ast-collector.cc
+++ b/gcc/rust/ast/rust-ast-collector.cc
@@ -398,6 +398,9 @@ TokenCollector::visit (Token &tok)
 case BYTE_STRING_LITERAL:
   push (Rust::Token::make_byte_string (tok.get_locus (), std::move 
(data)));
   break;
+case RAW_STRING_LITERAL:
+  push (Rust::Token::make_raw_string (tok.get_locus (), std::move (data)));
+  break;
 case INNER_DOC_COMMENT:
   push (Rust::Token::make_inner_doc_comment (tok.get_locus (),
 std::move (data)));
@@ -777,6 +780,9 @@ TokenCollector::visit (Literal &lit, location_t locus)
 case Literal::LitType::BYTE_STRING:
   push (Rust::Token::make_byte_string (locus, std::move (value)));
   break;
+case Literal::LitType::RAW_STRING:
+  push (Rust::Token::make_raw_string (locus, std::move (value)));
+  break;
 case Literal::LitType::INT:
   push (
Rust::Token::make_int (locus, std::move (value), lit.get_type_hint ()));
diff --git a/gcc/rust/ast/rust-ast.cc b/gcc/rust/ast/rust-ast.cc
index 2ff2e133037..bf7d31d8676 100644
--- a/gcc/rust/ast/rust-ast.cc
+++ b/gcc/rust/ast/rust-ast.cc
@@ -3506,6 +3506,7 @@ AttributeParser::parse_meta_item_inner ()
case STRING_LITERAL:
case BYTE_CHAR_LITERAL:
case BYTE_STRING_LITERAL:
+   case RAW_STRING_LITERAL:
case INT_LITERAL:
case FLOAT_LITERAL:
case TRUE_LITERAL:
@@ -3788,6 +3789,10 @@ AttributeParser::parse_literal ()
   skip_token ();
   return Literal (tok->as_string (), Literal::BYTE_STRING,
  tok->get_type_hint ());
+case RAW_STRING_LITERAL:
+  skip_token ();
+  return Literal (tok->as_string (), Literal::RAW_STRING,
+ tok->get_type_hint ());
 case INT_LITERAL:
   skip_token ();
   return Literal (tok->as_string (), Literal::INT, tok->get_type_hint ());
diff --git a/gcc/rust/ast/rust-ast.h b/gcc/rust/ast/rust-ast.h
index ee1c58c8fd6..4f40efff2a9 100644
--- a/gcc/rust/ast/rust-ast.h
+++ b/gcc/rust/ast/rust-ast.h
@@ -250,6 +250,7 @@ public:
   {
   case STRING_LITERAL:
   case BYTE_STRING_LITERAL:
+  case RAW_STRING_LITERAL:
return true;
   default:
return false;
@@ -311,6 +312,7 @@ public:
 STRING,
 BYTE,
 BYTE_STRING,
+RAW_STRING,
 INT,
 FLOAT,
 BOOL,
diff --git a/gcc/rust/hir/rust-ast-lower-base.cc 
b/gcc/rust/hir/rust-ast-lower-base.cc
index 0f928a4f462..906706e0205 100644
--- a/gcc/rust/hir/rust-ast-lower-base.cc
+++ b/gcc/rust/hir/rust-ast-lower-base.cc
@@ -928,6 +928,9 @@ ASTLoweringBase::lower_literal (const AST::Literal &literal)
 case AST::Literal::LitType::BYTE_STRING:
   type = HIR::Literal::LitType::BYTE_STRING;
   break;
+case AST::Literal::LitType::RAW_STRING: // TODO: Lower raw string literals.
+  rust_unreachable ();
+  break;
 case AST::Literal::LitType::INT:
   type = HIR::Literal::LitType::INT;
   break;
diff --git a/gcc/rust/lex/rust-lex.cc b/gcc/rust/lex/rust-lex.cc
index 3315e67dfc0..f4b8861adcc 100644
--- a/gcc/rust/lex/rust-lex.cc
+++ b/gcc/rust/lex/rust-lex.cc
@@ -2218,7 +2218,7 @@ Lexer::parse_raw_string (location_t loc, int 
initial_hash_count)
 
   str.shrink_to_fit ();
 
-  return Token::make_string (loc, std::move (str));
+  return Token::make_raw_string (loc, std::move (str));
 }
 
 template 
diff --git a/gcc/rust/lex/rust-token.cc b/gcc/rust/lex/rust-token.cc
index 05424766afc..8493889db9f 100644
--- a/gcc/rust/lex/rust-token.cc
+++ b/gcc/rust/lex/rust-token.cc
@@ -247,6 +247,9 @@ Token::as_string () const
case 

[Bug rust/119341] statement_list in rust-gcc.cc can use range for

2025-03-17 Thread pinskia at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=119341

Andrew Pinski  changed:

   What|Removed |Added

URL||https://gcc.gnu.org/piperma
   ||il/gcc-patches/2025-March/6
   ||78070.html
   Keywords||patch

-- 
You are receiving this mail because:
You are on the CC list for the bug.

[COMMITTED 032/145] gccrs: Change lookup_hir_extern_item return type

2025-03-17 Thread arthur . cohen
From: Pierre-Emmanuel Patry 

Wrap the return type with an optional and make the return type a pair
with the parent hid.

gcc/rust/ChangeLog:

* backend/rust-compile-resolve-path.cc (HIRCompileBase::query_compile):
Adapt code around new return type.
* checks/errors/rust-const-checker.cc 
(ConstChecker::check_function_call):
Likewise.
* checks/errors/rust-unsafe-checker.cc 
(UnsafeChecker::check_use_of_static):
Likewise.
(UnsafeChecker::check_function_call): Likewise.
* typecheck/rust-type-util.cc (query_type): Likewise.
* util/rust-hir-map.cc (Mappings::insert_hir_extern_item): Likewise.
(Mappings::lookup_hir_extern_item): Change return type.
* util/rust-hir-map.h: Update the function's prototype.

Signed-off-by: Pierre-Emmanuel Patry 
---
 gcc/rust/backend/rust-compile-resolve-path.cc |  8 +++-
 gcc/rust/checks/errors/rust-const-checker.cc  |  9 -
 gcc/rust/checks/errors/rust-unsafe-checker.cc | 18 ++
 gcc/rust/typecheck/rust-type-util.cc  | 11 ---
 gcc/rust/util/rust-hir-map.cc | 12 +---
 gcc/rust/util/rust-hir-map.h  |  5 -
 6 files changed, 26 insertions(+), 37 deletions(-)

diff --git a/gcc/rust/backend/rust-compile-resolve-path.cc 
b/gcc/rust/backend/rust-compile-resolve-path.cc
index bf294e4c879..c27074f89a4 100644
--- a/gcc/rust/backend/rust-compile-resolve-path.cc
+++ b/gcc/rust/backend/rust-compile-resolve-path.cc
@@ -201,10 +201,6 @@ HIRCompileBase::query_compile (HirId ref, TyTy::BaseType 
*lookup,
   const Analysis::NodeMapping &mappings,
   location_t expr_locus, bool is_qualified_path)
 {
-  HirId parent_block;
-  HIR::ExternalItem *resolved_extern_item
-= ctx->get_mappings ().lookup_hir_extern_item (ref, &parent_block);
-  bool is_hir_extern_item = resolved_extern_item != nullptr;
   bool is_fn = lookup->get_kind () == TyTy::TypeKind::FNDEF;
   if (auto resolved_item = ctx->get_mappings ().lookup_hir_item (ref))
 {
@@ -215,8 +211,10 @@ HIRCompileBase::query_compile (HirId ref, TyTy::BaseType 
*lookup,
return CompileItem::compile (*resolved_item, ctx, lookup, true,
 expr_locus);
 }
-  else if (is_hir_extern_item)
+  else if (auto hir_extern_item
+  = ctx->get_mappings ().lookup_hir_extern_item (ref))
 {
+  HIR::ExternalItem *resolved_extern_item = hir_extern_item->first;
   if (!lookup->has_substitutions_defined ())
return CompileExternItem::compile (resolved_extern_item, ctx, nullptr,
   true, expr_locus);
diff --git a/gcc/rust/checks/errors/rust-const-checker.cc 
b/gcc/rust/checks/errors/rust-const-checker.cc
index 1bb2c37d7eb..8c12012b33f 100644
--- a/gcc/rust/checks/errors/rust-const-checker.cc
+++ b/gcc/rust/checks/errors/rust-const-checker.cc
@@ -315,11 +315,9 @@ ConstChecker::check_function_call (HirId fn_id, location_t 
locus)
   // There are const extern functions (intrinsics)
   // TODO: Should we check the ABI is only "rust intrinsics"? Is that handled
   // elsewhere?
-  HirId parent_block;
-  auto maybe_extern_item
-= mappings.lookup_hir_extern_item (fn_id, &parent_block);
+  auto maybe_extern_item = mappings.lookup_hir_extern_item (fn_id);
   if (maybe_extern_item
-  && maybe_extern_item->get_extern_kind ()
+  && maybe_extern_item->first->get_extern_kind ()
   != ExternalItem::ExternKind::Function)
 return;
 
@@ -334,7 +332,8 @@ ConstChecker::check_function_call (HirId fn_id, location_t 
locus)
   if (maybe_extern_item)
 {
   {
-   auto fn = static_cast (maybe_extern_item);
+   auto fn
+ = static_cast (maybe_extern_item->first);
if (!is_const_extern_fn (*fn))
  is_error = true;
   }
diff --git a/gcc/rust/checks/errors/rust-unsafe-checker.cc 
b/gcc/rust/checks/errors/rust-unsafe-checker.cc
index fc83283a795..19a0489297f 100644
--- a/gcc/rust/checks/errors/rust-unsafe-checker.cc
+++ b/gcc/rust/checks/errors/rust-unsafe-checker.cc
@@ -70,15 +70,12 @@ UnsafeChecker::check_use_of_static (HirId node_id, 
location_t locus)
   if (unsafe_context.is_in_context ())
 return;
 
-  HirId extern_block;
-  auto maybe_extern_static
-= mappings.lookup_hir_extern_item (node_id, &extern_block);
-
   if (auto maybe_static_mut = mappings.lookup_hir_item (node_id))
 check_static_mut (*maybe_static_mut, locus);
 
-  if (maybe_extern_static)
-check_extern_static (static_cast (maybe_extern_static),
+  if (auto maybe_extern_static = mappings.lookup_hir_extern_item (node_id))
+check_extern_static (static_cast (
+  maybe_extern_static->first),
 locus);
 }
 
@@ -166,18 +163,15 @@ UnsafeChecker::check_function_call (HirId node_id, 
location_t locus)
   if (unsafe_context.is_in_context ())
 return;
 
-  HirId parent_exter

[COMMITTED 029/145] gccrs: Change return type of lookup_defid

2025-03-17 Thread arthur . cohen
From: Pierre-Emmanuel Patry 

Change the return type to an optional.

gcc/rust/ChangeLog:

* backend/rust-compile-base.cc (HIRCompileBase::resolve_method_address):
Change calling code to accomodate new return type.
* checks/errors/privacy/rust-privacy-reporter.cc:
Likewise.
* typecheck/rust-hir-type-check-base.cc 
(TypeCheckBase::get_marker_predicate):
Likewise.
* typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::visit):
Likewise.
* typecheck/rust-tyty-bounds.cc 
(TypeBoundsProbe::assemble_builtin_candidate):
Likewise.
* typecheck/rust-tyty.cc (ClosureType::setup_fn_once_output): Likewise.
* util/rust-hir-map.cc (Mappings::lookup_defid): Change function's
return type.
* util/rust-hir-map.h: Update function's prototype.

Signed-off-by: Pierre-Emmanuel Patry 
---
 gcc/rust/backend/rust-compile-base.cc   |  7 +++
 .../errors/privacy/rust-privacy-reporter.cc |  3 +--
 gcc/rust/typecheck/rust-hir-type-check-base.cc  |  3 +--
 gcc/rust/typecheck/rust-hir-type-check-expr.cc  | 17 ++---
 gcc/rust/typecheck/rust-tyty-bounds.cc  |  5 +++--
 gcc/rust/typecheck/rust-tyty.cc |  2 +-
 gcc/rust/util/rust-hir-map.cc   |  8 
 gcc/rust/util/rust-hir-map.h|  2 +-
 8 files changed, 20 insertions(+), 27 deletions(-)

diff --git a/gcc/rust/backend/rust-compile-base.cc 
b/gcc/rust/backend/rust-compile-base.cc
index 64ac121a28f..c7031edd30b 100644
--- a/gcc/rust/backend/rust-compile-base.cc
+++ b/gcc/rust/backend/rust-compile-base.cc
@@ -877,13 +877,12 @@ HIRCompileBase::resolve_method_address (TyTy::FnType 
*fntype,
   // Now we can try and resolve the address since this might be a forward
   // declared function, generic function which has not be compiled yet or
   // its an not yet trait bound function
-  HIR::Item *resolved_item = ctx->get_mappings ().lookup_defid (id);
-  if (resolved_item != nullptr)
+  if (auto resolved_item = ctx->get_mappings ().lookup_defid (id))
 {
   if (!fntype->has_substitutions_defined ())
-   return CompileItem::compile (resolved_item, ctx);
+   return CompileItem::compile (*resolved_item, ctx);
 
-  return CompileItem::compile (resolved_item, ctx, fntype);
+  return CompileItem::compile (*resolved_item, ctx, fntype);
 }
 
   // it might be resolved to a trait item
diff --git a/gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc 
b/gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc
index 3a304e881d5..f8df9f795ef 100644
--- a/gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc
+++ b/gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc
@@ -146,8 +146,7 @@ PrivacyReporter::check_for_privacy_violation (const NodeId 
&use_id,
if (!current_module.has_value ())
  return;
 
-   auto module = mappings.lookup_defid (vis.get_module_id ());
-   rust_assert (module != nullptr);
+   auto module = mappings.lookup_defid (vis.get_module_id ()).value ();
 
auto mod_node_id = module->get_mappings ().get_nodeid ();
 
diff --git a/gcc/rust/typecheck/rust-hir-type-check-base.cc 
b/gcc/rust/typecheck/rust-hir-type-check-base.cc
index 7e57698a068..7e34cef26c5 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-base.cc
+++ b/gcc/rust/typecheck/rust-hir-type-check-base.cc
@@ -414,8 +414,7 @@ TyTy::TypeBoundPredicate
 TypeCheckBase::get_marker_predicate (LangItem::Kind item_type, location_t 
locus)
 {
   DefId item_id = mappings.get_lang_item (item_type, locus);
-  HIR::Item *item = mappings.lookup_defid (item_id);
-  rust_assert (item != nullptr);
+  HIR::Item *item = mappings.lookup_defid (item_id).value ();
   rust_assert (item->get_item_kind () == HIR::Item::ItemKind::Trait);
 
   HIR::Trait &trait = *static_cast (item);
diff --git a/gcc/rust/typecheck/rust-hir-type-check-expr.cc 
b/gcc/rust/typecheck/rust-hir-type-check-expr.cc
index 25eb628c7b7..95e421d8720 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-expr.cc
+++ b/gcc/rust/typecheck/rust-hir-type-check-expr.cc
@@ -642,8 +642,7 @@ TypeCheckExpr::visit (HIR::RangeFromToExpr &expr)
 }
 
   // look it up and it _must_ be a struct definition
-  HIR::Item *item = mappings.lookup_defid (respective_lang_item_id);
-  rust_assert (item != nullptr);
+  HIR::Item *item = mappings.lookup_defid (respective_lang_item_id).value ();
 
   TyTy::BaseType *item_type = nullptr;
   bool ok
@@ -697,8 +696,7 @@ TypeCheckExpr::visit (HIR::RangeFromExpr &expr)
 }
 
   // look it up and it _must_ be a struct definition
-  HIR::Item *item = mappings.lookup_defid (respective_lang_item_id);
-  rust_assert (item != nullptr);
+  HIR::Item *item = mappings.lookup_defid (respective_lang_item_id).value ();
 
   TyTy::BaseType *item_type = nullptr;
   bool ok
@@ -745,8 +743,7 @@ TypeCheckExpr::visit (HIR::RangeToExpr &expr)
 }
 
   // look it up and it _must_ be a struct definition
-  HIR::Item *i

[COMMITTED 027/145] gccrs: Change crate_num_to_nodeid return type

2025-03-17 Thread arthur . cohen
From: Pierre-Emmanuel Patry 

Change the function's return type to an optional.

gcc/rust/ChangeLog:

* resolve/rust-ast-resolve-toplevel.h: Adapt the code to the new
return type.
* rust-session-manager.cc (Session::load_extern_crate): Likewise.
* util/rust-hir-map.cc (Mappings::crate_num_to_nodeid): Change the
return type.
* util/rust-hir-map.h: Update the function's prototype.

Signed-off-by: Pierre-Emmanuel Patry 
---
 gcc/rust/resolve/rust-ast-resolve-toplevel.h | 11 ---
 gcc/rust/rust-session-manager.cc |  8 +++-
 gcc/rust/util/rust-hir-map.cc|  9 -
 gcc/rust/util/rust-hir-map.h |  2 +-
 4 files changed, 12 insertions(+), 18 deletions(-)

diff --git a/gcc/rust/resolve/rust-ast-resolve-toplevel.h 
b/gcc/rust/resolve/rust-ast-resolve-toplevel.h
index 99fc4f6bfcc..565ca922e84 100644
--- a/gcc/rust/resolve/rust-ast-resolve-toplevel.h
+++ b/gcc/rust/resolve/rust-ast-resolve-toplevel.h
@@ -429,8 +429,7 @@ public:
 if (extern_crate.references_self ())
   {
CrateNum crate_num = mappings.get_current_crate ();
-   bool ok = mappings.crate_num_to_nodeid (crate_num, resolved_crate);
-   rust_assert (ok);
+   resolved_crate = mappings.crate_num_to_nodeid (crate_num).value ();
   }
 else
   {
@@ -442,11 +441,9 @@ public:
   extern_crate.get_referenced_crate ().c_str ());
return;
  }
-   auto found_crate_num = cnum.value ();
-
-   bool ok
- = mappings.crate_num_to_nodeid (found_crate_num, resolved_crate);
-   if (!ok)
+   if (auto resolved = mappings.crate_num_to_nodeid (*cnum))
+ resolved_crate = resolved.value ();
+   else
  {
rust_internal_error_at (extern_crate.get_locus (),
"failed to resolve crate to nodeid");
diff --git a/gcc/rust/rust-session-manager.cc b/gcc/rust/rust-session-manager.cc
index da66479d608..61a76d69a6b 100644
--- a/gcc/rust/rust-session-manager.cc
+++ b/gcc/rust/rust-session-manager.cc
@@ -1050,12 +1050,10 @@ Session::load_extern_crate (const std::string 
&crate_name, location_t locus)
   // has it already been loaded?
   if (auto crate_num = mappings.lookup_crate_name (crate_name))
 {
-  NodeId resolved_node_id = UNKNOWN_NODEID;
-  bool resolved
-   = mappings.crate_num_to_nodeid (*crate_num, resolved_node_id);
-  rust_assert (resolved);
+  auto resolved_node_id = mappings.crate_num_to_nodeid (*crate_num);
+  rust_assert (resolved_node_id);
 
-  return resolved_node_id;
+  return *resolved_node_id;
 }
 
   std::string relative_import_path = "";
diff --git a/gcc/rust/util/rust-hir-map.cc b/gcc/rust/util/rust-hir-map.cc
index 95d3b3a4d61..76642a1b132 100644
--- a/gcc/rust/util/rust-hir-map.cc
+++ b/gcc/rust/util/rust-hir-map.cc
@@ -175,15 +175,14 @@ Mappings::lookup_crate_name (const std::string 
&crate_name) const
   return tl::nullopt;
 }
 
-bool
-Mappings::crate_num_to_nodeid (const CrateNum &crate_num, NodeId &node_id) 
const
+tl::optional
+Mappings::crate_num_to_nodeid (const CrateNum &crate_num) const
 {
   auto it = ast_crate_mappings.find (crate_num);
   if (it == ast_crate_mappings.end ())
-return false;
+return tl::nullopt;
 
-  node_id = it->second->get_node_id ();
-  return true;
+  return it->second->get_node_id ();
 }
 
 bool
diff --git a/gcc/rust/util/rust-hir-map.h b/gcc/rust/util/rust-hir-map.h
index a68d81f34ef..8181abe00a9 100644
--- a/gcc/rust/util/rust-hir-map.h
+++ b/gcc/rust/util/rust-hir-map.h
@@ -85,7 +85,7 @@ public:
   std::string get_current_crate_name () const;
   tl::optional
   lookup_crate_name (const std::string &crate_name) const;
-  bool crate_num_to_nodeid (const CrateNum &crate_num, NodeId &node_id) const;
+  tl::optional crate_num_to_nodeid (const CrateNum &crate_num) const;
   bool node_is_crate (NodeId node_id) const;
 
   NodeId get_next_node_id ();
-- 
2.45.2



[COMMITTED 045/145] gccrs: Change lookup_canonical_path's return path

2025-03-17 Thread arthur . cohen
From: Pierre-Emmanuel Patry 

Change the function's return type to wrap it within an optional.

gcc/rust/ChangeLog:

* backend/rust-compile-base.cc: Change call site to accomodate new
return type.
* backend/rust-compile-base.h: Change parameter to use a reference.
* backend/rust-compile-extern.h: Likewise.
* backend/rust-compile-implitem.cc (CompileTraitItem::visit):
Change call site for new return type.
* backend/rust-compile-item.cc (CompileItem::visit): Likewise.
* resolve/rust-ast-resolve-type.cc (ResolveTypeToCanonicalPath::visit):
Likewise.
* typecheck/rust-hir-type-check-enumitem.cc (TypeCheckEnumItem::visit):
Likewise.
* typecheck/rust-hir-type-check-implitem.cc (TypeCheckImplItem::visit):
Likewise.
* typecheck/rust-hir-type-check-item.cc (TypeCheckItem::visit):
Likewise.
* typecheck/rust-hir-type-check.cc 
(TraitItemReference::get_type_from_fn):
Likewise.
* util/rust-hir-map.h: Update the function's prototype and change the
function's return type.

Signed-off-by: Pierre-Emmanuel Patry 
---
 gcc/rust/backend/rust-compile-base.cc | 10 ++--
 gcc/rust/backend/rust-compile-base.h  |  4 +-
 gcc/rust/backend/rust-compile-extern.h|  6 +--
 gcc/rust/backend/rust-compile-implitem.cc | 16 +++---
 gcc/rust/backend/rust-compile-item.cc | 26 --
 gcc/rust/resolve/rust-ast-resolve-type.cc |  3 +-
 .../typecheck/rust-hir-type-check-enumitem.cc | 24 +++--
 .../typecheck/rust-hir-type-check-implitem.cc |  7 +--
 .../typecheck/rust-hir-type-check-item.cc | 49 +++
 gcc/rust/typecheck/rust-hir-type-check.cc |  6 +--
 gcc/rust/util/rust-hir-map.h  | 11 ++---
 11 files changed, 61 insertions(+), 101 deletions(-)

diff --git a/gcc/rust/backend/rust-compile-base.cc 
b/gcc/rust/backend/rust-compile-base.cc
index add173c50a9..8166c3ae224 100644
--- a/gcc/rust/backend/rust-compile-base.cc
+++ b/gcc/rust/backend/rust-compile-base.cc
@@ -650,11 +650,11 @@ HIRCompileBase::compile_function (
   std::vector &function_params,
   const HIR::FunctionQualifiers &qualifiers, HIR::Visibility &visibility,
   AST::AttrVec &outer_attrs, location_t locus, HIR::BlockExpr *function_body,
-  const Resolver::CanonicalPath *canonical_path, TyTy::FnType *fntype)
+  const Resolver::CanonicalPath &canonical_path, TyTy::FnType *fntype)
 {
   tree compiled_fn_type = TyTyResolveCompile::compile (ctx, fntype);
   std::string ir_symbol_name
-= canonical_path->get () + fntype->subst_as_string ();
+= canonical_path.get () + fntype->subst_as_string ();
 
   // we don't mangle the main fn since we haven't implemented the main shim
   bool is_main_fn = fn_name.compare ("main") == 0;
@@ -677,7 +677,7 @@ HIRCompileBase::compile_function (
   // conditionally mangle the function name
   bool should_mangle = should_mangle_item (fndecl);
   if (!is_main_fn && should_mangle)
-asm_name = ctx->mangle_item (fntype, *canonical_path);
+asm_name = ctx->mangle_item (fntype, canonical_path);
   SET_DECL_ASSEMBLER_NAME (fndecl,
   get_identifier_with_length (asm_name.data (),
   asm_name.length ()));
@@ -767,10 +767,10 @@ HIRCompileBase::compile_function (
 
 tree
 HIRCompileBase::compile_constant_item (
-  TyTy::BaseType *resolved_type, const Resolver::CanonicalPath *canonical_path,
+  TyTy::BaseType *resolved_type, const Resolver::CanonicalPath &canonical_path,
   HIR::Expr *const_value_expr, location_t locus)
 {
-  const std::string &ident = canonical_path->get ();
+  const std::string &ident = canonical_path.get ();
 
   tree type = TyTyResolveCompile::compile (ctx, resolved_type);
   tree const_type = build_qualified_type (type, TYPE_QUAL_CONST);
diff --git a/gcc/rust/backend/rust-compile-base.h 
b/gcc/rust/backend/rust-compile-base.h
index 465a4caa720..eeb3ff02619 100644
--- a/gcc/rust/backend/rust-compile-base.h
+++ b/gcc/rust/backend/rust-compile-base.h
@@ -91,7 +91,7 @@ protected:
  TyTy::BaseType *fn_return_ty);
 
   tree compile_constant_item (TyTy::BaseType *resolved_type,
- const Resolver::CanonicalPath *canonical_path,
+ const Resolver::CanonicalPath &canonical_path,
  HIR::Expr *const_value_expr, location_t locus);
 
   tree compile_function (const std::string &fn_name, HIR::SelfParam 
&self_param,
@@ -99,7 +99,7 @@ protected:
 const HIR::FunctionQualifiers &qualifiers,
 HIR::Visibility &visibility, AST::AttrVec &outer_attrs,
 location_t locus, HIR::BlockExpr *function_body,
-const Resolver::CanonicalPath *canonical_path,
+const Resolver::CanonicalPath &canonical_path,
 TyTy::F

[COMMITTED 038/145] gccrs: Change lookup_hir_generic_param return type

2025-03-17 Thread arthur . cohen
From: Pierre-Emmanuel Patry 

Wrap the function's return type with an optional.

gcc/rust/ChangeLog:

* util/rust-hir-map.cc (Mappings::insert_hir_generic_param): Change
call site to accomodate the new return type.
(Mappings::lookup_hir_generic_param): Wrap the function's return type
with an optional.
* util/rust-hir-map.h: Update the function's prototype.

Signed-off-by: Pierre-Emmanuel Patry 
---
 gcc/rust/util/rust-hir-map.cc | 6 +++---
 gcc/rust/util/rust-hir-map.h  | 2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/gcc/rust/util/rust-hir-map.cc b/gcc/rust/util/rust-hir-map.cc
index c96743a54f8..c3929d8f3ce 100644
--- a/gcc/rust/util/rust-hir-map.cc
+++ b/gcc/rust/util/rust-hir-map.cc
@@ -571,19 +571,19 @@ void
 Mappings::insert_hir_generic_param (HIR::GenericParam *param)
 {
   auto id = param->get_mappings ().get_hirid ();
-  rust_assert (lookup_hir_generic_param (id) == nullptr);
+  rust_assert (!lookup_hir_generic_param (id));
 
   hirGenericParamMappings[id] = param;
   insert_node_to_hir (param->get_mappings ().get_nodeid (), id);
   insert_location (id, param->get_locus ());
 }
 
-HIR::GenericParam *
+tl::optional
 Mappings::lookup_hir_generic_param (HirId id)
 {
   auto it = hirGenericParamMappings.find (id);
   if (it == hirGenericParamMappings.end ())
-return nullptr;
+return tl::nullopt;
 
   return it->second;
 }
diff --git a/gcc/rust/util/rust-hir-map.h b/gcc/rust/util/rust-hir-map.h
index c7d0838d400..d04232f3d5d 100644
--- a/gcc/rust/util/rust-hir-map.h
+++ b/gcc/rust/util/rust-hir-map.h
@@ -152,7 +152,7 @@ public:
   tl::optional lookup_hir_path_expr_seg (HirId id);
 
   void insert_hir_generic_param (HIR::GenericParam *expr);
-  HIR::GenericParam *lookup_hir_generic_param (HirId id);
+  tl::optional lookup_hir_generic_param (HirId id);
 
   void insert_hir_type (HIR::Type *type);
   HIR::Type *lookup_hir_type (HirId id);
-- 
2.45.2



[COMMITTED 043/145] gccrs: Change lookup_hir_struct_field return type

2025-03-17 Thread arthur . cohen
From: Pierre-Emmanuel Patry 

Wrap the function's return type within an optional to differentiate
between a null pointer and a missing value.

gcc/rust/ChangeLog:

* util/rust-hir-map.cc (Mappings::insert_hir_struct_field): Change
call site to accomodate new return type.
(Mappings::lookup_hir_struct_field): Change the function's return
type.
* util/rust-hir-map.h: Update the function's prototype.

Signed-off-by: Pierre-Emmanuel Patry 
---
 gcc/rust/util/rust-hir-map.cc | 6 +++---
 gcc/rust/util/rust-hir-map.h  | 2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/gcc/rust/util/rust-hir-map.cc b/gcc/rust/util/rust-hir-map.cc
index f124be8b998..d1f55a372ac 100644
--- a/gcc/rust/util/rust-hir-map.cc
+++ b/gcc/rust/util/rust-hir-map.cc
@@ -672,18 +672,18 @@ void
 Mappings::insert_hir_struct_field (HIR::StructExprField *field)
 {
   auto id = field->get_mappings ().get_hirid ();
-  rust_assert (lookup_hir_struct_field (id) == nullptr);
+  rust_assert (!lookup_hir_struct_field (id));
 
   hirStructFieldMappings[id] = field;
   insert_node_to_hir (field->get_mappings ().get_nodeid (), id);
 }
 
-HIR::StructExprField *
+tl::optional
 Mappings::lookup_hir_struct_field (HirId id)
 {
   auto it = hirStructFieldMappings.find (id);
   if (it == hirStructFieldMappings.end ())
-return nullptr;
+return tl::nullopt;
 
   return it->second;
 }
diff --git a/gcc/rust/util/rust-hir-map.h b/gcc/rust/util/rust-hir-map.h
index 720dd976af4..740a8e6d499 100644
--- a/gcc/rust/util/rust-hir-map.h
+++ b/gcc/rust/util/rust-hir-map.h
@@ -167,7 +167,7 @@ public:
   tl::optional lookup_hir_self_param (HirId id);
 
   void insert_hir_struct_field (HIR::StructExprField *type);
-  HIR::StructExprField *lookup_hir_struct_field (HirId id);
+  tl::optional lookup_hir_struct_field (HirId id);
 
   void insert_hir_pattern (HIR::Pattern *pattern);
   HIR::Pattern *lookup_hir_pattern (HirId id);
-- 
2.45.2



[COMMITTED 040/145] gccrs: Change lookup_hir_smt's return type with optional

2025-03-17 Thread arthur . cohen
From: Pierre-Emmanuel Patry 

Wrap the function's return type within an optional in order to
differentiate missing values from null pointers.

gcc/rust/ChangeLog:

* util/rust-hir-map.cc (Mappings::insert_hir_stmt): Change call site
to accomodate new return type.
(Mappings::lookup_hir_stmt): Change the function's return type.
(Mappings::resolve_nodeid_to_stmt): Adapt call site to new return type.
* util/rust-hir-map.h: Update the function's prototype.

Signed-off-by: Pierre-Emmanuel Patry 
---
 gcc/rust/util/rust-hir-map.cc | 8 
 gcc/rust/util/rust-hir-map.h  | 2 +-
 2 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/gcc/rust/util/rust-hir-map.cc b/gcc/rust/util/rust-hir-map.cc
index ae11e67a57b..025b3121b27 100644
--- a/gcc/rust/util/rust-hir-map.cc
+++ b/gcc/rust/util/rust-hir-map.cc
@@ -612,18 +612,18 @@ void
 Mappings::insert_hir_stmt (HIR::Stmt *stmt)
 {
   auto id = stmt->get_mappings ().get_hirid ();
-  rust_assert (lookup_hir_stmt (id) == nullptr);
+  rust_assert (!lookup_hir_stmt (id));
 
   hirStmtMappings[id] = stmt;
   insert_node_to_hir (stmt->get_mappings ().get_nodeid (), id);
 }
 
-HIR::Stmt *
+tl::optional
 Mappings::lookup_hir_stmt (HirId id)
 {
   auto it = hirStmtMappings.find (id);
   if (it == hirStmtMappings.end ())
-return nullptr;
+return tl::nullopt;
 
   return it->second;
 }
@@ -796,7 +796,7 @@ Mappings::resolve_nodeid_to_stmt (NodeId id)
 return tl::nullopt;
 
   HirId resolved = it->second;
-  return {lookup_hir_stmt (resolved)};
+  return lookup_hir_stmt (resolved);
 }
 
 void
diff --git a/gcc/rust/util/rust-hir-map.h b/gcc/rust/util/rust-hir-map.h
index e4f47852ed9..f6db83d6e11 100644
--- a/gcc/rust/util/rust-hir-map.h
+++ b/gcc/rust/util/rust-hir-map.h
@@ -158,7 +158,7 @@ public:
   tl::optional lookup_hir_type (HirId id);
 
   void insert_hir_stmt (HIR::Stmt *stmt);
-  HIR::Stmt *lookup_hir_stmt (HirId id);
+  tl::optional lookup_hir_stmt (HirId id);
 
   void insert_hir_param (HIR::FunctionParam *type);
   HIR::FunctionParam *lookup_hir_param (HirId id);
-- 
2.45.2



[COMMITTED 036/145] gccrs: Change lookup_hir_expr return type to optional

2025-03-17 Thread arthur . cohen
From: Pierre-Emmanuel Patry 

Wrap the function's return type with an optional in order to
differentiate missing values from null pointers.

gcc/rust/ChangeLog:

* backend/rust-mangle-v0.cc (v0_path): Adapt call site to new returned
type.
* util/rust-hir-map.cc (Mappings::lookup_hir_expr): Change the
function's return type.
* util/rust-hir-map.h: Update the function's prototype.

Signed-off-by: Pierre-Emmanuel Patry 
---
 gcc/rust/backend/rust-mangle-v0.cc | 6 ++
 gcc/rust/util/rust-hir-map.cc  | 4 ++--
 gcc/rust/util/rust-hir-map.h   | 2 +-
 3 files changed, 5 insertions(+), 7 deletions(-)

diff --git a/gcc/rust/backend/rust-mangle-v0.cc 
b/gcc/rust/backend/rust-mangle-v0.cc
index 261e84405d5..d604dcf725c 100644
--- a/gcc/rust/backend/rust-mangle-v0.cc
+++ b/gcc/rust/backend/rust-mangle-v0.cc
@@ -384,8 +384,6 @@ v0_path (Rust::Compile::Context *ctx, const TyTy::BaseType 
*ty,
 
 auto hir_id = hid.value ();
 
-HIR::Expr *expr = mappings.lookup_hir_expr (hir_id);
-
 if (auto impl_item = mappings.lookup_hir_implitem (hir_id))
   {
switch (impl_item->first->get_impl_item_type ())
@@ -467,9 +465,9 @@ v0_path (Rust::Compile::Context *ctx, const TyTy::BaseType 
*ty,
  cpath.get ().c_str ());
  break;
}
-else if (expr != nullptr)
+else if (auto expr = mappings.lookup_hir_expr (hir_id))
   {
-   rust_assert (expr->get_expression_type ()
+   rust_assert (expr.value ()->get_expression_type ()
 == HIR::Expr::ExprType::Closure);
// Use HIR ID as disambiguator.
v0path = v0_closure (v0path, hir_id);
diff --git a/gcc/rust/util/rust-hir-map.cc b/gcc/rust/util/rust-hir-map.cc
index 89c990d965f..80786490f82 100644
--- a/gcc/rust/util/rust-hir-map.cc
+++ b/gcc/rust/util/rust-hir-map.cc
@@ -536,12 +536,12 @@ Mappings::insert_hir_expr (HIR::Expr *expr)
   insert_location (id, expr->get_locus ());
 }
 
-HIR::Expr *
+tl::optional
 Mappings::lookup_hir_expr (HirId id)
 {
   auto it = hirExprMappings.find (id);
   if (it == hirExprMappings.end ())
-return nullptr;
+return tl::nullopt;
 
   return it->second;
 }
diff --git a/gcc/rust/util/rust-hir-map.h b/gcc/rust/util/rust-hir-map.h
index a587ccca753..8d0f652a3d3 100644
--- a/gcc/rust/util/rust-hir-map.h
+++ b/gcc/rust/util/rust-hir-map.h
@@ -146,7 +146,7 @@ public:
   lookup_hir_implitem (HirId id);
 
   void insert_hir_expr (HIR::Expr *expr);
-  HIR::Expr *lookup_hir_expr (HirId id);
+  tl::optional lookup_hir_expr (HirId id);
 
   void insert_hir_path_expr_seg (HIR::PathExprSegment *expr);
   HIR::PathExprSegment *lookup_hir_path_expr_seg (HirId id);
-- 
2.45.2



[COMMITTED 024/145] gccrs: Change return type for lookup_hir_impl_block

2025-03-17 Thread arthur . cohen
From: Pierre-Emmanuel Patry 

Change the return type to an optiona. This allows to differentiate
between missing hir impl block and null pointers.

gcc/rust/ChangeLog:

* typecheck/rust-type-util.cc (query_type): Change call to the function
in order to accomodate the new return type.
* util/rust-hir-map.cc (Mappings::lookup_hir_impl_block): Change the
function's return type to an optional.
* util/rust-hir-map.h: Update the function's prototype.

Signed-off-by: Pierre-Emmanuel Patry 
---
 gcc/rust/typecheck/rust-type-util.cc | 8 
 gcc/rust/util/rust-hir-map.cc| 6 +++---
 gcc/rust/util/rust-hir-map.h | 2 +-
 3 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/gcc/rust/typecheck/rust-type-util.cc 
b/gcc/rust/typecheck/rust-type-util.cc
index 9a777863c2c..af38acd0c32 100644
--- a/gcc/rust/typecheck/rust-type-util.cc
+++ b/gcc/rust/typecheck/rust-type-util.cc
@@ -75,15 +75,15 @@ query_type (HirId reference, TyTy::BaseType **result)
 = mappings.lookup_hir_implitem (reference, &parent_impl_id);
   if (impl_item != nullptr)
 {
-  HIR::ImplBlock *impl_block
-   = mappings.lookup_hir_impl_block (parent_impl_id);
-  rust_assert (impl_block != nullptr);
+  auto impl_block = mappings.lookup_hir_impl_block (parent_impl_id);
+  rust_assert (impl_block);
 
   // found an impl item
   rust_debug_loc (impl_item->get_locus (), "resolved impl-item {%u} to",
  reference);
 
-  *result = TypeCheckItem::ResolveImplItem (*impl_block, *impl_item);
+  *result
+   = TypeCheckItem::ResolveImplItem (*impl_block.value (), *impl_item);
   context->query_completed (reference);
   return true;
 }
diff --git a/gcc/rust/util/rust-hir-map.cc b/gcc/rust/util/rust-hir-map.cc
index 184ab13494d..a5b1daf04c1 100644
--- a/gcc/rust/util/rust-hir-map.cc
+++ b/gcc/rust/util/rust-hir-map.cc
@@ -468,7 +468,7 @@ void
 Mappings::insert_hir_impl_block (HIR::ImplBlock *item)
 {
   auto id = item->get_mappings ().get_hirid ();
-  rust_assert (lookup_hir_impl_block (id) == nullptr);
+  rust_assert (!lookup_hir_impl_block (id));
 
   HirId impl_type_id = item->get_type ()->get_mappings ().get_hirid ();
   hirImplBlockMappings[id] = item;
@@ -476,12 +476,12 @@ Mappings::insert_hir_impl_block (HIR::ImplBlock *item)
   insert_node_to_hir (item->get_mappings ().get_nodeid (), id);
 }
 
-HIR::ImplBlock *
+tl::optional
 Mappings::lookup_hir_impl_block (HirId id)
 {
   auto it = hirImplBlockMappings.find (id);
   if (it == hirImplBlockMappings.end ())
-return nullptr;
+return tl::nullopt;
 
   return it->second;
 }
diff --git a/gcc/rust/util/rust-hir-map.h b/gcc/rust/util/rust-hir-map.h
index ef4d65639a0..109693e9c74 100644
--- a/gcc/rust/util/rust-hir-map.h
+++ b/gcc/rust/util/rust-hir-map.h
@@ -130,7 +130,7 @@ public:
   HIR::ExternalItem *lookup_hir_extern_item (HirId id, HirId *parent_block);
 
   void insert_hir_impl_block (HIR::ImplBlock *item);
-  HIR::ImplBlock *lookup_hir_impl_block (HirId id);
+  tl::optional lookup_hir_impl_block (HirId id);
   bool lookup_impl_block_type (HirId id, HIR::ImplBlock **impl_block);
 
   void insert_module (HIR::Module *module);
-- 
2.45.2



[COMMITTED 046/145] gccrs: Change lookup_macro_def return type

2025-03-17 Thread arthur . cohen
From: Pierre-Emmanuel Patry 

Wrap the function's return type within an optional.

gcc/rust/ChangeLog:

* resolve/rust-early-name-resolver-2.0.cc (Early::insert_once): Change
call site to accomodate the new return type.
(Early::visit): Likewise.
* resolve/rust-early-name-resolver.cc (EarlyNameResolver::visit):
Likewise.
* resolve/rust-toplevel-name-resolver-2.0.cc (TopLevel::visit):
Likewise.
* util/rust-hir-map.cc (Mappings::lookup_macro_def): Change the
function's return type.
* util/rust-hir-map.h: Update the function's prototype.

Signed-off-by: Pierre-Emmanuel Patry 
---
 .../resolve/rust-early-name-resolver-2.0.cc   | 25 +++
 gcc/rust/resolve/rust-early-name-resolver.cc  | 17 ++---
 .../rust-toplevel-name-resolver-2.0.cc|  3 +--
 gcc/rust/util/rust-hir-map.cc |  9 +++
 gcc/rust/util/rust-hir-map.h  |  2 +-
 5 files changed, 22 insertions(+), 34 deletions(-)

diff --git a/gcc/rust/resolve/rust-early-name-resolver-2.0.cc 
b/gcc/rust/resolve/rust-early-name-resolver-2.0.cc
index a79ffd86ef3..c0513931f85 100644
--- a/gcc/rust/resolve/rust-early-name-resolver-2.0.cc
+++ b/gcc/rust/resolve/rust-early-name-resolver-2.0.cc
@@ -30,26 +30,20 @@ void
 Early::insert_once (AST::MacroInvocation &invocation, NodeId resolved)
 {
   // TODO: Should we use `ctx.mark_resolved()`?
-  AST::MacroRulesDefinition *definition;
-  auto ok = ctx.mappings.lookup_macro_def (resolved, &definition);
-
-  rust_assert (ok);
+  auto definition = ctx.mappings.lookup_macro_def (resolved);
 
   AST::MacroRulesDefinition *existing;
   auto exists = ctx.mappings.lookup_macro_invocation (invocation, &existing);
 
   if (!exists)
-ctx.mappings.insert_macro_invocation (invocation, definition);
+ctx.mappings.insert_macro_invocation (invocation, definition.value ());
 }
 
 void
 Early::insert_once (AST::MacroRulesDefinition &def)
 {
   // TODO: Should we use `ctx.mark_resolved()`?
-  AST::MacroRulesDefinition *definition;
-  auto exists = ctx.mappings.lookup_macro_def (def.get_node_id (), 
&definition);
-
-  if (!exists)
+  if (!ctx.mappings.lookup_macro_def (def.get_node_id ()))
 ctx.mappings.insert_macro_def (&def);
 }
 
@@ -176,18 +170,17 @@ Early::visit (AST::MacroInvocation &invoc)
   // now do we need to keep mappings or something? or insert "uses" into our
   // ForeverStack? can we do that? are mappings simpler?
   auto &mappings = Analysis::Mappings::get ();
-  AST::MacroRulesDefinition *rules_def = nullptr;
-  if (!mappings.lookup_macro_def (definition->get_node_id (), &rules_def))
-{
-  // Macro definition not found, maybe it is not expanded yet.
-  return;
-}
+  auto rules_def = mappings.lookup_macro_def (definition->get_node_id ());
+
+  // Macro definition not found, maybe it is not expanded yet.
+  if (!rules_def)
+return;
 
   AST::MacroRulesDefinition *tmp_def = nullptr;
   if (mappings.lookup_macro_invocation (invoc, &tmp_def))
 return;
 
-  mappings.insert_macro_invocation (invoc, rules_def);
+  mappings.insert_macro_invocation (invoc, rules_def.value ());
 }
 
 void
diff --git a/gcc/rust/resolve/rust-early-name-resolver.cc 
b/gcc/rust/resolve/rust-early-name-resolver.cc
index e93cd0a0957..604b05da2b8 100644
--- a/gcc/rust/resolve/rust-early-name-resolver.cc
+++ b/gcc/rust/resolve/rust-early-name-resolver.cc
@@ -425,8 +425,7 @@ EarlyNameResolver::visit (AST::MacroRulesDefinition 
&rules_def)
* we could be inserting the same macro def over and over again until we
* implement some optimizations */
   // FIXME: ARTHUR: Remove that lookup and add proper optimizations instead
-  AST::MacroRulesDefinition *tmp = nullptr;
-  if (mappings.lookup_macro_def (rules_def.get_node_id (), &tmp))
+  if (mappings.lookup_macro_def (rules_def.get_node_id ()))
 return;
 
   mappings.insert_macro_def (&rules_def);
@@ -481,11 +480,9 @@ EarlyNameResolver::visit (AST::MacroInvocation &invoc)
 }
 
   // lookup the rules
-  AST::MacroRulesDefinition *rules_def = nullptr;
-  bool ok = mappings.lookup_macro_def (resolved_node, &rules_def);
-  rust_assert (ok);
+  auto rules_def = mappings.lookup_macro_def (resolved_node);
 
-  auto &outer_attrs = rules_def->get_outer_attrs ();
+  auto &outer_attrs = rules_def.value ()->get_outer_attrs ();
   bool is_builtin
 = std::any_of (outer_attrs.begin (), outer_attrs.end (),
   [] (AST::Attribute attr) {
@@ -495,12 +492,12 @@ EarlyNameResolver::visit (AST::MacroInvocation &invoc)
 
   if (is_builtin)
 {
-  auto builtin_kind
-   = builtin_macro_from_string (rules_def->get_rule_name ().as_string ());
+  auto builtin_kind = builtin_macro_from_string (
+   rules_def.value ()->get_rule_name ().as_string ());
   invoc.map_to_builtin (builtin_kind.value ());
 }
 
-  auto attributes = rules_def->get_outer_attrs ();
+  auto attributes = rules_def.value ()->get_outer_attrs ();
 

[COMMITTED 050/145] gccrs: mingw: Fix build with patch from Liu Hao

2025-03-17 Thread arthur . cohen
From: Arthur Cohen 

This commit adds Liu Hao's patch from
https://github.com/lhmouse/MINGW-packages/blob/5859d27b2b6101204a08ad9702cb2937f8797be9/mingw-w64-gcc/0100-rust-fix.patch

gcc/rust/ChangeLog:

* checks/errors/borrowck/rust-borrow-checker.cc (mkdir_wrapped): Remove.
(BorrowChecker::go): Use `mkdir` instead.
* expand/rust-proc-macro.cc (register_callback): Use Windows APIs to
open dynamic proc macro library.
(load_macros_array): Likewise.
* parse/rust-parse.cc (defined): Replace separators in paths using
std::replace.
---
 .../errors/borrowck/rust-borrow-checker.cc| 23 +++-
 gcc/rust/expand/rust-proc-macro.cc| 36 ++-
 gcc/rust/parse/rust-parse.cc  |  2 +-
 3 files changed, 34 insertions(+), 27 deletions(-)

diff --git a/gcc/rust/checks/errors/borrowck/rust-borrow-checker.cc 
b/gcc/rust/checks/errors/borrowck/rust-borrow-checker.cc
index 6eec021abca..29a9f4d27bb 100644
--- a/gcc/rust/checks/errors/borrowck/rust-borrow-checker.cc
+++ b/gcc/rust/checks/errors/borrowck/rust-borrow-checker.cc
@@ -26,20 +26,6 @@
 namespace Rust {
 namespace HIR {
 
-void
-mkdir_wrapped (const std::string &dirname)
-{
-  int ret;
-#ifdef _WIN32
-  ret = _mkdir (dirname.c_str ());
-#elif unix
-  ret = mkdir (dirname.c_str (), 0775);
-#elif __APPLE__
-  ret = mkdir (dirname.c_str (), 0775);
-#endif
-  rust_assert (ret == 0 || errno == EEXIST);
-}
-
 void
 dump_function_bir (const std::string &filename, BIR::Function &func,
   const std::string &name)
@@ -63,11 +49,11 @@ BorrowChecker::go (HIR::Crate &crate)
 
   if (enable_dump_bir)
 {
-  mkdir_wrapped ("bir_dump");
+  mkdir ("bir_dump", 0755);
   auto &mappings = Analysis::Mappings::get ();
   crate_name
= *mappings.get_crate_name (crate.get_mappings ().get_crate_num ());
-  mkdir_wrapped ("nll_facts_gccrs");
+  mkdir ("nll_facts_gccrs", 0755);
 }
 
   FunctionCollector collector;
@@ -95,8 +81,9 @@ BorrowChecker::go (HIR::Crate &crate)
 
   if (enable_dump_bir)
{
- mkdir_wrapped ("nll_facts_gccrs/"
-+ func->get_function_name ().as_string ());
+ auto dir
+   = "nll_facts_gccrs/" + func->get_function_name ().as_string ();
+ mkdir (dir.c_str (), 0755);
  auto dump_facts_to_file
= [&] (const std::string &suffix,
   void (Polonius::Facts::*fn) (std::ostream &) const) {
diff --git a/gcc/rust/expand/rust-proc-macro.cc 
b/gcc/rust/expand/rust-proc-macro.cc
index 7a02ff33058..22023e19a74 100644
--- a/gcc/rust/expand/rust-proc-macro.cc
+++ b/gcc/rust/expand/rust-proc-macro.cc
@@ -22,7 +22,10 @@
 #include "rust-token-converter.h"
 #include "rust-attributes.h"
 
-#ifndef _WIN32
+#ifdef _WIN32
+#define WIN32_LEAN_AND_MEAN
+#include 
+#else
 #include 
 #endif
 
@@ -100,12 +103,16 @@ static_assert (
 
 } // namespace
 
-template 
+template 
 bool
-register_callback (void *handle, Symbol, std::string symbol_name,
+register_callback (Handle handle, Symbol, std::string symbol_name,
   Callback callback)
 {
+#ifdef _WIN32
+  FARPROC addr = GetProcAddress (handle, symbol_name.c_str ());
+#else
   void *addr = dlsym (handle, symbol_name.c_str ());
+#endif
   if (addr == nullptr)
 {
   rust_error_at (UNDEF_LOCATION,
@@ -127,7 +134,19 @@ register_callback (void *handle, Symbol, std::string 
symbol_name,
 const ProcMacro::ProcmacroArray *
 load_macros_array (std::string path)
 {
-#ifndef _WIN32
+#ifdef _WIN32
+  HMODULE handle = LoadLibraryA (path.c_str ());
+  // We're leaking the handle since we can't ever unload it
+  if (!handle)
+{
+  char msg[300];
+  FormatMessageA (FORMAT_MESSAGE_FROM_SYSTEM
+   | FORMAT_MESSAGE_IGNORE_INSERTS,
+ nullptr, GetLastError (), 0, msg, sizeof msg, nullptr);
+  rust_debug ("Error whilst opening procedural macro: %s", msg);
+  return nullptr;
+}
+#else
   void *handle = dlopen (path.c_str (), RTLD_LAZY | RTLD_LOCAL);
   // We're leaking the handle since we can't ever unload it
   if (!handle)
@@ -135,6 +154,7 @@ load_macros_array (std::string path)
   rust_debug ("Error whilst opening procedural macro: %s", dlerror ());
   return nullptr;
 }
+#endif
 
   if (!REGISTER_CALLBACK (handle, __gccrs_proc_macro_ts_from_str_,
  tokenstream_from_string))
@@ -151,12 +171,12 @@ load_macros_array (std::string path)
   auto symbol_name = generate_proc_macro_decls_symbol (0 /* FIXME */);
 
   return *reinterpret_cast (
-dlsym (handle, symbol_name.c_str ()));
+#ifdef _WIN32
+GetProcAddress (handle, symbol_name.c_str ())
 #else
-  rust_sorry_at (UNDEF_LOCATION,
-"Procedural macros are not yet supported on windows host");
-  rust_unreachable ();
+dlsym (handle, symbol_name.c_str ())
 #endif
+  );
 }
 
 #undef REGISTER_CALLBACK
diff --git a/gcc/rust/parse/rust-pars

[COMMITTED 074/145] gccrs: Almost done with top level parsing

2025-03-17 Thread arthur . cohen
From: jjasmine 

gcc/rust/ChangeLog:

* expand/rust-macro-builtins-asm.cc (struct AsmParseError): title.
(enum InlineAsmDirSpec): title.
(enum InlineAsmOptions): title.
(struct AsmArg): title.
(parseAsmArg): title.
(parse_global_asm): title.
(parse_nonglobal_asm): title.
(parse_asm): title.
(parseDirSpec): title.
(parse_format_string): title.
(MacroBuiltin::global_asm_handler): title.
(MacroBuiltin::nonglobal_asm_handler): title.
* expand/rust-macro-builtins.cc: title.
* expand/rust-macro-builtins.h: title.
---
 gcc/rust/expand/rust-macro-builtins-asm.cc | 178 +
 gcc/rust/expand/rust-macro-builtins.cc |   6 +-
 gcc/rust/expand/rust-macro-builtins.h  |   6 +
 3 files changed, 186 insertions(+), 4 deletions(-)

diff --git a/gcc/rust/expand/rust-macro-builtins-asm.cc 
b/gcc/rust/expand/rust-macro-builtins-asm.cc
index e50694dffe5..7958d91537a 100644
--- a/gcc/rust/expand/rust-macro-builtins-asm.cc
+++ b/gcc/rust/expand/rust-macro-builtins-asm.cc
@@ -18,3 +18,181 @@
 
 #include "rust-macro-builtins.h"
 #include "rust-macro-builtins-helpers.h"
+#include "rust-macro-invoc-lexer.h"
+
+namespace Rust {
+
+struct AsmParseError
+{
+};
+
+// This is just an enum to hold some operands right now.
+enum InlineAsmDirSpec
+{
+  In,
+  Out,
+  InOut,
+  SplitInOut,
+  Const,
+  Sym,
+  Label,
+};
+
+enum InlineAsmOptions
+{
+
+};
+
+typedef std::string symbol_name;
+typedef std::vector Templates;
+typedef std::vector Operands;
+typedef std::map RegisterArgs;
+typedef std::map ClobberAbis;
+typedef std::map NamedValues;
+
+struct AsmArg
+{
+  Templates templates;
+  Operands operands;
+  std::map named_values;
+  RegisterArgs register_arguments;
+  ClobberAbis clobber_abis;
+  InlineAsmOptions options;
+  std::vector
+options_span; // TODO: @badumbatish @jjasmine I have no idea what span do, 
i
+ // copied it out of rustc_builtin_macros/src/asm.rs
+};
+
+tl::optional
+parseAsmArg (Parser &p, bool is_global_asm);
+static tl::optional
+parse_global_asm (location_t invoc_locus, AST::MacroInvocData &invoc);
+static tl::optional
+parse_nonglobal_asm (location_t invoc_locus, AST::MacroInvocData &invoc);
+static tl::optional
+parse_asm (location_t invoc_locus, AST::MacroInvocData &invoc,
+  bool is_global_asm);
+
+tl::optional
+parseDirSpec (Parser &p, TokenId last_token_id)
+{
+  return tl::nullopt;
+}
+tl::optional
+parseAsmArg (Parser &p, bool is_global_asm)
+{
+  return tl::nullopt;
+}
+
+tl::optional
+parse_format_string (Parser &parser, TokenId last_token_id)
+{
+  auto token = parser.peek_current_token ();
+
+  if (token->get_id () != last_token_id && token->get_id () == STRING_LITERAL)
+{
+  // very nice, we got a supposedly formatted string.
+  std::cout << token->get_token_description () << std::endl;
+  parser.skip_token ();
+  return "formatted string";
+}
+  else
+{
+  parser.skip_token ();
+  std::cout << token->get_token_description () << std::endl;
+
+  rust_error_at (token->get_locus (),
+"asm template must be a string literal");
+  return tl::nullopt;
+}
+}
+
+tl::optional
+MacroBuiltin::global_asm_handler (location_t invoc_locus,
+ AST::MacroInvocData &invoc)
+{
+  // Just to clarify the code
+  bool is_global_asm = true;
+  return parse_asm (invoc_locus, invoc, is_global_asm);
+}
+
+tl::optional
+MacroBuiltin::nonglobal_asm_handler (location_t invoc_locus,
+AST::MacroInvocData &invoc)
+{
+  // Just to clarify the code
+  bool is_global_asm = false;
+  return parse_asm (invoc_locus, invoc, is_global_asm);
+}
+
+static tl::optional
+parse_asm (location_t invoc_locus, AST::MacroInvocData &invoc,
+  bool is_global_asm)
+{
+  // From the rule of asm.
+  // We first peek and see if it is a format string or not.
+  // If yes, we process the first ever format string, and move on to the
+  // recurrent of format string Else we exit out
+
+  // After that, we peek and see if it is a reoccuring stream of format string
+  // or not. If it is, keep on going to do this format string. Else, move on
+
+  // After that, we peek and see if it is a reoccuring stream of operands or 
not
+  // If it is, keep on going to do this operand thingy.
+  // Else, move on
+
+  // We check if there is an optional "," at the end, per ABNF spec.
+  // If it is, consume it.
+
+  // Done
+  MacroInvocLexer lex (invoc.get_delim_tok_tree ().to_token_stream ());
+  Parser parser (lex);
+  auto last_token_id = macro_end_token (invoc.get_delim_tok_tree (), parser);
+
+  // Parse the first ever formatted string, success or not, will skip 1 token
+  auto fm_string = parse_format_string (parser, last_token_id);
+  if (fm_string == tl::nullopt)
+return tl::nullopt;
+
+  // formatted string stream
+  auto token = parser.peek_current_token ();
+ 

[COMMITTED 102/145] gccrs: Add support for AST to HIR inline asm translation

2025-03-17 Thread arthur . cohen
From: jjasmine 

gcc/rust/ChangeLog:

* ast/rust-expr.h:
Add support for AST to HIR inline asm translation
* hir/rust-ast-lower-base.cc (ASTLoweringBase::visit): Likewise.
* hir/rust-ast-lower-base.h: Likewise.
* hir/rust-ast-lower-expr.cc (ASTLoweringExpr::visit): Likewise.
* hir/rust-ast-lower-expr.h: Likewise.
* hir/tree/rust-hir-expr.h (class InlineAsm): Likewise.
---
 gcc/rust/ast/rust-expr.h| 10 +
 gcc/rust/hir/rust-ast-lower-base.cc |  5 +++
 gcc/rust/hir/rust-ast-lower-base.h  |  1 +
 gcc/rust/hir/rust-ast-lower-expr.cc | 14 +++
 gcc/rust/hir/rust-ast-lower-expr.h  |  1 +
 gcc/rust/hir/tree/rust-hir-expr.h   | 60 -
 6 files changed, 89 insertions(+), 2 deletions(-)

diff --git a/gcc/rust/ast/rust-expr.h b/gcc/rust/ast/rust-expr.h
index ad742bfe85d..cc8c6ea0350 100644
--- a/gcc/rust/ast/rust-expr.h
+++ b/gcc/rust/ast/rust-expr.h
@@ -4994,6 +4994,16 @@ public:
 
   void set_outer_attrs (std::vector v) override { outer_attrs = v; }
 
+  std::vector get_template_ () { return template_; }
+
+  std::vector get_template_strs () { return template_strs; }
+
+  std::vector get_operands () { return operands; }
+
+  std::vector get_clobber_abi () { return clobber_abi; }
+
+  std::set get_options () { return options; }
+
   InlineAsm *clone_expr_without_block_impl () const override
   {
 return new InlineAsm (*this);
diff --git a/gcc/rust/hir/rust-ast-lower-base.cc 
b/gcc/rust/hir/rust-ast-lower-base.cc
index 96969782c98..0f928a4f462 100644
--- a/gcc/rust/hir/rust-ast-lower-base.cc
+++ b/gcc/rust/hir/rust-ast-lower-base.cc
@@ -248,6 +248,11 @@ ASTLoweringBase::visit (AST::IfLetExpr &)
 void
 ASTLoweringBase::visit (AST::IfLetExprConseqElse &)
 {}
+
+void
+ASTLoweringBase::visit (AST::InlineAsm &)
+{}
+
 //  void ASTLoweringBase::visit(MatchCasematch_case) {}
 // void ASTLoweringBase:: (AST::MatchCaseBlockExpr &) {}
 // void ASTLoweringBase:: (AST::MatchCaseExpr &) {}
diff --git a/gcc/rust/hir/rust-ast-lower-base.h 
b/gcc/rust/hir/rust-ast-lower-base.h
index 94e0e48968c..1bd1343bd16 100644
--- a/gcc/rust/hir/rust-ast-lower-base.h
+++ b/gcc/rust/hir/rust-ast-lower-base.h
@@ -148,6 +148,7 @@ public:
   virtual void visit (AST::IfExprConseqElse &expr);
   virtual void visit (AST::IfLetExpr &expr);
   virtual void visit (AST::IfLetExprConseqElse &expr);
+  virtual void visit (AST::InlineAsm &expr);
   //  virtual void visit(MatchCase& match_case);
   // virtual void visit (AST::MatchCaseBlockExpr &match_case);
   // virtual void visit (AST::MatchCaseExpr &match_case);
diff --git a/gcc/rust/hir/rust-ast-lower-expr.cc 
b/gcc/rust/hir/rust-ast-lower-expr.cc
index a0eb5e32f25..be7ff413c93 100644
--- a/gcc/rust/hir/rust-ast-lower-expr.cc
+++ b/gcc/rust/hir/rust-ast-lower-expr.cc
@@ -829,6 +829,20 @@ ASTLoweringExpr::visit (AST::ClosureExprInnerTyped &expr)
expr.get_locus ());
 }
 
+void
+ASTLoweringExpr::visit (AST::InlineAsm &expr)
+{
+  auto crate_num = mappings.get_current_crate ();
+  Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
+mappings.get_next_hir_id (crate_num),
+mappings.get_next_localdef_id (crate_num));
+
+  translated
+= new HIR::InlineAsm (expr.get_locus (), expr.is_global_asm,
+ expr.get_template_ (), expr.get_template_strs (),
+ expr.get_operands (), expr.get_clobber_abi (),
+ expr.get_options (), mapping);
+}
 void
 ASTLoweringExpr::visit (AST::FormatArgs &fmt)
 {
diff --git a/gcc/rust/hir/rust-ast-lower-expr.h 
b/gcc/rust/hir/rust-ast-lower-expr.h
index cd7b74aa7f2..fc53786613b 100644
--- a/gcc/rust/hir/rust-ast-lower-expr.h
+++ b/gcc/rust/hir/rust-ast-lower-expr.h
@@ -122,6 +122,7 @@ public:
   void visit (AST::RangeFromToInclExpr &expr) override;
   void visit (AST::ClosureExprInner &expr) override;
   void visit (AST::ClosureExprInnerTyped &expr) override;
+  void visit (AST::InlineAsm &expr) override;
 
   // Extra visitor for FormatArgs nodes
   void visit (AST::FormatArgs &fmt) override;
diff --git a/gcc/rust/hir/tree/rust-hir-expr.h 
b/gcc/rust/hir/tree/rust-hir-expr.h
index 686c7311a4b..a0858a36f04 100644
--- a/gcc/rust/hir/tree/rust-hir-expr.h
+++ b/gcc/rust/hir/tree/rust-hir-expr.h
@@ -24,7 +24,7 @@
 #include "rust-hir.h"
 #include "rust-hir-path.h"
 #include "rust-operators.h"
-
+#include "rust-expr.h"
 namespace Rust {
 namespace HIR {
 
@@ -3865,13 +3865,69 @@ struct InlineAsmRegOrRegClass
 class InlineAsm : public ExprWithoutBlock
 {
   NodeId id;
+  location_t locus;
 
 public:
+  bool is_global_asm;
+
   std::vector template_;
   std::vector template_strs;
   std::vector operands;
-  AST::InlineAsmOption options;
+  std::vector clobber_abi;
+  std::set options;
+
   std::vector line_spans;
+
+  void accept_vis (HIRExpressionVisitor &vis) override{};
+
+  void accept_vis (HIRFullVisitor &vis) overrid

[COMMITTED 075/145] gccrs: Working on parseAsmArg

2025-03-17 Thread arthur . cohen
From: jjasmine 

gcc/rust/ChangeLog:

* expand/rust-macro-builtins-asm.cc (enum InlineAsmRegOrRegClass): 
title.
(parseAsmArg): title.
(check_identifier): title.
(parse_operand): title.
(parse_options): title.
(parse_reg): title.
(parseDirSpec): title.
(parse_asm): title.
---
 gcc/rust/expand/rust-macro-builtins-asm.cc | 151 ++---
 gcc/rust/expand/rust-macro-builtins-asm.h  |  87 
 2 files changed, 159 insertions(+), 79 deletions(-)
 create mode 100644 gcc/rust/expand/rust-macro-builtins-asm.h

diff --git a/gcc/rust/expand/rust-macro-builtins-asm.cc 
b/gcc/rust/expand/rust-macro-builtins-asm.cc
index 7958d91537a..f26c4610230 100644
--- a/gcc/rust/expand/rust-macro-builtins-asm.cc
+++ b/gcc/rust/expand/rust-macro-builtins-asm.cc
@@ -16,74 +16,32 @@
 // along with GCC; see the file COPYING3.  If not see
 // .
 
-#include "rust-macro-builtins.h"
-#include "rust-macro-builtins-helpers.h"
-#include "rust-macro-invoc-lexer.h"
+#include "rust-macro-builtins-asm.h"
 
 namespace Rust {
 
-struct AsmParseError
-{
-};
-
-// This is just an enum to hold some operands right now.
-enum InlineAsmDirSpec
-{
-  In,
-  Out,
-  InOut,
-  SplitInOut,
-  Const,
-  Sym,
-  Label,
-};
-
-enum InlineAsmOptions
-{
-
-};
-
-typedef std::string symbol_name;
-typedef std::vector Templates;
-typedef std::vector Operands;
-typedef std::map RegisterArgs;
-typedef std::map ClobberAbis;
-typedef std::map NamedValues;
-
-struct AsmArg
-{
-  Templates templates;
-  Operands operands;
-  std::map named_values;
-  RegisterArgs register_arguments;
-  ClobberAbis clobber_abis;
-  InlineAsmOptions options;
-  std::vector
-options_span; // TODO: @badumbatish @jjasmine I have no idea what span do, 
i
- // copied it out of rustc_builtin_macros/src/asm.rs
-};
-
-tl::optional
-parseAsmArg (Parser &p, bool is_global_asm);
-static tl::optional
-parse_global_asm (location_t invoc_locus, AST::MacroInvocData &invoc);
-static tl::optional
-parse_nonglobal_asm (location_t invoc_locus, AST::MacroInvocData &invoc);
-static tl::optional
-parse_asm (location_t invoc_locus, AST::MacroInvocData &invoc,
-  bool is_global_asm);
-
 tl::optional
-parseDirSpec (Parser &p, TokenId last_token_id)
+parseDirSpec (Parser &parser, TokenId last_token_id)
 {
   return tl::nullopt;
 }
-tl::optional
-parseAsmArg (Parser &p, bool is_global_asm)
+
+bool
+check_identifier (Parser &p, std::string ident)
 {
-  return tl::nullopt;
-}
+  auto token = p.peek_current_token ();
 
+  if (token->get_id () == IDENTIFIER
+  && (token->as_string () == ident || ident == ""))
+{
+  p.skip_token ();
+  return true;
+}
+  else
+{
+  return false;
+}
+}
 tl::optional
 parse_format_string (Parser &parser, TokenId last_token_id)
 {
@@ -125,6 +83,59 @@ MacroBuiltin::nonglobal_asm_handler (location_t invoc_locus,
   return parse_asm (invoc_locus, invoc, is_global_asm);
 }
 
+tl::optional
+parseAsmArg (Parser &parser, TokenId last_token_id,
+bool is_global_asm)
+{
+  auto token = parser.peek_current_token ();
+  AsmArg arg;
+  tl::optional fm_string;
+  while (token->get_id () != last_token_id)
+{
+  std::cout << token->get_token_description () << std::endl;
+
+  token = parser.peek_current_token ();
+
+  // We accept a comma token here.
+  if (token->get_id () != COMMA)
+   {
+ break;
+   }
+  parser.skip_token ();
+
+  // And if that token comma is also the trailing comma, we break
+  // TODO: Check with mentor see what last_token_id means
+  token = parser.peek_current_token ();
+  if (token->get_id () == COMMA && token->get_id () == last_token_id)
+   {
+ parser.skip_token ();
+ break;
+   }
+
+  // Ok after the left paren is good, we better be parsing correctly
+  // everything in here, which is operand in ABNF
+
+  // TODO: Parse clobber abi
+  if (check_identifier (parser, "clobber_abi"))
+   {
+ std::cout << "Clobber abi tee hee" << std::endl;
+ continue;
+   }
+
+  // TODO: Parse options
+  if (check_identifier (parser, "options"))
+   {
+ std::cout << "Parse optoins" << std::endl;
+ continue;
+   }
+
+  // Ok after we have check that neither clobber_abi nor options works, the
+  // only other logical choice is reg_operand
+  fm_string = parse_format_string (parser, last_token_id);
+}
+  return tl::nullopt;
+}
+
 static tl::optional
 parse_asm (location_t invoc_locus, AST::MacroInvocData &invoc,
   bool is_global_asm)
@@ -171,26 +182,8 @@ parse_asm (location_t invoc_locus, AST::MacroInvocData 
&invoc,
   fm_string = parse_format_string (parser, last_token_id);
 }
 
-  // operands stream
-  token = parser.peek_current_token ();
-  while (token->get_id () != last_token_id)
-{
-  std::cout << token->get_token_descript

[COMMITTED 107/145] gccrs: Successful parse of in and inout, albeit with str

2025-03-17 Thread arthur . cohen
From: jjasmine 

gcc/rust/ChangeLog:

* expand/rust-macro-builtins-asm.cc (parse_reg_operand):
Successful parse of in and inout, albeit with str
(check_identifier): Likewise.
(parse_asm_arg): Likewise.
* expand/rust-macro-builtins-asm.h (parse_format_string): Likewise.

gcc/testsuite/ChangeLog:

* rust/compile/inline_asm_parse_operand.rs: New test.
---
 gcc/rust/expand/rust-macro-builtins-asm.cc| 108 --
 gcc/rust/expand/rust-macro-builtins-asm.h |   4 +
 .../rust/compile/inline_asm_parse_operand.rs  |  28 +
 3 files changed, 130 insertions(+), 10 deletions(-)
 create mode 100644 gcc/testsuite/rust/compile/inline_asm_parse_operand.rs

diff --git a/gcc/rust/expand/rust-macro-builtins-asm.cc 
b/gcc/rust/expand/rust-macro-builtins-asm.cc
index 95a268a02de..23f78c01494 100644
--- a/gcc/rust/expand/rust-macro-builtins-asm.cc
+++ b/gcc/rust/expand/rust-macro-builtins-asm.cc
@@ -204,11 +204,15 @@ parse_reg_operand (Parser &parser, 
TokenId last_token_id,
   //   };
 
   AST::InlineAsmOperand reg_operand;
+  rust_debug("Enter parse_reg_operand");
   auto token = parser.peek_current_token ();
   auto iden_token = parser.peek_current_token ();
   auto &inline_asm = inline_asm_ctx.inline_asm;
   if (check_identifier (parser, ""))
 {
+
+  rust_debug("Didn't get passed identifier checking, %s", 
token->as_string().c_str());
+
   auto equal_token = parser.peek_current_token ();
   if (!parser.skip_token (EQUAL))
{
@@ -218,12 +222,40 @@ parse_reg_operand (Parser &parser, 
TokenId last_token_id,
 }
 
   token = parser.peek_current_token ();
+  rust_debug_loc(token->get_locus(), "Got pass identifier checking with %s",  
token->as_string().c_str());
+
 
   bool is_global_asm = inline_asm.is_global_asm;
-  if (!is_global_asm && check_identifier (parser, "in"))
+
+  // For the keyword IN, currently we count it as a seperate keyword called 
Rust::IN
+  // search for #define RS_TOKEN_LIST in code base.
+  if (!is_global_asm && parser.skip_token(IN))
 {
-  rust_unreachable ();
-  return tl::nullopt;
+  rust_debug("Enter parse_reg_operand in");
+
+  auto reg = parse_reg(parser, last_token_id, inline_asm_ctx);
+
+  if (parser.skip_token(UNDERSCORE)) {
+// We are sure to be failing a test here, based on asm.rs 
+// 
https://github.com/rust-lang/rust/blob/a330e49593ee890f9197727a3a558b6e6b37f843/compiler/rustc_builtin_macros/src/asm.rs#L112
+rust_unreachable();
+  }
+
+  auto expr = parse_format_string(parser, last_token_id, inline_asm_ctx) ;
+  reg_operand.register_type = AST::InlineAsmOperand::RegisterType::In;
+  
+  // Since reg is of type optional, we need to check if it is not 
optional first.
+  // TODO: We don't throw any errors since we should have throw any 
encountered parsing error in parse_reg
+  if (reg) {
+reg_operand.in.reg = reg.value();
+  }
+  
+  // Only clone_expr() if we know that we have parse an expression 
successfully
+  // if (expr) {
+  //   reg_operand.in.expr = expr->clone_expr();
+  // }
+
+  return reg_operand;
 }
   else if (!is_global_asm && check_identifier (parser, "out"))
 {
@@ -237,8 +269,51 @@ parse_reg_operand (Parser &parser, 
TokenId last_token_id,
 }
   else if (!is_global_asm && check_identifier (parser, "inout"))
 {
-  rust_unreachable ();
+  rust_debug("Enter parse_reg_operand inout");
+  
+  auto reg = parse_reg(parser, last_token_id, inline_asm_ctx);
+
+  if (parser.skip_token(UNDERSCORE)) {
+// We are sure to be failing a test here, based on asm.rs 
+// 
https://github.com/rust-lang/rust/blob/a330e49593ee890f9197727a3a558b6e6b37f843/compiler/rustc_builtin_macros/src/asm.rs#L112
+rust_unreachable();
+  }
+
+  // TODO: Is error propogation our top priority, the ? in rust's asm.rs 
is doing a lot of work.
+  // TODO: Not sure how to use parse_expr
+  auto expr = parse_format_string(parser, last_token_id, inline_asm_ctx) ;
+
+  std::unique_ptr out_expr;
+
+  if (parser.skip_token(MATCH_ARROW)) {
+rust_debug("Matched MATCH_ARROW");
+if (!parser.skip_token(UNDERSCORE)) {
+  
+  parse_format_string(parser, last_token_id, inline_asm_ctx) ;
+  //out_expr = parser.parse_expr();
+}
+
+reg_operand.register_type = 
AST::InlineAsmOperand::RegisterType::SplitInOut;
+// reg_operand.split_in_out.in_expr = expr->clone_expr();
+// reg_operand.split_in_out.out_expr = out_expr->clone_expr();
+// reg_operand.split_in_out.late = false;
+return reg_operand;
+
+  } else {
+reg_operand.register_type = AST::InlineAsmOperand::RegisterType::InOut;
+// reg_operand.in_out.expr = expr->clone_expr();
+// reg_operand.in_out.late = false;
+return reg_operand;
+  }
+//  if p.eat(&token::Fa

[COMMITTED 125/145] gccrs: Scaffold expected on parse_options and asm_arg

2025-03-17 Thread arthur . cohen
From: jjasmine 

gcc/rust/ChangeLog:

* expand/rust-macro-builtins-asm.cc (check_and_set):
Scaffold expected on parse_options and asm_arg
(parse_options): Likewise
(parse_asm_arg): Likewise
* expand/rust-macro-builtins-asm.h (check_and_set): Likewise
(parse_label): Likewise

Signed-off-by: badumbatish 
---
 gcc/rust/expand/rust-macro-builtins-asm.cc | 39 +++---
 gcc/rust/expand/rust-macro-builtins-asm.h  |  5 +--
 2 files changed, 28 insertions(+), 16 deletions(-)

diff --git a/gcc/rust/expand/rust-macro-builtins-asm.cc 
b/gcc/rust/expand/rust-macro-builtins-asm.cc
index 304edb1b936..7e484713ef7 100644
--- a/gcc/rust/expand/rust-macro-builtins-asm.cc
+++ b/gcc/rust/expand/rust-macro-builtins-asm.cc
@@ -35,6 +35,8 @@ std::map 
InlineAsmOptionMap{
   {AST::InlineAsmOption::RAW, "raw"},
 };
 
+std::set potentially_nonpromoted_keywords
+  = {"in", "out", "lateout", "inout", "inlateout", "const", "sym", "label"};
 tl::expected
 parse_clobber_abi (InlineAsmContext inline_asm_ctx)
 {
@@ -426,11 +428,10 @@ check_and_set (InlineAsmContext &inline_asm_ctx, 
AST::InlineAsmOption option)
   inline_asm.options.insert (option);
 }
 }
-int
+tl::expected
 parse_options (InlineAsmContext &inline_asm_ctx)
 {
   auto &parser = inline_asm_ctx.parser;
-  auto last_token_id = inline_asm_ctx.last_token_id;
   bool is_global_asm = inline_asm_ctx.inline_asm.is_global_asm;
   // Parse everything commitedly
   if (!parser.skip_token (LEFT_PAREN))
@@ -438,11 +439,11 @@ parse_options (InlineAsmContext &inline_asm_ctx)
   // We have shifted `options` to search for the left parenthesis next, we
   // should error out if this is not possible.
   // TODO: report some error.
-  return -1;
+  return tl::unexpected (COMMITTED);
 }
 
   auto token = parser.peek_current_token ();
-  while (token->get_id () != last_token_id && token->get_id () != RIGHT_PAREN)
+  while (!parser.skip_token (RIGHT_PAREN))
 {
   if (!is_global_asm && check_identifier (parser, "pure"))
{
@@ -489,7 +490,7 @@ parse_options (InlineAsmContext &inline_asm_ctx)
 ")", "att_syntax", "may_unwind", "nomem", "noreturn",
 "nostack", "preserves_flags", "pure", "raw",
 "readonly", token->as_string ().c_str ());
- return -1;
+ return tl::unexpected (COMMITTED);
}
   if (parser.skip_token (RIGHT_PAREN))
{
@@ -505,7 +506,8 @@ parse_options (InlineAsmContext &inline_asm_ctx)
{
  rust_unreachable ();
  token = parser.peek_current_token ();
- return -1;
+ return tl::unexpected (COMMITTED);
+ ;
}
 }
 
@@ -514,7 +516,7 @@ parse_options (InlineAsmContext &inline_asm_ctx)
   // let new_span = span_start.to(p.prev_token.span);
   // args.options_spans.push(new_span);
 
-  return 0;
+  return inline_asm_ctx;
 }
 
 bool
@@ -599,10 +601,14 @@ parse_asm_arg (InlineAsmContext inline_asm_ctx)
  inline_asm_ctx.consumed_comma_without_formatted_string = false;
  parser.skip_token ();
}
-  else
+  else if (token->get_id () == COMMA
+  && inline_asm_ctx.consumed_comma_without_formatted_string)
{
- // TODO: we consumed comma, and there happens to also be a comma
+ // We consumed comma, and there happens to also be a comma
  // error should be: expected expression, found `,`
+ rust_error_at (token->get_locus (), "expected expression, found %qs",
+",");
+ return tl::unexpected (COMMITTED);
  break;
}
 
@@ -620,14 +626,20 @@ parse_asm_arg (InlineAsmContext inline_asm_ctx)
   // TODO: Parse clobber abi, eat the identifier named "clobber_abi" if 
true
   if (check_identifier (parser, "clobber_abi"))
{
- parse_clobber_abi (inline_asm_ctx);
+ auto expected = parse_clobber_abi (inline_asm_ctx);
+ if (expected || expected.error () == COMMITTED)
+   return expected;
+
  continue;
}
 
   // TODO: Parse options
   if (check_identifier (parser, "options"))
{
- parse_options (inline_asm_ctx);
+ auto expected = parse_options (inline_asm_ctx);
+ if (expected || expected.error () == COMMITTED)
+   return expected;
+
  continue;
}
 
@@ -636,7 +648,10 @@ parse_asm_arg (InlineAsmContext inline_asm_ctx)
   // std::cout << "reg_operand" << std::endl;
 
   // TODO: BUBBLE UP THIS EXPECTED(...)
-  auto operand = parse_reg_operand (inline_asm_ctx);
+  auto expected = parse_reg_operand (inline_asm_ctx);
+  if (expected || expected.error () == COMMITTED)
+   return expected;
+  continue;
 }
   return tl::expected (inline_asm_ctx);
 }
diff --git a/gcc/rust/expand/rust-macro-builtins-asm.h 
b/gcc/rust/expand/rust-macro-builtins-asm.h
index d03adf004ca..a0bfd782e9b 10064

[COMMITTED 108/145] gccrs: Add potentially_nonpromoted_keywords set str

2025-03-17 Thread arthur . cohen
From: jjasmine 

gcc/rust/ChangeLog:

* expand/rust-macro-builtins-asm.cc (parse_reg_operand):
Add potentially_nonpromoted_keywords set str
(check_identifier): likewise
* expand/rust-macro-builtins-asm.h (parse_format_string):
likewise

gcc/testsuite/ChangeLog:

* rust/compile/inline_asm_parse_operand.rs: fix warnings
---
 gcc/rust/expand/rust-macro-builtins-asm.cc| 174 ++
 gcc/rust/expand/rust-macro-builtins-asm.h |   3 +
 .../rust/compile/inline_asm_parse_operand.rs  |  17 +-
 3 files changed, 110 insertions(+), 84 deletions(-)

diff --git a/gcc/rust/expand/rust-macro-builtins-asm.cc 
b/gcc/rust/expand/rust-macro-builtins-asm.cc
index 23f78c01494..55ba95a7331 100644
--- a/gcc/rust/expand/rust-macro-builtins-asm.cc
+++ b/gcc/rust/expand/rust-macro-builtins-asm.cc
@@ -204,14 +204,14 @@ parse_reg_operand (Parser &parser, 
TokenId last_token_id,
   //   };
 
   AST::InlineAsmOperand reg_operand;
-  rust_debug("Enter parse_reg_operand");
+  rust_debug ("Enter parse_reg_operand");
   auto token = parser.peek_current_token ();
   auto iden_token = parser.peek_current_token ();
   auto &inline_asm = inline_asm_ctx.inline_asm;
   if (check_identifier (parser, ""))
 {
-
-  rust_debug("Didn't get passed identifier checking, %s", 
token->as_string().c_str());
+  rust_debug ("Didn't get passed identifier checking, %s",
+ token->as_string ().c_str ());
 
   auto equal_token = parser.peek_current_token ();
   if (!parser.skip_token (EQUAL))
@@ -222,36 +222,40 @@ parse_reg_operand (Parser &parser, 
TokenId last_token_id,
 }
 
   token = parser.peek_current_token ();
-  rust_debug_loc(token->get_locus(), "Got pass identifier checking with %s",  
token->as_string().c_str());
-
+  rust_debug_loc (token->get_locus (), "Got pass identifier checking with %s",
+ token->as_string ().c_str ());
 
   bool is_global_asm = inline_asm.is_global_asm;
 
-  // For the keyword IN, currently we count it as a seperate keyword called 
Rust::IN
-  // search for #define RS_TOKEN_LIST in code base.
-  if (!is_global_asm && parser.skip_token(IN))
+  // For the keyword IN, currently we count it as a seperate keyword called
+  // Rust::IN search for #define RS_TOKEN_LIST in code base.
+  if (!is_global_asm && parser.skip_token (IN))
 {
-  rust_debug("Enter parse_reg_operand in");
+  rust_debug ("Enter parse_reg_operand in");
 
-  auto reg = parse_reg(parser, last_token_id, inline_asm_ctx);
+  auto reg = parse_reg (parser, last_token_id, inline_asm_ctx);
 
-  if (parser.skip_token(UNDERSCORE)) {
-// We are sure to be failing a test here, based on asm.rs 
-// 
https://github.com/rust-lang/rust/blob/a330e49593ee890f9197727a3a558b6e6b37f843/compiler/rustc_builtin_macros/src/asm.rs#L112
-rust_unreachable();
-  }
+  if (parser.skip_token (UNDERSCORE))
+   {
+ // We are sure to be failing a test here, based on asm.rs
+ // 
https://github.com/rust-lang/rust/blob/a330e49593ee890f9197727a3a558b6e6b37f843/compiler/rustc_builtin_macros/src/asm.rs#L112
+ rust_unreachable ();
+   }
 
-  auto expr = parse_format_string(parser, last_token_id, inline_asm_ctx) ;
+  auto expr = parse_format_string (parser, last_token_id, inline_asm_ctx);
   reg_operand.register_type = AST::InlineAsmOperand::RegisterType::In;
-  
-  // Since reg is of type optional, we need to check if it is not 
optional first.
-  // TODO: We don't throw any errors since we should have throw any 
encountered parsing error in parse_reg
-  if (reg) {
-reg_operand.in.reg = reg.value();
-  }
-  
-  // Only clone_expr() if we know that we have parse an expression 
successfully
-  // if (expr) {
+
+  // Since reg is of type optional, we need to check if it is not
+  // optional first.
+  // TODO: We don't throw any errors since we should have throw any
+  // encountered parsing error in parse_reg
+  if (reg)
+   {
+ reg_operand.in.reg = reg.value ();
+   }
+
+  // Only clone_expr() if we know that we have parse an expression
+  // successfully if (expr) {
   //   reg_operand.in.expr = expr->clone_expr();
   // }
 
@@ -269,51 +273,58 @@ parse_reg_operand (Parser &parser, 
TokenId last_token_id,
 }
   else if (!is_global_asm && check_identifier (parser, "inout"))
 {
-  rust_debug("Enter parse_reg_operand inout");
-  
-  auto reg = parse_reg(parser, last_token_id, inline_asm_ctx);
+  rust_debug ("Enter parse_reg_operand inout");
+
+  auto reg = parse_reg (parser, last_token_id, inline_asm_ctx);
 
-  if (parser.skip_token(UNDERSCORE)) {
-// We are sure to be failing a test here, based on asm.rs 
-// 
https://github.com/rust-lang/rust/blob/a330e49593ee890f9197727a3a558b6e6b37f843/compiler/rustc_builtin_macros/src/asm.rs#L112
-rust_

[COMMITTED 088/145] gccrs: Parsing of options(...) done.

2025-03-17 Thread arthur . cohen
From: jjasmine 

This is without any mutually exclusive options checked, or
any relationship with reg_operands. Very primitive.

gcc/rust/ChangeLog:

* ast/rust-expr.h: parsing of options(...)
* expand/rust-macro-builtins-asm.cc (check_and_set):
likewise.
(parse_options): likewise.
(parseAsmArg): likewise.
* expand/rust-macro-builtins-asm.h (check_and_set):
likewise.

gcc/testsuite/ChangeLog:

* rust/compile/inline_asm_legal_options.rs: New test.
---
 gcc/rust/ast/rust-expr.h  |  2 +-
 gcc/rust/expand/rust-macro-builtins-asm.cc| 30 +--
 gcc/rust/expand/rust-macro-builtins-asm.h |  2 +-
 .../rust/compile/inline_asm_legal_options.rs  | 12 
 4 files changed, 28 insertions(+), 18 deletions(-)
 create mode 100644 gcc/testsuite/rust/compile/inline_asm_legal_options.rs

diff --git a/gcc/rust/ast/rust-expr.h b/gcc/rust/ast/rust-expr.h
index 03336cdcc59..719a76cdbb3 100644
--- a/gcc/rust/ast/rust-expr.h
+++ b/gcc/rust/ast/rust-expr.h
@@ -4843,7 +4843,7 @@ public:
   std::vector operands;
   std::vector clobber_abi;
   // std::set options;
-  std::set options;
+  std::set options;
 
   std::vector line_spans;
 
diff --git a/gcc/rust/expand/rust-macro-builtins-asm.cc 
b/gcc/rust/expand/rust-macro-builtins-asm.cc
index bcf8b6fb3f2..443c8a3bce3 100644
--- a/gcc/rust/expand/rust-macro-builtins-asm.cc
+++ b/gcc/rust/expand/rust-macro-builtins-asm.cc
@@ -116,7 +116,7 @@ parse_clobber_abi (Parser &parser, TokenId 
last_token_id,
 
 void
 check_and_set (Parser &p, AST::InlineAsm &inlineAsm,
-  std::string option)
+  AST::InlineAsmOptions option)
 {
   if (inlineAsm.options.count (option) == 1)
 {
@@ -148,43 +148,40 @@ parse_options (Parser &parser, TokenId 
last_token_id,
 {
   if (!is_global_asm && check_identifier (parser, "pure"))
{
- check_and_set (parser, inlineAsm, "pure");
+ check_and_set (parser, inlineAsm, AST::InlineAsmOptions::PURE);
}
   else if (!is_global_asm && check_identifier (parser, "nomem"))
{
- check_and_set (parser, inlineAsm, "nomem");
+ check_and_set (parser, inlineAsm, AST::InlineAsmOptions::NOMEM);
}
   else if (!is_global_asm && check_identifier (parser, "readonly"))
{
- check_and_set (parser, inlineAsm, "readonly");
+ check_and_set (parser, inlineAsm, AST::InlineAsmOptions::READONLY);
}
   else if (!is_global_asm && check_identifier (parser, "preserves_flags"))
{
- check_and_set (parser, inlineAsm, "preserves_flags");
+ check_and_set (parser, inlineAsm,
+AST::InlineAsmOptions::PRESERVES_FLAGS);
}
   else if (!is_global_asm && check_identifier (parser, "noreturn"))
{
- check_and_set (parser, inlineAsm, "noreturn");
-   }
-  else if (!is_global_asm && check_identifier (parser, "noreturn"))
-   {
- check_and_set (parser, inlineAsm, "noreturn");
+ check_and_set (parser, inlineAsm, AST::InlineAsmOptions::NORETURN);
}
   else if (!is_global_asm && check_identifier (parser, "nostack"))
{
- check_and_set (parser, inlineAsm, "nostack");
+ check_and_set (parser, inlineAsm, AST::InlineAsmOptions::NOSTACK);
}
   else if (!is_global_asm && check_identifier (parser, "may_unwind"))
{
- check_and_set (parser, inlineAsm, "may_unwind");
+ check_and_set (parser, inlineAsm, AST::InlineAsmOptions::MAY_UNWIND);
}
   else if (check_identifier (parser, "att_syntax"))
{
- check_and_set (parser, inlineAsm, "att_syntax");
+ check_and_set (parser, inlineAsm, AST::InlineAsmOptions::ATT_SYNTAX);
}
   else if (check_identifier (parser, "raw"))
{
- check_and_set (parser, inlineAsm, "raw");
+ check_and_set (parser, inlineAsm, AST::InlineAsmOptions::RAW);
}
   else
{
@@ -201,6 +198,7 @@ parse_options (Parser &parser, TokenId 
last_token_id,
{
  // TODO: If the skip of comma is unsuccessful, which should be
  // illegal, pleaes emit the correct error.
+ std::cout << "Illegal comma" << std::endl;
  return -1;
}
 
@@ -315,13 +313,13 @@ parseAsmArg (Parser &parser, TokenId 
last_token_id,
   // TODO: Parse options
   if (check_identifier (parser, "options"))
{
- std::cout << "Parse optoins" << std::endl;
+ parse_options (parser, last_token_id, inlineAsm);
  continue;
}
 
   // Ok after we have check that neither clobber_abi nor options works, the
   // only other logical choice is reg_operand
-  std::cout << "reg_operand" << std::endl;
+  // std::cout << "reg_operand" << std::endl;
   fm_string = parse_format_string (parser, last_token_id);
 }
   return 0;
diff --git a/gcc/rust/expand/rust-macro-bu

[COMMITTED 099/145] gccrs: Got AST::Fragment to be created from InlineAsm

2025-03-17 Thread arthur . cohen
From: jjasmine 

gcc/rust/ChangeLog:

* ast/rust-expr.h (struct AnonConst):
Got AST::Fragment to be created from InlineAsm.
(struct InlineAsmOperand): Likewise.
(class InlineAsm): Likewise.
* expand/rust-macro-builtins-asm.cc (parse_reg_operand): Likewise.
(parse_asm): likewise
---
 gcc/rust/ast/rust-expr.h   | 129 -
 gcc/rust/expand/rust-macro-builtins-asm.cc |  31 -
 2 files changed, 150 insertions(+), 10 deletions(-)

diff --git a/gcc/rust/ast/rust-expr.h b/gcc/rust/ast/rust-expr.h
index 76e5fa7c447..1284c7367fe 100644
--- a/gcc/rust/ast/rust-expr.h
+++ b/gcc/rust/ast/rust-expr.h
@@ -6,6 +6,7 @@
 #include "rust-path.h"
 #include "rust-macro.h"
 #include "rust-operators.h"
+#include 
 
 namespace Rust {
 namespace AST {
@@ -4723,6 +4724,23 @@ struct AnonConst
 {
   NodeId id;
   std::unique_ptr value;
+  AnonConst () {}
+  AnonConst (const AnonConst &other)
+  {
+id = other.id;
+value = other.value == nullptr
+ ? nullptr
+ : std::unique_ptr (other.value->clone_expr ());
+  }
+
+  AnonConst operator= (const AnonConst &other)
+  {
+id = other.id;
+value = other.value == nullptr
+ ? nullptr
+ : std::unique_ptr (other.value->clone_expr ());
+return *this;
+  }
 };
 
 struct InlineAsmRegOrRegClass
@@ -4767,6 +4785,25 @@ struct InlineAsmOperand
   {
 InlineAsmRegOrRegClass reg;
 std::unique_ptr expr;
+
+In () {}
+In (const struct In &other)
+{
+  reg = other.reg;
+  expr = other.expr == nullptr
+  ? nullptr
+  : std::unique_ptr (other.expr->clone_expr ());
+}
+
+In operator= (const struct In &other)
+{
+  reg = other.reg;
+  expr = other.expr == nullptr
+  ? nullptr
+  : std::unique_ptr (other.expr->clone_expr ());
+
+  return *this;
+}
   };
 
   struct Out
@@ -4774,6 +4811,26 @@ struct InlineAsmOperand
 InlineAsmRegOrRegClass reg;
 bool late;
 std::unique_ptr expr; // can be null
+
+Out () {}
+Out (const struct Out &other)
+{
+  reg = other.reg;
+  late = other.late;
+  expr = other.expr == nullptr
+  ? nullptr
+  : std::unique_ptr (other.expr->clone_expr ());
+}
+
+Out operator= (const struct Out &other)
+{
+  reg = other.reg;
+  late = other.late;
+  expr = other.expr == nullptr
+  ? nullptr
+  : std::unique_ptr (other.expr->clone_expr ());
+  return *this;
+}
   };
 
   struct InOut
@@ -4781,6 +4838,26 @@ struct InlineAsmOperand
 InlineAsmRegOrRegClass reg;
 bool late;
 std::unique_ptr expr; // this can't be null
+
+InOut () {}
+InOut (const struct InOut &other)
+{
+  reg = other.reg;
+  late = other.late;
+  expr = other.expr == nullptr
+  ? nullptr
+  : std::unique_ptr (other.expr->clone_expr ());
+}
+
+InOut operator= (const struct InOut &other)
+{
+  reg = other.reg;
+  late = other.late;
+  expr = other.expr == nullptr
+  ? nullptr
+  : std::unique_ptr (other.expr->clone_expr ());
+  return *this;
+}
   };
 
   struct SplitInOut
@@ -4789,6 +4866,33 @@ struct InlineAsmOperand
 bool late;
 std::unique_ptr in_expr;
 std::unique_ptr out_expr; // could be null
+
+SplitInOut () {}
+SplitInOut (const struct SplitInOut &other)
+{
+  reg = other.reg;
+  late = other.late;
+  in_expr = other.in_expr == nullptr
+ ? nullptr
+ : std::unique_ptr (other.in_expr->clone_expr ());
+  out_expr = other.out_expr == nullptr
+  ? nullptr
+  : std::unique_ptr (other.out_expr->clone_expr ());
+}
+
+SplitInOut operator= (const struct SplitInOut &other)
+{
+  reg = other.reg;
+  late = other.late;
+  in_expr = other.in_expr == nullptr
+ ? nullptr
+ : std::unique_ptr (other.in_expr->clone_expr ());
+  out_expr = other.out_expr == nullptr
+  ? nullptr
+  : std::unique_ptr (other.out_expr->clone_expr ());
+
+  return *this;
+}
   };
 
   struct Const
@@ -4799,6 +4903,18 @@ struct InlineAsmOperand
   struct Sym
   {
 std::unique_ptr sym;
+
+Sym () {}
+Sym (const struct Sym &other)
+{
+  sym = std::unique_ptr (other.sym->clone_expr ());
+}
+
+Sym operator= (const struct Sym &other)
+{
+  sym = std::unique_ptr (other.sym->clone_expr ());
+  return *this;
+}
   };
   RegisterType registerType;
 
@@ -4809,6 +4925,12 @@ struct InlineAsmOperand
   struct Const cnst;
   struct Sym sym;
 
+  InlineAsmOperand () {}
+  InlineAsmOperand (const InlineAsmOperand &other)
+: in (other.in), out (other.out), inOut (other.inOut),
+  splitInOut (other.splitInOut), cnst (other.cnst), sym (other.sym)
+  {}
+
   locati

[COMMITTED 093/145] gccrs: Rename InlineAsmOptions to InlineAsmOption

2025-03-17 Thread arthur . cohen
From: jjasmine 

Rename InlineAsmOptions to InlineAsmOption for clarity

gcc/rust/ChangeLog:

* ast/rust-ast-full-decls.h (enum class):
Rename InlineAsmOptions to InlineAsmOption for clarity
* ast/rust-expr.h (enum class): Likewise.
* expand/rust-macro-builtins-asm.cc (check_and_set): Likewise.
(parse_options): Likewise.
* expand/rust-macro-builtins-asm.h (check_and_set): Likewise.
* hir/tree/rust-hir-expr.h: Likewise.
---
 gcc/rust/ast/rust-ast-full-decls.h |  2 +-
 gcc/rust/ast/rust-expr.h   |  5 ++-
 gcc/rust/expand/rust-macro-builtins-asm.cc | 42 +++---
 gcc/rust/expand/rust-macro-builtins-asm.h  |  2 +-
 gcc/rust/hir/tree/rust-hir-expr.h  |  2 +-
 5 files changed, 26 insertions(+), 27 deletions(-)

diff --git a/gcc/rust/ast/rust-ast-full-decls.h 
b/gcc/rust/ast/rust-ast-full-decls.h
index 26377b790d4..d2ba87613e6 100644
--- a/gcc/rust/ast/rust-ast-full-decls.h
+++ b/gcc/rust/ast/rust-ast-full-decls.h
@@ -145,7 +145,7 @@ struct MatchCase;
 class MatchExpr;
 class AwaitExpr;
 class AsyncBlockExpr;
-enum class InlineAsmOptions;
+enum class InlineAsmOption;
 struct AnonConst;
 struct InlineAsmRegOrRegClass;
 struct InlineAsmOperand;
diff --git a/gcc/rust/ast/rust-expr.h b/gcc/rust/ast/rust-expr.h
index 309174057c0..bceef827dfb 100644
--- a/gcc/rust/ast/rust-expr.h
+++ b/gcc/rust/ast/rust-expr.h
@@ -4706,7 +4706,7 @@ protected:
 };
 
 // Inline-assembly specific options
-enum class InlineAsmOptions
+enum class InlineAsmOption
 {
   PURE = 1 << 0,
   NOMEM = 1 << 1,
@@ -4848,8 +4848,7 @@ public:
   std::vector template_strs;
   std::vector operands;
   std::vector clobber_abi;
-  // std::set options;
-  std::set options;
+  std::set options;
 
   std::vector line_spans;
 
diff --git a/gcc/rust/expand/rust-macro-builtins-asm.cc 
b/gcc/rust/expand/rust-macro-builtins-asm.cc
index f6940ffb339..1bfc6e9f456 100644
--- a/gcc/rust/expand/rust-macro-builtins-asm.cc
+++ b/gcc/rust/expand/rust-macro-builtins-asm.cc
@@ -19,16 +19,16 @@
 #include "rust-macro-builtins-asm.h"
 
 namespace Rust {
-std::map InlineAsmOptionsMap{
-  {AST::InlineAsmOptions::PURE, "pure"},
-  {AST::InlineAsmOptions::NOMEM, "nomem"},
-  {AST::InlineAsmOptions::READONLY, "readonly"},
-  {AST::InlineAsmOptions::PRESERVES_FLAGS, "preserves_flags"},
-  {AST::InlineAsmOptions::NORETURN, "noreturn"},
-  {AST::InlineAsmOptions::NOSTACK, "nostack"},
-  {AST::InlineAsmOptions::MAY_UNWIND, "may_unwind"},
-  {AST::InlineAsmOptions::ATT_SYNTAX, "att_syntax"},
-  {AST::InlineAsmOptions::RAW, "raw"},
+std::map InlineAsmOptionMap{
+  {AST::InlineAsmOption::PURE, "pure"},
+  {AST::InlineAsmOption::NOMEM, "nomem"},
+  {AST::InlineAsmOption::READONLY, "readonly"},
+  {AST::InlineAsmOption::PRESERVES_FLAGS, "preserves_flags"},
+  {AST::InlineAsmOption::NORETURN, "noreturn"},
+  {AST::InlineAsmOption::NOSTACK, "nostack"},
+  {AST::InlineAsmOption::MAY_UNWIND, "may_unwind"},
+  {AST::InlineAsmOption::ATT_SYNTAX, "att_syntax"},
+  {AST::InlineAsmOption::RAW, "raw"},
 };
 
 int
@@ -174,14 +174,14 @@ parse_operand (Parser &parser, TokenId 
last_token_id,
 
 void
 check_and_set (Parser &parser, AST::InlineAsm &inlineAsm,
-  AST::InlineAsmOptions option)
+  AST::InlineAsmOption option)
 {
   if (inlineAsm.options.count (option) != 0)
 {
   // TODO: report an error of duplication
   rust_error_at (parser.peek_current_token ()->get_locus (),
 "the `%s` option was already provided",
-InlineAsmOptionsMap[option].c_str ());
+InlineAsmOptionMap[option].c_str ());
   return;
 }
   else
@@ -208,40 +208,40 @@ parse_options (Parser &parser, TokenId 
last_token_id,
 {
   if (!is_global_asm && check_identifier (parser, "pure"))
{
- check_and_set (parser, inlineAsm, AST::InlineAsmOptions::PURE);
+ check_and_set (parser, inlineAsm, AST::InlineAsmOption::PURE);
}
   else if (!is_global_asm && check_identifier (parser, "nomem"))
{
- check_and_set (parser, inlineAsm, AST::InlineAsmOptions::NOMEM);
+ check_and_set (parser, inlineAsm, AST::InlineAsmOption::NOMEM);
}
   else if (!is_global_asm && check_identifier (parser, "readonly"))
{
- check_and_set (parser, inlineAsm, AST::InlineAsmOptions::READONLY);
+ check_and_set (parser, inlineAsm, AST::InlineAsmOption::READONLY);
}
   else if (!is_global_asm && check_identifier (parser, "preserves_flags"))
{
  check_and_set (parser, inlineAsm,
-AST::InlineAsmOptions::PRESERVES_FLAGS);
+AST::InlineAsmOption::PRESERVES_FLAGS);
}
   else if (!is_global_asm && check_identifier (parser, "noreturn"))
{
- check_and_set (parser, inlineAsm, AST::InlineAsmOptions::NORETURN);
+ check_and_set (parser, inlineAsm, AST::InlineAsmOption::NORETURN

[COMMITTED 116/145] gccrs: Partial second layer of expected in parsing asm

2025-03-17 Thread arthur . cohen
From: jjasmine 

gcc/rust/ChangeLog:

* expand/rust-macro-builtins-asm.cc (parseDirSpec):
Partial second layer of expected in parsing asm
(parse_clobber_abi): Likewise
(parse_operand): Likewise
(parse_reg_operand): Likewise
(parse_asm_arg): Likewise
* expand/rust-macro-builtins-asm.h (parse_clobber_abi): Likewise
(parse_reg_operand): Likewise
(parse_operand): Likewise
---
 gcc/rust/expand/rust-macro-builtins-asm.cc | 81 --
 gcc/rust/expand/rust-macro-builtins-asm.h  | 25 ++-
 2 files changed, 37 insertions(+), 69 deletions(-)

diff --git a/gcc/rust/expand/rust-macro-builtins-asm.cc 
b/gcc/rust/expand/rust-macro-builtins-asm.cc
index 49d1fd4014f..ec17d9ad05a 100644
--- a/gcc/rust/expand/rust-macro-builtins-asm.cc
+++ b/gcc/rust/expand/rust-macro-builtins-asm.cc
@@ -31,14 +31,8 @@ std::map 
InlineAsmOptionMap{
   {AST::InlineAsmOption::RAW, "raw"},
 };
 
-int
-parseDirSpec (Parser &parser, TokenId last_token_id)
-{
-  return 0;
-}
-
-int
-parse_clobber_abi (InlineAsmContext &inline_asm_ctx)
+tl::expected
+parse_clobber_abi (InlineAsmContext inline_asm_ctx)
 {
   // clobber_abi := "clobber_abi("  *("," ) [","] ")"
   // PARSE EVERYTHING COMMITTEDLY IN THIS FUNCTION, WE CONFIRMED VIA 
clobber_abi
@@ -59,14 +53,16 @@ parse_clobber_abi (InlineAsmContext &inline_asm_ctx)
{
  rust_error_at (token->get_locus (),
 "expected %<(%>, found end of macro arguments");
- return -1;
+ return tl::unexpected (
+   "Expected something else instead of end of macro arguments");
}
   else
{
  rust_error_at (token->get_locus (), "expected %<(%>, found %qs",
 token->get_token_description ());
}
-  return -1;
+  return tl::unexpected (
+   "Expected left parenthesis instead of something else");
 }
 
   if (parser.skip_token (RIGHT_PAREN))
@@ -76,7 +72,8 @@ parse_clobber_abi (InlineAsmContext &inline_asm_ctx)
   rust_error_at (
parser.peek_current_token ()->get_locus (),
"at least one abi must be provided as an argument to %");
-  return -1;
+  return tl::unexpected (
+   "at least one abi must be provided as an argument to %");
 }
 
   std::vector new_abis;
@@ -109,7 +106,7 @@ parse_clobber_abi (InlineAsmContext &inline_asm_ctx)
  // TODO: If the skip of comma is unsuccessful, which should be
  // illegal, pleaes emit the correct error.
  rust_unreachable ();
- return -1;
+ return tl::unexpected ("SKIP OF COMMA UNSUCCESSFUL");
}
 
   token = parser.peek_current_token ();
@@ -123,7 +120,7 @@ parse_clobber_abi (InlineAsmContext &inline_asm_ctx)
   inline_asm.clobber_abi.push_back (abi);
 }
 
-  return 0;
+  return inline_asm_ctx;
 }
 
 tl::optional
@@ -183,15 +180,9 @@ parse_reg (InlineAsmContext &inline_asm_ctx)
   return reg_class;
 }
 
-int
-parse_operand (InlineAsmContext &inline_asm_ctx)
-{
-  return 0;
-}
-
 // From rustc
-tl::optional
-parse_reg_operand (InlineAsmContext &inline_asm_ctx)
+tl::expected
+parse_reg_operand (InlineAsmContext inline_asm_ctx)
 {
   // let name = if p.token.is_ident() && p.look_ahead(1, |t| *t == token::Eq) {
   //   let (ident, _) = p.token.ident().unwrap();
@@ -204,15 +195,11 @@ parse_reg_operand (InlineAsmContext &inline_asm_ctx)
   //   };
   auto &parser = inline_asm_ctx.parser;
   AST::InlineAsmOperand reg_operand;
-  rust_debug ("Enter parse_reg_operand");
   auto token = parser.peek_current_token ();
   auto iden_token = parser.peek_current_token ();
   auto &inline_asm = inline_asm_ctx.inline_asm;
   if (check_identifier (parser, ""))
 {
-  rust_debug ("Didn't get passed identifier checking, %s",
- token->as_string ().c_str ());
-
   auto equal_token = parser.peek_current_token ();
   if (!parser.skip_token (EQUAL))
{
@@ -227,8 +214,6 @@ parse_reg_operand (InlineAsmContext &inline_asm_ctx)
   // Rust::IN search for #define RS_TOKEN_LIST in code base.
   if (!is_global_asm && parser.skip_token (IN))
 {
-  rust_debug ("Enter parse_reg_operand in");
-
   auto reg = parse_reg (inline_asm_ctx);
 
   if (parser.skip_token (UNDERSCORE))
@@ -244,13 +229,11 @@ parse_reg_operand (InlineAsmContext &inline_asm_ctx)
   // instead of nullptr
   struct AST::InlineAsmOperand::In in (reg, nullptr);
   reg_operand.set_in (in);
-
-  return reg_operand;
+  inline_asm_ctx.inline_asm.operands.push_back (reg_operand);
+  return inline_asm_ctx;
 }
   else if (!is_global_asm && check_identifier (parser, "out"))
 {
-  rust_debug ("Enter parse_reg_operand out");
-
   auto reg = parse_reg (inline_asm_ctx);
 
   auto expr = parse_format_string (inline_asm_ctx);
@@ -259,18 +242,17 @@ parse_reg_operand (InlineAsmContext &inline_asm_ctx)
   // instead of nullptr
   struct AST::Inl

[COMMITTED 080/145] gccrs: Introduced is_global_asm to InlineAsm AST

2025-03-17 Thread arthur . cohen
From: jjasmine 

gcc/rust/ChangeLog:

* ast/rust-expr.h: Introduced is_global_asm to InlineAsm AST
---
 gcc/rust/ast/rust-expr.h | 1 +
 1 file changed, 1 insertion(+)

diff --git a/gcc/rust/ast/rust-expr.h b/gcc/rust/ast/rust-expr.h
index a5afbffee99..84fb5e8ab33 100644
--- a/gcc/rust/ast/rust-expr.h
+++ b/gcc/rust/ast/rust-expr.h
@@ -4841,6 +4841,7 @@ public:
   TupleClobber clobber_abi;
   InlineAsmOptions options;
   std::vector line_spans;
+  bool is_global_asm;
 };
 
 } // namespace AST
-- 
2.45.2



[COMMITTED 104/145] gccrs: Fix warnings

2025-03-17 Thread arthur . cohen
From: jjasmine 

gcc/rust/ChangeLog:

* expand/rust-macro-builtins-asm.cc (parse_reg_operand):
Fix compile warnings.
(parse_options): Likewise.
(parse_asm): Likewise.

gcc/testsuite/ChangeLog:

* rust/compile/inline_asm_illegal_options.rs:
---
 gcc/rust/expand/rust-macro-builtins-asm.cc | 10 +++---
 .../rust/compile/inline_asm_illegal_options.rs |  3 +++
 2 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/gcc/rust/expand/rust-macro-builtins-asm.cc 
b/gcc/rust/expand/rust-macro-builtins-asm.cc
index dd053df04b2..95a268a02de 100644
--- a/gcc/rust/expand/rust-macro-builtins-asm.cc
+++ b/gcc/rust/expand/rust-macro-builtins-asm.cc
@@ -219,7 +219,6 @@ parse_reg_operand (Parser &parser, TokenId 
last_token_id,
 
   token = parser.peek_current_token ();
 
-  bool is_explicit_reg = false;
   bool is_global_asm = inline_asm.is_global_asm;
   if (!is_global_asm && check_identifier (parser, "in"))
 {
@@ -354,7 +353,12 @@ parse_options (Parser &parser, TokenId 
last_token_id,
{
  // TODO: Unexpected error, please return the correct error
  rust_error_at (token->get_locus (),
-"Unexpected token encountered in parse_options");
+"expected one of %qs, %qs, %qs, %qs, %qs, %qs, %qs, "
+"%qs, %qs, or %qs, found %qs",
+")", "att_syntax", "may_unwind", "nomem", "noreturn",
+"nostack", "preserves_flags", "pure", "raw",
+"readonly", token->as_string ().c_str ());
+ return -1;
}
   if (parser.skip_token (RIGHT_PAREN))
{
@@ -519,7 +523,7 @@ parse_asm (location_t invoc_locus, AST::MacroInvocData 
&invoc,
   if (fm_string == tl::nullopt)
 {
   rust_error_at (parser.peek_current_token ()->get_locus (),
-"asm template must be a string literal");
+"%s template must be a string literal", "asm");
   return tl::nullopt;
 }
 
diff --git a/gcc/testsuite/rust/compile/inline_asm_illegal_options.rs 
b/gcc/testsuite/rust/compile/inline_asm_illegal_options.rs
index bf34c4489f1..2cf354d6d12 100644
--- a/gcc/testsuite/rust/compile/inline_asm_illegal_options.rs
+++ b/gcc/testsuite/rust/compile/inline_asm_illegal_options.rs
@@ -9,5 +9,8 @@ fn main() {
 unsafe {
 asm!("nop", options(nomem, nomem)); // { dg-error "the 'nomem' option 
was already provided" }
 asm!("nop", options(noreturn, noreturn)); // { dg-error "the 
'noreturn' option was already provided" }
+asm!("nop", options(xxx)); // { dg-error "expected one of '\\)', 
'att_syntax', 'may_unwind', 'nomem', 'noreturn', 'nostack', 'preserves_flags', 
'pure', 'raw', or 'readonly', found 'xxx'" }
+asm!("nop", options(+)); // { dg-error "expected one of '\\)', 
'att_syntax', 'may_unwind', 'nomem', 'noreturn', 'nostack', 'preserves_flags', 
'pure', 'raw', or 'readonly', found '\\+'" }
+
 }
 }
\ No newline at end of file
-- 
2.45.2



[COMMITTED 135/145] gccrs: Added FFIVector to get Polonius output on C++ side

2025-03-17 Thread arthur . cohen
From: Kushal Pal 

gcc/rust/ChangeLog:

* Make-lang.in: Compile new file, rust-polonius.cc
* checks/errors/borrowck/ffi-polonius/src/gccrs_ffi.rs: Opaque
type to represent FFIVector from C++.
* checks/errors/borrowck/ffi-polonius/src/gccrs_ffi_generated.rs:
Change types of fields in Output.
* checks/errors/borrowck/ffi-polonius/src/lib.rs: Added helper
functions to contruct Polonius output on C++ side,
used helpers to contruct Polonius output on C++ side.
* checks/errors/borrowck/polonius/rust-polonius-ffi.h (make_vector):
FFIVector is a wrapper around std::vector for transfering data
from Rust to C++.
(struct Output): Use pointers to FFIVector instead of bool to
store Polonius output data.
* checks/errors/borrowck/polonius/rust-polonius.h (FFIVector__new):
Helper function.
(FFIVector__new_vec_pair): Likewise.
(FFIVector__new_vec_triple): Likewise.
(FFIVector__push): Likewise.
(FFIVector__push_vec_pair): Likewise.
(FFIVector__push_vec_triple): Likewise.
* checks/errors/borrowck/rust-borrow-checker.cc (BorrowChecker::go):
Convert FFIVector to std::vector representation for easier
navigation.
* checks/errors/borrowck/polonius/rust-polonius.cc: New file,
implementation of helper functions.

Signed-off-by: Kushal Pal 
---
 gcc/rust/Make-lang.in |  7 ++
 .../borrowck/ffi-polonius/src/gccrs_ffi.rs|  5 ++
 .../ffi-polonius/src/gccrs_ffi_generated.rs   |  6 +-
 .../errors/borrowck/ffi-polonius/src/lib.rs   | 76 ++-
 .../borrowck/polonius/rust-polonius-ffi.h | 71 -
 .../errors/borrowck/polonius/rust-polonius.cc | 66 
 .../errors/borrowck/polonius/rust-polonius.h  | 24 ++
 .../errors/borrowck/rust-borrow-checker.cc| 16 +++-
 8 files changed, 258 insertions(+), 13 deletions(-)
 create mode 100644 gcc/rust/checks/errors/borrowck/polonius/rust-polonius.cc

diff --git a/gcc/rust/Make-lang.in b/gcc/rust/Make-lang.in
index 4f164c9753a..17f1feb6e46 100644
--- a/gcc/rust/Make-lang.in
+++ b/gcc/rust/Make-lang.in
@@ -171,6 +171,7 @@ GRS_OBJS = \
 rust/rust-borrow-checker.o \
 rust/rust-bir-builder-expr-stmt.o \
 rust/rust-bir-dump.o \
+rust/rust-polonius.o\
 rust/rust-hir-dot-operator.o \
 rust/rust-hir-path-probe.o \
 rust/rust-type-util.o \
@@ -487,6 +488,12 @@ rust/%.o: rust/checks/errors/borrowck/%.cc
$(COMPILE) $(RUST_CXXFLAGS) $(RUST_INCLUDES) $<
$(POSTCOMPILE)
 
+
+# build borrow checking pass polonius files in rust folder
+rust/%.o: rust/checks/errors/borrowck/polonius/%.cc
+   $(COMPILE) $(RUST_CXXFLAGS) $(RUST_INCLUDES) $<
+   $(POSTCOMPILE)
+
 # build rust/metadata files in rust folder
 rust/%.o: rust/metadata/%.cc
$(COMPILE) $(RUST_CXXFLAGS) $(RUST_INCLUDES) $<
diff --git a/gcc/rust/checks/errors/borrowck/ffi-polonius/src/gccrs_ffi.rs 
b/gcc/rust/checks/errors/borrowck/ffi-polonius/src/gccrs_ffi.rs
index 6d30d3c5cfb..0cb85078158 100644
--- a/gcc/rust/checks/errors/borrowck/ffi-polonius/src/gccrs_ffi.rs
+++ b/gcc/rust/checks/errors/borrowck/ffi-polonius/src/gccrs_ffi.rs
@@ -32,6 +32,11 @@ include!("gccrs_ffi_generated.rs");
 
 use crate::GccrsAtom;
 
+// Using opqaue types
+extern "C" {
+pub type FFIVector;
+}
+
 impl Into<(GccrsAtom, GccrsAtom)> for Pair
 where
 GccrsAtom: From + From,
diff --git 
a/gcc/rust/checks/errors/borrowck/ffi-polonius/src/gccrs_ffi_generated.rs 
b/gcc/rust/checks/errors/borrowck/ffi-polonius/src/gccrs_ffi_generated.rs
index db75a1d1509..b2c190d386a 100644
--- a/gcc/rust/checks/errors/borrowck/ffi-polonius/src/gccrs_ffi_generated.rs
+++ b/gcc/rust/checks/errors/borrowck/ffi-polonius/src/gccrs_ffi_generated.rs
@@ -52,7 +52,7 @@ pub struct FactsView {
 #[repr(C)]
 #[derive(Debug, Copy, Clone)]
 pub struct Output {
-pub loan_errors: bool,
-pub subset_errors: bool,
-pub move_errors: bool,
+pub loan_errors: *mut FFIVector,
+pub move_errors: *mut FFIVector,
+pub subset_errors: *mut FFIVector,
 }
diff --git a/gcc/rust/checks/errors/borrowck/ffi-polonius/src/lib.rs 
b/gcc/rust/checks/errors/borrowck/ffi-polonius/src/lib.rs
index b743be2e6be..782a63f8078 100644
--- a/gcc/rust/checks/errors/borrowck/ffi-polonius/src/lib.rs
+++ b/gcc/rust/checks/errors/borrowck/ffi-polonius/src/lib.rs
@@ -16,13 +16,30 @@
 // along with GCC; see the file COPYING3.  If not see
 // .
 
+#![feature(extern_types)]
+
 mod gccrs_ffi;
-mod gccrs_ffi_generated;
 
+use gccrs_ffi::FFIVector;
 use polonius_engine::{AllFacts, Atom, FactTypes, Output};
 use std::fmt::Debug;
 use std::hash::Hash;
 
+extern "C" {
+fn FFIVector__new() -> *mut FFIVector;
+fn FFIVector__new_vec_pair() -> *mut FFIVector;
+fn FFIVector__new_vec_triple() -> *mut FFIVector;
+fn FFIVector__push(vector: *mut FFIVector, element: usize);
+   

[COMMITTED 143/145] gccrs: Fix ffi and enum conventions

2025-03-17 Thread arthur . cohen
From: badumbatish 

gcc/rust/ChangeLog:

* ast/rust-fmt.h (enum ParseMode):
Drop typedef in Cpp

libgrust/ChangeLog:

* libformat_parser/generic_format_parser/src/lib.rs:
Remove repr(C)
* libformat_parser/src/bin.rs: Use ffi
* libformat_parser/src/lib.rs: pub ffi, create ParseMode and match
rustc's parse mode
---
 gcc/rust/ast/rust-fmt.h   |  6 +--
 .../generic_format_parser/src/lib.rs  |  1 -
 libgrust/libformat_parser/src/bin.rs  |  2 +-
 libgrust/libformat_parser/src/lib.rs  | 43 ++-
 4 files changed, 27 insertions(+), 25 deletions(-)

diff --git a/gcc/rust/ast/rust-fmt.h b/gcc/rust/ast/rust-fmt.h
index 1db391bafe7..a54faec6381 100644
--- a/gcc/rust/ast/rust-fmt.h
+++ b/gcc/rust/ast/rust-fmt.h
@@ -258,11 +258,11 @@ struct FormatArgsHandle
   RustString rust_string;
 };
 
-typedef enum
+enum ParseMode
 {
-  Format,
+  Format = 0,
   InlineAsm,
-} ParseMode;
+};
 
 extern "C" {
 
diff --git a/libgrust/libformat_parser/generic_format_parser/src/lib.rs 
b/libgrust/libformat_parser/generic_format_parser/src/lib.rs
index ad4d3d9a546..25f6b0ead17 100644
--- a/libgrust/libformat_parser/generic_format_parser/src/lib.rs
+++ b/libgrust/libformat_parser/generic_format_parser/src/lib.rs
@@ -78,7 +78,6 @@ enum InputStringKind {
 }
 
 /// The type of format string that we are parsing.
-#[repr(C)]
 #[derive(Copy, Clone, Debug, Eq, PartialEq)]
 pub enum ParseMode {
 /// A normal format string as per `format_args!`.
diff --git a/libgrust/libformat_parser/src/bin.rs 
b/libgrust/libformat_parser/src/bin.rs
index a7947afb11c..a48d0066bf2 100644
--- a/libgrust/libformat_parser/src/bin.rs
+++ b/libgrust/libformat_parser/src/bin.rs
@@ -6,6 +6,6 @@ fn main() {
 None,
 None,
 false,
-generic_format_parser::ParseMode::Format
+libformat_parser::ffi::ParseMode::Format,
 ));
 }
diff --git a/libgrust/libformat_parser/src/lib.rs 
b/libgrust/libformat_parser/src/lib.rs
index 42ad62892bd..d920cfaa63d 100644
--- a/libgrust/libformat_parser/src/lib.rs
+++ b/libgrust/libformat_parser/src/lib.rs
@@ -24,7 +24,7 @@ where
 // FIXME: Make an ffi module in a separate file
 // FIXME: Remember to leak the boxed type somehow
 // FIXME: How to encode the Option type? As a pointer? Option -> Option<&T> 
-> *const T could work maybe?
-mod ffi {
+pub mod ffi {
 use super::IntoFFI;
 
 // FIXME: We need to ensure we deal with memory properly - whether it's 
owned by the C++ side or the Rust side
@@ -79,14 +79,14 @@ mod ffi {
 
 // TODO: Not needed for now?
 // /// The type of format string that we are parsing.
-// #[derive(Copy, Clone, Debug, Eq, PartialEq)]
-// #[repr(C)]
-// pub enum ParseMode {
-// /// A normal format string as per `format_args!`.
-// Format,
-// /// An inline assembly template string for `asm!`.
-// InlineAsm,
-// }
+#[derive(Copy, Clone, Debug, Eq, PartialEq)]
+#[repr(C)]
+pub enum ParseMode {
+/// A normal format string as per `format_args!`.
+Format = 0,
+/// An inline assembly template string for `asm!`.
+InlineAsm,
+}
 
 /// A piece is a portion of the format string which represents the next 
part
 /// to emit. These are emitted as a stream by the `Parser` class.
@@ -327,17 +327,20 @@ mod ffi {
 
 // FIXME: Rename?
 pub mod rust {
-use generic_format_parser::{ParseMode, Parser, Piece};
-
+use crate::ffi::ParseMode;
+use generic_format_parser::{Parser, Piece};
 pub fn collect_pieces(
 input: &str,
 style: Option,
 snippet: Option,
 append_newline: bool,
-parse_mode: ParseMode
+parse_mode: ParseMode,
 ) -> Vec> {
-let parser = Parser::new(input, style, snippet, append_newline, 
parse_mode);
-
+let converted_parse_mode = match parse_mode {
+ParseMode::Format => generic_format_parser::ParseMode::Format,
+ParseMode::InlineAsm => 
generic_format_parser::ParseMode::InlineAsm,
+};
+let parser = Parser::new(input, style, snippet, append_newline, 
converted_parse_mode);
 parser.into_iter().collect()
 }
 }
@@ -361,12 +364,11 @@ pub struct RustString {
 #[repr(C)]
 pub struct FormatArgsHandle(PieceSlice, RustString);
 
-
 #[no_mangle]
 pub extern "C" fn collect_pieces(
 input: *const libc::c_char,
 append_newline: bool,
-parse_mode : generic_format_parser::ParseMode 
+parse_mode: crate::ffi::ParseMode,
 ) -> FormatArgsHandle {
 // FIXME: Add comment
 let str = unsafe { CStr::from_ptr(input) };
@@ -379,10 +381,11 @@ pub extern "C" fn collect_pieces(
 let s = unsafe { std::mem::transmute::<&'_ str, &'static str>(s) };
 
 // FIXME: No unwrap
-let pieces: Vec> = rust::collect_pieces(s, None, None, 
append_newline, parse_mode)
-.into_iter()
-.map(Into::into)
-.collect();
+  

[COMMITTED 084/145] gccrs: Top level parsing test for asm!

2025-03-17 Thread arthur . cohen
From: jjasmine 

gcc/testsuite/ChangeLog:

* rust/compile/inline_asm_ident_first.rs: New test.
---
 gcc/testsuite/rust/compile/inline_asm_ident_first.rs | 10 ++
 1 file changed, 10 insertions(+)
 create mode 100644 gcc/testsuite/rust/compile/inline_asm_ident_first.rs

diff --git a/gcc/testsuite/rust/compile/inline_asm_ident_first.rs 
b/gcc/testsuite/rust/compile/inline_asm_ident_first.rs
new file mode 100644
index 000..9a4eb7ee402
--- /dev/null
+++ b/gcc/testsuite/rust/compile/inline_asm_ident_first.rs
@@ -0,0 +1,10 @@
+#![feature(rustc_attrs)]
+
+#[rustc_builtin_macro]
+macro_rules! asm {
+() => {}
+}
+
+fn main() {
+asm!(i_am_a_dummy); // { dg-error "asm template must be a string literal" }
+}
\ No newline at end of file
-- 
2.45.2



[COMMITTED 089/145] gccrs: Wraps inline_asm tests in unsafe {}

2025-03-17 Thread arthur . cohen
From: jjasmine 

gcc/testsuite/ChangeLog:

* rust/compile/inline_asm_faulty_clobber.rs:
Wraps inline_asm tests in unsafe {}
* rust/compile/inline_asm_faulty_clobber_1.rs: likewise.
* rust/compile/inline_asm_faulty_clobber_2.rs: likewise.
* rust/compile/inline_asm_ident_first.rs: likewise.
* rust/compile/inline_asm_nop.rs: likewise.
* rust/compile/inline_asm_nop_2.rs: likewise.
---
 gcc/testsuite/rust/compile/inline_asm_faulty_clobber.rs   | 4 +++-
 gcc/testsuite/rust/compile/inline_asm_faulty_clobber_1.rs | 4 +++-
 gcc/testsuite/rust/compile/inline_asm_faulty_clobber_2.rs | 4 +++-
 gcc/testsuite/rust/compile/inline_asm_ident_first.rs  | 4 +++-
 gcc/testsuite/rust/compile/inline_asm_nop.rs  | 4 +++-
 gcc/testsuite/rust/compile/inline_asm_nop_2.rs| 4 +++-
 6 files changed, 18 insertions(+), 6 deletions(-)

diff --git a/gcc/testsuite/rust/compile/inline_asm_faulty_clobber.rs 
b/gcc/testsuite/rust/compile/inline_asm_faulty_clobber.rs
index 8d040ea40b4..67dc10bd75b 100644
--- a/gcc/testsuite/rust/compile/inline_asm_faulty_clobber.rs
+++ b/gcc/testsuite/rust/compile/inline_asm_faulty_clobber.rs
@@ -6,5 +6,7 @@ macro_rules! asm {
 }
 
 fn main() {
-asm!("nop", clobber_abi());  // { dg-error "at least one abi must be 
provided as an argument to `clobber_abi`" }
+unsafe {
+asm!("nop", clobber_abi());  // { dg-error "at least one abi must be 
provided as an argument to `clobber_abi`" }
+}
 }
\ No newline at end of file
diff --git a/gcc/testsuite/rust/compile/inline_asm_faulty_clobber_1.rs 
b/gcc/testsuite/rust/compile/inline_asm_faulty_clobber_1.rs
index 77af10177c4..2906ea4292a 100644
--- a/gcc/testsuite/rust/compile/inline_asm_faulty_clobber_1.rs
+++ b/gcc/testsuite/rust/compile/inline_asm_faulty_clobber_1.rs
@@ -6,5 +6,7 @@ macro_rules! asm {
 }
 
 fn main() {
-asm!("nop", clobber_abi);  // { dg-error "expected `\\(`, found end of 
macro arguments" }
+unsafe {
+asm!("nop", clobber_abi);  // { dg-error "expected `\\(`, found end of 
macro arguments" }
+}
 }
\ No newline at end of file
diff --git a/gcc/testsuite/rust/compile/inline_asm_faulty_clobber_2.rs 
b/gcc/testsuite/rust/compile/inline_asm_faulty_clobber_2.rs
index ae3607ffa77..e5bf1d1f7f6 100644
--- a/gcc/testsuite/rust/compile/inline_asm_faulty_clobber_2.rs
+++ b/gcc/testsuite/rust/compile/inline_asm_faulty_clobber_2.rs
@@ -6,5 +6,7 @@ macro_rules! asm {
 }
 
 fn main() {
-asm!("nop", clobber_abi+);  // { dg-error "expected `\\(`, found `\\+`" }
+unsafe {
+asm!("nop", clobber_abi+);  // { dg-error "expected `\\(`, found 
`\\+`" }
+}
 }
\ No newline at end of file
diff --git a/gcc/testsuite/rust/compile/inline_asm_ident_first.rs 
b/gcc/testsuite/rust/compile/inline_asm_ident_first.rs
index 9a4eb7ee402..a425b8e5ad4 100644
--- a/gcc/testsuite/rust/compile/inline_asm_ident_first.rs
+++ b/gcc/testsuite/rust/compile/inline_asm_ident_first.rs
@@ -6,5 +6,7 @@ macro_rules! asm {
 }
 
 fn main() {
-asm!(i_am_a_dummy); // { dg-error "asm template must be a string literal" }
+unsafe {
+asm!(i_am_a_dummy); // { dg-error "asm template must be a string 
literal" }
+}
 }
\ No newline at end of file
diff --git a/gcc/testsuite/rust/compile/inline_asm_nop.rs 
b/gcc/testsuite/rust/compile/inline_asm_nop.rs
index ffe3161cd73..7da9bef3e56 100644
--- a/gcc/testsuite/rust/compile/inline_asm_nop.rs
+++ b/gcc/testsuite/rust/compile/inline_asm_nop.rs
@@ -6,5 +6,7 @@ macro_rules! asm {
 }
 
 fn main() {
-asm!("nop");
+unsafe {
+asm!("nop");
+}
 }
diff --git a/gcc/testsuite/rust/compile/inline_asm_nop_2.rs 
b/gcc/testsuite/rust/compile/inline_asm_nop_2.rs
index 8437e8fc66c..76f53fadbe3 100644
--- a/gcc/testsuite/rust/compile/inline_asm_nop_2.rs
+++ b/gcc/testsuite/rust/compile/inline_asm_nop_2.rs
@@ -6,5 +6,7 @@ macro_rules! asm {
 }
 
 fn main() {
-asm!("nop",);
+unsafe {
+asm!("nop",);
+}
 }
-- 
2.45.2



[COMMITTED 091/145] gccrs: Resolve static decl warning

2025-03-17 Thread arthur . cohen
From: jjasmine 

gcc/rust/ChangeLog:

* expand/rust-macro-builtins-asm.h (parseAsmArg):
Resolve static decl warning
(parse_nonglobal_asm):
Resolve static decl warning
---
 gcc/rust/expand/rust-macro-builtins-asm.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/gcc/rust/expand/rust-macro-builtins-asm.h 
b/gcc/rust/expand/rust-macro-builtins-asm.h
index 163ad161b5a..469dc5ae906 100644
--- a/gcc/rust/expand/rust-macro-builtins-asm.h
+++ b/gcc/rust/expand/rust-macro-builtins-asm.h
@@ -11,11 +11,11 @@ int
 parseAsmArg (Parser &p, TokenId last_token_id,
 AST::InlineAsm &inlineAsm,
 bool consumed_comma_without_formatted_string);
-static tl::optional
+tl::optional
 parse_global_asm (location_t invoc_locus, AST::MacroInvocData &invoc);
-static tl::optional
+tl::optional
 parse_nonglobal_asm (location_t invoc_locus, AST::MacroInvocData &invoc);
-static tl::optional
+tl::optional
 parse_asm (location_t invoc_locus, AST::MacroInvocData &invoc,
   bool is_global_asm);
 
-- 
2.45.2



[COMMITTED 081/145] gccrs: Make InlineAsm non-abstract for usage in parsing.

2025-03-17 Thread arthur . cohen
From: jjasmine 

gcc/rust/ChangeLog:

* ast/rust-expr.h: Make InlineAsm non-abstract for usage in parsing.
---
 gcc/rust/ast/rust-expr.h | 44 
 1 file changed, 36 insertions(+), 8 deletions(-)

diff --git a/gcc/rust/ast/rust-expr.h b/gcc/rust/ast/rust-expr.h
index 84fb5e8ab33..03336cdcc59 100644
--- a/gcc/rust/ast/rust-expr.h
+++ b/gcc/rust/ast/rust-expr.h
@@ -4809,11 +4809,8 @@ struct InlineAsmPlaceHolder
 struct InlineAsmTemplatePiece
 {
   bool is_placeholder;
-  union
-  {
-std::string string;
-InlineAsmPlaceHolder placeholder;
-  };
+  std::string string;
+  InlineAsmPlaceHolder placeholder;
 };
 
 struct TupleClobber
@@ -4832,16 +4829,47 @@ struct TupleTemplateStr
 };
 
 // Inline Assembly Node
-class InlineAsm : public ExprWithoutBlock
+class InlineAsm : private ExprWithoutBlock
 {
+private:
+  location_t locus;
+  // TODO: Not sure how outer_attrs plays with InlineAsm, I put it here in 
order
+  // to override, very hacky.
+  std::vector outer_attrs;
+
 public:
   std::vector template_;
   std::vector template_strs;
   std::vector operands;
-  TupleClobber clobber_abi;
-  InlineAsmOptions options;
+  std::vector clobber_abi;
+  // std::set options;
+  std::set options;
+
   std::vector line_spans;
+
   bool is_global_asm;
+
+  InlineAsm (location_t locus, bool is_global_asm)
+: locus (locus), is_global_asm (is_global_asm)
+  {}
+  void accept_vis (ASTVisitor &vis) override{};
+
+  std::string as_string () const override { return "InlineAsm AST Node"; }
+
+  location_t get_locus () const override { return locus; }
+
+  void mark_for_strip () override {}
+
+  bool is_marked_for_strip () const override { return false; }
+
+  std::vector &get_outer_attrs () override { return outer_attrs; }
+
+  void set_outer_attrs (std::vector v) override { outer_attrs = v; }
+
+  ExprWithoutBlock *clone_expr_without_block_impl () const override
+  {
+return nullptr;
+  }
 };
 
 } // namespace AST
-- 
2.45.2



[COMMITTED 121/145] gccrs: Added ExprType::InlineAsm

2025-03-17 Thread arthur . cohen
From: jjasmine 

gcc/rust/ChangeLog:

* hir/tree/rust-hir-expr.h:
Added ExprType::InlineAsm
* hir/tree/rust-hir.h:
Added ExprType::InlineAsm
---
 gcc/rust/hir/tree/rust-hir-expr.h | 3 +--
 gcc/rust/hir/tree/rust-hir.h  | 1 +
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/gcc/rust/hir/tree/rust-hir-expr.h 
b/gcc/rust/hir/tree/rust-hir-expr.h
index 9c66f3e79d5..d8cfe68bbc5 100644
--- a/gcc/rust/hir/tree/rust-hir-expr.h
+++ b/gcc/rust/hir/tree/rust-hir-expr.h
@@ -3893,8 +3893,7 @@ public:
 
   ExprType get_expression_type () const final override
   {
-// TODO: Not sure if this expression type is UnsafeBlock or not.
-return ExprType::UnsafeBlock;
+return ExprType::InlineAsm;
   }
   std::vector get_template_ ()
   {
diff --git a/gcc/rust/hir/tree/rust-hir.h b/gcc/rust/hir/tree/rust-hir.h
index a0f5d8145ec..d7245d4a4a8 100644
--- a/gcc/rust/hir/tree/rust-hir.h
+++ b/gcc/rust/hir/tree/rust-hir.h
@@ -303,6 +303,7 @@ public:
 Await,
 AsyncBlock,
 Path,
+InlineAsm,
   };
 
   BaseKind get_hir_kind () override final { return EXPR; }
-- 
2.45.2



[COMMITTED 120/145] gccrs: Add test case for using asm! outside of unsafe {}

2025-03-17 Thread arthur . cohen
From: jjasmine 

gcc/testsuite/ChangeLog:

* rust/compile/inline_asm_outside_unsafe.rs: New test.
---
 .../rust/compile/inline_asm_outside_unsafe.rs | 11 +++
 1 file changed, 11 insertions(+)
 create mode 100644 gcc/testsuite/rust/compile/inline_asm_outside_unsafe.rs

diff --git a/gcc/testsuite/rust/compile/inline_asm_outside_unsafe.rs 
b/gcc/testsuite/rust/compile/inline_asm_outside_unsafe.rs
new file mode 100644
index 000..02b9ab0c393
--- /dev/null
+++ b/gcc/testsuite/rust/compile/inline_asm_outside_unsafe.rs
@@ -0,0 +1,11 @@
+#![feature(rustc_attrs)]
+
+#[rustc_builtin_macro]
+macro_rules! asm {
+() => {}
+}
+
+fn main() {
+asm!("nop"); // { dg-error "use of inline assembly is unsafe and requires 
unsafe function or block" }
+}
+
-- 
2.45.2



[COMMITTED 112/145] gccrs: Scaffolding validation of asm!

2025-03-17 Thread arthur . cohen
From: jjasmine 

gcc/rust/ChangeLog:

* expand/rust-macro-builtins-asm.cc (parse_asm):
Scaffolding validation of asm!
(validate): Likewise
* expand/rust-macro-builtins-asm.h (validate): Likewise
---
 gcc/rust/expand/rust-macro-builtins-asm.cc | 31 +-
 gcc/rust/expand/rust-macro-builtins-asm.h  |  3 ++-
 2 files changed, 27 insertions(+), 7 deletions(-)

diff --git a/gcc/rust/expand/rust-macro-builtins-asm.cc 
b/gcc/rust/expand/rust-macro-builtins-asm.cc
index 47a47607cfe..3073761d5d6 100644
--- a/gcc/rust/expand/rust-macro-builtins-asm.cc
+++ b/gcc/rust/expand/rust-macro-builtins-asm.cc
@@ -703,13 +703,26 @@ parse_asm (location_t invoc_locus, AST::MacroInvocData 
&invoc,
   // operands stream, also handles the optional ","
   parse_asm_arg (parser, last_token_id, inline_asm_ctx);
 
-  AST::SingleASTNode single = AST::SingleASTNode (
-inline_asm_ctx.inline_asm.clone_expr_without_block ());
-  std::vector single_vec = {single};
+  // TODO: I'm putting the validation here because the rust reference put it
+  // here Per Arthur's advice we would actually do the validation in a 
different
+  // stage. and visit on the InlineAsm AST instead of it's context.
+  auto is_valid = validate (inline_asm_ctx);
 
-  AST::Fragment fragment_ast
-= AST::Fragment (single_vec, std::vector> ());
-  return fragment_ast;
+  if (is_valid)
+{
+  AST::SingleASTNode single = AST::SingleASTNode (
+   inline_asm_ctx.inline_asm.clone_expr_without_block ());
+  std::vector single_vec = {single};
+
+  AST::Fragment fragment_ast
+   = AST::Fragment (single_vec,
+std::vector> ());
+  return fragment_ast;
+}
+  else
+{
+  return tl::nullopt;
+}
 }
 
 tl::optional
@@ -749,4 +762,10 @@ parse_label (Parser &parser, TokenId 
last_token_id,
   return tl::nullopt;
 }
 }
+
+bool
+validate (InlineAsmContext &inline_asm_ctx)
+{
+  return true;
+}
 } // namespace Rust
diff --git a/gcc/rust/expand/rust-macro-builtins-asm.h 
b/gcc/rust/expand/rust-macro-builtins-asm.h
index 267c1b609d9..293d790ca40 100644
--- a/gcc/rust/expand/rust-macro-builtins-asm.h
+++ b/gcc/rust/expand/rust-macro-builtins-asm.h
@@ -75,7 +75,8 @@ parse_format_string (Parser &parser, TokenId 
last_token_id,
 tl::optional
 parse_label (Parser &parser, TokenId last_token_id,
 InlineAsmContext &inline_asm_ctx);
-
+bool
+validate (InlineAsmContext &inline_asm_ctx);
 std::set potentially_nonpromoted_keywords
   = {"in", "out", "lateout", "inout", "inlateout", "const", "sym", "label"};
 
-- 
2.45.2



[COMMITTED 113/145] gccrs: Update parser to parse strings in the first stage

2025-03-17 Thread arthur . cohen
From: jjasmine 

gcc/rust/ChangeLog:

* expand/rust-macro-builtins-asm.cc (parse_reg_operand):
Update parser to parse strings in the first stage
(parse_label): not needed right now
---
 gcc/rust/expand/rust-macro-builtins-asm.cc | 62 +-
 1 file changed, 13 insertions(+), 49 deletions(-)

diff --git a/gcc/rust/expand/rust-macro-builtins-asm.cc 
b/gcc/rust/expand/rust-macro-builtins-asm.cc
index 3073761d5d6..80ae4162a3e 100644
--- a/gcc/rust/expand/rust-macro-builtins-asm.cc
+++ b/gcc/rust/expand/rust-macro-builtins-asm.cc
@@ -385,21 +385,6 @@ parse_reg_operand (Parser &parser, 
TokenId last_token_id,
   rust_unreachable ();
   return tl::nullopt;
 }
-  else if (auto label_str = parse_label (parser, last_token_id, 
inline_asm_ctx))
-{
-  auto block = parser.parse_block_expr ();
-  struct AST::InlineAsmOperand::Label label (label_str,
-block ? block->clone_expr ()
-  : nullptr);
-  reg_operand.set_label (label);
-  return reg_operand;
-}
-  else if (inline_asm_ctx.allows_templates ())
-{
-  // TODO: If we allow templating, do sth here
-  rust_unreachable ();
-  return tl::nullopt;
-}
   else
 {
   // TODO: It is  weird that we can't seem to match any identifier,
@@ -725,43 +710,22 @@ parse_asm (location_t invoc_locus, AST::MacroInvocData 
&invoc,
 }
 }
 
-tl::optional
-parse_label (Parser &parser, TokenId last_token_id,
-InlineAsmContext &inline_asm_ctx)
-{
-  auto token = parser.peek_current_token ();
+// bool
+// is_label (const std::string &potential_label)
+// {
 
-  if (token->get_id () != last_token_id && token->get_id () == STRING_LITERAL)
-{
-  // very nice, we got a string.
-  auto label = token->as_string ();
+//   if (potential_label.empty () || potential_label.back () != ':')
+// return false;
 
-  bool flag = true;
-  if (label.empty () || label.back () != ':')
-   flag = false; // Check if string is empty or does not end with a colon
+//   // Check if all characters before the last colon are digits
+//   for (size_t i = 0; i < potential_label.length () - 1; i++)
+//   {
+// if (potential_label[i] < '0' || potential_label[i] > '9')
+//   return false;
+//   }
 
-  // Check if all characters before the last colon are digits
-  for (int i = 0; i < label.length () - 1 && flag == true; i++)
-   {
- if (label[i] < '0' || label[i] > '9')
-   flag = false;
-   }
-
-  if (flag == true)
-   {
- parser.skip_token ();
- return token->as_string ();
-   }
-  else
-   {
- return tl::nullopt;
-   }
-}
-  else
-{
-  return tl::nullopt;
-}
-}
+//   return true;
+// }
 
 bool
 validate (InlineAsmContext &inline_asm_ctx)
-- 
2.45.2



[COMMITTED 115/145] gccrs: Expected first layer done

2025-03-17 Thread arthur . cohen
From: jjasmine 

gcc/rust/ChangeLog:

* expand/rust-macro-builtins-asm.cc (parse_reg):
Expected first layer done
(parse_reg_operand): Likewise.
(parse_asm_arg): Likewise.
(parse_format_strings): Likewise.
(parse_asm): Likewise.
(validate): Likewise.
* expand/rust-macro-builtins-asm.h (parse_asm_arg): Likewise.
(validate): Likewise.
(parse_format_strings): Likewise.
---
 gcc/rust/expand/rust-macro-builtins-asm.cc | 74 --
 gcc/rust/expand/rust-macro-builtins-asm.h  | 23 +--
 2 files changed, 61 insertions(+), 36 deletions(-)

diff --git a/gcc/rust/expand/rust-macro-builtins-asm.cc 
b/gcc/rust/expand/rust-macro-builtins-asm.cc
index 21634725aa2..49d1fd4014f 100644
--- a/gcc/rust/expand/rust-macro-builtins-asm.cc
+++ b/gcc/rust/expand/rust-macro-builtins-asm.cc
@@ -131,7 +131,6 @@ parse_reg (InlineAsmContext &inline_asm_ctx)
 {
   using RegType = AST::InlineAsmRegOrRegClass::Type;
   auto &parser = inline_asm_ctx.parser;
-  auto last_token_id = inline_asm_ctx.last_token_id;
 
   if (!parser.skip_token (LEFT_PAREN))
 {
@@ -204,7 +203,6 @@ parse_reg_operand (InlineAsmContext &inline_asm_ctx)
   //   None
   //   };
   auto &parser = inline_asm_ctx.parser;
-  auto last_token_id = inline_asm_ctx.last_token_id;
   AST::InlineAsmOperand reg_operand;
   rust_debug ("Enter parse_reg_operand");
   auto token = parser.peek_current_token ();
@@ -562,8 +560,8 @@ MacroBuiltin::asm_handler (location_t invoc_locus, 
AST::MacroInvocData &invoc,
   return parse_asm (invoc_locus, invoc, is_global_asm);
 }
 
-int
-parse_asm_arg (InlineAsmContext &inline_asm_ctx)
+tl::expected
+parse_asm_arg (InlineAsmContext inline_asm_ctx)
 {
   auto &parser = inline_asm_ctx.parser;
   auto last_token_id = inline_asm_ctx.last_token_id;
@@ -625,7 +623,7 @@ parse_asm_arg (InlineAsmContext &inline_asm_ctx)
   // std::cout << "reg_operand" << std::endl;
   auto operand = parse_reg_operand (inline_asm_ctx);
 }
-  return 0;
+  return tl::expected (inline_asm_ctx);
 }
 
 tl::optional
@@ -655,14 +653,48 @@ parse_asm (location_t invoc_locus, AST::MacroInvocData 
&invoc,
   AST::InlineAsm inline_asm (invoc_locus, is_global_asm);
   auto inline_asm_ctx = InlineAsmContext (inline_asm, parser, last_token_id);
 
+  // operands stream, also handles the optional ","
+  tl::expected resulting_context
+= tl::expected (inline_asm_ctx);
+  resulting_context.and_then (parse_format_strings)
+.and_then (parse_asm_arg)
+.and_then (validate);
+
+  // TODO: I'm putting the validation here because the rust reference put it
+  // here Per Arthur's advice we would actually do the validation in a 
different
+  // stage. and visit on the InlineAsm AST instead of it's context.
+  auto is_valid = (bool) resulting_context;
+
+  if (is_valid)
+{
+  AST::SingleASTNode single = AST::SingleASTNode (
+   inline_asm_ctx.inline_asm.clone_expr_without_block ());
+  std::vector single_vec = {single};
+
+  AST::Fragment fragment_ast
+   = AST::Fragment (single_vec,
+std::vector> ());
+  return fragment_ast;
+}
+  else
+{
+  return tl::nullopt;
+}
+}
+
+tl::expected
+parse_format_strings (InlineAsmContext inline_asm_ctx)
+{
   // Parse the first ever formatted string, success or not, will skip 1 token
+  auto parser = inline_asm_ctx.parser;
+  auto last_token_id = inline_asm_ctx.last_token_id;
   auto fm_string = parse_format_string (inline_asm_ctx);
 
   if (fm_string == tl::nullopt)
 {
   rust_error_at (parser.peek_current_token ()->get_locus (),
 "%s template must be a string literal", "asm");
-  return tl::nullopt;
+  return tl::unexpected ("ERROR");
 }
 
   // formatted string stream
@@ -685,29 +717,7 @@ parse_asm (location_t invoc_locus, AST::MacroInvocData 
&invoc,
}
 }
 
-  // operands stream, also handles the optional ","
-  parse_asm_arg (inline_asm_ctx);
-
-  // TODO: I'm putting the validation here because the rust reference put it
-  // here Per Arthur's advice we would actually do the validation in a 
different
-  // stage. and visit on the InlineAsm AST instead of it's context.
-  auto is_valid = validate (inline_asm_ctx);
-
-  if (is_valid)
-{
-  AST::SingleASTNode single = AST::SingleASTNode (
-   inline_asm_ctx.inline_asm.clone_expr_without_block ());
-  std::vector single_vec = {single};
-
-  AST::Fragment fragment_ast
-   = AST::Fragment (single_vec,
-std::vector> ());
-  return fragment_ast;
-}
-  else
-{
-  return tl::nullopt;
-}
+  return tl::expected (inline_asm_ctx);
 }
 
 // bool
@@ -727,9 +737,9 @@ parse_asm (location_t invoc_locus, AST::MacroInvocData 
&invoc,
 //   return true;
 // }
 
-bool
-validate (InlineAsmContext &inline_asm_ctx)
+tl::expected
+validate (InlineAsmContext inline_asm_ctx)
 {
-  return true;
+  return tl::expected 

[COMMITTED 144/145] gccrs: [gccrs#2987] Patch ICE when deriving Clone and Copy

2025-03-17 Thread arthur . cohen
From: Liam Naddell 

gcc/rust/ChangeLog:
* expand/rust-expand-visitor.cc:
Fix ICE caused by unique_ptr UB and buggy iterator use

gcc/testsuite/ChangeLog:
* rust/compile/issue-2987.rs:
Add test for deriving Clone and Copy at the same time

Signed-off-by: Liam Naddell 
---
 gcc/rust/expand/rust-expand-visitor.cc   | 17 +
 gcc/testsuite/rust/compile/issue-2987.rs | 17 +
 2 files changed, 26 insertions(+), 8 deletions(-)
 create mode 100644 gcc/testsuite/rust/compile/issue-2987.rs

diff --git a/gcc/rust/expand/rust-expand-visitor.cc 
b/gcc/rust/expand/rust-expand-visitor.cc
index 61147e2d726..38399d0d74b 100644
--- a/gcc/rust/expand/rust-expand-visitor.cc
+++ b/gcc/rust/expand/rust-expand-visitor.cc
@@ -167,10 +167,10 @@ ExpandVisitor::expand_inner_items (
 
   for (auto it = items.begin (); it != items.end (); it++)
 {
-  auto &item = *it;
-  if (item->has_outer_attrs ())
+  Rust::AST::Item &item = **it;
+  if (item.has_outer_attrs ())
{
- auto &attrs = item->get_outer_attrs ();
+ auto &attrs = item.get_outer_attrs ();
 
  for (auto attr_it = attrs.begin (); attr_it != attrs.end ();
   /* erase => No increment*/)
@@ -190,16 +190,17 @@ ExpandVisitor::expand_inner_items (
  if (maybe_builtin.has_value ())
{
  auto new_item
-   = builtin_derive_item (*item, current,
+   = builtin_derive_item (item, current,
   maybe_builtin.value ());
- // this inserts the derive *before* the item - is it a
- // problem?
+
  it = items.insert (it, std::move (new_item));
}
  else
{
+ // Macro is not a builtin, so it must be a
+ // user-defined derive macro.
  auto new_items
-   = derive_item (*item, to_derive, expander);
+   = derive_item (item, to_derive, expander);
  std::move (new_items.begin (), new_items.end (),
 std::inserter (items, it));
}
@@ -215,7 +216,7 @@ ExpandVisitor::expand_inner_items (
{
  attr_it = attrs.erase (attr_it);
  auto new_items
-   = expand_item_attribute (*item, current.get_path (),
+   = expand_item_attribute (item, current.get_path (),
 expander);
  it = items.erase (it);
  std::move (new_items.begin (), new_items.end (),
diff --git a/gcc/testsuite/rust/compile/issue-2987.rs 
b/gcc/testsuite/rust/compile/issue-2987.rs
new file mode 100644
index 000..1ab5fdc3647
--- /dev/null
+++ b/gcc/testsuite/rust/compile/issue-2987.rs
@@ -0,0 +1,17 @@
+// { dg-options "-w" } Currently there are a lot of warnings produced from 
inside clone/copy
+// builtins
+
+#[lang = "copy"]
+trait Copy {}
+
+#[lang = "clone"]
+trait Clone {
+fn clone(&self) -> Self;
+}
+
+#[derive(Copy)]
+#[derive(Clone)]
+struct Empty;
+
+#[derive(Copy,Clone)]
+struct Empty2;
-- 
2.45.2



[COMMITTED 133/145] gccrs: [gccrs#3051] Remove unnecessary #include from rust-expr.h

2025-03-17 Thread arthur . cohen
From: Liam Naddell 

gcc/rust/ChangeLog:
* ast/rust-expr.h:
Remove unnecessary include.

Signed-off-by: Liam Naddell 
---
 gcc/rust/ast/rust-expr.h | 2 --
 1 file changed, 2 deletions(-)

diff --git a/gcc/rust/ast/rust-expr.h b/gcc/rust/ast/rust-expr.h
index 8fc89d87878..25afe235e4e 100644
--- a/gcc/rust/ast/rust-expr.h
+++ b/gcc/rust/ast/rust-expr.h
@@ -6,8 +6,6 @@
 #include "rust-path.h"
 #include "rust-macro.h"
 #include "rust-operators.h"
-#include "rust-system.h"
-#include 
 
 namespace Rust {
 namespace AST {
-- 
2.45.2



[COMMITTED 098/145] gccrs: Working towards parse_reg and parse_reg_operand

2025-03-17 Thread arthur . cohen
From: jjasmine 

gcc/rust/ChangeLog:

* expand/rust-macro-builtins-asm.cc (parse_reg):
Working towards parse_reg and parse_reg_operand
(parse_reg_operand):
Working towards parse_reg and parse_reg_operand
(parse_asm_arg):
Add todo about errors
* expand/rust-macro-builtins-asm.h (parse_global_asm):
remove dead code.
(parse_nonglobal_asm):
remove dead code.
---
 gcc/rust/expand/rust-macro-builtins-asm.cc | 29 +++---
 gcc/rust/expand/rust-macro-builtins-asm.h  |  4 ---
 2 files changed, 20 insertions(+), 13 deletions(-)

diff --git a/gcc/rust/expand/rust-macro-builtins-asm.cc 
b/gcc/rust/expand/rust-macro-builtins-asm.cc
index 3ce9bf9d78b..9e02400d77f 100644
--- a/gcc/rust/expand/rust-macro-builtins-asm.cc
+++ b/gcc/rust/expand/rust-macro-builtins-asm.cc
@@ -141,11 +141,10 @@ parse_reg (Parser &parser, TokenId 
last_token_id,
   // InlineAsmRegOrRegClass of reg or reg class
   auto token = parser.peek_current_token ();
   auto tok_id = token->get_id ();
-
-  if (tok_id == IDENTIFIER)
+  AST::InlineAsmRegOrRegClass regClass;
+  if (parser.skip_token (IDENTIFIER))
 {
   // construct a InlineAsmRegOrRegClass
-  AST::InlineAsmRegOrRegClass regClass;
   regClass.type = RegType::RegClass;
   regClass.regClass.Symbol = token->as_string ();
 }
@@ -156,18 +155,27 @@ parse_reg (Parser &parser, TokenId 
last_token_id,
 
   // construct a InlineAsmRegOrRegClass
   // parse_format_string
+  regClass.type = RegType::Reg;
+  inlineAsmCtx.is_explicit = true;
+  regClass.regClass.Symbol = token->as_string ();
 }
   else
 {
-  // TODO
+  // TODO: This should emit error
+  //  return
+  //  
Err(p.dcx().create_err(errors::ExpectedRegisterClassOrExplicitRegister
+  //  {
+  //   span: p.token.span,
+  //   }));
 }
   if (!parser.skip_token (RIGHT_PAREN))
 {
-  // we expect a left parenthesis here, please return the correct error.
+  // TODO: we expect a left parenthesis here, please return the correct
+  // error.
   return tl::nullopt;
 }
 
-  return tl::nullopt;
+  return regClass;
 }
 
 int
@@ -193,7 +201,7 @@ parse_reg_operand (Parser &parser, TokenId 
last_token_id,
   //   };
 
   using RegisterType = AST::InlineAsmOperand::RegisterType;
-
+  AST::InlineAsmOperand reg_operand;
   auto token = parser.peek_current_token ();
   auto iden_token = parser.peek_current_token ();
   auto &inlineAsm = inlineAsmCtx.inlineAsm;
@@ -221,8 +229,9 @@ parse_reg_operand (Parser &parser, TokenId 
last_token_id,
 {}
   else if (!is_global_asm && check_identifier (parser, "inlateout"))
 {}
-  else if (false && check_identifier (parser, "const"))
+  else if (parser.peek_current_token ()->get_id () == CONST)
 {
+  rust_unreachable ();
   // todo: Please handle const
 }
   else if (false && check_identifier (parser, "sym"))
@@ -237,7 +246,7 @@ parse_reg_operand (Parser &parser, TokenId 
last_token_id,
 {
   return tl::nullopt;
 }
-  return tl::nullopt;
+  return reg_operand;
 }
 void
 check_and_set (Parser &parser, InlineAsmContext &inlineAsmCtx,
@@ -410,6 +419,8 @@ parse_asm_arg (Parser &parser, TokenId 
last_token_id,
}
   else
{
+ // TODO: we consumed comma, and there happens to also be a comma
+ // error should be: expected expression, found `,`
  break;
}
 
diff --git a/gcc/rust/expand/rust-macro-builtins-asm.h 
b/gcc/rust/expand/rust-macro-builtins-asm.h
index a35d7707e6c..c7fcf301735 100644
--- a/gcc/rust/expand/rust-macro-builtins-asm.h
+++ b/gcc/rust/expand/rust-macro-builtins-asm.h
@@ -34,10 +34,6 @@ int
 parse_asm_arg (Parser &p, TokenId last_token_id,
   InlineAsmContext &inlineAsmCtx);
 
-tl::optional
-parse_global_asm (location_t invoc_locus, AST::MacroInvocData &invoc);
-tl::optional
-parse_nonglobal_asm (location_t invoc_locus, AST::MacroInvocData &invoc);
 tl::optional
 parse_asm (location_t invoc_locus, AST::MacroInvocData &invoc,
   bool is_global_asm);
-- 
2.45.2



[COMMITTED 134/145] rust: fix HIR dump for MatchExpr

2025-03-17 Thread arthur . cohen
From: Marc Poulhiès 

The visitor was still using the as_string() method.

gcc/rust/ChangeLog:

* hir/rust-hir-dump.cc (Dump::do_matcharm): New.
(Dump::do_matchcase): New.
(Dump::visit(MatchExpr)): Adjust, don't use as_string.
* hir/rust-hir-dump.h (Dump::do_matcharm, Dump::do_matchcase): New.

Signed-off-by: Marc Poulhiès 
---
 gcc/rust/hir/rust-hir-dump.cc | 43 +--
 gcc/rust/hir/rust-hir-dump.h  |  2 ++
 2 files changed, 38 insertions(+), 7 deletions(-)

diff --git a/gcc/rust/hir/rust-hir-dump.cc b/gcc/rust/hir/rust-hir-dump.cc
index f0fd9141f5e..4ae9cba858d 100644
--- a/gcc/rust/hir/rust-hir-dump.cc
+++ b/gcc/rust/hir/rust-hir-dump.cc
@@ -352,6 +352,31 @@ Dump::do_expr (Expr &e)
   do_outer_attrs (oa);
 }
 
+void
+Dump::do_matcharm (MatchArm &e)
+{
+  begin ("MatchArm");
+  // FIXME Can't remember how to handle that. Let's see later.
+  // do_outer_attrs(e);
+  visit_collection ("match_arm_patterns", e.get_patterns ());
+  visit_field ("guard_expr", e.get_guard_expr ());
+  end ("MatchArm");
+}
+
+void
+Dump::do_matchcase (HIR::MatchCase &e)
+{
+  begin ("MatchCase");
+
+  begin_field ("arm");
+  do_matcharm (e.get_arm ());
+  end_field ("arm");
+
+  visit_field ("expr", e.get_expr ());
+
+  end ("MatchCase");
+}
+
 void
 Dump::do_pathexpr (PathExpr &e)
 {
@@ -1437,16 +1462,20 @@ Dump::visit (MatchExpr &e)
   begin ("MatchExpr");
   do_inner_attrs (e);
   do_expr (e);
+
   visit_field ("branch_value", e.get_scrutinee_expr ());
 
-  std::string str;
-  if (e.get_match_cases ().empty ())
-str = "none";
+  if (!e.has_match_arms ())
+{
+  put_field ("match_arms", "empty");
+}
   else
-for (const auto &arm : e.get_match_cases ())
-  str += "\n " + arm.as_string ();
-  put_field ("match_arms", str);
-
+{
+  begin_field ("match_arms");
+  for (auto &arm : e.get_match_cases ())
+   do_matchcase (arm);
+  end_field ("match_arms");
+}
   end ("MatchExpr");
 }
 
diff --git a/gcc/rust/hir/rust-hir-dump.h b/gcc/rust/hir/rust-hir-dump.h
index bb5c3606b51..b3a2020742f 100644
--- a/gcc/rust/hir/rust-hir-dump.h
+++ b/gcc/rust/hir/rust-hir-dump.h
@@ -100,6 +100,8 @@ private:
   void do_structfield (StructField &);
   void do_maybenamedparam (MaybeNamedParam &);
   void do_struct (Struct &);
+  void do_matcharm (MatchArm &);
+  void do_matchcase (MatchCase &);
 
   void visit (AST::Attribute &attribute);
   virtual void visit (Lifetime &) override;
-- 
2.45.2



[COMMITTED 138/145] gccrs: Improve error messages for operator expressions

2025-03-17 Thread arthur . cohen
From: Antonio Gomes 

gcc/rust/ChangeLog:
* hir/tree/rust-hir-expr.h: Add new get_operator_str method in
ArithmeticOrLogicalExpr and CompoundAssignmentExpr
* hir/tree/rust-hir.cc: Likewise
* typecheck/rust-hir-type-check-expr.cc: Improve error message for
operator expressions to display the correct operator symbol

gcc/testsuite/ChangeLog:
* rust/compile/shadow1.rs: Fix test for new error message

Signed-off-by: Antonio Gomes 
---
 gcc/rust/hir/tree/rust-hir-expr.h |  4 
 gcc/rust/hir/tree/rust-hir.cc | 19 ---
 .../typecheck/rust-hir-type-check-expr.cc |  6 --
 gcc/testsuite/rust/compile/shadow1.rs |  2 +-
 4 files changed, 25 insertions(+), 6 deletions(-)

diff --git a/gcc/rust/hir/tree/rust-hir-expr.h 
b/gcc/rust/hir/tree/rust-hir-expr.h
index d8cfe68bbc5..ff9fcee1a46 100644
--- a/gcc/rust/hir/tree/rust-hir-expr.h
+++ b/gcc/rust/hir/tree/rust-hir-expr.h
@@ -417,6 +417,8 @@ public:
   std::unique_ptr &get_lhs () { return main_or_left_expr; }
   std::unique_ptr &get_rhs () { return right_expr; }
 
+  std::string get_operator_str () const;
+
 protected:
   /* Use covariance to implement clone function as returning this object rather
* than base */
@@ -766,6 +768,8 @@ public:
   void visit_lhs (HIRFullVisitor &vis) { main_or_left_expr->accept_vis (vis); }
   void visit_rhs (HIRFullVisitor &vis) { right_expr->accept_vis (vis); }
 
+  std::string get_operator_str () const;
+
 protected:
   /* Use covariance to implement clone function as returning this object rather
* than base */
diff --git a/gcc/rust/hir/tree/rust-hir.cc b/gcc/rust/hir/tree/rust-hir.cc
index 8388d580f97..ac0a256fd47 100644
--- a/gcc/rust/hir/tree/rust-hir.cc
+++ b/gcc/rust/hir/tree/rust-hir.cc
@@ -1299,7 +1299,7 @@ AssignmentExpr::as_string () const
 }
 
 std::string
-CompoundAssignmentExpr::as_string () const
+CompoundAssignmentExpr::get_operator_str () const
 {
   std::string operator_str;
   operator_str.reserve (1);
@@ -1344,7 +1344,14 @@ CompoundAssignmentExpr::as_string () const
 
   operator_str += "=";
 
+  return operator_str;
+}
+
+std::string
+CompoundAssignmentExpr::as_string () const
+{
   std::string str ("CompoundAssignmentExpr: ");
+  std::string operator_str = get_operator_str ();
   if (main_or_left_expr == nullptr || right_expr == nullptr)
 {
   str += "error. this is probably a parsing failure.";
@@ -1574,7 +1581,7 @@ ErrorPropagationExpr::as_string () const
 }
 
 std::string
-ArithmeticOrLogicalExpr::as_string () const
+ArithmeticOrLogicalExpr::get_operator_str () const
 {
   std::string operator_str;
   operator_str.reserve (1);
@@ -1617,8 +1624,14 @@ ArithmeticOrLogicalExpr::as_string () const
   break;
 }
 
+  return operator_str;
+}
+
+std::string
+ArithmeticOrLogicalExpr::as_string () const
+{
   std::string str = main_or_left_expr->as_string () + " ";
-  str += operator_str + " ";
+  str += get_operator_str () + " ";
   str += right_expr->as_string ();
 
   return "( " + str + " (" + get_mappings ().as_string () + "))";
diff --git a/gcc/rust/typecheck/rust-hir-type-check-expr.cc 
b/gcc/rust/typecheck/rust-hir-type-check-expr.cc
index 7cbcdedbe4d..6212660e571 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-expr.cc
+++ b/gcc/rust/typecheck/rust-hir-type-check-expr.cc
@@ -272,7 +272,8 @@ TypeCheckExpr::visit (HIR::CompoundAssignmentExpr &expr)
   if (!valid)
 {
   rust_error_at (expr.get_locus (),
-"cannot apply this operator to types %s and %s",
+"cannot apply operator %qs to types %s and %s",
+expr.get_operator_str ().c_str (),
 lhs->as_string ().c_str (), rhs->as_string ().c_str ());
   return;
 }
@@ -303,7 +304,8 @@ TypeCheckExpr::visit (HIR::ArithmeticOrLogicalExpr &expr)
   if (!valid)
 {
   rust_error_at (expr.get_locus (),
-"cannot apply this operator to types %s and %s",
+"cannot apply operator %qs to types %s and %s",
+expr.get_operator_str ().c_str (),
 lhs->as_string ().c_str (), rhs->as_string ().c_str ());
   return;
 }
diff --git a/gcc/testsuite/rust/compile/shadow1.rs 
b/gcc/testsuite/rust/compile/shadow1.rs
index cef972adf33..43d2764a98d 100644
--- a/gcc/testsuite/rust/compile/shadow1.rs
+++ b/gcc/testsuite/rust/compile/shadow1.rs
@@ -2,5 +2,5 @@ fn main() {
 let mut x = 5;
 let mut x;
 x = true;
-x = x + 2; // { dg-error "cannot apply this operator to types bool and 
"  }
+x = x + 2; // { dg-error "cannot apply operator .+. to types bool and 
"  }
 }
-- 
2.45.2



[COMMITTED 021/145] gccrs: Change return type for lookup_hir_item to optional

2025-03-17 Thread arthur . cohen
From: Pierre-Emmanuel Patry 

gcc/rust/ChangeLog:

* backend/rust-compile-resolve-path.cc (HIRCompileBase::query_compile):
Adapt function call to new return type.
* backend/rust-mangle-v0.cc (v0_path): Likewise.
* checks/errors/rust-const-checker.cc 
(ConstChecker::check_function_call):
Likewise.
* checks/errors/rust-unsafe-checker.cc 
(UnsafeChecker::check_use_of_static):
Likewise.
(UnsafeChecker::check_function_call): Likewise.
(UnsafeChecker::check_function_attr): Likewise.
* checks/lints/rust-lint-marklive.cc (MarkLive::go): Likewise.
(MarkLive::visit): Likewise.
* typecheck/rust-hir-trait-resolve.cc 
(TraitResolver::resolve_path_to_trait):
Likewise.
* typecheck/rust-type-util.cc (query_type): Likewise.
* util/rust-hir-map.cc (Mappings::insert_hir_item): Likewise.
(Mappings::lookup_hir_item): Change function return type to use
optional.
* util/rust-hir-map.h: Update function prototype.

Signed-off-by: Pierre-Emmanuel Patry 
---
 gcc/rust/backend/rust-compile-resolve-path.cc |  8 +++-
 gcc/rust/backend/rust-mangle-v0.cc|  9 -
 gcc/rust/checks/errors/rust-const-checker.cc  |  5 +++--
 gcc/rust/checks/errors/rust-unsafe-checker.cc | 16 
 gcc/rust/checks/lints/rust-lint-marklive.cc   | 11 +--
 gcc/rust/typecheck/rust-hir-trait-resolve.cc  |  9 +
 gcc/rust/typecheck/rust-type-util.cc  |  8 
 gcc/rust/util/rust-hir-map.cc |  7 +++
 gcc/rust/util/rust-hir-map.h  |  2 +-
 9 files changed, 36 insertions(+), 39 deletions(-)

diff --git a/gcc/rust/backend/rust-compile-resolve-path.cc 
b/gcc/rust/backend/rust-compile-resolve-path.cc
index 48c8d4ed5b6..1f7dc7d83c1 100644
--- a/gcc/rust/backend/rust-compile-resolve-path.cc
+++ b/gcc/rust/backend/rust-compile-resolve-path.cc
@@ -201,20 +201,18 @@ HIRCompileBase::query_compile (HirId ref, TyTy::BaseType 
*lookup,
   const Analysis::NodeMapping &mappings,
   location_t expr_locus, bool is_qualified_path)
 {
-  HIR::Item *resolved_item = ctx->get_mappings ().lookup_hir_item (ref);
   HirId parent_block;
   HIR::ExternalItem *resolved_extern_item
 = ctx->get_mappings ().lookup_hir_extern_item (ref, &parent_block);
-  bool is_hir_item = resolved_item != nullptr;
   bool is_hir_extern_item = resolved_extern_item != nullptr;
   bool is_fn = lookup->get_kind () == TyTy::TypeKind::FNDEF;
-  if (is_hir_item)
+  if (auto resolved_item = ctx->get_mappings ().lookup_hir_item (ref))
 {
   if (!lookup->has_substitutions_defined ())
-   return CompileItem::compile (resolved_item, ctx, nullptr, true,
+   return CompileItem::compile (*resolved_item, ctx, nullptr, true,
 expr_locus);
   else
-   return CompileItem::compile (resolved_item, ctx, lookup, true,
+   return CompileItem::compile (*resolved_item, ctx, lookup, true,
 expr_locus);
 }
   else if (is_hir_extern_item)
diff --git a/gcc/rust/backend/rust-mangle-v0.cc 
b/gcc/rust/backend/rust-mangle-v0.cc
index b2f11b19fcd..9be7b2d2bf4 100644
--- a/gcc/rust/backend/rust-mangle-v0.cc
+++ b/gcc/rust/backend/rust-mangle-v0.cc
@@ -387,7 +387,6 @@ v0_path (Rust::Compile::Context *ctx, const TyTy::BaseType 
*ty,
 HIR::ImplItem *impl_item
   = mappings.lookup_hir_implitem (hir_id, &parent_impl_id);
 HIR::TraitItem *trait_item = mappings.lookup_hir_trait_item (hir_id);
-HIR::Item *item = mappings.lookup_hir_item (hir_id);
 HIR::Expr *expr = mappings.lookup_hir_expr (hir_id);
 
 if (impl_item != nullptr)
@@ -428,11 +427,11 @@ v0_path (Rust::Compile::Context *ctx, const 
TyTy::BaseType *ty,
break;
  }
   }
-else if (item != nullptr)
-  switch (item->get_item_kind ())
+else if (auto item = mappings.lookup_hir_item (hir_id))
+  switch (item.value ()->get_item_kind ())
{
  case HIR::Item::ItemKind::Function: {
-   HIR::Function *fn = static_cast (item);
+   HIR::Function *fn = static_cast (*item);
v0path = v0_function_path (v0path, ctx, ty, fn,
   v0_identifier (seg.get ()));
  }
@@ -453,7 +452,7 @@ v0_path (Rust::Compile::Context *ctx, const TyTy::BaseType 
*ty,
case HIR::Item::ItemKind::Impl:
  // Trait impl or inherent impl.
  {
-   HIR::ImplBlock *impl_block = static_cast (item);
+   HIR::ImplBlock *impl_block = static_cast (*item);
v0path = v0_inherent_or_trait_impl_path (ctx, impl_block);
  }
  break;
diff --git a/gcc/rust/checks/errors/rust-const-checker.cc 
b/gcc/rust/checks/errors/rust-const-checker.cc
index 0a992397524..1bb2c37d7eb 100644
--- a/gcc/rust/checks/errors/rust-const-checker.cc
+++ b/gcc/rust/checks/err

[COMMITTED 030/145] gccrs: Change lookup_local_defid return type to optional

2025-03-17 Thread arthur . cohen
From: Pierre-Emmanuel Patry 

Make the API more convenient by changing the function's return type. We
can now differentiate between a stored null pointer and a missing value.

gcc/rust/ChangeLog:

* util/rust-hir-map.cc (Mappings::insert_defid_mapping): Adapt call
to new return type.
(Mappings::insert_local_defid_mapping): Likewise.
(Mappings::lookup_local_defid): Change return type to wrap it with an
optional.
* util/rust-hir-map.h: Update the function's prototype.

Signed-off-by: Pierre-Emmanuel Patry 
---
 gcc/rust/util/rust-hir-map.cc | 12 ++--
 gcc/rust/util/rust-hir-map.h  |  3 ++-
 2 files changed, 8 insertions(+), 7 deletions(-)

diff --git a/gcc/rust/util/rust-hir-map.cc b/gcc/rust/util/rust-hir-map.cc
index 899f71be49d..dcedea97e09 100644
--- a/gcc/rust/util/rust-hir-map.cc
+++ b/gcc/rust/util/rust-hir-map.cc
@@ -313,7 +313,7 @@ Mappings::insert_defid_mapping (DefId id, HIR::Item *item)
   LocalDefId local_def_id = id.localDefId;
 
   rust_assert (!lookup_defid (id));
-  rust_assert (lookup_local_defid (crate_num, local_def_id) == nullptr);
+  rust_assert (!lookup_local_defid (crate_num, local_def_id));
   rust_assert (lookup_trait_item_defid (id) == nullptr);
 
   defIdMappings[id] = item;
@@ -337,7 +337,7 @@ Mappings::insert_defid_mapping (DefId id, HIR::TraitItem 
*item)
   LocalDefId local_def_id = id.localDefId;
 
   rust_assert (!lookup_defid (id));
-  rust_assert (lookup_local_defid (crate_num, local_def_id) == nullptr);
+  rust_assert (!lookup_local_defid (crate_num, local_def_id));
   rust_assert (lookup_trait_item_defid (id) == nullptr);
 
   defIdTraitItemMappings[id] = item;
@@ -718,20 +718,20 @@ void
 Mappings::insert_local_defid_mapping (CrateNum crateNum, LocalDefId id,
  HIR::Item *item)
 {
-  rust_assert (lookup_local_defid (crateNum, id) == nullptr);
+  rust_assert (!lookup_local_defid (crateNum, id));
   localDefIdMappings[crateNum][id] = item;
 }
 
-HIR::Item *
+tl::optional
 Mappings::lookup_local_defid (CrateNum crateNum, LocalDefId id)
 {
   auto it = localDefIdMappings.find (crateNum);
   if (it == localDefIdMappings.end ())
-return nullptr;
+return tl::nullopt;
 
   auto iy = it->second.find (id);
   if (iy == it->second.end ())
-return nullptr;
+return tl::nullopt;
 
   return iy->second;
 }
diff --git a/gcc/rust/util/rust-hir-map.h b/gcc/rust/util/rust-hir-map.h
index 8a927c9ad4f..b4c39a94e82 100644
--- a/gcc/rust/util/rust-hir-map.h
+++ b/gcc/rust/util/rust-hir-map.h
@@ -112,7 +112,8 @@ public:
 
   void insert_local_defid_mapping (CrateNum crateNum, LocalDefId id,
   HIR::Item *item);
-  HIR::Item *lookup_local_defid (CrateNum crateNum, LocalDefId id);
+  tl::optional lookup_local_defid (CrateNum crateNum,
+   LocalDefId id);
 
   void insert_hir_item (HIR::Item *item);
   tl::optional lookup_hir_item (HirId id);
-- 
2.45.2



[COMMITTED 031/145] gccrs: Change return type of lookup trait defid functions.

2025-03-17 Thread arthur . cohen
From: Pierre-Emmanuel Patry 

Wrap the return type with an optional.

gcc/rust/ChangeLog:

* backend/rust-compile-base.cc (HIRCompileBase::resolve_method_address):
Update code around lookup return type.
* typecheck/rust-tyty-bounds.cc 
(TypeCheckBase::get_predicate_from_bound):
Likewise.
* typecheck/rust-tyty.cc (ClosureType::setup_fn_once_output):
Likewise.
* util/rust-hir-map.cc (Mappings::insert_defid_mapping): Likewise.
(Mappings::lookup_trait_item_defid): Update return type with an
optional.
(Mappings::get_lang_item): Likewise.
* util/rust-hir-map.h: Update the functions prototype.

Signed-off-by: Pierre-Emmanuel Patry 
---
 gcc/rust/backend/rust-compile-base.cc  |  2 +-
 gcc/rust/typecheck/rust-tyty-bounds.cc |  7 +--
 gcc/rust/typecheck/rust-tyty.cc|  4 ++--
 gcc/rust/util/rust-hir-map.cc  | 10 +-
 gcc/rust/util/rust-hir-map.h   |  6 +++---
 5 files changed, 16 insertions(+), 13 deletions(-)

diff --git a/gcc/rust/backend/rust-compile-base.cc 
b/gcc/rust/backend/rust-compile-base.cc
index c7031edd30b..add173c50a9 100644
--- a/gcc/rust/backend/rust-compile-base.cc
+++ b/gcc/rust/backend/rust-compile-base.cc
@@ -887,7 +887,7 @@ HIRCompileBase::resolve_method_address (TyTy::FnType 
*fntype,
 
   // it might be resolved to a trait item
   HIR::TraitItem *trait_item
-= ctx->get_mappings ().lookup_trait_item_defid (id);
+= ctx->get_mappings ().lookup_trait_item_defid (id).value ();
   HIR::Trait *trait = ctx->get_mappings ().lookup_trait_item_mapping (
 trait_item->get_mappings ().get_hirid ());
 
diff --git a/gcc/rust/typecheck/rust-tyty-bounds.cc 
b/gcc/rust/typecheck/rust-tyty-bounds.cc
index a18a0e40ddf..43404385cdd 100644
--- a/gcc/rust/typecheck/rust-tyty-bounds.cc
+++ b/gcc/rust/typecheck/rust-tyty-bounds.cc
@@ -245,8 +245,11 @@ TypeCheckBase::get_predicate_from_bound (HIR::TypePath 
&type_path,
rust_assert (fn.has_return_type ());
TypeCheckType::Resolve (fn.get_return_type ().get ());
 
-   HIR::TraitItem *trait_item = mappings.lookup_trait_item_lang_item (
- LangItem::Kind::FN_ONCE_OUTPUT, final_seg->get_locus ());
+   HIR::TraitItem *trait_item
+ = mappings
+ .lookup_trait_item_lang_item (LangItem::Kind::FN_ONCE_OUTPUT,
+   final_seg->get_locus ())
+ .value ();
 
std::vector bindings;
location_t output_locus = fn.get_return_type ()->get_locus ();
diff --git a/gcc/rust/typecheck/rust-tyty.cc b/gcc/rust/typecheck/rust-tyty.cc
index 2e9a551e4c7..565f2bc58aa 100644
--- a/gcc/rust/typecheck/rust-tyty.cc
+++ b/gcc/rust/typecheck/rust-tyty.cc
@@ -2238,8 +2238,8 @@ ClosureType::setup_fn_once_output () const
   rust_assert (!trait_ref->is_error ());
 
   // resolve to trait item
-  HIR::TraitItem *trait_item = mappings.lookup_trait_item_defid 
(trait_item_id);
-  rust_assert (trait_item != nullptr);
+  HIR::TraitItem *trait_item
+= mappings.lookup_trait_item_defid (trait_item_id).value ();
   rust_assert (trait_item->get_item_kind ()
   == HIR::TraitItem::TraitItemKind::TYPE);
   std::string item_identifier = trait_item->trait_identifier ();
diff --git a/gcc/rust/util/rust-hir-map.cc b/gcc/rust/util/rust-hir-map.cc
index dcedea97e09..41e4b048eb5 100644
--- a/gcc/rust/util/rust-hir-map.cc
+++ b/gcc/rust/util/rust-hir-map.cc
@@ -314,7 +314,7 @@ Mappings::insert_defid_mapping (DefId id, HIR::Item *item)
 
   rust_assert (!lookup_defid (id));
   rust_assert (!lookup_local_defid (crate_num, local_def_id));
-  rust_assert (lookup_trait_item_defid (id) == nullptr);
+  rust_assert (!lookup_trait_item_defid (id));
 
   defIdMappings[id] = item;
   insert_local_defid_mapping (crate_num, local_def_id, item);
@@ -338,17 +338,17 @@ Mappings::insert_defid_mapping (DefId id, HIR::TraitItem 
*item)
 
   rust_assert (!lookup_defid (id));
   rust_assert (!lookup_local_defid (crate_num, local_def_id));
-  rust_assert (lookup_trait_item_defid (id) == nullptr);
+  rust_assert (!lookup_trait_item_defid (id));
 
   defIdTraitItemMappings[id] = item;
 }
 
-HIR::TraitItem *
+tl::optional
 Mappings::lookup_trait_item_defid (DefId id)
 {
   auto it = defIdTraitItemMappings.find (id);
   if (it == defIdTraitItemMappings.end ())
-return nullptr;
+return tl::nullopt;
 
   return it->second;
 }
@@ -1264,7 +1264,7 @@ Mappings::get_lang_item (LangItem::Kind item_type, 
location_t locus)
   return item;
 }
 
-HIR::TraitItem *
+tl::optional
 Mappings::lookup_trait_item_lang_item (LangItem::Kind item, location_t locus)
 {
   DefId trait_item_id = get_lang_item (item, locus);
diff --git a/gcc/rust/util/rust-hir-map.h b/gcc/rust/util/rust-hir-map.h
index b4c39a94e82..ff0da5556a7 100644
--- a/gcc/rust/util/rust-hir-map.h
+++ b/gcc/rust/util/rust-hir-map.h
@@ -108,7 +108,7 @@ public:
   void insert_defid_mapping (DefId id, HIR::Item *item);
   tl::optional lookup_defid

[COMMITTED 033/145] gccrs: Change lookup_module function return type

2025-03-17 Thread arthur . cohen
From: Pierre-Emmanuel Patry 

Wrap the return type into an optional.

gcc/rust/ChangeLog:

* checks/errors/privacy/rust-visibility-resolver.cc: Update function
call to match the new return type.
* typecheck/rust-hir-type-check-path.cc 
(TypeCheckExpr::resolve_root_path):
Likewise.
* typecheck/rust-hir-type-check-type.cc 
(TypeCheckType::resolve_root_path):
Likewise.
* util/rust-hir-map.cc (Mappings::insert_module): Likewise.
(Mappings::lookup_module): Change the function's return type.
* util/rust-hir-map.h: Update the function's prototype.

Signed-off-by: Pierre-Emmanuel Patry 
---
 .../errors/privacy/rust-visibility-resolver.cc   | 16 +++-
 gcc/rust/typecheck/rust-hir-type-check-path.cc   |  2 +-
 gcc/rust/typecheck/rust-hir-type-check-type.cc   |  2 +-
 gcc/rust/util/rust-hir-map.cc|  6 +++---
 gcc/rust/util/rust-hir-map.h |  2 +-
 5 files changed, 13 insertions(+), 15 deletions(-)

diff --git a/gcc/rust/checks/errors/privacy/rust-visibility-resolver.cc 
b/gcc/rust/checks/errors/privacy/rust-visibility-resolver.cc
index 3066fee422b..464ce86e177 100644
--- a/gcc/rust/checks/errors/privacy/rust-visibility-resolver.cc
+++ b/gcc/rust/checks/errors/privacy/rust-visibility-resolver.cc
@@ -85,17 +85,15 @@ VisibilityResolver::resolve_module_path (const 
HIR::SimplePath &restriction,
 // these items as private?
 return true;
 
-  auto module = mappings.lookup_module (ref);
-  if (!module)
+  if (auto module = mappings.lookup_module (ref))
 {
-  invalid_path.emit ();
-  return false;
-}
+  // Fill in the resolved `DefId`
+  id = module.value ()->get_mappings ().get_defid ();
 
-  // Fill in the resolved `DefId`
-  id = module->get_mappings ().get_defid ();
-
-  return true;
+  return true;
+}
+  invalid_path.emit ();
+  return false;
 }
 
 bool
diff --git a/gcc/rust/typecheck/rust-hir-type-check-path.cc 
b/gcc/rust/typecheck/rust-hir-type-check-path.cc
index 0cc9f0e1203..c6552099bed 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-path.cc
+++ b/gcc/rust/typecheck/rust-hir-type-check-path.cc
@@ -243,7 +243,7 @@ TypeCheckExpr::resolve_root_path (HIR::PathInExpression 
&expr, size_t *offset,
}
   auto ref = hid.value ();
 
-  auto seg_is_module = (nullptr != mappings.lookup_module (ref));
+  auto seg_is_module = mappings.lookup_module (ref).has_value ();
   auto seg_is_crate = mappings.is_local_hirid_crate (ref);
   if (seg_is_module || seg_is_crate)
{
diff --git a/gcc/rust/typecheck/rust-hir-type-check-type.cc 
b/gcc/rust/typecheck/rust-hir-type-check-type.cc
index eb4abbbed3f..2a3e35cd7b0 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-type.cc
+++ b/gcc/rust/typecheck/rust-hir-type-check-type.cc
@@ -391,7 +391,7 @@ TypeCheckType::resolve_root_path (HIR::TypePath &path, 
size_t *offset,
}
   auto ref = hid.value ();
 
-  auto seg_is_module = (nullptr != mappings.lookup_module (ref));
+  auto seg_is_module = mappings.lookup_module (ref).has_value ();
   auto seg_is_crate = mappings.is_local_hirid_crate (ref);
   if (seg_is_module || seg_is_crate)
{
diff --git a/gcc/rust/util/rust-hir-map.cc b/gcc/rust/util/rust-hir-map.cc
index 7c4fd1ba0bd..c77ffea354e 100644
--- a/gcc/rust/util/rust-hir-map.cc
+++ b/gcc/rust/util/rust-hir-map.cc
@@ -489,18 +489,18 @@ void
 Mappings::insert_module (HIR::Module *module)
 {
   auto id = module->get_mappings ().get_hirid ();
-  rust_assert (lookup_module (id) == nullptr);
+  rust_assert (!lookup_module (id));
 
   hirModuleMappings[id] = module;
   insert_node_to_hir (module->get_mappings ().get_nodeid (), id);
 }
 
-HIR::Module *
+tl::optional
 Mappings::lookup_module (HirId id)
 {
   auto it = hirModuleMappings.find (id);
   if (it == hirModuleMappings.end ())
-return nullptr;
+return tl::nullopt;
 
   return it->second;
 }
diff --git a/gcc/rust/util/rust-hir-map.h b/gcc/rust/util/rust-hir-map.h
index da3aa9f5389..753e06f7966 100644
--- a/gcc/rust/util/rust-hir-map.h
+++ b/gcc/rust/util/rust-hir-map.h
@@ -138,7 +138,7 @@ public:
   tl::optional lookup_impl_block_type (HirId id);
 
   void insert_module (HIR::Module *module);
-  HIR::Module *lookup_module (HirId id);
+  tl::optional lookup_module (HirId id);
 
   void insert_hir_implitem (HirId parent_impl_id, HIR::ImplItem *item);
   HIR::ImplItem *lookup_hir_implitem (HirId id, HirId *parent_impl_id);
-- 
2.45.2



[COMMITTED 028/145] gccrs: Change crate name retrieval function return types

2025-03-17 Thread arthur . cohen
From: Pierre-Emmanuel Patry 

Change their return type to a const reference in order to avoid copies
when possible. Also wrap this new return type into an optional.

gcc/rust/ChangeLog:

* checks/errors/borrowck/rust-borrow-checker.cc (BorrowChecker::go):
Adapt the code to the new return types.
* resolve/rust-ast-resolve.cc (NameResolution::go): Likewise.
* util/rust-hir-map.cc (Mappings::get_crate_name): Change return type
to const string reference optional.
(Mappings::get_current_crate_name): Likewise.
* util/rust-hir-map.h: Update function prototypes.

Signed-off-by: Pierre-Emmanuel Patry 
---
 .../errors/borrowck/rust-borrow-checker.cc   |  6 ++
 gcc/rust/resolve/rust-ast-resolve.cc |  4 +---
 gcc/rust/util/rust-hir-map.cc| 16 ++--
 gcc/rust/util/rust-hir-map.h |  4 ++--
 4 files changed, 11 insertions(+), 19 deletions(-)

diff --git a/gcc/rust/checks/errors/borrowck/rust-borrow-checker.cc 
b/gcc/rust/checks/errors/borrowck/rust-borrow-checker.cc
index da4738c05b9..6eec021abca 100644
--- a/gcc/rust/checks/errors/borrowck/rust-borrow-checker.cc
+++ b/gcc/rust/checks/errors/borrowck/rust-borrow-checker.cc
@@ -65,10 +65,8 @@ BorrowChecker::go (HIR::Crate &crate)
 {
   mkdir_wrapped ("bir_dump");
   auto &mappings = Analysis::Mappings::get ();
-  bool ok = mappings.get_crate_name (crate.get_mappings ().get_crate_num 
(),
-crate_name);
-  rust_assert (ok);
-
+  crate_name
+   = *mappings.get_crate_name (crate.get_mappings ().get_crate_num ());
   mkdir_wrapped ("nll_facts_gccrs");
 }
 
diff --git a/gcc/rust/resolve/rust-ast-resolve.cc 
b/gcc/rust/resolve/rust-ast-resolve.cc
index bb8bcc4c239..a467d1e38b4 100644
--- a/gcc/rust/resolve/rust-ast-resolve.cc
+++ b/gcc/rust/resolve/rust-ast-resolve.cc
@@ -63,9 +63,7 @@ NameResolution::go (AST::Crate &crate)
 {
   // lookup current crate name
   CrateNum cnum = mappings.get_current_crate ();
-  std::string crate_name;
-  bool ok = mappings.get_crate_name (cnum, crate_name);
-  rust_assert (ok);
+  const auto &crate_name = mappings.get_crate_name (cnum).value ();
 
   // setup the ribs
   NodeId scope_node_id = crate.get_node_id ();
diff --git a/gcc/rust/util/rust-hir-map.cc b/gcc/rust/util/rust-hir-map.cc
index 76642a1b132..cb4e95ae8f0 100644
--- a/gcc/rust/util/rust-hir-map.cc
+++ b/gcc/rust/util/rust-hir-map.cc
@@ -138,15 +138,14 @@ Mappings::get_current_crate () const
   return currentCrateNum;
 }
 
-bool
-Mappings::get_crate_name (CrateNum crate_num, std::string &name) const
+tl::optional
+Mappings::get_crate_name (CrateNum crate_num) const
 {
   auto it = crate_names.find (crate_num);
   if (it == crate_names.end ())
-return false;
+return tl::nullopt;
 
-  name.assign (it->second);
-  return true;
+  return it->second;
 }
 
 void
@@ -155,13 +154,10 @@ Mappings::set_crate_name (CrateNum crate_num, const 
std::string &name)
   crate_names[crate_num] = name;
 }
 
-std::string
+const std::string &
 Mappings::get_current_crate_name () const
 {
-  std::string name;
-  bool ok = get_crate_name (get_current_crate (), name);
-  rust_assert (ok);
-  return name;
+  return get_crate_name (get_current_crate ()).value ();
 }
 
 tl::optional
diff --git a/gcc/rust/util/rust-hir-map.h b/gcc/rust/util/rust-hir-map.h
index 8181abe00a9..4ac719ffea7 100644
--- a/gcc/rust/util/rust-hir-map.h
+++ b/gcc/rust/util/rust-hir-map.h
@@ -80,9 +80,9 @@ public:
   CrateNum get_next_crate_num (const std::string &name);
   void set_current_crate (CrateNum crateNum);
   CrateNum get_current_crate () const;
-  bool get_crate_name (CrateNum crate_num, std::string &name) const;
+  tl::optional get_crate_name (CrateNum crate_num) const;
   void set_crate_name (CrateNum crate_num, const std::string &name);
-  std::string get_current_crate_name () const;
+  const std::string &get_current_crate_name () const;
   tl::optional
   lookup_crate_name (const std::string &crate_name) const;
   tl::optional crate_num_to_nodeid (const CrateNum &crate_num) const;
-- 
2.45.2



[COMMITTED 035/145] gccrs: Change lookup_hir_implitem return type

2025-03-17 Thread arthur . cohen
From: Pierre-Emmanuel Patry 

Wrap the return type within an optional. Now return the parent id within
a pair instead of taking an out reference.

gcc/rust/ChangeLog:

* backend/rust-compile-item.cc (CompileItem::visit): Change call site
to accept new return type.
* backend/rust-compile-resolve-path.cc (HIRCompileBase::query_compile):
Likewise.
* backend/rust-mangle-v0.cc (v0_path): Likewise.
* checks/errors/rust-unsafe-checker.cc (UnsafeChecker::visit):
Likewise.
* checks/lints/rust-lint-marklive.cc (MarkLive::go): Likewise.
(MarkLive::visit): Likewise.
* typecheck/rust-type-util.cc (query_type): Likewise.
* util/rust-hir-map.cc (Mappings::insert_hir_implitem): Likewise.
(Mappings::lookup_hir_implitem): Change return type.
* util/rust-hir-map.h: Update the function's prototype.

Signed-off-by: Pierre-Emmanuel Patry 
---
 gcc/rust/backend/rust-compile-item.cc |  7 ++--
 gcc/rust/backend/rust-compile-resolve-path.cc | 12 +++
 gcc/rust/backend/rust-mangle-v0.cc| 10 +++---
 gcc/rust/checks/errors/rust-unsafe-checker.cc |  6 ++--
 gcc/rust/checks/lints/rust-lint-marklive.cc   | 33 ---
 gcc/rust/typecheck/rust-type-util.cc  | 16 -
 gcc/rust/util/rust-hir-map.cc | 14 +++-
 gcc/rust/util/rust-hir-map.h  | 10 +++---
 8 files changed, 35 insertions(+), 73 deletions(-)

diff --git a/gcc/rust/backend/rust-compile-item.cc 
b/gcc/rust/backend/rust-compile-item.cc
index 9d65b610ec5..e7b3a17edf7 100644
--- a/gcc/rust/backend/rust-compile-item.cc
+++ b/gcc/rust/backend/rust-compile-item.cc
@@ -152,15 +152,12 @@ CompileItem::visit (HIR::Function &function)
 {
   // if this is part of a trait impl block which is not generic we need to
   // ensure associated types are setup
-  HirId parent_impl_block = UNKNOWN_HIRID;
   HirId id = function.get_mappings ().get_hirid ();
-  HIR::ImplItem *impl_item
-   = ctx->get_mappings ().lookup_hir_implitem (id, &parent_impl_block);
-  if (impl_item != nullptr)
+  if (auto impl_item = ctx->get_mappings ().lookup_hir_implitem (id))
{
  Resolver::AssociatedImplTrait *impl = nullptr;
  bool found = ctx->get_tyctx ()->lookup_associated_trait_impl (
-   parent_impl_block, &impl);
+   impl_item->second, &impl);
  if (found)
impl->setup_raw_associated_types ();
}
diff --git a/gcc/rust/backend/rust-compile-resolve-path.cc 
b/gcc/rust/backend/rust-compile-resolve-path.cc
index c27074f89a4..7c9b303b851 100644
--- a/gcc/rust/backend/rust-compile-resolve-path.cc
+++ b/gcc/rust/backend/rust-compile-resolve-path.cc
@@ -238,18 +238,14 @@ HIRCompileBase::query_compile (HirId ref, TyTy::BaseType 
*lookup,
}
}
 
-  HirId parent_impl_id = UNKNOWN_HIRID;
-  HIR::ImplItem *resolved_item
-   = ctx->get_mappings ().lookup_hir_implitem (ref, &parent_impl_id);
-  bool is_impl_item = resolved_item != nullptr;
-  if (is_impl_item)
+  if (auto resolved_item = ctx->get_mappings ().lookup_hir_implitem (ref))
{
  if (!lookup->has_substitutions_defined ())
-   return CompileInherentImplItem::Compile (resolved_item, ctx,
+   return CompileInherentImplItem::Compile (resolved_item->first, ctx,
 nullptr, true, expr_locus);
  else
-   return CompileInherentImplItem::Compile (resolved_item, ctx, lookup,
-true, expr_locus);
+   return CompileInherentImplItem::Compile (resolved_item->first, ctx,
+lookup, true, expr_locus);
}
   else
{
diff --git a/gcc/rust/backend/rust-mangle-v0.cc 
b/gcc/rust/backend/rust-mangle-v0.cc
index 0b6d9455242..261e84405d5 100644
--- a/gcc/rust/backend/rust-mangle-v0.cc
+++ b/gcc/rust/backend/rust-mangle-v0.cc
@@ -384,17 +384,15 @@ v0_path (Rust::Compile::Context *ctx, const 
TyTy::BaseType *ty,
 
 auto hir_id = hid.value ();
 
-HirId parent_impl_id = UNKNOWN_HIRID;
-HIR::ImplItem *impl_item
-  = mappings.lookup_hir_implitem (hir_id, &parent_impl_id);
 HIR::Expr *expr = mappings.lookup_hir_expr (hir_id);
 
-if (impl_item != nullptr)
+if (auto impl_item = mappings.lookup_hir_implitem (hir_id))
   {
-   switch (impl_item->get_impl_item_type ())
+   switch (impl_item->first->get_impl_item_type ())
  {
case HIR::ImplItem::FUNCTION: {
- HIR::Function *fn = static_cast (impl_item);
+ HIR::Function *fn
+   = static_cast (impl_item->first);
  v0path
= v0_function_path (v0path, ctx, ty, fn->get_generic_params (),
v0_identifier (seg.get ()));
diff --git a/gcc/rust/checks/errors/rust-unsafe

[COMMITTED 037/145] gccrs: Change lookup_hir_path_expr_seg return type

2025-03-17 Thread arthur . cohen
From: Pierre-Emmanuel Patry 

Make the function's return type optional in order to differentiate
between null pointers and missing value.

gcc/rust/ChangeLog:

* util/rust-hir-map.cc (Mappings::insert_hir_path_expr_seg): Change
call site to accomodate the new return type.
(Mappings::lookup_hir_path_expr_seg): Wrap the function's return type
with an optional.
* util/rust-hir-map.h: Update the function's prototype.

Signed-off-by: Pierre-Emmanuel Patry 
---
 gcc/rust/util/rust-hir-map.cc | 6 +++---
 gcc/rust/util/rust-hir-map.h  | 2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/gcc/rust/util/rust-hir-map.cc b/gcc/rust/util/rust-hir-map.cc
index 80786490f82..c96743a54f8 100644
--- a/gcc/rust/util/rust-hir-map.cc
+++ b/gcc/rust/util/rust-hir-map.cc
@@ -550,19 +550,19 @@ void
 Mappings::insert_hir_path_expr_seg (HIR::PathExprSegment *expr)
 {
   auto id = expr->get_mappings ().get_hirid ();
-  rust_assert (lookup_hir_path_expr_seg (id) == nullptr);
+  rust_assert (!lookup_hir_path_expr_seg (id));
 
   hirPathSegMappings[id] = expr;
   insert_node_to_hir (expr->get_mappings ().get_nodeid (), id);
   insert_location (id, expr->get_locus ());
 }
 
-HIR::PathExprSegment *
+tl::optional
 Mappings::lookup_hir_path_expr_seg (HirId id)
 {
   auto it = hirPathSegMappings.find (id);
   if (it == hirPathSegMappings.end ())
-return nullptr;
+return tl::nullopt;
 
   return it->second;
 }
diff --git a/gcc/rust/util/rust-hir-map.h b/gcc/rust/util/rust-hir-map.h
index 8d0f652a3d3..c7d0838d400 100644
--- a/gcc/rust/util/rust-hir-map.h
+++ b/gcc/rust/util/rust-hir-map.h
@@ -149,7 +149,7 @@ public:
   tl::optional lookup_hir_expr (HirId id);
 
   void insert_hir_path_expr_seg (HIR::PathExprSegment *expr);
-  HIR::PathExprSegment *lookup_hir_path_expr_seg (HirId id);
+  tl::optional lookup_hir_path_expr_seg (HirId id);
 
   void insert_hir_generic_param (HIR::GenericParam *expr);
   HIR::GenericParam *lookup_hir_generic_param (HirId id);
-- 
2.45.2



[COMMITTED 039/145] gccrs: Change lookup_hir_type return type with an optional

2025-03-17 Thread arthur . cohen
From: Pierre-Emmanuel Patry 

Wrap the function's return type with an optional in order to tell
appart a null pointer from a missing value.

gcc/rust/ChangeLog:

* util/rust-hir-map.cc (Mappings::insert_hir_type): Change call site
to accomodate the new return type.
(Mappings::lookup_hir_type): Change the function's return type.
* util/rust-hir-map.h: Update the function's prototype.

Signed-off-by: Pierre-Emmanuel Patry 
---
 gcc/rust/util/rust-hir-map.cc | 6 +++---
 gcc/rust/util/rust-hir-map.h  | 2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/gcc/rust/util/rust-hir-map.cc b/gcc/rust/util/rust-hir-map.cc
index c3929d8f3ce..ae11e67a57b 100644
--- a/gcc/rust/util/rust-hir-map.cc
+++ b/gcc/rust/util/rust-hir-map.cc
@@ -592,18 +592,18 @@ void
 Mappings::insert_hir_type (HIR::Type *type)
 {
   auto id = type->get_mappings ().get_hirid ();
-  rust_assert (lookup_hir_type (id) == nullptr);
+  rust_assert (!lookup_hir_type (id));
 
   hirTypeMappings[id] = type;
   insert_node_to_hir (type->get_mappings ().get_nodeid (), id);
 }
 
-HIR::Type *
+tl::optional
 Mappings::lookup_hir_type (HirId id)
 {
   auto it = hirTypeMappings.find (id);
   if (it == hirTypeMappings.end ())
-return nullptr;
+return tl::nullopt;
 
   return it->second;
 }
diff --git a/gcc/rust/util/rust-hir-map.h b/gcc/rust/util/rust-hir-map.h
index d04232f3d5d..e4f47852ed9 100644
--- a/gcc/rust/util/rust-hir-map.h
+++ b/gcc/rust/util/rust-hir-map.h
@@ -155,7 +155,7 @@ public:
   tl::optional lookup_hir_generic_param (HirId id);
 
   void insert_hir_type (HIR::Type *type);
-  HIR::Type *lookup_hir_type (HirId id);
+  tl::optional lookup_hir_type (HirId id);
 
   void insert_hir_stmt (HIR::Stmt *stmt);
   HIR::Stmt *lookup_hir_stmt (HirId id);
-- 
2.45.2



[COMMITTED 025/145] gccrs: Change return type of lookup_impl_block_type

2025-03-17 Thread arthur . cohen
From: Pierre-Emmanuel Patry 

Change the return type to an optional.

gcc/rust/ChangeLog:

* typecheck/rust-type-util.cc (query_type): Adapt code to accomodate
the new return type.
* util/rust-hir-map.cc (Mappings::lookup_impl_block_type): Change
the function's return type and remove the out pointer argument.
* util/rust-hir-map.h: Update the function's prototype.

Signed-off-by: Pierre-Emmanuel Patry 
---
 gcc/rust/typecheck/rust-type-util.cc | 8 +++-
 gcc/rust/util/rust-hir-map.cc| 9 -
 gcc/rust/util/rust-hir-map.h | 2 +-
 3 files changed, 8 insertions(+), 11 deletions(-)

diff --git a/gcc/rust/typecheck/rust-type-util.cc 
b/gcc/rust/typecheck/rust-type-util.cc
index af38acd0c32..6847d87ad96 100644
--- a/gcc/rust/typecheck/rust-type-util.cc
+++ b/gcc/rust/typecheck/rust-type-util.cc
@@ -89,12 +89,10 @@ query_type (HirId reference, TyTy::BaseType **result)
 }
 
   // is it an impl_type?
-  HIR::ImplBlock *impl_block_by_type = nullptr;
-  bool found_impl_block_type
-= mappings.lookup_impl_block_type (reference, &impl_block_by_type);
-  if (found_impl_block_type)
+  if (auto impl_block_by_type = mappings.lookup_impl_block_type (reference))
 {
-  *result = TypeCheckItem::ResolveImplBlockSelf (*impl_block_by_type);
+  *result
+   = TypeCheckItem::ResolveImplBlockSelf (*impl_block_by_type.value ());
   context->query_completed (reference);
   return true;
 }
diff --git a/gcc/rust/util/rust-hir-map.cc b/gcc/rust/util/rust-hir-map.cc
index a5b1daf04c1..99c2493da14 100644
--- a/gcc/rust/util/rust-hir-map.cc
+++ b/gcc/rust/util/rust-hir-map.cc
@@ -486,15 +486,14 @@ Mappings::lookup_hir_impl_block (HirId id)
   return it->second;
 }
 
-bool
-Mappings::lookup_impl_block_type (HirId id, HIR::ImplBlock **impl_block)
+tl::optional
+Mappings::lookup_impl_block_type (HirId id)
 {
   auto it = hirImplBlockTypeMappings.find (id);
   if (it == hirImplBlockTypeMappings.end ())
-return false;
+return tl::nullopt;
 
-  *impl_block = it->second;
-  return true;
+  return it->second;
 }
 
 void
diff --git a/gcc/rust/util/rust-hir-map.h b/gcc/rust/util/rust-hir-map.h
index 109693e9c74..912d42a4b97 100644
--- a/gcc/rust/util/rust-hir-map.h
+++ b/gcc/rust/util/rust-hir-map.h
@@ -131,7 +131,7 @@ public:
 
   void insert_hir_impl_block (HIR::ImplBlock *item);
   tl::optional lookup_hir_impl_block (HirId id);
-  bool lookup_impl_block_type (HirId id, HIR::ImplBlock **impl_block);
+  tl::optional lookup_impl_block_type (HirId id);
 
   void insert_module (HIR::Module *module);
   HIR::Module *lookup_module (HirId id);
-- 
2.45.2



[COMMITTED 020/145] gccrs: Change return type of resolve_nodeid_to_stmt

2025-03-17 Thread arthur . cohen
From: Pierre-Emmanuel Patry 

Change the return type to an optional.

gcc/rust/ChangeLog:

* util/rust-hir-map.cc (Mappings::resolve_nodeid_to_stmt): Change the
return type and remove pointer out argument.
* util/rust-hir-map.h: Update function prototype.

Signed-off-by: Pierre-Emmanuel Patry 
---
 gcc/rust/util/rust-hir-map.cc | 10 --
 gcc/rust/util/rust-hir-map.h  |  2 +-
 2 files changed, 5 insertions(+), 7 deletions(-)

diff --git a/gcc/rust/util/rust-hir-map.cc b/gcc/rust/util/rust-hir-map.cc
index b8d393759d6..57a766758d9 100644
--- a/gcc/rust/util/rust-hir-map.cc
+++ b/gcc/rust/util/rust-hir-map.cc
@@ -805,17 +805,15 @@ Mappings::lookup_location (HirId id)
   return it->second;
 }
 
-bool
-Mappings::resolve_nodeid_to_stmt (NodeId id, HIR::Stmt **stmt)
+tl::optional
+Mappings::resolve_nodeid_to_stmt (NodeId id)
 {
   auto it = nodeIdToHirMappings.find (id);
   if (it == nodeIdToHirMappings.end ())
-return false;
+return tl::nullopt;
 
   HirId resolved = it->second;
-  auto resolved_stmt = lookup_hir_stmt (resolved);
-  *stmt = resolved_stmt;
-  return resolved_stmt != nullptr;
+  return {lookup_hir_stmt (resolved)};
 }
 
 void
diff --git a/gcc/rust/util/rust-hir-map.h b/gcc/rust/util/rust-hir-map.h
index 6fd0e7a52f4..6ed3beee8fc 100644
--- a/gcc/rust/util/rust-hir-map.h
+++ b/gcc/rust/util/rust-hir-map.h
@@ -176,7 +176,7 @@ public:
   void insert_location (HirId id, location_t locus);
   location_t lookup_location (HirId id);
 
-  bool resolve_nodeid_to_stmt (NodeId id, HIR::Stmt **stmt);
+  tl::optional resolve_nodeid_to_stmt (NodeId id);
 
   std::set &get_hirids_within_crate (CrateNum crate)
   {
-- 
2.45.2



[COMMITTED 001/145] gccrs: contrib: Add libgrust to update-copyright.py script

2025-03-17 Thread arthur . cohen
From: Sahil Yeole 

contrib/ChangeLog:
* update-copyright.py: Add libgrust folder.

Signed-off-by: Sahil Yeole 
---
 contrib/update-copyright.py | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/contrib/update-copyright.py b/contrib/update-copyright.py
index 0e45609d5eb..838c44f33eb 100755
--- a/contrib/update-copyright.py
+++ b/contrib/update-copyright.py
@@ -792,6 +792,7 @@ class GCCCmdLine (CmdLine):
 self.add_dir ('libgfortran')
 # libgo is imported from upstream.
 self.add_dir ('libgomp')
+self.add_dir ('libgrust')
 self.add_dir ('libiberty')
 self.add_dir ('libitm')
 self.add_dir ('libobjc')
@@ -819,6 +820,7 @@ class GCCCmdLine (CmdLine):
 'libgcc',
 'libgfortran',
 'libgomp',
+'libgrust',
 'libiberty',
 'libitm',
 'libobjc',
-- 
2.45.2



[COMMITTED 041/145] gccrs: Change lookup_hir_param return type with optional

2025-03-17 Thread arthur . cohen
From: Pierre-Emmanuel Patry 

Wrap the function's return type within an optional to differentiate
between a null ppointer and a missing value.

gcc/rust/ChangeLog:

* util/rust-hir-map.cc (Mappings::insert_hir_param): Change call site
to accomodate new return type.
(Mappings::lookup_hir_param): Change the function's return type.
* util/rust-hir-map.h: Updat ethe function's prototype.

Signed-off-by: Pierre-Emmanuel Patry 
---
 gcc/rust/util/rust-hir-map.cc | 6 +++---
 gcc/rust/util/rust-hir-map.h  | 2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/gcc/rust/util/rust-hir-map.cc b/gcc/rust/util/rust-hir-map.cc
index 025b3121b27..e2c3b98eff5 100644
--- a/gcc/rust/util/rust-hir-map.cc
+++ b/gcc/rust/util/rust-hir-map.cc
@@ -632,18 +632,18 @@ void
 Mappings::insert_hir_param (HIR::FunctionParam *param)
 {
   auto id = param->get_mappings ().get_hirid ();
-  rust_assert (lookup_hir_param (id) == nullptr);
+  rust_assert (!lookup_hir_param (id));
 
   hirParamMappings[id] = param;
   insert_node_to_hir (param->get_mappings ().get_nodeid (), id);
 }
 
-HIR::FunctionParam *
+tl::optional
 Mappings::lookup_hir_param (HirId id)
 {
   auto it = hirParamMappings.find (id);
   if (it == hirParamMappings.end ())
-return nullptr;
+return tl::nullopt;
 
   return it->second;
 }
diff --git a/gcc/rust/util/rust-hir-map.h b/gcc/rust/util/rust-hir-map.h
index f6db83d6e11..6f97dcb6bc6 100644
--- a/gcc/rust/util/rust-hir-map.h
+++ b/gcc/rust/util/rust-hir-map.h
@@ -161,7 +161,7 @@ public:
   tl::optional lookup_hir_stmt (HirId id);
 
   void insert_hir_param (HIR::FunctionParam *type);
-  HIR::FunctionParam *lookup_hir_param (HirId id);
+  tl::optional lookup_hir_param (HirId id);
 
   void insert_hir_self_param (HIR::SelfParam *type);
   HIR::SelfParam *lookup_hir_self_param (HirId id);
-- 
2.45.2



[COMMITTED 009/145] gccrs: borrowck: Testsuite

2025-03-17 Thread arthur . cohen
From: Jakub Dupak 

gcc/testsuite/ChangeLog:

* rust/borrowck/borrowck.exp: New test.
* rust/borrowck/position_dependant_outlives.rs: New test.
* rust/borrowck/reference.rs: New test.
* rust/borrowck/return_ref_to_local.rs: New test.
* rust/borrowck/subset.rs: New test.
* rust/borrowck/test_move.rs: New test.
* rust/borrowck/test_move_behind_reference.rs: New test.
* rust/borrowck/test_move_conditional.rs: New test.
* rust/borrowck/tmp.rs: New test.
* rust/borrowck/use_while_mut.rs: New test.
* rust/borrowck/use_while_mut_fr.rs: New test.
* rust/borrowck/well_formed_function_inputs.rs: New test.
---
 gcc/testsuite/rust/borrowck/borrowck.exp  | 35 +++
 .../borrowck/position_dependant_outlives.rs   | 11 +++
 gcc/testsuite/rust/borrowck/reference.rs  | 99 +++
 .../rust/borrowck/return_ref_to_local.rs  |  6 ++
 gcc/testsuite/rust/borrowck/subset.rs | 27 +
 gcc/testsuite/rust/borrowck/test_move.rs  | 16 +++
 .../borrowck/test_move_behind_reference.rs| 27 +
 .../rust/borrowck/test_move_conditional.rs| 28 ++
 gcc/testsuite/rust/borrowck/tmp.rs| 79 +++
 gcc/testsuite/rust/borrowck/use_while_mut.rs  |  7 ++
 .../rust/borrowck/use_while_mut_fr.rs |  8 ++
 .../borrowck/well_formed_function_inputs.rs   | 16 +++
 12 files changed, 359 insertions(+)
 create mode 100644 gcc/testsuite/rust/borrowck/borrowck.exp
 create mode 100644 gcc/testsuite/rust/borrowck/position_dependant_outlives.rs
 create mode 100644 gcc/testsuite/rust/borrowck/reference.rs
 create mode 100644 gcc/testsuite/rust/borrowck/return_ref_to_local.rs
 create mode 100644 gcc/testsuite/rust/borrowck/subset.rs
 create mode 100644 gcc/testsuite/rust/borrowck/test_move.rs
 create mode 100644 gcc/testsuite/rust/borrowck/test_move_behind_reference.rs
 create mode 100644 gcc/testsuite/rust/borrowck/test_move_conditional.rs
 create mode 100644 gcc/testsuite/rust/borrowck/tmp.rs
 create mode 100644 gcc/testsuite/rust/borrowck/use_while_mut.rs
 create mode 100644 gcc/testsuite/rust/borrowck/use_while_mut_fr.rs
 create mode 100644 gcc/testsuite/rust/borrowck/well_formed_function_inputs.rs

diff --git a/gcc/testsuite/rust/borrowck/borrowck.exp 
b/gcc/testsuite/rust/borrowck/borrowck.exp
new file mode 100644
index 000..6e96a7d11d8
--- /dev/null
+++ b/gcc/testsuite/rust/borrowck/borrowck.exp
@@ -0,0 +1,35 @@
+# Copyright (C) 2021-2023 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+# 
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+# 
+# You should have received a copy of the GNU General Public License
+# along with GCC; see the file COPYING3.  If not see
+# .
+
+# Compile tests, no torture testing.
+#
+# These tests raise errors in the front end; torture testing doesn't apply.
+
+# Load support procs.
+load_lib rust-dg.exp
+
+# Initialize `dg'.
+dg-init
+
+# Main loop.
+set saved-dg-do-what-default ${dg-do-what-default}
+
+set dg-do-what-default "compile"
+dg-runtest [lsort [glob -nocomplain $srcdir/$subdir/*.rs]] "" ""
+set dg-do-what-default ${saved-dg-do-what-default}
+
+# All done.
+dg-finish
diff --git a/gcc/testsuite/rust/borrowck/position_dependant_outlives.rs 
b/gcc/testsuite/rust/borrowck/position_dependant_outlives.rs
new file mode 100644
index 000..7856934a6b3
--- /dev/null
+++ b/gcc/testsuite/rust/borrowck/position_dependant_outlives.rs
@@ -0,0 +1,11 @@
+// { dg-additional-options "-frust-compile-until=compilation 
-frust-borrowcheck" }
+
+pub fn position_dependent_outlives<'a>(x: &'a mut i32, cond: bool) -> &'a mut 
i32 {
+let y = &mut *x;
+if cond {
+return y;
+} else {
+*x = 0;
+return x;
+}
+}
\ No newline at end of file
diff --git a/gcc/testsuite/rust/borrowck/reference.rs 
b/gcc/testsuite/rust/borrowck/reference.rs
new file mode 100644
index 000..b825a9686b7
--- /dev/null
+++ b/gcc/testsuite/rust/borrowck/reference.rs
@@ -0,0 +1,99 @@
+// { dg-additional-options "-frust-compile-until=compilation 
-frust-borrowcheck" }
+
+
+#[lang = "sized"]
+pub trait Sized {}
+
+struct Reference<'a> {
+value: &'a i32,
+}
+
+impl<'a> Reference<'a> {
+fn new<'a>(value: &'a i32) -> Reference<'a> {
+Reference { value: value }
+}
+}
+
+struct ReferenceMut<'a> {
+value: &'a mut i32,
+}
+
+impl<'a> ReferenceMut<'a> {
+fn new<'a>(value: &'a mut i32) -> ReferenceMut<'a> {
+ReferenceMut { value: value }
+}
+}
+
+fn immutable_bo

[COMMITTED 013/145] gccrs: Remove unused Context parameter for some backend methods

2025-03-17 Thread arthur . cohen
From: Owen Avery 

gcc/rust/ChangeLog:

* backend/rust-compile-base.cc
(HIRCompileBase::compile_function_body):
Adjust unit_expression calls.
(HIRCompileBase::unit_expression):
Remove unused Context parameter.
* backend/rust-compile-base.h
(HIRCompileBase::unit_expression): Likewise.
* backend/rust-compile-block.cc
(CompileBlock::visit): Adjust unit_expression calls.
* backend/rust-compile-expr.cc
(CompileExpr::visit): Likewise.
* backend/rust-compile-pattern.cc
(CompilePatternLet::visit): Likewise.
* backend/rust-compile-resolve-path.cc
(ResolvePathRef::attempt_constructor_expression_lookup):
Likewise.
* backend/rust-compile-type.cc
(TyTyResolveCompile::get_implicit_enumeral_node_type):
Remove unused Context parameter.
(TyTyResolveCompile::get_unit_type):
Likewise.
(TyTyResolveCompile::visit):
Adjust get_implicit_enumeral_node_type and get_unit_type calls.
* backend/rust-compile-type.h
(TyTyResolveCompile::get_implicit_enumeral_node_type):
Remove unused Context parameter.
(TyTyResolveCompile::get_unit_type):
Likewise.

Signed-off-by: Owen Avery 
---
 gcc/rust/backend/rust-compile-base.cc |  8 
 gcc/rust/backend/rust-compile-base.h  |  2 +-
 gcc/rust/backend/rust-compile-block.cc|  2 +-
 gcc/rust/backend/rust-compile-expr.cc |  6 +++---
 gcc/rust/backend/rust-compile-pattern.cc  |  2 +-
 gcc/rust/backend/rust-compile-resolve-path.cc |  2 +-
 gcc/rust/backend/rust-compile-type.cc | 10 +-
 gcc/rust/backend/rust-compile-type.h  |  4 ++--
 8 files changed, 18 insertions(+), 18 deletions(-)

diff --git a/gcc/rust/backend/rust-compile-base.cc 
b/gcc/rust/backend/rust-compile-base.cc
index e3138205a5b..64ac121a28f 100644
--- a/gcc/rust/backend/rust-compile-base.cc
+++ b/gcc/rust/backend/rust-compile-base.cc
@@ -611,7 +611,7 @@ HIRCompileBase::compile_function_body (tree fndecl,
  ctx->add_statement (return_value);
 
  // now just return unit expression
- tree unit_expr = unit_expression (ctx, locus);
+ tree unit_expr = unit_expression (locus);
  tree return_stmt
= Backend::return_statement (fndecl, unit_expr, locus);
  ctx->add_statement (return_stmt);
@@ -622,7 +622,7 @@ HIRCompileBase::compile_function_body (tree fndecl,
   // we can only do this if the function is of unit type otherwise other
   // errors should have occurred
   location_t locus = function_body.get_locus ();
-  tree return_value = unit_expression (ctx, locus);
+  tree return_value = unit_expression (locus);
   tree return_stmt
= Backend::return_statement (fndecl, return_value, locus);
   ctx->add_statement (return_stmt);
@@ -991,9 +991,9 @@ HIRCompileBase::resolve_method_address (TyTy::FnType 
*fntype,
 }
 
 tree
-HIRCompileBase::unit_expression (Context *ctx, location_t locus)
+HIRCompileBase::unit_expression (location_t locus)
 {
-  tree unit_type = TyTyResolveCompile::get_unit_type (ctx);
+  tree unit_type = TyTyResolveCompile::get_unit_type ();
   return Backend::constructor_expression (unit_type, false, {}, -1, locus);
 }
 
diff --git a/gcc/rust/backend/rust-compile-base.h 
b/gcc/rust/backend/rust-compile-base.h
index 316638a4a6b..465a4caa720 100644
--- a/gcc/rust/backend/rust-compile-base.h
+++ b/gcc/rust/backend/rust-compile-base.h
@@ -102,7 +102,7 @@ protected:
 const Resolver::CanonicalPath *canonical_path,
 TyTy::FnType *fntype);
 
-  static tree unit_expression (Context *ctx, location_t locus);
+  static tree unit_expression (location_t locus);
 
   void setup_fndecl (tree fndecl, bool is_main_entry_point, bool is_generic_fn,
 HIR::Visibility &visibility,
diff --git a/gcc/rust/backend/rust-compile-block.cc 
b/gcc/rust/backend/rust-compile-block.cc
index b36a15fd2fd..eb8af2a0d3b 100644
--- a/gcc/rust/backend/rust-compile-block.cc
+++ b/gcc/rust/backend/rust-compile-block.cc
@@ -75,7 +75,7 @@ CompileBlock::visit (HIR::BlockExpr &expr)
   else if (result != nullptr)
 {
   location_t locus = expr.get_locus ();
-  tree compiled_expr = unit_expression (ctx, expr.get_locus ());
+  tree compiled_expr = unit_expression (expr.get_locus ());
   tree result_reference = Backend::var_expression (result, locus);
 
   tree assignment
diff --git a/gcc/rust/backend/rust-compile-expr.cc 
b/gcc/rust/backend/rust-compile-expr.cc
index 0980e6cf38a..4d43a444221 100644
--- a/gcc/rust/backend/rust-compile-expr.cc
+++ b/gcc/rust/backend/rust-compile-expr.cc
@@ -76,7 +76,7 @@ CompileExpr::visit (HIR::TupleExpr &expr)
 {
   if (expr.is_unit ())
 {
-  translated = unit_expression (ctx, expr.get_locus ());
+  translated = unit_expression (expr.get_locus ());
   return;
 }

[COMMITTED 023/145] gccrs: Change return type of lookup_hir_extern_block

2025-03-17 Thread arthur . cohen
From: Pierre-Emmanuel Patry 

Change the return type to an optional in order to easily differentiate
between a null pointer and an missing value.

gcc/rust/ChangeLog:

* checks/errors/rust-unsafe-checker.cc 
(UnsafeChecker::check_function_call):
Adapt function call to new return type.
* typecheck/rust-type-util.cc (query_type): Likewise.
* util/rust-hir-map.cc (Mappings::insert_hir_extern_block): Likewise.
(Mappings::lookup_hir_extern_block): Change return type to an optional.
* util/rust-hir-map.h: Update the function's prototype.

Signed-off-by: Pierre-Emmanuel Patry 
---
 gcc/rust/checks/errors/rust-unsafe-checker.cc | 2 +-
 gcc/rust/typecheck/rust-type-util.cc  | 8 
 gcc/rust/util/rust-hir-map.cc | 6 +++---
 gcc/rust/util/rust-hir-map.h  | 2 +-
 4 files changed, 9 insertions(+), 9 deletions(-)

diff --git a/gcc/rust/checks/errors/rust-unsafe-checker.cc 
b/gcc/rust/checks/errors/rust-unsafe-checker.cc
index 18e79e3ce65..fc83283a795 100644
--- a/gcc/rust/checks/errors/rust-unsafe-checker.cc
+++ b/gcc/rust/checks/errors/rust-unsafe-checker.cc
@@ -177,7 +177,7 @@ UnsafeChecker::check_function_call (HirId node_id, 
location_t locus)
 
   if (maybe_extern)
 check_extern_call (static_cast (maybe_extern),
-  mappings.lookup_hir_extern_block (parent_extern_block),
+  *mappings.lookup_hir_extern_block (parent_extern_block),
   locus);
 }
 
diff --git a/gcc/rust/typecheck/rust-type-util.cc 
b/gcc/rust/typecheck/rust-type-util.cc
index f6f8c8e9e14..9a777863c2c 100644
--- a/gcc/rust/typecheck/rust-type-util.cc
+++ b/gcc/rust/typecheck/rust-type-util.cc
@@ -105,11 +105,11 @@ query_type (HirId reference, TyTy::BaseType **result)
 = mappings.lookup_hir_extern_item (reference, &parent_extern_block_id);
   if (extern_item != nullptr)
 {
-  HIR::ExternBlock *block
-   = mappings.lookup_hir_extern_block (parent_extern_block_id);
-  rust_assert (block != nullptr);
+  auto block = mappings.lookup_hir_extern_block (parent_extern_block_id);
+  rust_assert (block.has_value ());
 
-  *result = TypeCheckTopLevelExternItem::Resolve (extern_item, *block);
+  *result
+   = TypeCheckTopLevelExternItem::Resolve (extern_item, *block.value ());
   context->query_completed (reference);
   return true;
 }
diff --git a/gcc/rust/util/rust-hir-map.cc b/gcc/rust/util/rust-hir-map.cc
index 0d134bf4ab9..184ab13494d 100644
--- a/gcc/rust/util/rust-hir-map.cc
+++ b/gcc/rust/util/rust-hir-map.cc
@@ -426,18 +426,18 @@ void
 Mappings::insert_hir_extern_block (HIR::ExternBlock *block)
 {
   auto id = block->get_mappings ().get_hirid ();
-  rust_assert (lookup_hir_extern_block (id) == nullptr);
+  rust_assert (!lookup_hir_extern_block (id).has_value ());
 
   hirExternBlockMappings[id] = block;
   insert_node_to_hir (block->get_mappings ().get_nodeid (), id);
 }
 
-HIR::ExternBlock *
+tl::optional
 Mappings::lookup_hir_extern_block (HirId id)
 {
   auto it = hirExternBlockMappings.find (id);
   if (it == hirExternBlockMappings.end ())
-return nullptr;
+return tl::nullopt;
 
   return it->second;
 }
diff --git a/gcc/rust/util/rust-hir-map.h b/gcc/rust/util/rust-hir-map.h
index 5565ef93f1c..ef4d65639a0 100644
--- a/gcc/rust/util/rust-hir-map.h
+++ b/gcc/rust/util/rust-hir-map.h
@@ -124,7 +124,7 @@ public:
   tl::optional lookup_hir_trait_item (HirId id);
 
   void insert_hir_extern_block (HIR::ExternBlock *block);
-  HIR::ExternBlock *lookup_hir_extern_block (HirId id);
+  tl::optional lookup_hir_extern_block (HirId id);
 
   void insert_hir_extern_item (HIR::ExternalItem *item, HirId parent_block);
   HIR::ExternalItem *lookup_hir_extern_item (HirId id, HirId *parent_block);
-- 
2.45.2



  1   2   >