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

Implement Chalk's debug methods using TLS #3748

Merged
merged 1 commit into from
Apr 10, 2020

Conversation

flodiebold
Copy link
Member

Chalk now panics if we don't implement these methods and run with CHALK_DEBUG, so I thought I'd try to implement them 'properly'. Sadly, it seems impossible to do without transmuting lifetimes somewhere. The problem is that we need a &dyn HirDatabase to get names etc., which we can't just put into TLS. I thought I could just use scoped-tls, but that doesn't support references to unsized types. So I put the &dyn into another struct and put the reference to that into the TLS, but I have to transmute the lifetime to 'static for that to work. I think this is sound, but I still don't really want to do it this way...

Having names in the Chalk debug output is very nice, but maybe IDs will have to suffice 😞

@flodiebold flodiebold requested a review from matklad March 27, 2020 18:07
@Diggsey
Copy link

Diggsey commented Mar 27, 2020

What could have been... rust-lang/rust#25232 😛

OP: FnOnce() -> R,
{
let ctx = DebugContext(p);
let static_p: &DebugContext<'static> = unsafe { std::mem::transmute(&ctx) };
Copy link
Member

Choose a reason for hiding this comment

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

transmuteless alternative:, let p: &'static dyn HirDatabase = unsafe { &*(p as const _) };

Copy link
Member Author

Choose a reason for hiding this comment

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

Doesn't work, I get lifetime 'static required 🤔

Copy link
Member Author

Choose a reason for hiding this comment

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

The problem is that the dyn HirDatabase type might have borrowed data in it. I.e. the lifetime error goes away when I make the parameter &dyn HirDatabase + 'static, but then of course I get lifetime errors at the call site.

@matklad
Copy link
Member

matklad commented Mar 27, 2020

tbh, it's TLS and not unsafe that worries me most here... Like, this is just debug, so using tls is probably fine, but still, it feels like a giant hack. I'd much prefer to, eg, parametrize the solver with an option debug trait object.

But I do believe that debuggability is super important, so TLS is better than no debug at all

/me tries not to remember ripping out debug infra just couple of months ago.

Chalk now panics if we don't implement these methods and run with CHALK_DEBUG,
so I thought I'd try to implement them 'properly'. Sadly, it seems impossible to
do without transmuting lifetimes somewhere. The problem is that we need a `&dyn
HirDatabase` to get names etc., which we can't just put into TLS. I thought I
could just use `scoped-tls`, but that doesn't support references to unsized
types. So I put the `&dyn` into another struct and put the reference to *that*
into the TLS, but I have to transmute the lifetime to 'static for that to work.
@flodiebold flodiebold marked this pull request as ready for review April 10, 2020 13:06
@flodiebold
Copy link
Member Author

@matklad Let's merge this, unless you have objections? We explicitly only use the TLS if CHALK_DEBUG is set, so I'm pretty sure this should have no effect at all on normal usage, but it's helpful for debugging.

@matklad
Copy link
Member

matklad commented Apr 10, 2020 via email

@bors
Copy link
Contributor

bors bot commented Apr 10, 2020

@bors bors bot merged commit 0a891d1 into rust-lang:master Apr 10, 2020
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.

5 participants