[COMMITTED 08/43] gccrs: lower: Use enum instead of bool for checking for valid impl Trait params.

arthur.cohen@opensrcsec.com arthur.cohen@opensrcsec.com
Thu Sep 10 08:19:21 GMT 2026


From: Arthur Cohen <arthur.cohen@embecosm.com>

Refactor the check of `_: impl Trait` function arguments to use an enum
class instead of a boolean.

gcc/rust/ChangeLog:

	* hir/rust-ast-lower-type.h: New enum, change API to use it.
	* hir/rust-ast-lower-implitem.cc (ASTLowerImplItem::visit): Use the new ImplTrait enum.
	* hir/rust-ast-lower-item.cc (ASTLoweringItem::visit): Likewise.
	* hir/rust-ast-lower-type.cc (ASTLoweringType::ASTLoweringType): Likewise.
	(ASTLoweringType::translate): Likewise.
	(ASTLoweringType::visit): Likewise.
---
 gcc/rust/hir/rust-ast-lower-implitem.cc |  2 +-
 gcc/rust/hir/rust-ast-lower-item.cc     |  2 +-
 gcc/rust/hir/rust-ast-lower-type.cc     |  8 ++++----
 gcc/rust/hir/rust-ast-lower-type.h      | 20 +++++++++++++++-----
 4 files changed, 21 insertions(+), 11 deletions(-)

diff --git a/gcc/rust/hir/rust-ast-lower-implitem.cc b/gcc/rust/hir/rust-ast-lower-implitem.cc
index e26e05cf118..bb08c3c36fa 100644
--- a/gcc/rust/hir/rust-ast-lower-implitem.cc
+++ b/gcc/rust/hir/rust-ast-lower-implitem.cc
@@ -139,7 +139,7 @@ ASTLowerImplItem::visit (AST::Function &function)
   std::unique_ptr<HIR::Type> return_type
     = function.has_return_type () ? std::unique_ptr<HIR::Type> (
 	ASTLoweringType::translate (function.get_return_type (), false,
-				    true /* impl trait is allowed here*/))
+				    ASTLoweringType::ImplTrait::Allow))
 				  : nullptr;
 
   Defaultness defaultness
diff --git a/gcc/rust/hir/rust-ast-lower-item.cc b/gcc/rust/hir/rust-ast-lower-item.cc
index 7f278902a09..84ed9c90d3c 100644
--- a/gcc/rust/hir/rust-ast-lower-item.cc
+++ b/gcc/rust/hir/rust-ast-lower-item.cc
@@ -412,7 +412,7 @@ ASTLoweringItem::visit (AST::Function &function)
   std::unique_ptr<HIR::Type> return_type
     = function.has_return_type () ? std::unique_ptr<HIR::Type> (
 	ASTLoweringType::translate (function.get_return_type (), false,
-				    true /* impl trait is allowed here*/))
+				    ASTLoweringType::ImplTrait::Allow))
 				  : nullptr;
 
   std::vector<HIR::FunctionParam> function_params;
diff --git a/gcc/rust/hir/rust-ast-lower-type.cc b/gcc/rust/hir/rust-ast-lower-type.cc
index d3154d252d2..3acdfeaa1e5 100644
--- a/gcc/rust/hir/rust-ast-lower-type.cc
+++ b/gcc/rust/hir/rust-ast-lower-type.cc
@@ -210,14 +210,14 @@ ASTLowerQualifiedPathInType::visit (AST::QualifiedPathInType &path)
 }
 
 ASTLoweringType::ASTLoweringType (bool default_to_static_lifetime,
-				  bool impl_trait_allowed)
+				  ImplTrait impl_trait_allowed)
   : ASTLoweringBase (), default_to_static_lifetime (default_to_static_lifetime),
     impl_trait_allowed (impl_trait_allowed), translated (nullptr)
 {}
 
 HIR::Type *
 ASTLoweringType::translate (AST::Type &type, bool default_to_static_lifetime,
-			    bool impl_trait_allowed)
+			    ImplTrait impl_trait_allowed)
 {
   ASTLoweringType resolver (default_to_static_lifetime, impl_trait_allowed);
   type.accept_vis (resolver);
@@ -492,7 +492,7 @@ ASTLoweringType::visit (AST::ParenthesisedType &type)
 void
 ASTLoweringType::visit (AST::ImplTraitType &type)
 {
-  if (!impl_trait_allowed)
+  if (impl_trait_allowed == ImplTrait::Forbid)
     emit_impl_trait_error (type.get_locus ());
 
   std::vector<std::unique_ptr<HIR::TypeParamBound>> bounds;
@@ -514,7 +514,7 @@ ASTLoweringType::visit (AST::ImplTraitType &type)
 void
 ASTLoweringType::visit (AST::ImplTraitTypeOneBound &type)
 {
-  if (!impl_trait_allowed)
+  if (impl_trait_allowed == ImplTrait::Forbid)
     emit_impl_trait_error (type.get_locus ());
 
   std::vector<std::unique_ptr<HIR::TypeParamBound>> bounds;
diff --git a/gcc/rust/hir/rust-ast-lower-type.h b/gcc/rust/hir/rust-ast-lower-type.h
index 3772317177f..fa3cd314d4e 100644
--- a/gcc/rust/hir/rust-ast-lower-type.h
+++ b/gcc/rust/hir/rust-ast-lower-type.h
@@ -65,9 +65,18 @@ class ASTLoweringType : public ASTLoweringBase
   using Rust::HIR::ASTLoweringBase::visit;
 
 public:
-  static HIR::Type *translate (AST::Type &type,
-			       bool default_to_static_lifetime = false,
-			       bool impl_trait_allowed = false);
+  /**
+   * Allow `arg: impl Trait` types or error out on them
+   */
+  enum class ImplTrait
+  {
+    Allow,
+    Forbid,
+  };
+
+  static HIR::Type *
+  translate (AST::Type &type, bool default_to_static_lifetime = false,
+	     ImplTrait impl_trait_allowed = ImplTrait::Forbid);
 
   void visit (AST::BareFunctionType &fntype) override;
   void visit (AST::TupleType &tuple) override;
@@ -88,11 +97,12 @@ public:
   void emit_impl_trait_error (location_t locus);
 
 private:
-  ASTLoweringType (bool default_to_static_lifetime, bool impl_trait_allowed);
+  ASTLoweringType (bool default_to_static_lifetime,
+		   ImplTrait impl_trait_allowed);
 
   /** Used when compiling const and static items. */
   bool default_to_static_lifetime;
-  bool impl_trait_allowed;
+  ImplTrait impl_trait_allowed;
 
   HIR::Type *translated;
 };
-- 
2.50.1



More information about the Gcc-rust mailing list