Skip to content

Shorten type hints for std::iter Iterators #6154

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 6 commits into from
Oct 7, 2020
Merged

Conversation

Veykril
Copy link
Member

@Veykril Veykril commented Oct 6, 2020

Fixes #3750.

This re-exports the hir_expand::name::known module to be able to fetch the Iterator and iter names.
I'm not sure if there is anything to do with Solution::Ambig in normalize_trait_assoc_type or whether discarding those results is always wanted.

Comment on lines 228 to 233
let iter_trait = module.scope(db, None).into_iter().find_map(|(name, def)| match def {
hir::ScopeDef::ModuleDef(ModuleDef::Trait(r#trait)) if name == known::Iterator => {
Some(r#trait)
}
_ => None,
})?;
Copy link
Member

Choose a reason for hiding this comment

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

Rather then iterating through the scope, we should ask for Iterator directly. Currently, this handled by the FamousDefs type in assists. It is somewhat ad-hoc, long term, I think we need to make use of rustc_diagnostic_item attribute, but, for now, using FamousDefs as an API is the way to go!

Copy link
Member Author

Choose a reason for hiding this comment

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

FamousDefs is crate private, I assume making it pub for this should be alright?

Copy link
Member Author

Choose a reason for hiding this comment

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

I had to change a thing in FamousDefs aside from the visibility. It now also checks the crate you directly pass to it whether that is std/core, due to the crate the hint has already being std/core.

Copy link
Member Author

Choose a reason for hiding this comment

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

And the FIXTURE const can't be cfg(test) gated anymore for some reason. The cfg just doesn't seem to work properly across crate boundaries?

@matklad
Copy link
Member

matklad commented Oct 6, 2020

cc @SomeoneToIgnore

@matklad
Copy link
Member

matklad commented Oct 6, 2020 via email

@Veykril Veykril force-pushed the iter-hints branch 3 times, most recently from 821a55f to 4a39de1 Compare October 6, 2020 19:12
@Veykril
Copy link
Member Author

Veykril commented Oct 6, 2020

@Veykril
Copy link
Member Author

Veykril commented Oct 6, 2020

So sorry for the billion force-pushes I keep forgetting to run cargo test -p xtask to check for whitespaces 😅

@matklad
Copy link
Member

matklad commented Oct 6, 2020 via email

Copy link
Contributor

@SomeoneToIgnore SomeoneToIgnore left a comment

Choose a reason for hiding this comment

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

Looks like a great step forward!

One interesting finding:
image

I wonder, if this is caused by some internal limit being too low?

.path_to_root(db)
.into_iter()
.rev()
.find(|module| module.name(db) == Some(known::iter))?;
Copy link
Contributor

Choose a reason for hiding this comment

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

The way we use known::item here and core_iter_Iterator() is interesting.
We seem to use more or less the same semantics here (some predefined knowledge about sdlib), both using hir inside, yet in totally different parts of the code.

Any thoughts? (maybe @matklad ?)

Copy link
Member Author

Choose a reason for hiding this comment

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

I suppose we could add module lookup to FamousDefs and then check if the module exports the type or something along those lines? Then we wouldn't have to use known here I think

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, I am not sure what this should look like, but funneling everything via FamousDefs seems prudent for future refactorability.

I think the right worklist here:

  • make sure that IDE bits only use FamousDefs
  • replace FamousDefs::FIXTURE with more fine-grained opt-ins at the fixture level (we want to have syntax like //- core with:Iterator,IntoIterator)
  • try to replace as much of path lookup as possible with lang-items and diagnostics items, but keep an eye on ide/compiler split

For this PR, only keeping everything scoped to FamousDefs should be sufficient.

@SomeoneToIgnore
Copy link
Contributor

SomeoneToIgnore commented Oct 6, 2020

Is there a better way to check this?

(I cannot reply to this question directly due to force pushes).
I believe the majority of this method will mutate into a more high-level function, returning the list of all impl Trait possible for the type given.
Yet this might not be feasible to fix now, before we decide what to do with the fact that we need 2 totally different places in the code to define Iter trait related data.

@Veykril
Copy link
Member Author

Veykril commented Oct 7, 2020

The iter structs comes from two module definitions, core::iter::sources and core::iter::adapters, so one possibility would be to check the types declaration module for these two, the other option would be to check if core::iter re-exports the type(which I am kinda doing there right now, just not in a nice way in my eyes). Hence my question if there was a better way to do this.

@Veykril
Copy link
Member Author

Veykril commented Oct 7, 2020

I wonder, if this is caused by some internal limit being too low?

This seems to be chalk failing at that depth, it returns Solution::Ambig with Unknown at that point.

@Veykril
Copy link
Member Author

Veykril commented Oct 7, 2020

Okay I think everything so far got addressed now 👍

@SomeoneToIgnore
Copy link
Contributor

SomeoneToIgnore commented Oct 7, 2020

Thanks for the fixes, the same place in RA now looks way nicer:
image

Interesting that there are hints with the bloated Filter and FilterMap in the associated types, yet the Chalk limit is clearly not reached by this time.
Maybe we need to apply the same hint_iterator logic once more to the associated type.
But that can land in a different PR, I think now we have a good set up to iterate from already, thank you.

Chalk limits is also an interesting thing to tackle, the chaining hints loose their new appearance too fast IMO.
But I think we'd better to wait for issues on the topic with good test examples :)

bors r+

@bors
Copy link
Contributor

bors bot commented Oct 7, 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.

Shorten type hints for iterator
3 participants