Unnamed repository; edit this file 'description' to name the repository.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//! Things which exist to solve practical issues, but which shouldn't exist.
//!
//! Please avoid adding new usages of the functions in this module

use parser::Edition;

use crate::{ast, AstNode};

pub fn parse_expr_from_str(s: &str, edition: Edition) -> Option<ast::Expr> {
    let s = s.trim();

    let file = ast::SourceFile::parse(
        // Need a newline because the text may contain line comments.
        &format!("const _: () = ({s}\n);"),
        edition,
    );
    let expr = file.syntax_node().descendants().find_map(ast::ParenExpr::cast)?;
    // Can't check the text because the original text may contain whitespace and comments.
    // Wrap in parentheses to better allow for verification. Of course, the real fix is
    // to get rid of this hack.
    expr.expr()
}
d='n92' href='#n92'>92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
//!
use crate::debug::TableEntry;
use crate::durability::Durability;
use crate::hash::FxIndexMap;
use crate::plumbing::CycleRecoveryStrategy;
use crate::plumbing::InputQueryStorageOps;
use crate::plumbing::QueryStorageMassOps;
use crate::plumbing::QueryStorageOps;
use crate::revision::Revision;
use crate::runtime::StampedValue;
use crate::Database;
use crate::Query;
use crate::Runtime;
use crate::{DatabaseKeyIndex, QueryDb};
use indexmap::map::Entry;
use parking_lot::RwLock;
use std::convert::TryFrom;
use std::iter;
use tracing::debug;

/// Input queries store the result plus a list of the other queries
/// that they invoked. This means we can avoid recomputing them when
/// none of those inputs have changed.
pub struct InputStorage<Q>
where
    Q: Query,
{
    group_index: u16,
    slots: RwLock<FxIndexMap<Q::Key, Slot<Q::Value>>>,
}

struct Slot<V> {
    database_key_index: DatabaseKeyIndex,
    stamped_value: RwLock<StampedValue<V>>,
}

impl<Q> std::panic::RefUnwindSafe for InputStorage<Q>
where
    Q: Query,
    Q::Key: std::panic::RefUnwindSafe,
    Q::Value: std::panic::RefUnwindSafe,
{
}

impl<Q> QueryStorageOps<Q> for InputStorage<Q>
where
    Q: Query,
{
    const CYCLE_STRATEGY: crate::plumbing::CycleRecoveryStrategy = CycleRecoveryStrategy::Panic;

    fn new(group_index: u16) -> Self {
        InputStorage { group_index, slots: Default::default() }
    }

    fn fmt_index(
        &self,
        _db: &<Q as QueryDb<'_>>::DynDb,
        index: DatabaseKeyIndex,
        fmt: &mut std::fmt::Formatter<'_>,
    ) -> std::fmt::Result {
        assert_eq!(index.group_index, self.group_index);
        assert_eq!(index.query_index, Q::QUERY_INDEX);
        let slot_map = self.slots.read();
        let key = slot_map.get_index(index.key_index as usize).unwrap().0;
        write!(fmt, "{}({:?})", Q::QUERY_NAME, key)
    }

    fn maybe_changed_after(
        &self,
        db: &<Q as QueryDb<'_>>::DynDb,
        input: DatabaseKeyIndex,
        revision: Revision,
    ) -> bool {
        assert_eq!(input.group_index, self.group_index);
        assert_eq!(input.query_index, Q::QUERY_INDEX);
        debug_assert!(revision < db.salsa_runtime().current_revision());
        let slots = &self.slots.read();
        let slot = slots.get_index(input.key_index as usize).unwrap().1;

        debug!("maybe_changed_after(slot={:?}, revision={:?})", Q::default(), revision,);

        let changed_at = slot.stamped_value.read().changed_at;

        debug!("maybe_changed_after: changed_at = {:?}", changed_at);

        changed_at > revision
    }

    fn fetch(&self, db: &<Q as QueryDb<'_>>::DynDb, key: &Q::Key) -> Q::Value {
        db.unwind_if_cancelled();

        let slots = &self.slots.read();
        let slot = slots
            .get(key)
            .unwrap_or_else(|| panic!("no value set for {:?}({:?})", Q::default(), key));

        let StampedValue { value, durability, changed_at } = slot.stamped_value.read().clone();

        db.salsa_runtime().report_query_read_and_unwind_if_cycle_resulted(
            slot.database_key_index,
            durability,
            changed_at,
        );

        value
    }

    fn durability(&self, _db: &<Q as QueryDb<'_>>::DynDb, key: &Q::Key) -> Durability {
        match self.slots.read().get(key) {
            Some(slot) => slot.stamped_value.read().durability,
            None => panic!("no value set for {:?}({:?})", Q::default(), key),
        }
    }

    fn entries<C>(&self, _db: &<Q as QueryDb<'_>>::DynDb) -> C
    where
        C: std::iter::FromIterator<TableEntry<Q::Key, Q::Value>>,
    {
        let slots = self.slots.read();
        slots
            .iter()
            .map(|(key, slot)| {
                TableEntry::new(key.clone(), Some(slot.stamped_value.read().value.clone()))
            })
            .collect()
    }
}

impl<Q> QueryStorageMassOps for InputStorage<Q>
where
    Q: Query,
{
    fn purge(&self) {
        *self.slots.write() = Default::default();
    }
}

impl<Q> InputQueryStorageOps<Q> for InputStorage<Q>
where
    Q: Query,
{
    fn set(&self, runtime: &mut Runtime, key: &Q::Key, value: Q::Value, durability: Durability) {
        tracing::debug!("{:?}({:?}) = {:?} ({:?})", Q::default(), key, value, durability);

        // The value is changing, so we need a new revision (*). We also
        // need to update the 'last changed' revision by invoking
        // `guard.mark_durability_as_changed`.
        //
        // CAREFUL: This will block until the global revision lock can
        // be acquired. If there are still queries executing, they may
        // need to read from this input. Therefore, we wait to acquire
        // the lock on `map` until we also hold the global query write
        // lock.
        //
        // (*) Technically, since you can't presently access an input
        // for a non-existent key, and you can't enumerate the set of
        // keys, we only need a new revision if the key used to
        // exist. But we may add such methods in the future and this
        // case doesn't generally seem worth optimizing for.
        runtime.with_incremented_revision(|next_revision| {
            let mut slots = self.slots.write();

            // Do this *after* we acquire the lock, so that we are not
            // racing with somebody else to modify this same cell.
            // (Otherwise, someone else might write a *newer* revision
            // into the same cell while we block on the lock.)
            let stamped_value = StampedValue { value, durability, changed_at: next_revision };

            match slots.entry(key.clone()) {
                Entry::Occupied(entry) => {
                    let mut slot_stamped_value = entry.get().stamped_value.write();
                    let old_durability = slot_stamped_value.durability;
                    *slot_stamped_value = stamped_value;
                    Some(old_durability)
                }

                Entry::Vacant(entry) => {
                    let key_index = u32::try_from(entry.index()).unwrap();
                    let database_key_index = DatabaseKeyIndex {
                        group_index: self.group_index,
                        query_index: Q::QUERY_INDEX,
                        key_index,
                    };
                    entry.insert(Slot {
                        database_key_index,
                        stamped_value: RwLock::new(stamped_value),
                    });
                    None
                }
            }
        });
    }
}

/// Same as `InputStorage`, but optimized for queries that take no inputs.
pub struct UnitInputStorage<Q>
where
    Q: Query<Key = ()>,
{
    group_index: u16,
    slot: UnitSlot<Q::Value>,
}

struct UnitSlot<V> {
    database_key_index: DatabaseKeyIndex,
    stamped_value: RwLock<Option<StampedValue<V>>>,
}

impl<Q> std::panic::RefUnwindSafe for UnitInputStorage<Q>
where
    Q: Query<Key = ()>,
    Q::Key: std::panic::RefUnwindSafe,
    Q::Value: std::panic::RefUnwindSafe,
{
}

impl<Q> QueryStorageOps<Q> for UnitInputStorage<Q>
where
    Q: Query<Key = ()>,
{
    const CYCLE_STRATEGY: crate::plumbing::CycleRecoveryStrategy = CycleRecoveryStrategy::Panic;

    fn new(group_index: u16) -> Self {
        let database_key_index =
            DatabaseKeyIndex { group_index, query_index: Q::QUERY_INDEX, key_index: 0 };
        UnitInputStorage {
            group_index,
            slot: UnitSlot { database_key_index, stamped_value: RwLock::new(None) },
        }
    }

    fn fmt_index(
        &self,
        _db: &<Q as QueryDb<'_>>::DynDb,
        index: DatabaseKeyIndex,
        fmt: &mut std::fmt::Formatter<'_>,
    ) -> std::fmt::Result {
        assert_eq!(index.group_index, self.group_index);
        assert_eq!(index.query_index, Q::QUERY_INDEX);
        write!(fmt, "{}", Q::QUERY_NAME)
    }

    fn maybe_changed_after(
        &self,
        db: &<Q as QueryDb<'_>>::DynDb,
        input: DatabaseKeyIndex,
        revision: Revision,
    ) -> bool {
        assert_eq!(input.group_index, self.group_index);
        assert_eq!(input.query_index, Q::QUERY_INDEX);
        debug_assert!(revision < db.salsa_runtime().current_revision());

        debug!("maybe_changed_after(slot={:?}, revision={:?})", Q::default(), revision,);

        let changed_at = self.slot.stamped_value.read().as_ref().unwrap().changed_at;

        debug!("maybe_changed_after: changed_at = {:?}", changed_at);

        changed_at > revision
    }

    fn fetch(&self, db: &<Q as QueryDb<'_>>::DynDb, &(): &Q::Key) -> Q::Value {
        db.unwind_if_cancelled();

        let StampedValue { value, durability, changed_at } = self
            .slot
            .stamped_value
            .read()
            .clone()
            .unwrap_or_else(|| panic!("no value set for {:?}", Q::default()));

        db.salsa_runtime().report_query_read_and_unwind_if_cycle_resulted(
            self.slot.database_key_index,
            durability,
            changed_at,
        );

        value
    }

    fn durability(&self, _db: &<Q as QueryDb<'_>>::DynDb, &(): &Q::Key) -> Durability {
        match &*self.slot.stamped_value.read() {
            Some(stamped_value) => stamped_value.durability,
            None => panic!("no value set for {:?}", Q::default(),),
        }
    }

    fn entries<C>(&self, _db: &<Q as QueryDb<'_>>::DynDb) -> C
    where
        C: std::iter::FromIterator<TableEntry<Q::Key, Q::Value>>,
    {
        iter::once(TableEntry::new(
            (),
            self.slot.stamped_value.read().as_ref().map(|it| it.value.clone()),
        ))
        .collect()
    }
}

impl<Q> QueryStorageMassOps for UnitInputStorage<Q>
where
    Q: Query<Key = ()>,
{
    fn purge(&self) {
        *self.slot.stamped_value.write() = Default::default();
    }
}

impl<Q> InputQueryStorageOps<Q> for UnitInputStorage<Q>
where
    Q: Query<Key = ()>,
{
    fn set(&self, runtime: &mut Runtime, (): &Q::Key, value: Q::Value, durability: Durability) {
        tracing::debug!("{:?} = {:?} ({:?})", Q::default(), value, durability);

        // The value is changing, so we need a new revision (*). We also
        // need to update the 'last changed' revision by invoking
        // `guard.mark_durability_as_changed`.
        //
        // CAREFUL: This will block until the global revision lock can
        // be acquired. If there are still queries executing, they may
        // need to read from this input. Therefore, we wait to acquire
        // the lock on `map` until we also hold the global query write
        // lock.
        //
        // (*) Technically, since you can't presently access an input
        // for a non-existent key, and you can't enumerate the set of
        // keys, we only need a new revision if the key used to
        // exist. But we may add such methods in the future and this
        // case doesn't generally seem worth optimizing for.
        runtime.with_incremented_revision(|next_revision| {
            let mut stamped_value_slot = self.slot.stamped_value.write();

            // Do this *after* we acquire the lock, so that we are not
            // racing with somebody else to modify this same cell.
            // (Otherwise, someone else might write a *newer* revision
            // into the same cell while we block on the lock.)
            let stamped_value = StampedValue { value, durability, changed_at: next_revision };

            match &mut *stamped_value_slot {
                Some(slot_stamped_value) => {
                    let old_durability = slot_stamped_value.durability;
                    *slot_stamped_value = stamped_value;
                    Some(old_durability)
                }

                stamped_value_slot @ None => {
                    *stamped_value_slot = Some(stamped_value);
                    None
                }
            }
        });
    }
}

/// Check that `Slot<Q, MP>: Send + Sync` as long as
/// `DB::DatabaseData: Send + Sync`, which in turn implies that
/// `Q::Key: Send + Sync`, `Q::Value: Send + Sync`.
#[allow(dead_code)]
fn check_send_sync<Q>()
where
    Q: Query,
    Q::Key: Send + Sync,
    Q::Value: Send + Sync,
{
    fn is_send_sync<T: Send + Sync>() {}
    is_send_sync::<Slot<Q::Value>>();
    is_send_sync::<UnitSlot<Q::Value>>();
}

/// Check that `Slot<Q, MP>: 'static` as long as
/// `DB::DatabaseData: 'static`, which in turn implies that
/// `Q::Key: 'static`, `Q::Value: 'static`.
#[allow(dead_code)]
fn check_static<Q>()
where
    Q: Query + 'static,
    Q::Key: 'static,
    Q::Value: 'static,
{
    fn is_static<T: 'static>() {}
    is_static::<Slot<Q::Value>>();
    is_static::<UnitSlot<Q::Value>>();
}