Skip to content

Commit febf2cf

Browse files
committed
Reduce node id sharing
Also removes some redundant copies and improves TypePath copying. gcc/rust/ChangeLog: * ast/rust-ast-builder.cc (Builder::trait_bound): Replace copy with move. (Builder::trait_impl): Likewise. (Builder::new_const_param): Replace cloning with reconstruction. * ast/rust-path.h (TypePath::TypePath): Use TypeNoBounds copy constructor. (TypePath::operator=): Use TypeNoBounds copy assignment. * expand/rust-derive-clone.cc (DeriveClone::clone_impl): Produce trait bounds lazily. * expand/rust-derive-copy.cc (DeriveCopy::copy_impl): Likewise. * expand/rust-derive-debug.cc (DeriveDebug::stub_derive_impl): Likewise. * expand/rust-derive-default.cc (DeriveDefault::default_impl): Likewise. * expand/rust-derive-eq.cc (DeriveEq::eq_impls): Likewise. * expand/rust-derive-hash.cc (DeriveHash::hash_impl): Likewise. * expand/rust-derive-ord.cc (DeriveOrd::cmp_impl): Likewise. * expand/rust-derive-partial-eq.cc (DerivePartialEq::partialeq_impls): Likewise. * expand/rust-derive.cc (DeriveVisitor::setup_impl_generics): Likewise. * expand/rust-derive.h (DeriveVisitor::setup_impl_generics): Likewise. Signed-off-by: Owen Avery <[email protected]>
1 parent b940d2c commit febf2cf

12 files changed

+61
-52
lines changed

gcc/rust/ast/rust-ast-builder.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ Builder::loop (std::vector<std::unique_ptr<Stmt>> &&stmts)
510510
std::unique_ptr<TypeParamBound>
511511
Builder::trait_bound (TypePath bound)
512512
{
513-
return std::make_unique<TraitBound> (bound, loc);
513+
return std::make_unique<TraitBound> (std::move (bound), loc);
514514
}
515515

516516
std::unique_ptr<Item>
@@ -520,7 +520,7 @@ Builder::trait_impl (TypePath trait_path, std::unique_ptr<Type> target,
520520
WhereClause where_clause, Visibility visibility) const
521521
{
522522
return std::unique_ptr<Item> (
523-
new TraitImpl (trait_path, /* unsafe */ false,
523+
new TraitImpl (std::move (trait_path), /* unsafe */ false,
524524
/* exclam */ false, std::move (trait_items),
525525
std::move (generics), std::move (target), where_clause,
526526
visibility, {}, {}, loc));
@@ -567,7 +567,7 @@ std::unique_ptr<GenericParam>
567567
Builder::new_const_param (ConstGenericParam &param) const
568568
{
569569
return std::make_unique<ConstGenericParam> (param.get_name (),
570-
param.get_type ().clone_type (),
570+
param.get_type ().reconstruct (),
571571
param.get_default_value (),
572572
param.get_outer_attrs (),
573573
param.get_locus ());

gcc/rust/ast/rust-path.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,10 +1216,10 @@ class TypePath : public TypeNoBounds
12161216

12171217
// Copy constructor with vector clone
12181218
TypePath (TypePath const &other)
1219-
: has_opening_scope_resolution (other.has_opening_scope_resolution),
1219+
: TypeNoBounds (other),
1220+
has_opening_scope_resolution (other.has_opening_scope_resolution),
12201221
locus (other.locus)
12211222
{
1222-
node_id = other.node_id;
12231223
segments.reserve (other.segments.size ());
12241224
for (const auto &e : other.segments)
12251225
segments.push_back (e->clone_type_path_segment ());
@@ -1228,7 +1228,7 @@ class TypePath : public TypeNoBounds
12281228
// Overloaded assignment operator with clone
12291229
TypePath &operator= (TypePath const &other)
12301230
{
1231-
node_id = other.node_id;
1231+
TypeNoBounds::operator= (other);
12321232
has_opening_scope_resolution = other.has_opening_scope_resolution;
12331233
locus = other.locus;
12341234

gcc/rust/expand/rust-derive-clone.cc

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,17 +89,17 @@ DeriveClone::clone_impl (
8989
std::unique_ptr<AssociatedItem> &&clone_fn, std::string name,
9090
const std::vector<std::unique_ptr<GenericParam>> &type_generics)
9191
{
92-
// we should have two of these, so we don't run into issues with
93-
// two paths sharing a node id
94-
auto clone_bound = builder.type_path (LangItem::Kind::CLONE);
95-
auto clone_trait_path = builder.type_path (LangItem::Kind::CLONE);
92+
auto clone_trait_path
93+
= [this] () { return builder.type_path (LangItem::Kind::CLONE); };
9694

9795
auto trait_items = vec (std::move (clone_fn));
9896

99-
auto generics = setup_impl_generics (name, type_generics,
100-
builder.trait_bound (clone_bound));
97+
auto generics = setup_impl_generics (name, type_generics, [&, this] () {
98+
return builder.trait_bound (clone_trait_path ());
99+
});
101100

102-
return builder.trait_impl (clone_trait_path, std::move (generics.self_type),
101+
return builder.trait_impl (clone_trait_path (),
102+
std::move (generics.self_type),
103103
std::move (trait_items),
104104
std::move (generics.impl));
105105
}

gcc/rust/expand/rust-derive-copy.cc

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,14 @@ DeriveCopy::copy_impl (
4242
std::string name,
4343
const std::vector<std::unique_ptr<GenericParam>> &type_generics)
4444
{
45-
// we should have two of these, so we don't run into issues with
46-
// two paths sharing a node id
47-
auto copy_bound = builder.type_path (LangItem::Kind::COPY);
48-
auto copy_trait_path = builder.type_path (LangItem::Kind::COPY);
45+
auto copy_trait_path
46+
= [this] () { return builder.type_path (LangItem::Kind::COPY); };
4947

50-
auto generics = setup_impl_generics (name, type_generics,
51-
builder.trait_bound (copy_bound));
48+
auto generics = setup_impl_generics (name, type_generics, [&, this] () {
49+
return builder.trait_bound (copy_trait_path ());
50+
});
5251

53-
return builder.trait_impl (copy_trait_path, std::move (generics.self_type),
52+
return builder.trait_impl (copy_trait_path (), std::move (generics.self_type),
5453
{}, std::move (generics.impl));
5554
}
5655

gcc/rust/expand/rust-derive-debug.cc

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,14 @@ DeriveDebug::stub_derive_impl (
8181
{
8282
auto trait_items = vec (stub_debug_fn ());
8383

84-
auto debug = builder.type_path ({"core", "fmt", "Debug"}, true);
85-
auto generics
86-
= setup_impl_generics (name, type_generics, builder.trait_bound (debug));
87-
88-
return builder.trait_impl (debug, std::move (generics.self_type),
84+
auto debug = [this] () {
85+
return builder.type_path ({"core", "fmt", "Debug"}, true);
86+
};
87+
auto generics = setup_impl_generics (name, type_generics, [&, this] () {
88+
return builder.trait_bound (debug ());
89+
});
90+
91+
return builder.trait_impl (debug (), std::move (generics.self_type),
8992
std::move (trait_items),
9093
std::move (generics.impl));
9194
}

gcc/rust/expand/rust-derive-default.cc

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,17 @@ DeriveDefault::default_impl (
6969
std::unique_ptr<AssociatedItem> &&default_fn, std::string name,
7070
const std::vector<std::unique_ptr<GenericParam>> &type_generics)
7171
{
72-
auto default_path = builder.type_path ({"core", "default", "Default"}, true);
72+
auto default_path = [this] () {
73+
return builder.type_path ({"core", "default", "Default"}, true);
74+
};
7375

7476
auto trait_items = vec (std::move (default_fn));
7577

76-
auto generics = setup_impl_generics (name, type_generics,
77-
builder.trait_bound (default_path));
78+
auto generics = setup_impl_generics (name, type_generics, [&, this] () {
79+
return builder.trait_bound (default_path ());
80+
});
7881

79-
return builder.trait_impl (default_path, std::move (generics.self_type),
82+
return builder.trait_impl (default_path (), std::move (generics.self_type),
8083
std::move (trait_items),
8184
std::move (generics.impl));
8285
}

gcc/rust/expand/rust-derive-eq.cc

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,19 +118,17 @@ DeriveEq::eq_impls (
118118
std::unique_ptr<AssociatedItem> &&fn, std::string name,
119119
const std::vector<std::unique_ptr<GenericParam>> &type_generics)
120120
{
121-
// We create two copies of the type-path to avoid duplicate NodeIds
122-
auto eq = get_eq_trait_path (builder);
123-
auto eq_bound = builder.trait_bound (get_eq_trait_path (builder));
121+
auto eq = [this] () { return get_eq_trait_path (builder); };
122+
auto eq_bound = [&, this] () { return builder.trait_bound (eq ()); };
124123

125124
auto steq = builder.type_path (LangItem::Kind::STRUCTURAL_TEQ);
126125

127126
auto trait_items = vec (std::move (fn));
128127

129-
auto eq_generics
130-
= setup_impl_generics (name, type_generics, std::move (eq_bound));
128+
auto eq_generics = setup_impl_generics (name, type_generics, eq_bound);
131129
auto steq_generics = setup_impl_generics (name, type_generics);
132130

133-
auto eq_impl = builder.trait_impl (eq, std::move (eq_generics.self_type),
131+
auto eq_impl = builder.trait_impl (eq (), std::move (eq_generics.self_type),
134132
std::move (trait_items),
135133
std::move (eq_generics.impl));
136134

gcc/rust/expand/rust-derive-hash.cc

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,17 @@ DeriveHash::hash_impl (
7777
std::unique_ptr<AssociatedItem> &&hash_fn, std::string name,
7878
const std::vector<std::unique_ptr<GenericParam>> &type_generics)
7979
{
80-
auto hash_path = builder.type_path ({"core", "hash", "Hash"}, true);
80+
auto hash_path = [this] () {
81+
return builder.type_path ({"core", "hash", "Hash"}, true);
82+
};
8183

8284
auto trait_items = vec (std::move (hash_fn));
8385

84-
auto generics = setup_impl_generics (name, type_generics,
85-
builder.trait_bound (hash_path));
86+
auto generics = setup_impl_generics (name, type_generics, [&, this] () {
87+
return builder.trait_bound (hash_path ());
88+
});
8689

87-
return builder.trait_impl (hash_path, std::move (generics.self_type),
90+
return builder.trait_impl (hash_path (), std::move (generics.self_type),
8891
std::move (trait_items),
8992
std::move (generics.impl));
9093
}

gcc/rust/expand/rust-derive-ord.cc

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,19 @@ DeriveOrd::cmp_impl (
5858
auto fn = cmp_fn (std::move (fn_block), type_name);
5959

6060
auto trait = ordering == Ordering::Partial ? "PartialOrd" : "Ord";
61-
auto trait_path = builder.type_path ({"core", "cmp", trait}, true);
61+
auto trait_path = [&, this] () {
62+
return builder.type_path ({"core", "cmp", trait}, true);
63+
};
6264

6365
auto trait_bound
64-
= builder.trait_bound (builder.type_path ({"core", "cmp", trait}, true));
66+
= [&, this] () { return builder.trait_bound (trait_path ()); };
6567

6668
auto trait_items = vec (std::move (fn));
6769

6870
auto cmp_generics
69-
= setup_impl_generics (type_name.as_string (), type_generics,
70-
std::move (trait_bound));
71+
= setup_impl_generics (type_name.as_string (), type_generics, trait_bound);
7172

72-
return builder.trait_impl (trait_path, std::move (cmp_generics.self_type),
73+
return builder.trait_impl (trait_path (), std::move (cmp_generics.self_type),
7374
std::move (trait_items),
7475
std::move (cmp_generics.impl));
7576
}

gcc/rust/expand/rust-derive-partial-eq.cc

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,18 @@ DerivePartialEq::partialeq_impls (
4242
std::unique_ptr<AssociatedItem> &&eq_fn, std::string name,
4343
const std::vector<std::unique_ptr<GenericParam>> &type_generics)
4444
{
45-
auto eq = builder.type_path (LangItem::Kind::EQ);
45+
auto eq = [this] () { return builder.type_path (LangItem::Kind::EQ); };
4646
auto speq = builder.type_path (LangItem::Kind::STRUCTURAL_PEQ);
4747

4848
auto trait_items = vec (std::move (eq_fn));
4949

5050
// no extra bound on StructuralPeq
51-
auto peq_generics
52-
= setup_impl_generics (name, type_generics, builder.trait_bound (eq));
51+
auto peq_generics = setup_impl_generics (name, type_generics, [&, this] () {
52+
return builder.trait_bound (eq ());
53+
});
5354
auto speq_generics = setup_impl_generics (name, type_generics);
5455

55-
auto peq = builder.trait_impl (eq, std::move (peq_generics.self_type),
56+
auto peq = builder.trait_impl (eq (), std::move (peq_generics.self_type),
5657
std::move (trait_items),
5758
std::move (peq_generics.impl));
5859

0 commit comments

Comments
 (0)