https://gcc.gnu.org/g:6db8a24ca93e4199f3adcaf6b5680ab0d683353c
commit r17-4095-g6db8a24ca93e4199f3adcaf6b5680ab0d683353c Author: Arthur Cohen <[email protected]> Date: Fri Aug 7 12:27:35 2026 +0200 gccrs: lower: Use enum instead of bool for checking for valid impl Trait params. 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. Diff: --- 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 e26e05cf1188..bb08c3c36fab 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 7f278902a090..84ed9c90d3ca 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 d3154d252d22..3acdfeaa1e59 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 3772317177f5..fa3cd314d4e3 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; };
