From: Kushal Pal <kushalpal...@gmail.com> gcc/rust/ChangeLog:
* checks/errors/borrowck/rust-bir-builder-internal.h: Use STATIC_FREE_REGION, use value of FreeRegion for origin. * checks/errors/borrowck/rust-bir-builder.h: Use free region value. * checks/errors/borrowck/rust-bir-dump.cc (Dump::visit_scope): Likewise. * checks/errors/borrowck/rust-bir-fact-collector.h (points): Likewise. * checks/errors/borrowck/rust-bir-free-region.h (struct FreeRegion): Make FreeRegion a struct. * checks/errors/borrowck/rust-bir-place.h: Use FreeRegion instead of Origin in PlaceDB. * typecheck/rust-tyty-variance-analysis.cc (FieldVisitorCtx::add_constraints_from_region): Use value of FreeRegion for origin. (FieldVisitorCtx::add_constrints_from_param): Likewise. (Term::make_transform): Likewise. Signed-off-by: Kushal Pal <kushalpal...@gmail.com> --- .../borrowck/rust-bir-builder-internal.h | 11 +++--- .../checks/errors/borrowck/rust-bir-builder.h | 6 ++-- .../checks/errors/borrowck/rust-bir-dump.cc | 2 +- .../errors/borrowck/rust-bir-fact-collector.h | 36 ++++++++++--------- .../errors/borrowck/rust-bir-free-region.h | 30 +++++++++++++--- .../checks/errors/borrowck/rust-bir-place.h | 14 +++++--- .../typecheck/rust-tyty-variance-analysis.cc | 6 ++-- 7 files changed, 68 insertions(+), 37 deletions(-) diff --git a/gcc/rust/checks/errors/borrowck/rust-bir-builder-internal.h b/gcc/rust/checks/errors/borrowck/rust-bir-builder-internal.h index 74be02b90f6..afaeb12d458 100644 --- a/gcc/rust/checks/errors/borrowck/rust-bir-builder-internal.h +++ b/gcc/rust/checks/errors/borrowck/rust-bir-builder-internal.h @@ -215,7 +215,7 @@ protected: } else if (region.is_static ()) { - free_regions.push_back (0); + free_regions.push_back (STATIC_FREE_REGION); } else if (region.is_anonymous ()) { @@ -314,9 +314,10 @@ protected: // Helpers to add BIR statements { auto mutability = ty->as<const TyTy::ReferenceType> ()->mutability (); auto loan = ctx.place_db.add_loan ({mutability, place_id, location}); - push_tmp_assignment (new BorrowExpr (place_id, loan, - ctx.place_db.get_next_free_region ()), - ty, location); + push_tmp_assignment ( + new BorrowExpr (place_id, loan, + ctx.place_db.get_next_free_region ().value), + ty, location); return translated; } @@ -609,7 +610,7 @@ protected: {ty->as<const TyTy::ReferenceType> ()->mutability (), place_id, location}); return_expr (new BorrowExpr (place_id, loan, - ctx.place_db.get_next_free_region ()), + ctx.place_db.get_next_free_region ().value), ty, location); return translated; } diff --git a/gcc/rust/checks/errors/borrowck/rust-bir-builder.h b/gcc/rust/checks/errors/borrowck/rust-bir-builder.h index e3d61b5b36e..4e48ced3299 100644 --- a/gcc/rust/checks/errors/borrowck/rust-bir-builder.h +++ b/gcc/rust/checks/errors/borrowck/rust-bir-builder.h @@ -94,8 +94,8 @@ private: ctx.fn_free_regions[bound.second.get_index ()]); auto last_bound = universal_region_bounds.back (); - rust_debug ("\t\t %lu: %lu", (unsigned long) last_bound.first, - (unsigned long) last_bound.second); + rust_debug ("\t\t %lu: %lu", (unsigned long) last_bound.first.value, + (unsigned long) last_bound.second.value); } // TODO: handle type_region constraints @@ -178,7 +178,7 @@ private: if (generic_param->get_kind () == HIR::GenericParam::GenericKind::LIFETIME) { - result[regions[region_index++]] + result[regions[region_index++].value] = static_cast<HIR::LifetimeParam *> (generic_param.get ()); } } diff --git a/gcc/rust/checks/errors/borrowck/rust-bir-dump.cc b/gcc/rust/checks/errors/borrowck/rust-bir-dump.cc index 924c7d9a6e8..e69c0002b3c 100644 --- a/gcc/rust/checks/errors/borrowck/rust-bir-dump.cc +++ b/gcc/rust/checks/errors/borrowck/rust-bir-dump.cc @@ -380,7 +380,7 @@ Dump::visit_scope (ScopeId id, size_t depth) print_comma_separated (stream, func.place_db[local].regions.get_regions (), [this] (FreeRegion region_id) { - stream << "'?" << region_id; + stream << "'?" << region_id.value; }); stream << "]\n"; } diff --git a/gcc/rust/checks/errors/borrowck/rust-bir-fact-collector.h b/gcc/rust/checks/errors/borrowck/rust-bir-fact-collector.h index fa7cbb38eef..ed2133a361b 100644 --- a/gcc/rust/checks/errors/borrowck/rust-bir-fact-collector.h +++ b/gcc/rust/checks/errors/borrowck/rust-bir-fact-collector.h @@ -107,14 +107,15 @@ protected: // Main collection entry points (for different categories). for (auto ®ion : universal_regions) { - facts.universal_region.emplace_back (region); - facts.placeholder.emplace_back (region, next_loan++); - facts.known_placeholder_subset.emplace_back (0, region); + facts.universal_region.emplace_back (region.value); + facts.placeholder.emplace_back (region.value, next_loan++); + facts.known_placeholder_subset.emplace_back (0, region.value); } // Copy already collected subset facts, that are universally valid. for (auto &bound : universal_region_bounds) - facts.known_placeholder_subset.emplace_back (bound.first, bound.second); + facts.known_placeholder_subset.emplace_back (bound.first.value, + bound.second.value); } void visit_places (const std::vector<PlaceId> &args) @@ -131,7 +132,7 @@ protected: // Main collection entry points (for different categories). facts.path_is_var.emplace_back (place_id.value, place_id.value); for (auto ®ion : place.regions) facts.use_of_var_derefs_origin.emplace_back (place_id.value, - region); + region.value); // TODO: drop_of_var_derefs_origin break; @@ -178,10 +179,11 @@ protected: // Main collection entry points (for different categories). rust_debug ("\tSanitize deref of %s", base.tyty->as_string ().c_str ()); - std::vector<Polonius::Origin> regions; + std::vector<FreeRegion> regions; regions.insert (regions.end (), base.regions.begin () + 1, base.regions.end ()); - FreeRegions r (std::move (regions)); + FreeRegions r; + r.set_from (std::move (regions)); push_subset_all (place.tyty, r, place.regions); } void sanizite_field (PlaceId place_id) @@ -199,7 +201,8 @@ protected: // Main collection entry points (for different categories). .query_field_regions (base.tyty->as<TyTy::ADTType> (), 0, place.variable_or_field_index, base.regions); // FIXME - FreeRegions f (std::move (r)); + FreeRegions f; + f.set_from (std::move (r)); push_subset_all (place.tyty, f, place.regions); } @@ -326,14 +329,14 @@ protected: // Main collection entry points (for different categories). issue_loan (expr.get_origin (), expr.get_loan ()); } - push_subset (main_loan_place.regions[0], expr.get_origin ()); + push_subset (main_loan_place.regions[0], {expr.get_origin ()}); } else { issue_loan (expr.get_origin (), expr.get_loan ()); } - auto loan_regions = base_place.regions.prepend (expr.get_origin ()); + auto loan_regions = base_place.regions.prepend ({expr.get_origin ()}); push_subset (ref_place.tyty, loan_regions, ref_place.regions); } @@ -632,19 +635,20 @@ protected: // Generic BIR operations. protected: // Subset helpers. void push_subset (FreeRegion lhs, FreeRegion rhs) { - rust_debug ("\t\tpush_subset: '?%lu: '?%lu", (unsigned long) lhs, - (unsigned long) rhs); + rust_debug ("\t\tpush_subset: '?%lu: '?%lu", (unsigned long) lhs.value, + (unsigned long) rhs.value); - facts.subset_base.emplace_back (lhs, rhs, get_current_point_mid ()); + facts.subset_base.emplace_back (lhs.value, rhs.value, + get_current_point_mid ()); } void push_subset_all (FreeRegion lhs, FreeRegion rhs) { - rust_debug ("\t\tpush_subset_all: '?%lu: '?%lu", (unsigned long) lhs, - (unsigned long) rhs); + rust_debug ("\t\tpush_subset_all: '?%lu: '?%lu", (unsigned long) lhs.value, + (unsigned long) rhs.value); for (auto point : cfg_points_all) - facts.subset_base.emplace_back (lhs, rhs, point); + facts.subset_base.emplace_back (lhs.value, rhs.value, point); } void push_subset (Variance variance, FreeRegion lhs, FreeRegion rhs) diff --git a/gcc/rust/checks/errors/borrowck/rust-bir-free-region.h b/gcc/rust/checks/errors/borrowck/rust-bir-free-region.h index c09e8883dad..735b1722194 100644 --- a/gcc/rust/checks/errors/borrowck/rust-bir-free-region.h +++ b/gcc/rust/checks/errors/borrowck/rust-bir-free-region.h @@ -24,7 +24,19 @@ namespace Rust { -using FreeRegion = size_t; +struct FreeRegion +{ + size_t value; + // some overloads for comparision + bool operator== (const FreeRegion &rhs) const { return value == rhs.value; } + bool operator!= (const FreeRegion &rhs) const { return !(operator== (rhs)); } + bool operator< (const FreeRegion &rhs) const { return value < rhs.value; } + bool operator> (const FreeRegion &rhs) const { return value > rhs.value; } + bool operator<= (const FreeRegion &rhs) const { return !(operator> (rhs)); } + bool operator>= (const FreeRegion &rhs) const { return !(operator< (rhs)); } +}; + +static constexpr FreeRegion STATIC_FREE_REGION = {0}; class FreeRegions { @@ -46,6 +58,14 @@ public: this->regions.push_back ({region}); } } + void set_from (std::vector<FreeRegion> &®ions) + { + this->regions.clear (); + for (auto ®ion : regions) + { + this->regions.push_back (region); + } + } WARN_UNUSED_RESULT FreeRegions prepend (FreeRegion region) const { @@ -55,13 +75,14 @@ public: } FreeRegions (std::vector<FreeRegion> &®ions) : regions (regions) {} + FreeRegions () {} WARN_UNUSED_RESULT std::string to_string () const { std::stringstream result; for (auto ®ion : regions) { - result << region; + result << region.value; result << ", "; } // Remove the last ", " from the string. @@ -83,7 +104,8 @@ public: WARN_UNUSED_RESULT FreeRegion get_next_free_region () const { - return next_free_region++; + ++next_free_region.value; + return {next_free_region.value - 1}; } FreeRegions bind_regions (std::vector<TyTy::Region> regions, @@ -95,7 +117,7 @@ public: if (region.is_early_bound ()) free_regions.push_back (parent_free_regions[region.get_index ()]); else if (region.is_static ()) - free_regions.push_back (0); + free_regions.push_back (STATIC_FREE_REGION); else if (region.is_anonymous ()) free_regions.push_back (get_next_free_region ()); else if (region.is_named ()) diff --git a/gcc/rust/checks/errors/borrowck/rust-bir-place.h b/gcc/rust/checks/errors/borrowck/rust-bir-place.h index 1a7a1428df9..01fe1fbc63c 100644 --- a/gcc/rust/checks/errors/borrowck/rust-bir-place.h +++ b/gcc/rust/checks/errors/borrowck/rust-bir-place.h @@ -187,7 +187,7 @@ private: std::vector<Loan> loans; - Polonius::Origin next_free_region = 1; + FreeRegion next_free_region = {1}; public: PlaceDB () @@ -216,7 +216,11 @@ public: const Scope &get_scope (ScopeId id) const { return scopes[id]; } - FreeRegion get_next_free_region () { return next_free_region++; } + FreeRegion get_next_free_region () + { + ++next_free_region.value; + return {next_free_region.value - 1}; + } FreeRegion peek_next_free_region () const { return next_free_region; } @@ -256,8 +260,8 @@ public: ->get_variance_analysis_ctx () .query_type_variances (new_place_ref.tyty); std::vector<Polonius::Origin> regions; - for (size_t i = 0; i < variances.size (); i++) - regions.push_back (next_free_region++); + for (size_t i = 0; i < variances.size (); ++i) + regions.push_back (next_free_region.value++); new_place_ref.regions.set_from (std::move (regions)); @@ -352,7 +356,7 @@ public: void set_next_free_region (Polonius::Origin next_free_region) { - this->next_free_region = next_free_region; + this->next_free_region.value = next_free_region; } PlaceId lookup_or_add_variable (NodeId id, TyTy::BaseType *tyty) diff --git a/gcc/rust/typecheck/rust-tyty-variance-analysis.cc b/gcc/rust/typecheck/rust-tyty-variance-analysis.cc index d7116f5eb80..15c16db421a 100644 --- a/gcc/rust/typecheck/rust-tyty-variance-analysis.cc +++ b/gcc/rust/typecheck/rust-tyty-variance-analysis.cc @@ -570,7 +570,7 @@ FieldVisitorCtx::add_constraints_from_region (const Region ®ion, { if (region.is_early_bound ()) { - regions.push_back (parent_regions[region.get_index ()]); + regions.push_back (parent_regions[region.get_index ()].value); } else if (region.is_late_bound ()) { @@ -585,7 +585,7 @@ FieldVisitorCtx::add_constrints_from_param (ParamType ¶m, Variance variance) for (size_t i = type_param_ranges[param_i]; i < type_param_ranges[param_i + 1]; i++) { - regions.push_back (parent_regions[i]); + regions.push_back (parent_regions[i].value); } } @@ -621,4 +621,4 @@ Term::make_transform (Term lhs, Term rhs) } // namespace VarianceAnalysis } // namespace TyTy -} // namespace Rust \ No newline at end of file +} // namespace Rust -- 2.45.2