Skip to content

Missing #[salsa::db] on impl block should fail compilation #613

Closed
@MichaReiser

Description

@MichaReiser

The following example misses a #[salsa::db] on the impl Db for Pimpl but it only fails at runtime with a panic in as_view.

Ideally, we would catch this error at compile time. If this isn't possible, improve the error message to guide users towards the proper fix.

use salsa::Accumulator;

#[salsa::db]
trait Db: salsa::Database {}

#[salsa::db]
pub(crate) struct Pimpl {
    storage: salsa::Storage<Self>,
}

#[salsa::db]
impl salsa::Database for Pimpl {
    fn salsa_event(&self, _event: &dyn Fn() -> salsa::Event) {}
}

// #[salsa::db] <-----MISSING
impl Db for Pimpl {
}

#[salsa::input]
struct File {
    #[return_ref]
    contents: String,
}

#[salsa::tracked]
struct Element<'db> {}

#[salsa::accumulator]
struct Diagnostic(&'static str);

#[salsa::tracked]
fn parse_single_file(db: &dyn Db, file: File) -> Option<Element<'_>> {
    Diagnostic("argh").accumulate(db);
    None
}

impl Pimpl {
    pub(crate) fn new() -> Self {
        Self {
            storage: Default::default(),
        }
    }

}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn it_works() {
        let mut pimpl = Pimpl::new();
        let this = &mut pimpl;
        let file = File::new(this, "Hello World!".to_string());
        let node = parse_single_file(this, file);
        // FIXME: This panics with 'Unwrap on None value'
        parse_single_file::accumulated::<Diagnostic>(this, file);
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions