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

feature: add #[salsa::interned_sans_lifetime] macro #632

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

davidbarsky
Copy link
Contributor

(This consolidates #618, #620, and #623 into a single PR, logical PR.)

Here's what this solves:

  1. the 'db lifetime on interned structs makes migrating some rust-analyzer's existing interned values really challenging.
  2. rust-analyzer wants to fiddle with the underlying ID.
  3. We'd like to be able to fetch the associated, interned data without having the wrapped, interned struct on hand.

We'll probably port rust-analyzer over the new, idiomatic API instead, but this makes the migration a lot easier!

Copy link

netlify bot commented Dec 17, 2024

Deploy Preview for salsa-rs canceled.

Name Link
🔨 Latest commit 6d23f3b
🔍 Latest deploy log https://app.netlify.com/sites/salsa-rs/deploys/67634d5e1af9d300087f7a67

Copy link

codspeed-hq bot commented Dec 17, 2024

CodSpeed Performance Report

Merging #632 will not alter performance

Comparing davidbarsky:davidbarsky/add-interned-sans-lifetime-macro (6d23f3b) with master (0ac5c1c)

Summary

✅ 9 untouched benchmarks

@nikomatsakis
Copy link
Member

@davidbarsky ....

---- components/salsa-macros/src/lib.rs - interned_sans_lifetime (line 91) stdout ----
error[E0433]: failed to resolve: use of undeclared crate or module `salsa`
  --> components/salsa-macros/src/lib.rs:107:3
   |
18 | #[salsa::interned_sans_lifetime(id = CustomSalsaIdWrapper)]
   |   ^^^^^ use of undeclared crate or module `salsa`

@MichaReiser
Copy link
Contributor

We discussed that we want to discourage this pattern but it would also be nice if we can avoid duplicating the interned macro. Would it be possible to gate the functionality behind a feature? E.g. that the interned macro only supports a no_lifetime argument if salsa_macros is used with the compat feature.

@Veykril
Copy link
Member

Veykril commented Dec 18, 2024

Given the goal is for this to only ease the transition for r-a it should be fine (and honestly preferred in my eyes) to gate this behind a feature until r-a no longer needs it (at which point we can remove it again).

@davidbarsky
Copy link
Contributor Author

To expand as to why this is a separate macro instead of something like #[salsa::interned(no_lifetime)], there are two reasons:

  1. I wrote this for expediency's sake to confirm whether this is something that I even want/need in rust-analyzer. Copying is pretty easy, after all!
  2. I didn't know how to make $db_lt optional in a macro_rules-based macro: namely, it's not clear to me how I can do control flow as I'd do in a proc macro, but this might be my inexperience with macro_rules :).
    1. If it's not possible to do control flow in a macro_rules-based macro; a default lifetime parameter a la https://internals.rust-lang.org/t/default-lifetime-parameters/12073 would've been great, but, unfortunately, that doesn't exist today.

@MichaReiser
Copy link
Contributor

MichaReiser commented Dec 18, 2024

It's hard to tell for me what the difference is between the two macros but would it be possible to change $db_lt to either be <'db> (so that it includes the <..> or an empty string ifno_lifetime` is used.

Or you might be able to do something similar to clone and no_clone

https://github.com/MichaReiser/salsa/blob/e68679b3a9c2b5cfd8eab92de89edf4073b03601/components/salsa-macro-rules/src/maybe_clone.rs#L5-L21

@davidbarsky
Copy link
Contributor Author

It's hard to tell for me what the difference is between the two macros

I do too, but difftastic helped here. I don't think it's a particularly useful paste, but if you have it handy, difft components/salsa-macro-rules/src/setup_interned_struct.rs components/salsa-macro-rules/src/setup_interned_struct_sans_lifetime.rs will show the difference. The primary differences are:

  1. The addition of a StructData parameter to the macro. I didn't want pull in paste, so I just made the ident in the proc macro.
  2. The addition of the $Id parameter, to allow the ID overriding behavior we need in rust-analyzer.
  3. Moving salsa::plumbing::interned::Configuration out of the const _: () = {...} block in order to get the data through the underlying ID alone.
  4. The removal of lifetimes and a few select additions of 'static, as needed.

I honestly recommend cloning this PR and diffing via difftastic.

would it be possible to change $db_lt to either be <'db> (so that it includes the <..>) or an empty string if no_lifetime is used?

Not that I'm aware of, at least with macro_rules!-based macros. I don't think we can invoke maybe_*-style macros in the type position. From a "how can we unify these macros", I think it'd require moving things away from salsa-macro-rules into a (mostly) pure quote!-based approach.

@davidbarsky davidbarsky force-pushed the davidbarsky/add-interned-sans-lifetime-macro branch from 1612f16 to 6d23f3b Compare December 18, 2024 22:31
@davidbarsky
Copy link
Contributor Author

I fixed the rustdoc failures, but I can't reproduce the test failure w.r.t to cancellation on my Mac. That's a bit concerning, but I think I'll try and debug this tomorrow.

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 this pull request may close these issues.

4 participants