Skip to content

Support tracked associated functions #858

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
regexident opened this issue May 10, 2025 · 4 comments · May be fixed by #859
Closed

Support tracked associated functions #858

regexident opened this issue May 10, 2025 · 4 comments · May be fixed by #859

Comments

@regexident
Copy link
Contributor

In #319 @nikomatsakis motivated the introduction of support for tracked methods (i.e. associated functions with a self argument).

I would like to propose extending this to type-level associated functions (i.e. without self argument) as well.

Tracked associated functions would have to follow the same function signature rules as their equivalent free functions.

As such the following:

#[salsa::input]
struct MyInput { ... }

#[salsa::tracked]
struct MyOutput { ... }

#[salsa::tracked]
fn output_from_input(db: &dyn crate::Db, input: MyInput) -> MyOutput { ... }

// ...

let input = MyInput::new(db,);
let output: MyOutput = output_from_input(db, input);

… could be rewritten as such:

#[salsa::input]
struct MyInput { ... }

#[salsa::tracked]
struct MyOutput { ... }

#[salsa::tracked]
impl MyOutput {
    #[salsa::tracked]
    fn from_input(db: &dyn crate::Db, input: MyInput) -> Self { ... }
}

// ...

let input = MyInput::new(db,);
let output = MyOutput::from_input(db, input);

While I'm using a "constructor" function here as a motivational example, they are obviously merely one of may use cases for tracked associated functions.

As such support for tracked associated functions would allow for neatly grouping tracked functions into (even untracked, and otherwise unrelated) types:

#[salsa::input]
struct MyInput { ... }

#[salsa::tracked]
struct MyOutput { ... }

struct OutputGenerator;

#[salsa::tracked]
impl OutputGenerator {
    #[salsa::tracked]
    fn output_from_input(db: &dyn crate::Db, input: MyInput) -> MyOutput { ... }
}

// ...

let input = MyInput::new(db,);
let output: MyOutput = OutputGenerator::output(db, input);

The OutputGenerator type kind of like acts like a "namespace" type here, but with additional benefits (for those who need id), such as providing the tracked functions access to its associated types (a major limitation of free functions in a "namespace" module).


I'd argue that tracked associated functions would not only fill a gap in salsa's current expressivity, they also tend to feel more idiomatic and and support for semantic grouping (beyond merely showing functions in a common module) makes the code look more cleaned up overall.

@MichaReiser
Copy link
Contributor

This is already supported. You can add a salsa::tracked attribute to an impl block and you can then mark specific methods as tracked. What's currently not supported is methods without a self argument but that restriction should be easy to lift

@regexident
Copy link
Contributor Author

regexident commented May 10, 2025

What's currently not supported is methods without a self argument but that restriction should be easy to lift

That's exactly what I'm proposing. 😊

I would like to propose extending this to type-level associated functions (i.e. without self argument) as well.

And I already have a working branch that I'll open a PR for in a bit.

@MichaReiser
Copy link
Contributor

I think this is the same as #585

@regexident
Copy link
Contributor Author

Oh, no. How the heck did I miss that when searching? Feel free to close as duplicate then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants