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

Add Item::from_def_id_and_kind to reduce duplication in rustdoc #77820

Merged
merged 4 commits into from
Nov 18, 2020

Conversation

jyn514
Copy link
Member

@jyn514 jyn514 commented Oct 11, 2020

This makes it harder to make typos, and also makes it much more clear what's intentionally different rather than a typo (look for what_rustc_thinks).

Found this while working on #76998, I really didn't want to add const_visibility in 20 different places.

r? @GuillaumeGomez

@jyn514 jyn514 added C-cleanup Category: PRs that clean code up or issues documenting cleanup. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. labels Oct 11, 2020
@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 11, 2020
src/librustdoc/clean/types.rs Outdated Show resolved Hide resolved
src/librustdoc/clean/types.rs Outdated Show resolved Hide resolved
@petrochenkov petrochenkov self-assigned this Oct 11, 2020
@petrochenkov petrochenkov added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 11, 2020
@jyn514
Copy link
Member Author

jyn514 commented Oct 11, 2020

@petrochenkov do you understand this error?

error: internal compiler error: compiler/rustc_middle/src/ty/query/mod.rs:105:1: `tcx.visibility(DefId(0:4 ~ foo[8787]::Bar))` unsupported by its crate
query stack during panic:
#0 [visibility] computing visibility of `Struct`
end of query stack

I can change it to use the hir::Visibility instead, but do you know why it's necessary?

@jyn514
Copy link
Member Author

jyn514 commented Oct 11, 2020

It's coming from this code:

impl Clean<Item> for doctree::Struct<'_> {
    fn clean(&self, cx: &DocContext<'_>) -> Item {
        Item::from_hir_id_and_kind(
            self.id,
            StructItem(Struct {
                struct_type: self.struct_type,
                generics: self.generics.clean(cx),
                fields: self.fields.clean(cx),
                fields_stripped: false,
            }),
            cx,
        )
    }
}

@jyn514
Copy link
Member Author

jyn514 commented Oct 11, 2020

Hmm, this comes from impl Default for Providers. Maybe rustdoc forgot to call providers::provide somewhere?

@jyn514
Copy link
Member Author

jyn514 commented Oct 11, 2020

Looks like the crate that should provide this is

visibility => { cdata.get_visibility(def_id.index) }

@petrochenkov
Copy link
Contributor

@jyn514

error: internal compiler error: compiler/rustc_middle/src/ty/query/mod.rs:105:1: tcx.visibility(DefId(0:4 ~ foo[8787]::Bar)) unsupported by its crate

IIRC, the visibility query is only implemented for other crates, but not for the current crate (LOCAL_CRATE).
You could try implementing it for the local crate, it would be generally useful, I think.
Every time Visibility::from_hir is used, that new query could be used instead.
It's preferable to do that as a separate PR because there may be performance effects.

@jyn514

This comment has been minimized.

@petrochenkov

This comment has been minimized.

@jyn514

This comment has been minimized.

@petrochenkov

This comment has been minimized.

@jyn514

This comment has been minimized.

@jyn514

This comment has been minimized.

@petrochenkov petrochenkov added S-blocked Status: Marked as blocked ❌ on something else such as an RFC or other implementation work. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Oct 13, 2020
@bors

This comment has been minimized.

@petrochenkov petrochenkov added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-blocked Status: Marked as blocked ❌ on something else such as an RFC or other implementation work. labels Oct 21, 2020
@petrochenkov
Copy link
Contributor

#78077 has landed.

@jyn514
Copy link
Member Author

jyn514 commented Oct 22, 2020

Rebased and updated. This is still panicking on most tests:

error: internal compiler error: compiler/rustc_middle/src/hir/map/mod.rs:477:41: couldn't find hir id HirId { owner: DefId(0:0 ~ union[8787]), local_id: 0 } in the HIR map

Backtrace:

   8: rustc_middle::util::bug::bug_fmt
             at ./compiler/rustc_middle/src/util/bug.rs:14:5
   9: rustc_middle::hir::map::Map::get::{{closure}}
             at ./compiler/rustc_middle/src/hir/map/mod.rs:477:41
  10: core::option::Option<T>::unwrap_or_else
             at ./library/core/src/option.rs:427:21
  11: rustc_middle::hir::map::Map::get
             at ./compiler/rustc_middle/src/hir/map/mod.rs:477:9
  12: rustc_middle::ty::<impl rustc_middle::ty::context::TyCtxt>::opt_item_name::{{closure}}
             at ./compiler/rustc_middle/src/ty/mod.rs:2796:32
  13: core::option::Option<T>::and_then
             at ./library/core/src/option.rs:662:24
  14: rustc_middle::ty::<impl rustc_middle::ty::context::TyCtxt>::opt_item_name
             at ./compiler/rustc_middle/src/ty/mod.rs:2794:9
  15: rustdoc::clean::types::Item::from_def_id_and_kind
             at ./src/librustdoc/clean/types.rs:127:19
  16: rustdoc::clean::types::Item::from_hir_id_and_kind
             at ./src/librustdoc/clean/types.rs:118:9
  17: <rustdoc::doctree::Module as rustdoc::clean::Clean<rustdoc::clean::types::Item>>::clean
             at ./src/librustdoc/clean/mod.rs:265:33
  18: rustdoc::clean::utils::krate
             at ./src/librustdoc/clean/utils.rs:44:22

I think the issue is that opt_item_name doesn't check for the crate root, only item_name:

pub fn opt_item_name(self, def_id: DefId) -> Option<Ident> {
def_id
.as_local()
.and_then(|def_id| self.hir().get(self.hir().local_def_id_to_hir_id(def_id)).ident())
}

pub fn item_name(self, id: DefId) -> Symbol {
if id.index == CRATE_DEF_INDEX {
self.original_crate_name(id.krate)
} else {
let def_key = self.def_key(id);
match def_key.disambiguated_data.data {
// The name of a constructor is that of its parent.
rustc_hir::definitions::DefPathData::Ctor => {
self.item_name(DefId { krate: id.krate, index: def_key.parent.unwrap() })
}
_ => def_key.disambiguated_data.data.get_opt_name().unwrap_or_else(|| {
bug!("item_name: no name for {:?}", self.def_path(id));
}),
}
}
}

I'll try rewriting item_name in terms of opt_item_name and see if that helps.

@jyn514
Copy link
Member Author

jyn514 commented Nov 17, 2020

@Manishearth FYI - Guillaume and I discussed offline that there's no way to get precisely the previous behavior back. I can recover pub(self) (in theory, I need to work out exactly how) and pub(super), but the previous behavior with super::b::super will always be normalized to pub(super) now - the information about how it was written just isn't present in the metadata, only in the HIR.

@Manishearth
Copy link
Member

Yeah that's fine, I care about self and super only, the others are rare enough

@jyn514
Copy link
Member Author

jyn514 commented Nov 17, 2020

@bors r=petrochenkov rollup=never

I want to be able to bisect if this causes regressions.

@bors
Copy link
Contributor

bors commented Nov 17, 2020

📌 Commit 46a99790cdf4dc633fe8b295b506b15c7ad72f1c has been approved by petrochenkov

@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-team Status: Awaiting decision from the relevant subteam (see the T-<team> label). labels Nov 17, 2020
@bors

This comment has been minimized.

@bors bors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Nov 17, 2020
Visibility needs much less information than a full path, since modules
can never have generics. This allows constructing a Visibility from only
a DefId.

Note that this means that paths are now normalized to their DefPath.
In other words, `pub(self)` or `pub(super)` now always shows `pub(in
path)` instead of preserving the original text.
This also uses an exhaustive match to avoid future similar bugs.
- Add `Item::from_hir_id_and_kind` convenience wrapper
- Make name parameter mandatory

  `tcx.opt_item_name` doesn't handle renames, so this is necessary
  for any item that could be renamed, which is almost all of them.

- Override visibilities to be `Inherited` for enum variants

  `tcx.visibility` returns the effective visibility, not the visibility
  that was written in the source code. `pub enum E { A, B }` always has
  public variants `A` and `B`, so there's no sense printing `pub` again.

- Don't duplicate handling of `Visibility::Crate`

  Instead, represent it as just another `Restricted` path.
It was completely unused.
@jyn514
Copy link
Member Author

jyn514 commented Nov 17, 2020

@bors r=petrochenkov rollup=never

@bors
Copy link
Contributor

bors commented Nov 17, 2020

📌 Commit 0e1a302 has been approved by petrochenkov

@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-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 17, 2020
@bors
Copy link
Contributor

bors commented Nov 18, 2020

⌛ Testing commit 0e1a302 with merge c4f836a...

@bors
Copy link
Contributor

bors commented Nov 18, 2020

☀️ Test successful - checks-actions
Approved by: petrochenkov
Pushing c4f836a to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Nov 18, 2020
@bors bors merged commit c4f836a into rust-lang:master Nov 18, 2020
@rustbot rustbot added this to the 1.50.0 milestone Nov 18, 2020
@jyn514 jyn514 deleted the from-inner branch November 18, 2020 13:39
@jyn514
Copy link
Member Author

jyn514 commented Nov 18, 2020

@petrochenkov thank you so much for all your help, I wouldn't have been able to do this without you :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-cleanup Category: PRs that clean code up or issues documenting cleanup. merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants