Skip to content

Commit fbc7f0e

Browse files
committed
TraitImpl: track the polarity of the impl
The internal representation now tracks whether a trait impl is a positive or negative impl. Signed-off-by: Ben Boeckel <[email protected]>
1 parent d2b12ea commit fbc7f0e

File tree

3 files changed

+29
-20
lines changed

3 files changed

+29
-20
lines changed

gcc/rust/hir/rust-ast-lower-item.h

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -520,13 +520,12 @@ class ASTLoweringItem : public ASTLoweringBase
520520
impl_item_ids.push_back (lowered->get_impl_mappings ().get_hirid ());
521521
}
522522

523-
HIR::ImplBlock *hir_impl_block
524-
= new HIR::ImplBlock (mapping, std::move (impl_items),
525-
std::move (generic_params),
526-
std::unique_ptr<HIR::Type> (impl_type), nullptr,
527-
where_clause, vis, impl_block.get_inner_attrs (),
528-
impl_block.get_outer_attrs (),
529-
impl_block.get_locus ());
523+
Polarity polarity = Positive;
524+
HIR::ImplBlock *hir_impl_block = new HIR::ImplBlock (
525+
mapping, std::move (impl_items), std::move (generic_params),
526+
std::unique_ptr<HIR::Type> (impl_type), nullptr, where_clause, polarity,
527+
vis, impl_block.get_inner_attrs (), impl_block.get_outer_attrs (),
528+
impl_block.get_locus ());
530529
translated = hir_impl_block;
531530

532531
mappings->insert_defid_mapping (mapping.get_defid (), translated);
@@ -689,14 +688,13 @@ class ASTLoweringItem : public ASTLoweringBase
689688
impl_item_ids.push_back (lowered->get_impl_mappings ().get_hirid ());
690689
}
691690

692-
HIR::ImplBlock *hir_impl_block
693-
= new HIR::ImplBlock (mapping, std::move (impl_items),
694-
std::move (generic_params),
695-
std::unique_ptr<HIR::Type> (impl_type),
696-
std::unique_ptr<HIR::TypePath> (trait_ref),
697-
where_clause, vis, impl_block.get_inner_attrs (),
698-
impl_block.get_outer_attrs (),
699-
impl_block.get_locus ());
691+
Polarity polarity = impl_block.is_exclam () ? Positive : Negative;
692+
HIR::ImplBlock *hir_impl_block = new HIR::ImplBlock (
693+
mapping, std::move (impl_items), std::move (generic_params),
694+
std::unique_ptr<HIR::Type> (impl_type),
695+
std::unique_ptr<HIR::TypePath> (trait_ref), where_clause, polarity, vis,
696+
impl_block.get_inner_attrs (), impl_block.get_outer_attrs (),
697+
impl_block.get_locus ());
700698
translated = hir_impl_block;
701699

702700
mappings->insert_defid_mapping (mapping.get_defid (), translated);

gcc/rust/hir/tree/rust-hir-item.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2561,6 +2561,7 @@ class ImplBlock : public VisItem
25612561
std::unique_ptr<Type> impl_type;
25622562
std::unique_ptr<TypePath> trait_ref;
25632563
WhereClause where_clause;
2564+
Polarity polarity;
25642565
AST::AttrVec inner_attrs;
25652566
Location locus;
25662567
std::vector<std::unique_ptr<ImplItem>> impl_items;
@@ -2571,20 +2572,20 @@ class ImplBlock : public VisItem
25712572
std::vector<std::unique_ptr<GenericParam>> generic_params,
25722573
std::unique_ptr<Type> impl_type,
25732574
std::unique_ptr<TypePath> trait_ref, WhereClause where_clause,
2574-
Visibility vis, AST::AttrVec inner_attrs, AST::AttrVec outer_attrs,
2575-
Location locus)
2575+
Polarity polarity, Visibility vis, AST::AttrVec inner_attrs,
2576+
AST::AttrVec outer_attrs, Location locus)
25762577
: VisItem (std::move (mappings), std::move (vis), std::move (outer_attrs)),
25772578
generic_params (std::move (generic_params)),
25782579
impl_type (std::move (impl_type)), trait_ref (std::move (trait_ref)),
2579-
where_clause (std::move (where_clause)),
2580+
where_clause (std::move (where_clause)), polarity (polarity),
25802581
inner_attrs (std::move (inner_attrs)), locus (locus),
25812582
impl_items (std::move (impl_items))
25822583
{}
25832584

25842585
ImplBlock (ImplBlock const &other)
25852586
: VisItem (other), impl_type (other.impl_type->clone_type ()),
2586-
where_clause (other.where_clause), inner_attrs (other.inner_attrs),
2587-
locus (other.locus)
2587+
where_clause (other.where_clause), polarity (other.polarity),
2588+
inner_attrs (other.inner_attrs), locus (other.locus)
25882589
{
25892590
generic_params.reserve (other.generic_params.size ());
25902591
for (const auto &e : other.generic_params)
@@ -2600,6 +2601,7 @@ class ImplBlock : public VisItem
26002601
VisItem::operator= (other);
26012602
impl_type = other.impl_type->clone_type ();
26022603
where_clause = other.where_clause;
2604+
polarity = other.polarity;
26032605
inner_attrs = other.inner_attrs;
26042606
locus = other.locus;
26052607

@@ -2640,6 +2642,9 @@ class ImplBlock : public VisItem
26402642
// Returns whether impl has where clause.
26412643
bool has_where_clause () const { return !where_clause.is_empty (); }
26422644

2645+
// Returns the polarity of the impl.
2646+
Polarity get_polarity () const { return polarity; }
2647+
26432648
// Returns whether impl has inner attributes.
26442649
bool has_inner_attrs () const { return !inner_attrs.empty (); }
26452650

gcc/rust/util/rust-common.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ enum Unsafety
3535
Normal
3636
};
3737

38+
enum Polarity
39+
{
40+
Positive,
41+
Negative
42+
};
43+
3844
} // namespace Rust
3945

4046
#endif // RUST_COMMON

0 commit comments

Comments
 (0)