Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions compiler/rustc_hir_analysis/src/check/wfcheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2478,7 +2478,7 @@ impl<'tcx> WfCheckingCtxt<'_, 'tcx> {
let mut r = Ok(());

let mut validate_and = |and: &And<TyCtxt<'_>, _>| {
for c in and.0.iter() {
for c in and.iter() {
match c {
LeafRegionConstraint::Ambiguity(_)
| LeafRegionConstraint::RegionOutlives(..)
Expand All @@ -2503,7 +2503,7 @@ impl<'tcx> WfCheckingCtxt<'_, 'tcx> {
};

validate_and(&constraint.and_constraint);
for and in constraint.or_constraint.0.iter() {
for and in constraint.or_constraint.iter() {
validate_and(and);
}

Expand Down Expand Up @@ -2607,12 +2607,11 @@ impl<'tcx> WfCheckingCtxt<'_, 'tcx> {
expected: &SolverRegionConstraint<'tcx>,
actual: &SolverRegionConstraint<'tcx>,
) {
let span_of_and = |c: &And<_, _>| {
c.0.iter().map(|leaf| leaf.span()).reduce(|span: Span, acc| acc.to(span))
};
let span_of_and =
|c: &And<_, _>| c.iter().map(|leaf| leaf.span()).reduce(|span: Span, acc| acc.to(span));

let span_of_or = |c: &Or<_, _>| {
c.0.iter().flat_map(|and| span_of_and(and)).reduce(|span, acc| acc.to(span))
c.iter().flat_map(|and| span_of_and(and)).reduce(|span, acc| acc.to(span))
};

let check_leaf_constraint =
Expand All @@ -2638,21 +2637,21 @@ impl<'tcx> WfCheckingCtxt<'_, 'tcx> {
};

let check_and_constraint = |expected: &And<_, _>, actual: &And<_, _>| {
if expected.0.len() != actual.0.len() {
if expected.len() != actual.len() {
Err((span_of_and(expected), span_of_and(actual)))
} else {
for (expected, actual) in expected.0.iter().zip(&actual.0) {
for (expected, actual) in expected.iter().zip(actual) {
check_leaf_constraint(expected, actual)?;
}
Ok(())
}
};

let check_or_constraint = |expected: &Or<_, _>, actual: &Or<_, _>| {
if expected.0.len() != actual.0.len() {
if expected.len() != actual.len() {
Err((span_of_or(expected), span_of_or(actual)))
} else {
for (expected, actual) in expected.0.iter().zip(&actual.0) {
for (expected, actual) in expected.iter().zip(actual) {
check_and_constraint(expected, actual)?;
}
Ok(())
Expand Down
5 changes: 2 additions & 3 deletions compiler/rustc_infer/src/infer/outlives/obligations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,12 +303,11 @@ impl<'tcx> InferCtxt<'tcx> {
}

// FIXME(-Zassumptions-on-binders): actually implement OR as an OR
for c in constraint.and_constraint.0.into_iter().chain(
for c in constraint.and_constraint.into_iter().chain(
constraint
.or_constraint
.0
.into_iter()
.flat_map(|and_constraint| and_constraint.0.into_iter()),
.flat_map(|and_constraint| and_constraint.into_iter()),
) {
match c {
Ambiguity(span) => {
Expand Down
61 changes: 59 additions & 2 deletions compiler/rustc_type_ir/src/region_constraint.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//! The bulk of the logic for implementing `-Zassumptions-on-binders`
use std::ops::Deref;

use derive_where::derive_where;
use indexmap::IndexSet;
Expand Down Expand Up @@ -288,7 +289,9 @@ impl<I: Interner, S: ConstraintSpan> LeafRegionConstraint<I, S> {
/// - No AND is a superset of another AND
/// - All ANDs are in canonical form
/// - If any AND is empty, i.e. trivially true, it is the only AND
pub struct Or<I: Interner, S: Clone + std::fmt::Debug = NoSpan>(pub SmallVec<[And<I, S>; 1]>);
// FIXME(-Zassumptions-on-binders): move to a separate module to avoid accidental
// mutation in this module?
pub struct Or<I: Interner, S: Clone + std::fmt::Debug = NoSpan>(SmallVec<[And<I, S>; 1]>);
impl<I: Interner> Or<I> {
pub fn with_spans<S: ConstraintSpan>(self, span: S) -> Or<I, S> {
Or(self.0.into_iter().map(|and| and.with_spans(span.clone())).collect())
Expand Down Expand Up @@ -417,14 +420,42 @@ impl<I: Interner, S: Clone + std::fmt::Debug> std::fmt::Debug for Or<I, S> {
}
}

impl<I: Interner, S: Clone + std::fmt::Debug> IntoIterator for Or<I, S> {
type Item = And<I, S>;
type IntoIter = <SmallVec<[And<I, S>; 1]> as IntoIterator>::IntoIter;

fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}

impl<'a, I: Interner, S: Clone + std::fmt::Debug> IntoIterator for &'a Or<I, S> {
type Item = &'a And<I, S>;
type IntoIter = <&'a SmallVec<[And<I, S>; 1]> as IntoIterator>::IntoIter;

fn into_iter(self) -> Self::IntoIter {
(&self.0).into_iter()
}
}

impl<I: Interner, S: Clone + std::fmt::Debug> Deref for Or<I, S> {
type Target = SmallVec<[And<I, S>; 1]>;

fn deref(&self) -> &Self::Target {
&self.0
}
}

#[derive_where(Clone, Hash, PartialEq, Eq; I: Interner, S)]
#[derive(TypeVisitable_Generic, GenericTypeVisitable, TypeFoldable_Generic)]
#[cfg_attr(feature = "nightly", derive(StableHash_NoContext))]
/// An AND of leaf constraints. Always in "canonical form", meaning:
/// - No leaf constraints are present twice in this AND
/// - No leaf constraint is trivially true, i.e. a reflexive `'a: 'a`
// FIXME(-Zassumptions-on-binders): move to a separate module to avoid accidental
// mutation in this module?
pub struct And<I: Interner, S: Clone + std::fmt::Debug = NoSpan>(
pub SmallVec<[LeafRegionConstraint<I, S>; 1]>,
SmallVec<[LeafRegionConstraint<I, S>; 1]>,
);
impl<I: Interner> And<I> {
pub fn with_spans<S: ConstraintSpan>(self, span: S) -> And<I, S> {
Expand Down Expand Up @@ -501,6 +532,32 @@ impl<I: Interner, S: Clone + std::fmt::Debug> std::fmt::Debug for And<I, S> {
}
}

impl<I: Interner, S: Clone + std::fmt::Debug> IntoIterator for And<I, S> {
type Item = LeafRegionConstraint<I, S>;
type IntoIter = <SmallVec<[LeafRegionConstraint<I, S>; 1]> as IntoIterator>::IntoIter;

fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}

impl<'a, I: Interner, S: Clone + std::fmt::Debug> IntoIterator for &'a And<I, S> {
type Item = &'a LeafRegionConstraint<I, S>;
type IntoIter = <&'a SmallVec<[LeafRegionConstraint<I, S>; 1]> as IntoIterator>::IntoIter;

fn into_iter(self) -> Self::IntoIter {
(&self.0).into_iter()
}
}

impl<I: Interner, S: Clone + std::fmt::Debug> Deref for And<I, S> {
type Target = SmallVec<[LeafRegionConstraint<I, S>; 1]>;

fn deref(&self) -> &Self::Target {
&self.0
}
}

#[derive_where(Clone, Hash, PartialEq; I: Interner, S)]
#[derive(TypeVisitable_Generic, GenericTypeVisitable, TypeFoldable_Generic)]
#[cfg_attr(feature = "nightly", derive(StableHash_NoContext))]
Expand Down
Loading