Skip to content
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

Proposal: "late" fields on tracked structs #581

Open
nikomatsakis opened this issue Sep 28, 2024 · 0 comments
Open

Proposal: "late" fields on tracked structs #581

nikomatsakis opened this issue Sep 28, 2024 · 0 comments
Labels
bikeshed 🚴‍♀️ Debating API details and the like rfc Active discussion about a possible future feature
Milestone

Comments

@nikomatsakis
Copy link
Member

TL;DR

Permit fields to be tagged as "late"

#[salsa::tracked]
struct Variable {
    name: Id<'db>,
    #[late] ty: Ty<'db>,
}

When you create a tracked struct, you specify only the early fields:

let v = Variable::new(name);

but you can later specify the late fields, so long as it is within the same tracked function (query):

v.set_ty(db, ty);

Panics occur if:

  • there is an attempt to access v.ty(db) but it has not yet been set
  • you reach the end of the tracked function without setting ty

Background

Salsa currently supports a rather obscure feature called "specified" functions that lets you emulate this:

#[salsa::tracked(specify)]
fn ty<'db>(db: &'db dyn Database, v: Variable<'db>) -> Ty<'db> {
    panic!("ty not specified");
}

This lets you specify the value of ty for some v so long as v was created in the current query (and ty has not yet been called on s):

ty::specify(db, v, value)

Supporting the late pattern directly would

  • let us remove specifiable functions, simplifying salsa in my opinion;
  • be mildly more efficient, since we could create space in the fields struct for the field instead of storing the value in the memo table
  • let us check at the end of the query that all late fields were assigned
@nikomatsakis nikomatsakis added rfc Active discussion about a possible future feature bikeshed 🚴‍♀️ Debating API details and the like labels Sep 28, 2024
@nikomatsakis nikomatsakis added this to the Salsa 3.0 milestone Sep 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bikeshed 🚴‍♀️ Debating API details and the like rfc Active discussion about a possible future feature
Projects
None yet
Development

No branches or pull requests

1 participant