Skip to content

Associated type basics & Deref support #1408

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

Merged
merged 9 commits into from
Jun 16, 2019
Merged

Conversation

flodiebold
Copy link
Member

This adds the necessary Chalk integration to handle associated types and uses it to implement support for Deref in the * operator and autoderef; so e.g. dot completions through an Arc work now.

It doesn't yet implement resolution of associated types in paths, though. Also, there's a big FIXME about handling variables in the solution we get from Chalk correctly.

@flodiebold flodiebold requested a review from matklad June 15, 2019 16:37
Copy link
Member

@matklad matklad left a comment

Choose a reason for hiding this comment

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

Excited about this one!

Left some comments!

fn normalize(
&self,
krate: Crate,
goal: crate::ty::Canonical<crate::ty::traits::ProjectionPredicate>,
Copy link
Member

Choose a reason for hiding this comment

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

Maybe we should make ty a facade module and use ty::Smth everywhere?

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmm. I agree for ProjectionPredicate, but when I started doing this for everything in traits, it feld weird for e.g. Solver and the query functions...

let parameter_kinds = generic_params
.params_including_parent()
.into_iter()
.map(|p| chalk_ir::ParameterKind::Ty(lalrpop_intern::intern(&p.name.to_string())))
Copy link
Member

Choose a reason for hiding this comment

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

I feel uneasy about lalrpop_intern: it uses threadlocals, so is not thread safe, and it leaks data.I guess nothing really bad happens, but, longer term, I wish we did something smarter here.

Copy link
Member

Choose a reason for hiding this comment

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

Or rather, something smarter in chalk

Copy link
Member

Choose a reason for hiding this comment

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

Heh, lalrpop_intern::InternedString is marked Send, which doesn't really makes sense because itnterner is thread local.

I guess, in theory it could be useful, if you access interner on the one thread, and pass a reference to it to another thread, but that looks like a niche use-case.

OTOH, accidentally using the same index on two different threads seems like a footgun

Copy link
Member

Choose a reason for hiding this comment

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

Hm, actually, could this be a real bug? We can call chalk from different threads, and, if it caches InternedString inside, that would lead to a wrong result!

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, good question... I was unhappy about having to add the lalrpop dependency anyway, ideally we wouldn't need to provide this at all. I expect Chalk doesn't actually use the name except for debug output...

@nikomatsakis @scalexm Do you know whether this is a problem? Would it be possible to remove the parameter names / make them optional?

Copy link
Member

Choose a reason for hiding this comment

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

Ah, if that's only for debug it's centrally fine short-term. Long term, yeah, it would be cool to separate debug interface from the main interface.

Copy link
Member Author

Choose a reason for hiding this comment

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

Hm, actually it turns out it does use the name: When there's an 'unselected' projection like Vec::SomeAssociatedType where it's not clear from which trait the associated type actually comes. So we aren't concerned with that yet, but we'll need to handle it somehow.

Maybe it'd be better to just have some AssociatedTypeNameId in Chalk and let us do the interning ourselves...

@flodiebold
Copy link
Member Author

Addressed most comments, the big open question is the interning now...

@matklad
Copy link
Member

matklad commented Jun 16, 2019

bors r+

bors bot added a commit that referenced this pull request Jun 16, 2019
1408: Associated type basics & Deref support r=matklad a=flodiebold

This adds the necessary Chalk integration to handle associated types and uses it to implement support for `Deref` in the `*` operator and autoderef; so e.g. dot completions through an `Arc` work now.

It doesn't yet implement resolution of associated types in paths, though. Also, there's a big FIXME about handling variables in the solution we get from Chalk correctly.

Co-authored-by: Florian Diebold <[email protected]>
@bors
Copy link
Contributor

bors bot commented Jun 16, 2019

Build succeeded

@bors bors bot merged commit ad3673d into rust-lang:master Jun 16, 2019
@matklad
Copy link
Member

matklad commented Jun 16, 2019 via email

teoxoy added a commit to teoxoy/rust-analyzer that referenced this pull request Jan 22, 2025
The limit was introduced in rust-lang#1408 (comment) to avoid infinite cycles but it effectively caps the number of derefs to 10. Types like `ID3D12Device14` from the `windows` crate run into this because it derefs to `ID3D12Device13`, 13 to 12 and so on. Increasing it to 20 is a quick fix; a better cycle detection method would be nicer long term.
lnicola pushed a commit to lnicola/rust that referenced this pull request Jan 27, 2025
The limit was introduced in rust-lang/rust-analyzer#1408 (comment) to avoid infinite cycles but it effectively caps the number of derefs to 10. Types like `ID3D12Device14` from the `windows` crate run into this because it derefs to `ID3D12Device13`, 13 to 12 and so on. Increasing it to 20 is a quick fix; a better cycle detection method would be nicer long term.
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.

2 participants