From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1643) id 7787F3810AF1; Wed, 8 Jun 2022 12:49:03 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7787F3810AF1 Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Thomas Schwinge To: gcc-cvs@gcc.gnu.org Subject: [gcc/devel/rust/master] Make TyTy::BaseType::destructure recursive X-Act-Checkin: gcc X-Git-Author: Philip Herron X-Git-Refname: refs/heads/devel/rust/master X-Git-Oldrev: 9204e7a1ac617126b65c27668524aebf8f65f134 X-Git-Newrev: a647c005908d79bc6b50739c43b7ac105bc193a9 Message-Id: <20220608124903.7787F3810AF1@sourceware.org> Date: Wed, 8 Jun 2022 12:49:03 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 08 Jun 2022 12:49:03 -0000 https://gcc.gnu.org/g:a647c005908d79bc6b50739c43b7ac105bc193a9 commit a647c005908d79bc6b50739c43b7ac105bc193a9 Author: Philip Herron Date: Fri May 20 17:35:35 2022 +0100 Make TyTy::BaseType::destructure recursive In the case of Generic Associated Types we end up placeholders->projections->generic-param->i32 This means we need to keep destructuring the TyTy object until we finally get the real type at the end. In order to do this safely we need to ensure we add in recursion limits and apply this where it matters such as compiling types in general too. Diff: --- gcc/rust/backend/rust-compile-type.cc | 12 +++++-- gcc/rust/backend/rust-compile-type.h | 7 ++-- gcc/rust/lang.opt | 4 +++ gcc/rust/typecheck/rust-tyty.cc | 62 ++++++++++++++++++++++------------- 4 files changed, 54 insertions(+), 31 deletions(-) diff --git a/gcc/rust/backend/rust-compile-type.cc b/gcc/rust/backend/rust-compile-type.cc index 38740274a44..102bc0a3f65 100644 --- a/gcc/rust/backend/rust-compile-type.cc +++ b/gcc/rust/backend/rust-compile-type.cc @@ -112,9 +112,15 @@ TyTyResolveCompile::visit (const TyTy::PlaceholderType &type) void TyTyResolveCompile::visit (const TyTy::ParamType ¶m) { - // FIXME make this reuse the same machinery from constexpr code - recursion_count++; - rust_assert (recursion_count < kDefaultRecusionLimit); + if (recurisve_ops++ >= rust_max_recursion_depth) + { + rust_error_at (Location (), + "% count exceeds limit of %i (use " + "% to increase the limit)", + rust_max_recursion_depth); + translated = error_mark_node; + return; + } param.resolve ()->accept_vis (*this); } diff --git a/gcc/rust/backend/rust-compile-type.h b/gcc/rust/backend/rust-compile-type.h index 3e1f903f761..262b8fc51a0 100644 --- a/gcc/rust/backend/rust-compile-type.h +++ b/gcc/rust/backend/rust-compile-type.h @@ -63,16 +63,13 @@ public: private: TyTyResolveCompile (Context *ctx, bool trait_object_mode) : ctx (ctx), trait_object_mode (trait_object_mode), - translated (error_mark_node), recursion_count (0) + translated (error_mark_node), recurisve_ops (0) {} Context *ctx; bool trait_object_mode; tree translated; - - // FIXME this needs to be derived from the gcc config option - size_t recursion_count; - static const size_t kDefaultRecusionLimit = 5; + int recurisve_ops; }; } // namespace Compile diff --git a/gcc/rust/lang.opt b/gcc/rust/lang.opt index a6dabbfd727..58ec9a75e9a 100644 --- a/gcc/rust/lang.opt +++ b/gcc/rust/lang.opt @@ -66,6 +66,10 @@ frust-dump- Rust Joined RejectNegative -frust-dump- Dump Rust frontend internal information. +frust-max-recursion-depth= +Rust RejectNegative Type(int) Var(rust_max_recursion_depth) Init(64) +-frust-max-recursion-depth=integer + frust-mangling= Rust Joined RejectNegative Enum(frust_mangling) Var(flag_rust_mangling) -frust-mangling=[legacy|v0] Choose which version to use for name mangling diff --git a/gcc/rust/typecheck/rust-tyty.cc b/gcc/rust/typecheck/rust-tyty.cc index d0e8b761622..f03bc8234c8 100644 --- a/gcc/rust/typecheck/rust-tyty.cc +++ b/gcc/rust/typecheck/rust-tyty.cc @@ -252,33 +252,49 @@ BaseType::get_root () const const BaseType * BaseType::destructure () const { - switch (get_kind ()) + int recurisve_ops = 0; + const BaseType *x = this; + while (true) { - case TyTy::TypeKind::PARAM: { - const TyTy::ParamType *p = static_cast (this); - return p->resolve (); - } - break; - - case TyTy::TypeKind::PLACEHOLDER: { - const TyTy::PlaceholderType *p - = static_cast (this); - rust_assert (p->can_resolve ()); - return p->resolve (); - } - break; - - case TyTy::TypeKind::PROJECTION: { - const TyTy::ProjectionType *p - = static_cast (this); - return p->get (); - } + if (recurisve_ops++ >= rust_max_recursion_depth) + { + rust_error_at ( + Location (), + "% count exceeds limit of %i (use " + "% to increase the limit)", + rust_max_recursion_depth); + return new ErrorType (get_ref ()); + } - default: - return this; + switch (x->get_kind ()) + { + case TyTy::TypeKind::PARAM: { + const TyTy::ParamType *p = static_cast (x); + x = p->resolve (); + } + break; + + case TyTy::TypeKind::PLACEHOLDER: { + const TyTy::PlaceholderType *p + = static_cast (x); + rust_assert (p->can_resolve ()); + x = p->resolve (); + } + break; + + case TyTy::TypeKind::PROJECTION: { + const TyTy::ProjectionType *p + = static_cast (x); + x = p->get (); + } + break; + + default: + return x; + } } - return this; + return x; } TyVar::TyVar (HirId ref) : ref (ref)