Skip to content

Commit

Permalink
Rollup merge of rust-lang#99698 - compiler-errors:no-doc-hidden, r=cj…
Browse files Browse the repository at this point in the history
…gillot

Prefer visibility map parents that are not `doc(hidden)` first

Far simpler approach to rust-lang#98876.

This only fixes the case where the parent is `doc(hidden)`, not where the child is `doc(hidden)` since I don't know how to get the attrs on the import statement given a `ModChild`... I'll try to follow up with that, but this is a good first step.
  • Loading branch information
matthiaskrgr authored Jul 26, 2022
2 parents 18f0e7d + 42a4419 commit bd58d85
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 5 deletions.
20 changes: 15 additions & 5 deletions compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,9 +375,13 @@ pub(in crate::rmeta) fn provide(providers: &mut Providers) {
use std::collections::vec_deque::VecDeque;

let mut visible_parent_map: DefIdMap<DefId> = Default::default();
// This is a secondary visible_parent_map, storing the DefId of parents that re-export
// the child as `_`. Since we prefer parents that don't do this, merge this map at the
// end, only if we're missing any keys from the former.
// This is a secondary visible_parent_map, storing the DefId of
// parents that re-export the child as `_` or module parents
// which are `#[doc(hidden)]`. Since we prefer paths that don't
// do this, merge this map at the end, only if we're missing
// keys from the former.
// This is a rudimentary check that does not catch all cases,
// just the easiest.
let mut fallback_map: DefIdMap<DefId> = Default::default();

// Issue 46112: We want the map to prefer the shortest
Expand Down Expand Up @@ -412,6 +416,11 @@ pub(in crate::rmeta) fn provide(providers: &mut Providers) {
return;
}

if ty::util::is_doc_hidden(tcx, parent) {
fallback_map.insert(def_id, parent);
return;
}

match visible_parent_map.entry(def_id) {
Entry::Occupied(mut entry) => {
// If `child` is defined in crate `cnum`, ensure
Expand Down Expand Up @@ -439,8 +448,9 @@ pub(in crate::rmeta) fn provide(providers: &mut Providers) {
}
}

// Fill in any missing entries with the (less preferable) path ending in `::_`.
// We still use this path in a diagnostic that suggests importing `::*`.
// Fill in any missing entries with the less preferable path.
// If this path re-exports the child as `_`, we still use this
// path in a diagnostic that suggests importing `::*`.
for (child, parent) in fallback_map {
visible_parent_map.entry(child).or_insert(parent);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#![crate_type = "lib"]

extern crate core;

pub mod __private {
#[doc(hidden)]
pub use core::option::Option::{self, None, Some};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#![crate_type = "lib"]

extern crate core;

#[doc(hidden)]
pub mod __private {
pub use core::option::Option::{self, None, Some};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// aux-build:hidden-child.rs

// FIXME(compiler-errors): This currently suggests the wrong thing.
// UI test exists to track the problem.

extern crate hidden_child;

fn main() {
let x: Option<i32> = 1i32; //~ ERROR mismatched types
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
error[E0308]: mismatched types
--> $DIR/hidden-child.rs:9:26
|
LL | let x: Option<i32> = 1i32;
| ----------- ^^^^ expected enum `Option`, found `i32`
| |
| expected due to this
|
= note: expected enum `Option<i32>`
found type `i32`
help: try wrapping the expression in `hidden_child::__private::Some`
|
LL | let x: Option<i32> = hidden_child::__private::Some(1i32);
| ++++++++++++++++++++++++++++++ +

error: aborting due to previous error

For more information about this error, try `rustc --explain E0308`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// aux-build:hidden-parent.rs

extern crate hidden_parent;

fn main() {
let x: Option<i32> = 1i32; //~ ERROR mismatched types
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
error[E0308]: mismatched types
--> $DIR/hidden-parent.rs:6:26
|
LL | let x: Option<i32> = 1i32;
| ----------- ^^^^ expected enum `Option`, found `i32`
| |
| expected due to this
|
= note: expected enum `Option<i32>`
found type `i32`
help: try wrapping the expression in `Some`
|
LL | let x: Option<i32> = Some(1i32);
| +++++ +

error: aborting due to previous error

For more information about this error, try `rustc --explain E0308`.

0 comments on commit bd58d85

Please sign in to comment.