Skip to content

Commit 161a690

Browse files
bors[bot]mathstuf
andauthored
Merge #770
770: TraitImpl: track the polarity of the impl r=philberty a=mathstuf The internal representation now tracks whether a trait impl is a positive or negative impl. Signed-off-by: Ben Boeckel <[email protected]> Fixes: #732 Co-authored-by: Ben Boeckel <[email protected]>
2 parents d4bfbb0 + fbc7f0e commit 161a690

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
@@ -2562,6 +2562,7 @@ class ImplBlock : public VisItem
25622562
std::unique_ptr<Type> impl_type;
25632563
std::unique_ptr<TypePath> trait_ref;
25642564
WhereClause where_clause;
2565+
Polarity polarity;
25652566
AST::AttrVec inner_attrs;
25662567
Location locus;
25672568
std::vector<std::unique_ptr<ImplItem>> impl_items;
@@ -2572,20 +2573,20 @@ class ImplBlock : public VisItem
25722573
std::vector<std::unique_ptr<GenericParam>> generic_params,
25732574
std::unique_ptr<Type> impl_type,
25742575
std::unique_ptr<TypePath> trait_ref, WhereClause where_clause,
2575-
Visibility vis, AST::AttrVec inner_attrs, AST::AttrVec outer_attrs,
2576-
Location locus)
2576+
Polarity polarity, Visibility vis, AST::AttrVec inner_attrs,
2577+
AST::AttrVec outer_attrs, Location locus)
25772578
: VisItem (std::move (mappings), std::move (vis), std::move (outer_attrs)),
25782579
generic_params (std::move (generic_params)),
25792580
impl_type (std::move (impl_type)), trait_ref (std::move (trait_ref)),
2580-
where_clause (std::move (where_clause)),
2581+
where_clause (std::move (where_clause)), polarity (polarity),
25812582
inner_attrs (std::move (inner_attrs)), locus (locus),
25822583
impl_items (std::move (impl_items))
25832584
{}
25842585

25852586
ImplBlock (ImplBlock const &other)
25862587
: VisItem (other), impl_type (other.impl_type->clone_type ()),
2587-
where_clause (other.where_clause), inner_attrs (other.inner_attrs),
2588-
locus (other.locus)
2588+
where_clause (other.where_clause), polarity (other.polarity),
2589+
inner_attrs (other.inner_attrs), locus (other.locus)
25892590
{
25902591
generic_params.reserve (other.generic_params.size ());
25912592
for (const auto &e : other.generic_params)
@@ -2601,6 +2602,7 @@ class ImplBlock : public VisItem
26012602
VisItem::operator= (other);
26022603
impl_type = other.impl_type->clone_type ();
26032604
where_clause = other.where_clause;
2605+
polarity = other.polarity;
26042606
inner_attrs = other.inner_attrs;
26052607
locus = other.locus;
26062608

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

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

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)