From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1643) id B476A3852765; Wed, 8 Jun 2022 12:19:25 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B476A3852765 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] Refactor TypeBoundPredicate to be below the definition for SubstitutionRef X-Act-Checkin: gcc X-Git-Author: Philip Herron X-Git-Refname: refs/heads/devel/rust/master X-Git-Oldrev: 2dfc19647774cb26a0f735bda8006068a40cfba0 X-Git-Newrev: 9411c061aaeae0be39c678e0885472c37b112141 Message-Id: <20220608121925.B476A3852765@sourceware.org> Date: Wed, 8 Jun 2022 12:19:25 +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:19:25 -0000 https://gcc.gnu.org/g:9411c061aaeae0be39c678e0885472c37b112141 commit 9411c061aaeae0be39c678e0885472c37b112141 Author: Philip Herron Date: Mon Mar 14 17:35:57 2022 +0000 Refactor TypeBoundPredicate to be below the definition for SubstitutionRef This means TypeBoundPredicate will now be able to inherit all behaviours of normal generics so we do not duplicate the work in handling generics it will also allow us to more easily check for unconstrained type parameters on traits. Diff: --- gcc/rust/typecheck/rust-tyty-bounds.cc | 54 ++++++++++++++ gcc/rust/typecheck/rust-tyty.h | 124 +++++++++++++-------------------- 2 files changed, 103 insertions(+), 75 deletions(-) diff --git a/gcc/rust/typecheck/rust-tyty-bounds.cc b/gcc/rust/typecheck/rust-tyty-bounds.cc index 1d1c481ce73..bc1e76ef69f 100644 --- a/gcc/rust/typecheck/rust-tyty-bounds.cc +++ b/gcc/rust/typecheck/rust-tyty-bounds.cc @@ -67,6 +67,10 @@ TypeCheckBase::resolve_trait_path (HIR::TypePath &path) namespace TyTy { +TypeBoundPredicate::TypeBoundPredicate (DefId reference, Location locus) + : reference (reference), locus (locus), args (nullptr) +{} + std::string TypeBoundPredicate::as_string () const { @@ -192,5 +196,55 @@ TypeBoundPredicateItem::needs_implementation () const return !get_raw_item ()->is_optional (); } +// TypeBoundsMappings + +TypeBoundsMappings::TypeBoundsMappings ( + std::vector specified_bounds) + : specified_bounds (specified_bounds) +{} + +std::vector & +TypeBoundsMappings::get_specified_bounds () +{ + return specified_bounds; +} + +const std::vector & +TypeBoundsMappings::get_specified_bounds () const +{ + return specified_bounds; +} + +size_t +TypeBoundsMappings::num_specified_bounds () const +{ + return specified_bounds.size (); +} + +std::string +TypeBoundsMappings::raw_bounds_as_string () const +{ + std::string buf; + for (size_t i = 0; i < specified_bounds.size (); i++) + { + const TypeBoundPredicate &b = specified_bounds.at (i); + bool has_next = (i + 1) < specified_bounds.size (); + buf += b.get_name () + (has_next ? " + " : ""); + } + return buf; +} + +std::string +TypeBoundsMappings::bounds_as_string () const +{ + return "bounds:[" + raw_bounds_as_string () + "]"; +} + +void +TypeBoundsMappings::add_bound (TypeBoundPredicate predicate) +{ + specified_bounds.push_back (predicate); +} + } // namespace TyTy } // namespace Rust diff --git a/gcc/rust/typecheck/rust-tyty.h b/gcc/rust/typecheck/rust-tyty.h index 82262abab09..d1c2170ba34 100644 --- a/gcc/rust/typecheck/rust-tyty.h +++ b/gcc/rust/typecheck/rust-tyty.h @@ -108,92 +108,24 @@ private: const Resolver::TraitItemReference *trait_item_ref; }; -class TypeBoundPredicate -{ -public: - TypeBoundPredicate (DefId reference, Location locus) - : reference (reference), locus (locus), args (nullptr) - {} - - std::string as_string () const; - - const Resolver::TraitReference *get () const; - - Location get_locus () const { return locus; } - - std::string get_name () const; - - // check that this predicate is object-safe see: - // https://doc.rust-lang.org/reference/items/traits.html#object-safety - bool is_object_safe (bool emit_error, Location locus) const; - - void apply_generic_arguments (HIR::GenericArgs *generic_args); - - bool contains_item (const std::string &search) const; - - TypeBoundPredicateItem - lookup_associated_item (const std::string &search) const; - - HIR::GenericArgs *get_generic_args () { return args; } - - const HIR::GenericArgs *get_generic_args () const { return args; } - - bool has_generic_args () const - { - if (args == nullptr) - return false; - - return args->has_generic_args (); - } - -private: - DefId reference; - Location locus; - HIR::GenericArgs *args; -}; - class TypeBoundsMappings { protected: - TypeBoundsMappings (std::vector specified_bounds) - : specified_bounds (specified_bounds) - {} + TypeBoundsMappings (std::vector specified_bounds); public: - std::vector &get_specified_bounds () - { - return specified_bounds; - } + std::vector &get_specified_bounds (); - const std::vector &get_specified_bounds () const - { - return specified_bounds; - } + const std::vector &get_specified_bounds () const; - size_t num_specified_bounds () const { return specified_bounds.size (); } + size_t num_specified_bounds () const; - std::string raw_bounds_as_string () const - { - std::string buf; - for (size_t i = 0; i < specified_bounds.size (); i++) - { - const TypeBoundPredicate &b = specified_bounds.at (i); - bool has_next = (i + 1) < specified_bounds.size (); - buf += b.get_name () + (has_next ? " + " : ""); - } - return buf; - } + std::string raw_bounds_as_string () const; - std::string bounds_as_string () const - { - return "bounds:[" + raw_bounds_as_string () + "]"; - } + std::string bounds_as_string () const; protected: - void add_bound (TypeBoundPredicate predicate) - { - specified_bounds.push_back (predicate); - } + void add_bound (TypeBoundPredicate predicate); std::vector specified_bounds; }; @@ -1007,6 +939,48 @@ protected: SubstitutionArgumentMappings used_arguments; }; +class TypeBoundPredicate +{ +public: + TypeBoundPredicate (DefId reference, Location locus); + + std::string as_string () const; + + const Resolver::TraitReference *get () const; + + Location get_locus () const { return locus; } + + std::string get_name () const; + + // check that this predicate is object-safe see: + // https://doc.rust-lang.org/reference/items/traits.html#object-safety + bool is_object_safe (bool emit_error, Location locus) const; + + void apply_generic_arguments (HIR::GenericArgs *generic_args); + + bool contains_item (const std::string &search) const; + + TypeBoundPredicateItem + lookup_associated_item (const std::string &search) const; + + HIR::GenericArgs *get_generic_args () { return args; } + + const HIR::GenericArgs *get_generic_args () const { return args; } + + bool has_generic_args () const + { + if (args == nullptr) + return false; + + return args->has_generic_args (); + } + +private: + DefId reference; + Location locus; + HIR::GenericArgs *args; +}; + // https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.VariantDef.html class VariantDef {