Repository: astral-sh/ruff
File: crates/ty_python_semantic/src/types/constraints.rs (Rust)

Code before:
                    for post in posts {
                        maybe_write_prefix(f)?;
                        write!(f, "{} → {}", ante.display(self.db), post.display(self.db))?;
                    }
                }

                if first {
                    f.write_str("[no sequents]")?;
                }
                Ok(())
            }
        }

        DisplaySequentMap {
            map: self,
            prefix,
            db,
        }
    }
}

impl PartialEq for SequentMap<'_> {
    fn eq(&self, _other: &Self) -> bool {
        false
    }
}

#[expect(unsafe_code)]
unsafe impl salsa::Update for SequentMap<'_> {
    unsafe fn maybe_update(old_pointer: *mut Self, new_map: Self) -> bool {
        let old_map: &mut Self = unsafe { &mut *old_pointer };
        *old_map = new_map;
        true
    }
}

/// The collection of constraints that we know to be true or false at a certain point when
/// traversing a BDD.
#[derive(Debug)]
pub(crate) struct PathAssignments<'db> {
    map: SequentMap<'db>,
    assignments: FxOrderMap<ConstraintAssignment<'db>, usize>,
}

impl<'db> PathAssignments<'db> {
    /// Walks one of the outgoing edges of an internal BDD node. `assignment` describes the
    /// constraint that the BDD node checks, and whether we are following the `if_true` or
    /// `if_false` edge.
    ///
    /// This new assignment might cause this path to become impossible — for instance, if we were
    /// already assuming (from an earlier edge in the path) a constraint that is disjoint with this

Review comment (suggestion):
I think this is the same as setting `[no_eq]` on the query (salsa will not do any backdating, meaning all queries reading the `sequent_map` of a particular interior node will re-run even if it creates the exact same `SeqMap`. Are there any other fields that we could base `Eq` on (e.g., the ones that don't change :)).

Looks very straightforward otherwise :)

Code after:
                    f.write_str("[no sequents]")?;
                }
                Ok(())
            }
        }

        DisplaySequentMap {
            map: self,
            prefix,
            db,
        }
    }
}

/// The collection of constraints that we know to be true or false at a certain point when
/// traversing a BDD.
#[derive(Debug)]
pub(crate) struct PathAssignments<'db> {
    map: SequentMap<'db>,
    assignments: FxIndexMap<ConstraintAssignment<'db>, usize>,
    /// Constraints that we have discovered, mapped to whether we have processed them yet. (This
    /// ensures a stable order for all of the derived constraints that we create, while still
    /// letting us create them lazily.)
    discovered: FxIndexMap<ConstrainedTypeVar<'db>, bool>,
}

impl<'db> PathAssignments<'db> {
    fn new(constraints: impl IntoIterator<Item = ConstrainedTypeVar<'db>>) -> Self {
        let discovered = constraints
            .into_iter()
            .map(|constraint| (constraint, false))
            .collect();
        Self {
            map: SequentMap::default(),
            assignments: FxIndexMap::default(),
            discovered,
        }
    }

    /// Walks one of the outgoing edges of an internal BDD node. `assignment` describes the
    /// constraint that the BDD node checks, and whether we are following the `if_true` or
    /// `if_false` edge.
    ///
    /// This new assignment might cause this path to become impossible — for instance, if we were
    /// already assuming (from an earlier edge in the path) a constraint that is disjoint with this
    /// one. We might also be able to infer _other_ assignments that do not appear in the BDD
    /// directly, but which are implied from a combination of constraints that we _have_ seen.
    ///
    /// To handle all of this, you provide a callback. If the path has become impossible, we will
    /// return `None` _without invoking the callback_. If the path does not contain any
    /// contradictions, we will invoke the callback and return its result (wrapped in `Some`).