https://gcc.gnu.org/g:6a9e567f32a837a5477e6efcb09a9a9d30535b24
commit r17-6262-g6a9e567f32a837a5477e6efcb09a9a9d30535b24 Author: Pierre-Emmanuel Patry <[email protected]> Date: Sat Nov 7 15:25:29 2026 +0100 gccrs: Use RawPath for TypePath We want to keep both segments and opening scope resolution flag in the same place. To do so we enforce usage of a structure that keep both and should avoid using vector of segment. gcc/rust/ChangeLog: * hir/rust-ast-lower-type.cc (ASTLowerTypePath::visit): Use RawPath within constructor. * hir/rust-hir-dump.cc (Dump::visit): Use new getter. * hir/tree/rust-hir-path.cc (GenericArgs::GenericArgs): Reformat. (TypePathFunction::TypePathFunction): Likewise. (TypePath::TypePath): Use RawPath instead of separate opening scope flag. (TypePath::operator=): Use flags. * hir/tree/rust-hir-path.h (class GenericArgs): Reformat. (class TypePathFunction): Likewise. (class TypePath): Likewise and use RawPath. Remove duplicated function. (class QualifiedPathInType): Reformat. * hir/tree/rust-hir.cc (TypePath::to_string): Use getter. (TypePath::as_simple_path): Likewise. * typecheck/rust-hir-type-check-type.cc (TypeResolveGenericParam::apply_trait_bounds): Update ctor call. Signed-off-by: Pierre-Emmanuel Patry <[email protected]> Diff: --- gcc/rust/hir/rust-ast-lower-type.cc | 8 +++-- gcc/rust/hir/rust-hir-dump.cc | 2 +- gcc/rust/hir/tree/rust-hir-path.cc | 21 ++++++------- gcc/rust/hir/tree/rust-hir-path.h | 43 ++++++++++---------------- gcc/rust/hir/tree/rust-hir.cc | 4 +-- gcc/rust/typecheck/rust-hir-type-check-type.cc | 3 +- 6 files changed, 35 insertions(+), 46 deletions(-) diff --git a/gcc/rust/hir/rust-ast-lower-type.cc b/gcc/rust/hir/rust-ast-lower-type.cc index cca5cb30c13a..8c3970a0e66e 100644 --- a/gcc/rust/hir/rust-ast-lower-type.cc +++ b/gcc/rust/hir/rust-ast-lower-type.cc @@ -142,10 +142,12 @@ ASTLowerTypePath::visit (AST::TypePath &path) Analysis::NodeMapping mapping (crate_num, path.get_node_id (), hirid, mappings.get_next_localdef_id (crate_num)); + RawPath<std::unique_ptr<HIR::TypePathSegment>> translated_path ( + std::move (translated_segments), path.has_opening_scope_resolution ()); + translated - = new HIR::TypePath (std::move (mapping), std::move (translated_segments), - path.get_locus (), - path.has_opening_scope_resolution ()); + = new HIR::TypePath (std::move (mapping), std::move (translated_path), + path.get_locus ()); } HIR::QualifiedPathInType * diff --git a/gcc/rust/hir/rust-hir-dump.cc b/gcc/rust/hir/rust-hir-dump.cc index a1893b163eb8..3d1f4982f904 100644 --- a/gcc/rust/hir/rust-hir-dump.cc +++ b/gcc/rust/hir/rust-hir-dump.cc @@ -786,7 +786,7 @@ Dump::visit (TypePath &e) { begin ("TypePath"); put_field ("has_opening_scope_resolution", - std::to_string (e.has_opening_scope_resolution_op ())); + std::to_string (e.has_opening_scope_resolution ())); visit_collection ("segments", e.get_segments ()); diff --git a/gcc/rust/hir/tree/rust-hir-path.cc b/gcc/rust/hir/tree/rust-hir-path.cc index 8f5dec819459..f120152ce098 100644 --- a/gcc/rust/hir/tree/rust-hir-path.cc +++ b/gcc/rust/hir/tree/rust-hir-path.cc @@ -82,7 +82,7 @@ GenericArgs::operator= (GenericArgs const &other) } GenericArgs::GenericArgs (std::vector<Lifetime> lifetime_args, - std::vector<std::unique_ptr<Type> > type_args, + std::vector<std::unique_ptr<Type>> type_args, std::vector<GenericArgsBinding> binding_args, std::vector<ConstGenericArg> const_args, location_t locus) @@ -219,7 +219,7 @@ TypePathSegmentGeneric::TypePathSegmentGeneric (Analysis::NodeMapping mappings, TypePathSegmentGeneric::TypePathSegmentGeneric ( Analysis::NodeMapping mappings, std::string segment_name, bool has_separating_scope_resolution, std::vector<Lifetime> lifetime_args, - std::vector<std::unique_ptr<Type> > type_args, + std::vector<std::unique_ptr<Type>> type_args, std::vector<GenericArgsBinding> binding_args, std::vector<ConstGenericArg> const_args, location_t locus) : TypePathSegment (std::move (mappings), std::move (segment_name), @@ -229,7 +229,7 @@ TypePathSegmentGeneric::TypePathSegmentGeneric ( locus)) {} -TypePathFunction::TypePathFunction (std::vector<std::unique_ptr<Type> > inputs, +TypePathFunction::TypePathFunction (std::vector<std::unique_ptr<Type>> inputs, std::unique_ptr<Type> type) : inputs (std::move (inputs)), return_type (std::move (type)) {} @@ -278,16 +278,14 @@ TypePathSegmentFunction::TypePathSegmentFunction ( {} TypePath::TypePath (Analysis::NodeMapping mappings, - std::vector<std::unique_ptr<TypePathSegment> > segments, - location_t locus, bool has_opening_scope_resolution) - : TypeNoBounds (mappings, locus), - has_opening_scope_resolution (has_opening_scope_resolution), - segments (std::move (segments)) + RawPath<std::unique_ptr<TypePathSegment>> path, + location_t locus) + : TypeNoBounds (mappings, locus), RawPath (std::move (path)) {} TypePath::TypePath (TypePath const &other) : TypeNoBounds (other.mappings, other.locus), - has_opening_scope_resolution (other.has_opening_scope_resolution) + RawPath ({}, other.has_opening_scope_resolution ()) { segments.reserve (other.segments.size ()); for (const auto &e : other.segments) @@ -297,7 +295,7 @@ TypePath::TypePath (TypePath const &other) TypePath & TypePath::operator= (TypePath const &other) { - has_opening_scope_resolution = other.has_opening_scope_resolution; + opening_scope_resolution = other.has_opening_scope_resolution (); locus = other.locus; mappings = other.mappings; @@ -381,8 +379,7 @@ QualifiedPathInExpression::QualifiedPathInExpression ( QualifiedPathInType::QualifiedPathInType ( Analysis::NodeMapping mappings, QualifiedPathType qual_path_type, std::unique_ptr<TypePathSegment> associated_segment, - std::vector<std::unique_ptr<TypePathSegment> > path_segments, - location_t locus) + std::vector<std::unique_ptr<TypePathSegment>> path_segments, location_t locus) : TypeNoBounds (mappings, locus), path_type (std::move (qual_path_type)), associated_segment (std::move (associated_segment)), segments (std::move (path_segments)) diff --git a/gcc/rust/hir/tree/rust-hir-path.h b/gcc/rust/hir/tree/rust-hir-path.h index c912faaa0968..4844d3359ced 100644 --- a/gcc/rust/hir/tree/rust-hir-path.h +++ b/gcc/rust/hir/tree/rust-hir-path.h @@ -153,7 +153,7 @@ private: class GenericArgs { std::vector<Lifetime> lifetime_args; - std::vector<std::unique_ptr<Type> > type_args; + std::vector<std::unique_ptr<Type>> type_args; std::vector<GenericArgsBinding> binding_args; std::vector<ConstGenericArg> const_args; location_t locus; @@ -167,7 +167,7 @@ public: } GenericArgs (std::vector<Lifetime> lifetime_args, - std::vector<std::unique_ptr<Type> > type_args, + std::vector<std::unique_ptr<Type>> type_args, std::vector<GenericArgsBinding> binding_args, std::vector<ConstGenericArg> const_args, location_t locus); @@ -199,7 +199,7 @@ public: return lifetime_args; } - std::vector<std::unique_ptr<Type> > &get_type_args () { return type_args; } + std::vector<std::unique_ptr<Type>> &get_type_args () { return type_args; } std::vector<GenericArgsBinding> &get_binding_args () { return binding_args; } @@ -541,7 +541,7 @@ public: std::string segment_name, bool has_separating_scope_resolution, std::vector<Lifetime> lifetime_args, - std::vector<std::unique_ptr<Type> > type_args, + std::vector<std::unique_ptr<Type>> type_args, std::vector<GenericArgsBinding> binding_args, std::vector<ConstGenericArg> const_args, location_t locus); @@ -567,7 +567,7 @@ public: // A function as represented in a type path class TypePathFunction { - std::vector<std::unique_ptr<Type> > inputs; + std::vector<std::unique_ptr<Type>> inputs; std::unique_ptr<Type> return_type; public: @@ -578,7 +578,7 @@ public: bool has_inputs () const { return !inputs.empty (); } // Constructor - TypePathFunction (std::vector<std::unique_ptr<Type> > inputs, + TypePathFunction (std::vector<std::unique_ptr<Type>> inputs, std::unique_ptr<Type> type); // Copy constructor with clone @@ -595,11 +595,11 @@ public: std::string to_string () const; - const std::vector<std::unique_ptr<Type> > &get_params () const + const std::vector<std::unique_ptr<Type>> &get_params () const { return inputs; }; - std::vector<std::unique_ptr<Type> > &get_params () { return inputs; }; + std::vector<std::unique_ptr<Type>> &get_params () { return inputs; }; const Type &get_return_type () const { return *return_type; }; Type &get_return_type () { return *return_type; }; @@ -641,12 +641,9 @@ public: }; // Path used inside types -class TypePath : public TypeNoBounds +class TypePath : public TypeNoBounds, + public RawPath<std::unique_ptr<TypePathSegment>> { -public: - bool has_opening_scope_resolution; - std::vector<std::unique_ptr<TypePathSegment> > segments; - protected: /* Use covariance to implement clone function as returning this object rather * than base */ @@ -660,13 +657,6 @@ protected: } public: - /* Returns whether the TypePath has an opening scope resolution operator (i.e. - * is global path or crate-relative path, not module-relative) */ - bool has_opening_scope_resolution_op () const - { - return has_opening_scope_resolution; - } - // Returns whether the TypePath is in an invalid state. bool is_error () const { return segments.empty (); } @@ -674,14 +664,13 @@ public: static TypePath create_error () { return TypePath (Analysis::NodeMapping::get_error (), - std::vector<std::unique_ptr<TypePathSegment> > (), + RawPath<std::unique_ptr<TypePathSegment>>{{}, false}, UNDEF_LOCATION); } // Constructor TypePath (Analysis::NodeMapping mappings, - std::vector<std::unique_ptr<TypePathSegment> > segments, - location_t locus, bool has_opening_scope_resolution = false); + RawPath<std::unique_ptr<TypePathSegment>> path, location_t locus); // Copy constructor with vector clone TypePath (TypePath const &other); @@ -707,7 +696,7 @@ public: size_t get_num_segments () const { return segments.size (); } - std::vector<std::unique_ptr<TypePathSegment> > &get_segments () + std::vector<std::unique_ptr<TypePathSegment>> &get_segments () { return segments; } @@ -831,7 +820,7 @@ class QualifiedPathInType : public TypeNoBounds { QualifiedPathType path_type; std::unique_ptr<TypePathSegment> associated_segment; - std::vector<std::unique_ptr<TypePathSegment> > segments; + std::vector<std::unique_ptr<TypePathSegment>> segments; protected: /* Use covariance to implement clone function as returning this object rather @@ -852,7 +841,7 @@ public: QualifiedPathInType ( Analysis::NodeMapping mappings, QualifiedPathType qual_path_type, std::unique_ptr<TypePathSegment> associated_segment, - std::vector<std::unique_ptr<TypePathSegment> > path_segments, + std::vector<std::unique_ptr<TypePathSegment>> path_segments, location_t locus = UNDEF_LOCATION); // Copy constructor with vector clone @@ -874,7 +863,7 @@ public: TypePathSegment &get_associated_segment () { return *associated_segment; } - std::vector<std::unique_ptr<TypePathSegment> > &get_segments () + std::vector<std::unique_ptr<TypePathSegment>> &get_segments () { return segments; } diff --git a/gcc/rust/hir/tree/rust-hir.cc b/gcc/rust/hir/tree/rust-hir.cc index 34a1a645f8b8..d64699a6cbb8 100644 --- a/gcc/rust/hir/tree/rust-hir.cc +++ b/gcc/rust/hir/tree/rust-hir.cc @@ -2153,7 +2153,7 @@ TypePath::to_string () const { std::string str; - if (has_opening_scope_resolution) + if (has_opening_scope_resolution ()) { str = "::"; } @@ -2319,7 +2319,7 @@ TypePath::as_simple_path () const } return AST::SimplePath (std::move (simple_segments), - has_opening_scope_resolution, locus); + has_opening_scope_resolution (), locus); } std::string diff --git a/gcc/rust/typecheck/rust-hir-type-check-type.cc b/gcc/rust/typecheck/rust-hir-type-check-type.cc index 7b8176508de7..d4c2c3588861 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-type.cc +++ b/gcc/rust/typecheck/rust-hir-type-check-type.cc @@ -922,8 +922,9 @@ TypeResolveGenericParam::apply_trait_bounds (HIR::TypeParam ¶m, param.get_mappings ().get_nodeid (), implicit_id, param.get_mappings ().get_local_defid ()); + RawPath<std::unique_ptr<HIR::TypePathSegment>> path ({}, false); implicit_self_bound = std::make_unique<HIR::TypePath> ( - HIR::TypePath (mappings, {}, BUILTINS_LOCATION, false)); + HIR::TypePath (mappings, std::move (path), BUILTINS_LOCATION)); } std::map<DefId, std::vector<TyTy::TypeBoundPredicate>> predicates;
