salsa-rs / salsa

A generic framework for on-demand, incrementalized computation. Inspired by adapton, glimmer, and rustc's query system.
https://salsa-rs.netlify.app/
Apache License 2.0
2.14k stars 152 forks source link

Support tracked methods without self #585

Open nikomatsakis opened 1 month ago

nikomatsakis commented 1 month ago

This is an error today:

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

#[salsa::tracked]
impl<'db> SymTy<'db> {
    #[salsa::tracked]
    pub fn unit(db: &'db dyn Db) -> Self {}
}

...but why? I can still desugar this relatively easily to a regular function calling __unit (see below), so why can't the macro do it?

#[salsa::tracked]
fn __unit(db: &'db dyn Db) -> SymTy<'db> { ... }
nikomatsakis commented 1 month ago

Mentoring instructions

Tracked methods are defined in the proc macro here:

https://github.com/salsa-rs/salsa/blob/a20b894cc2318c5a6077659e2d8aef7c784c38e7/components/salsa-macros/src/tracked_impl.rs#L33-L43

It works by modifying tracked methods in place with this method:

https://github.com/salsa-rs/salsa/blob/a20b894cc2318c5a6077659e2d8aef7c784c38e7/components/salsa-macros/src/tracked_impl.rs#L45-L53

so that they expand to a call to the setup_method_body! macro-rules macro:

https://github.com/salsa-rs/salsa/blob/a20b894cc2318c5a6077659e2d8aef7c784c38e7/components/salsa-macro-rules/src/setup_method_body.rs#L23-L38

We need to modify these to be a bit more general.