[gccrs COMMIT 1/2] lower: Use enum instead of bool for checking for valid impl Trait params.

gerris.rs@gmail.com gerris.rs@gmail.com
Sat Aug 8 19:46:34 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.
---
This change was merged into the gccrs repository and is posted here for
upstream visibility and potential drive-by review, as requested by GCC
release managers.
Each commit email contains a link to its details on github from where you can
find the Pull-Request and associated discussions.


Commit on github: https://github.com/Rust-GCC/gccrs/commit/f4485e8af81172087220a33200477b36fbc87995

The commit has NOT been mentioned in any issue.

The commit has been mentioned in the following pull-request(s):
 - https://github.com/Rust-GCC/gccrs/pull/4769

 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 e26e05cf1..bb08c3c36 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 7f278902a..84ed9c90d 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 d3154d252..3acdfeaa1 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 377231717..fa3cd314d 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;
 };

base-commit: 40b339792f6e9197fea912b3264ecf7b022f6279
-- 
2.54.0



More information about the Gcc-rust mailing list