Skip to content

Commit

Permalink
Document flip polarity
Browse files Browse the repository at this point in the history
  • Loading branch information
spastorino committed Oct 22, 2021
1 parent 7829d9d commit c4c76a4
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
3 changes: 3 additions & 0 deletions compiler/rustc_infer/src/traits/mod.rs
Expand Up @@ -56,6 +56,9 @@ pub type PredicateObligation<'tcx> = Obligation<'tcx, ty::Predicate<'tcx>>;
pub type TraitObligation<'tcx> = Obligation<'tcx, ty::PolyTraitPredicate<'tcx>>;

impl PredicateObligation<'tcx> {
/// Flips the polarity of the inner predicate.
///
/// Given `T: Trait` predicate it returns `T: !Trait` and given `T: !Trait` returns `T: Trait`.
pub fn flip_polarity(&self, tcx: TyCtxt<'tcx>) -> Option<PredicateObligation<'tcx>> {
Some(PredicateObligation {
cause: self.cause.clone(),
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_middle/src/ty/mod.rs
Expand Up @@ -190,6 +190,7 @@ pub enum ImplPolarity {
}

impl ImplPolarity {
/// Flips polarity by turning `Positive` into `Negative` and `Negative` into `Positive`.
pub fn flip(&self) -> Option<ImplPolarity> {
match self {
ImplPolarity::Positive => Some(ImplPolarity::Negative),
Expand Down Expand Up @@ -492,6 +493,9 @@ impl<'tcx> Predicate<'tcx> {
self.inner.kind
}

/// Flips the polarity of a Predicate.
///
/// Given `T: Trait` predicate it returns `T: !Trait` and given `T: !Trait` returns `T: Trait`.
pub fn flip_polarity(&self, tcx: TyCtxt<'tcx>) -> Option<Predicate<'tcx>> {
let kind = self
.inner
Expand Down

0 comments on commit c4c76a4

Please sign in to comment.