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

Improve suggestions for importing out-of-scope traits reexported as _ #91412

Merged
merged 5 commits into from
Dec 21, 2021

Conversation

compiler-errors
Copy link
Member

@compiler-errors compiler-errors commented Dec 1, 2021

  1. Fix up the parent_map query to prefer visible parents that don't export items with the name _.
    • I'm not sure if I modified this query properly. Not sure if we want to check for other idents than kw::Underscore.
    • This also has the side-effect of not doing BFS on any modules re-exported as _, but I think that's desirable here too (or else we get suggestions for paths like a::_::b like in this doctest example).
  2. Bail in try_print_visible_def_path if the def_id is re-exported as _, which will fall back to printing the def-id's real (definition) path.
    • Side-effect of this is that we print paths that are not actually public, but it seems we already sometimes suggest useing paths that are private anyways. See this doctest example for demonstration of current behavior.
  3. Suggest a glob-import (for example my_library::prelude::*) if the trait in question is only pub-exported as _, as a fallback.
    use foo::bar::prelude::*; // trait MyTrait
    
    • I think this is good fallback behavior to suggest instead of doing nothing. Thanks to the original issue filer for suggesting this.

I was somewhat opinionated about behaviors in this PR, and I'm totally open to limiting the impact of my changes or only landing parts of this. Happy to receive feedback if there are better ways to the same end.

Fixes #86035

@rust-highfive
Copy link
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @jackh726 (or someone else) soon.

Please see the contribution instructions for more information.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Dec 1, 2021
@jplatte
Copy link
Contributor

jplatte commented Dec 1, 2021

Not sure if we want to check for other idents than kw::Underscore.

What about #[doc(hidden)] modules that export the symbol out symbols that are themselves #[doc(hidden)]? Are those accounted for already?

@compiler-errors
Copy link
Member Author

Not sure if we want to check for other idents than kw::Underscore.

What about #[doc(hidden)] modules that export the symbol out symbols that are themselves #[doc(hidden)]? Are those accounted for already?

I don't think this factors in doc attributes... Not sure what the precedent on rustc using doc attributes for diagnostics is, so we might want to fix this in a separate issue.

@apiraino apiraino added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Dec 1, 2021
@bors
Copy link
Contributor

bors commented Dec 2, 2021

☔ The latest upstream changes (presumably #91354) made this pull request unmergeable. Please resolve the merge conflicts.

@bors
Copy link
Contributor

bors commented Dec 3, 2021

☔ The latest upstream changes (presumably #91491) made this pull request unmergeable. Please resolve the merge conflicts.

@compiler-errors
Copy link
Member Author

(rebased onto master)

Copy link
Member

@jackh726 jackh726 left a comment

Choose a reason for hiding this comment

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

I'm not really familiar with this code and not comfortable doing a final review, but I left some initial comments.

src/test/ui/imports/unnamed_pub_trait.stderr Show resolved Hide resolved
return Ok((self, false));
}
} else {
// FIXME(compiler-errors): What if we don't have an item corresponding to our
Copy link
Member

Choose a reason for hiding this comment

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

Good question, is there a test case that covers this case?

Copy link
Contributor

Choose a reason for hiding this comment

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

The previous logic didn't do anything, and it's just a missing diagnostic, right? Maybe we could emit a note that the thing is unimportable?

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, this is more like the visible_parent for the item doesn't actually contain the item at all. This is actually probably a bug! (e.g. maybe the visible_parents_map has a bug).

Comment on lines 463 to 466
if let Some(reexport) = reexports
.into_iter()
.find(|child| child.vis.is_public() && child.ident.name != kw::Underscore)
.map(|child| child.ident.name)
Copy link
Member

Choose a reason for hiding this comment

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

Do we actually need to collect into a vec?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point. Probably not.

compiler/rustc_middle/src/ty/print/pretty.rs Outdated Show resolved Hide resolved
@jackh726
Copy link
Member

r? rust-lang/compiler

Not really familiar with this area.

@rust-highfive rust-highfive assigned oli-obk and unassigned jackh726 Dec 18, 2021
@oli-obk
Copy link
Contributor

oli-obk commented Dec 18, 2021

Maybe we should allow use foo::_; to import just the things reexported as _. So basically like use foo::*, but more targeted.

But that's a language change, we can do the suggestion logic first, as this PR does.

Copy link
Member Author

@compiler-errors compiler-errors left a comment

Choose a reason for hiding this comment

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

Addressed comments.

@rustbot label -S-waiting-on-author +S-waiting-on-review

return Ok((self, false));
}
} else {
// FIXME(compiler-errors): What if we don't have an item corresponding to our
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, this is more like the visible_parent for the item doesn't actually contain the item at all. This is actually probably a bug! (e.g. maybe the visible_parents_map has a bug).

Comment on lines 463 to 466
if let Some(reexport) = reexports
.into_iter()
.find(|child| child.vis.is_public() && child.ident.name != kw::Underscore)
.map(|child| child.ident.name)
Copy link
Member Author

Choose a reason for hiding this comment

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

Good point. Probably not.

src/test/ui/imports/unnamed_pub_trait.stderr Show resolved Hide resolved
@oli-obk
Copy link
Contributor

oli-obk commented Dec 21, 2021

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Dec 21, 2021

📌 Commit 682a342 has been approved by oli-obk

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 21, 2021
bors added a commit to rust-lang-ci/rust that referenced this pull request Dec 21, 2021
…askrgr

Rollup of 7 pull requests

Successful merges:

 - rust-lang#90345 (Stabilise entry_insert)
 - rust-lang#91412 (Improve suggestions for importing out-of-scope traits reexported as `_`)
 - rust-lang#91770 (Suggest adding a `#[cfg(test)]` to to a test module)
 - rust-lang#91823 (Make `PTR::as_ref` and similar methods `const`.)
 - rust-lang#92127 (Move duplicates removal when generating results instead of when displaying them)
 - rust-lang#92129 (JoinHandle docs: add missing 'the')
 - rust-lang#92131 (Sync rustc_codegen_cranelift)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit af09d24 into rust-lang:master Dec 21, 2021
@rustbot rustbot added this to the 1.59.0 milestone Dec 21, 2021
@In-line
Copy link
Contributor

In-line commented Dec 22, 2021

Hey, why #87349 was benchmarked, while this one not?
cc: @estebank

@oli-obk
Copy link
Contributor

oli-obk commented Dec 22, 2021

It wasn't benchmarked because it was rolled up. The rollup PR mentioned above was benchmarked

@compiler-errors compiler-errors deleted the issue-86035 branch November 2, 2022 02:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

rustc suggests to use path::_; with as _ trait re-exports
9 participants