Skip to content

Add a ton more database lifetimes#22548

Open
Veykril wants to merge 1 commit into
rust-lang:masterfrom
Veykril:lukaswirth/push-ykqkymxvnkyz
Open

Add a ton more database lifetimes#22548
Veykril wants to merge 1 commit into
rust-lang:masterfrom
Veykril:lukaswirth/push-ykqkymxvnkyz

Conversation

@Veykril

@Veykril Veykril commented Jun 8, 2026

Copy link
Copy Markdown
Member

Blocked on salsa-rs/salsa#1111

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 8, 2026
@Veykril Veykril changed the title Unleash more lifetimes Add a ton more database lifetimes Jun 8, 2026
@ChayimFriedman2

Copy link
Copy Markdown
Contributor

To be honest I'm not a fan of all of our IDs gaining lifetimes. I'd much rather a permanent option for disabling the lifetime for both interneds and tracked structs, considering it isn't required for safety.

But, at the very least, I assume we want to wait for @dfireBird's PR to be merged.

@Veykril

Veykril commented Jun 8, 2026

Copy link
Copy Markdown
Member Author

I'd much rather a permanent option for disabling the lifetime for both interneds and tracked structs, considering it isn't required for safety.

Huh? How is that not required for safety? The entire point of the lifetimes is to prevent usage of stale things

@Veykril

Veykril commented Jun 8, 2026

Copy link
Copy Markdown
Member Author

Also the entire lifetime escape mechanism was only introduced to ease our transition, it was never meant to be a permanent thing.

@flodiebold

Copy link
Copy Markdown
Member

FWIW, I agree with Chayim, the lifetimes clutter everything up for questionable gain.

@ChayimFriedman2

Copy link
Copy Markdown
Contributor

Huh? How is that not required for safety? The entire point of the lifetimes is to prevent usage of stale things

I might be wrong, but things I've heard and my knowledge of the Salsa codebase imply this is false.

Furthermore you fundamentally cannot rely on the lifetimes for safety (without branding) since you can use the same ID to access a different database.

It can help correctness a bit but I don't think this is worth it.

Also the entire lifetime escape mechanism was only introduced to ease our transition, it was never meant to be a permanent thing.

I know that, I suggest we keep it (and extend it to tracked structs).

@Veykril

Veykril commented Jun 11, 2026

Copy link
Copy Markdown
Member Author

Fair, I do agree its a bit ugly to have the lifetime infect everything.

I might be wrong, but things I've heard and my knowledge of the Salsa codebase imply this is false.

Furthermore you fundamentally cannot rely on the lifetimes for safety (without branding) since you can use the same ID to access a different database.

I do think salsa relies on the lifetime for safety though fwiw (with the intern gc and what not, at this point), though you are right that the mixing of databases might be problematic 🤔 Will look into that actually.

Though yea, maybe adding the attribute for tracked structs would be good either way (the attr should also be marked unsafe for that matter though)

@ChayimFriedman2

Copy link
Copy Markdown
Contributor

So, you were right. I was able to construct a UB example when using the interneds GC.

Given that, I do support this. IMO preventing unsafety and the need for global reasoning is worth the lifetimes.

@Veykril

Veykril commented Jun 11, 2026

Copy link
Copy Markdown
Member Author

Is it a small self contained example, curious to see in that case.

But, at the very least, I assume we want to wait for dfireBird's PR to be merged.

Also naturally will wait on the lifetime PR to land first, rebasing this PR is a lot easier than other PRs on top of this

@ChayimFriedman2

Copy link
Copy Markdown
Contributor

Is it a small self contained example, curious to see in that case.

use salsa::Setter;

#[salsa::interned(revisions = 1, no_lifetime)]
struct Interned {
    #[returns(ref)]
    s: String,
}

#[salsa::input(debug)]
struct Input {
    i: String,
}

#[salsa::tracked]
fn foo(db: &dyn salsa::Database, i: Input) -> Interned {
    Interned::new(db, i.i(db))
}

#[test]
fn test() {
    let mut db = salsa::DatabaseImpl::default();
    let input = Input::builder("0".to_owned()).new(&db);
    let foo_input = Input::new(&db, "abc def dsgdf".to_owned());
    let foo_input2 = Input::new(&db, String::new());
    let interned = foo(&db, foo_input);

    input.set_i(&mut db).to("123".to_owned());

    let s = &**interned.s(&db);
    foo(&db, foo_input2);
    dbg!(s);
}

@rustbot

This comment has been minimized.

@ada4a ada4a left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, I've been working on implementing what's apparently a subset of this PR^^ Glad to see that my changes seem to match up with what was done here

View changes since this review

Comment thread crates/hir-ty/src/infer.rs Outdated
}

pub fn method_resolution<'db>(&self, expr: ExprId) -> Option<(FunctionId, GenericArgs<'db>)> {
pub fn method_resolution<'a>(&self, expr: ExprId) -> Option<(FunctionId, GenericArgs<'a>)> {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is identical to:

Suggested change
pub fn method_resolution<'a>(&self, expr: ExprId) -> Option<(FunctionId, GenericArgs<'a>)> {
pub fn method_resolution(&self, expr: ExprId) -> Option<(FunctionId, GenericArgs<'static>)> {

which is imo clearer than introducing another lifetime generic?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh dear, the fact this compiles is not good. This is a pretty severe soundness bug as that lifetime is just straight up wrong

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh... In that case you would probably be unhappy to learn that the same pattern occurs throughout that impl block

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea i put a fixme on the actual method that does the bad transmute

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already talked about that, this is pretty much unavoidable and it only makes GC potentially unsound, which is why we marked it unsafe.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, that was that thing.

@Veykril Veykril force-pushed the lukaswirth/push-ykqkymxvnkyz branch from b3a6ada to aeda25b Compare June 14, 2026 07:15
@rustbot

rustbot commented Jun 14, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@Veykril Veykril force-pushed the lukaswirth/push-ykqkymxvnkyz branch from aeda25b to a686ceb Compare June 14, 2026 07:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants