Skip to content

Commit d572e8b

Browse files
committed
Auto merge of rust-lang#139571 - matthiaskrgr:rollup-3qdo3ud, r=matthiaskrgr
Rollup of 7 pull requests Successful merges: - rust-lang#137412 (Ensure `swap_nonoverlapping` is really always untyped) - rust-lang#138869 (Try not to use verbatim paths in `Command::current_dir`) - rust-lang#138993 (Make `cfg_match!` a semitransparent macro) - rust-lang#139099 (Promise `array::from_fn` is generated in order of increasing indices) - rust-lang#139364 (Make the compiler suggest actual paths instead of visible paths if the visible paths are through any doc hidden path.) - rust-lang#139468 (Don't call `Span::with_parent` on the good path in `has_stashed_diagnostic`) - rust-lang#139481 (Add job summary links to post-merge report) r? `@ghost` `@rustbot` modify labels: rollup
2 parents f06e5c1 + 63941e4 commit d572e8b

24 files changed

+708
-148
lines changed

compiler/rustc_errors/src/lib.rs

+44-26
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,8 @@ struct DiagCtxtInner {
589589
/// add more information). All stashed diagnostics must be emitted with
590590
/// `emit_stashed_diagnostics` by the time the `DiagCtxtInner` is dropped,
591591
/// otherwise an assertion failure will occur.
592-
stashed_diagnostics: FxIndexMap<(Span, StashKey), (DiagInner, Option<ErrorGuaranteed>)>,
592+
stashed_diagnostics:
593+
FxIndexMap<StashKey, FxIndexMap<Span, (DiagInner, Option<ErrorGuaranteed>)>>,
593594

594595
future_breakage_diagnostics: Vec<DiagInner>,
595596

@@ -912,8 +913,12 @@ impl<'a> DiagCtxtHandle<'a> {
912913
// FIXME(Centril, #69537): Consider reintroducing panic on overwriting a stashed diagnostic
913914
// if/when we have a more robust macro-friendly replacement for `(span, key)` as a key.
914915
// See the PR for a discussion.
915-
let key = (span.with_parent(None), key);
916-
self.inner.borrow_mut().stashed_diagnostics.insert(key, (diag, guar));
916+
self.inner
917+
.borrow_mut()
918+
.stashed_diagnostics
919+
.entry(key)
920+
.or_default()
921+
.insert(span.with_parent(None), (diag, guar));
917922

918923
guar
919924
}
@@ -922,9 +927,10 @@ impl<'a> DiagCtxtHandle<'a> {
922927
/// and [`StashKey`] as the key. Panics if the found diagnostic is an
923928
/// error.
924929
pub fn steal_non_err(self, span: Span, key: StashKey) -> Option<Diag<'a, ()>> {
925-
let key = (span.with_parent(None), key);
926930
// FIXME(#120456) - is `swap_remove` correct?
927-
let (diag, guar) = self.inner.borrow_mut().stashed_diagnostics.swap_remove(&key)?;
931+
let (diag, guar) = self.inner.borrow_mut().stashed_diagnostics.get_mut(&key).and_then(
932+
|stashed_diagnostics| stashed_diagnostics.swap_remove(&span.with_parent(None)),
933+
)?;
928934
assert!(!diag.is_error());
929935
assert!(guar.is_none());
930936
Some(Diag::new_diagnostic(self, diag))
@@ -943,9 +949,10 @@ impl<'a> DiagCtxtHandle<'a> {
943949
where
944950
F: FnMut(&mut Diag<'_>),
945951
{
946-
let key = (span.with_parent(None), key);
947952
// FIXME(#120456) - is `swap_remove` correct?
948-
let err = self.inner.borrow_mut().stashed_diagnostics.swap_remove(&key);
953+
let err = self.inner.borrow_mut().stashed_diagnostics.get_mut(&key).and_then(
954+
|stashed_diagnostics| stashed_diagnostics.swap_remove(&span.with_parent(None)),
955+
);
949956
err.map(|(err, guar)| {
950957
// The use of `::<ErrorGuaranteed>` is safe because level is `Level::Error`.
951958
assert_eq!(err.level, Error);
@@ -966,9 +973,10 @@ impl<'a> DiagCtxtHandle<'a> {
966973
key: StashKey,
967974
new_err: Diag<'_>,
968975
) -> ErrorGuaranteed {
969-
let key = (span.with_parent(None), key);
970976
// FIXME(#120456) - is `swap_remove` correct?
971-
let old_err = self.inner.borrow_mut().stashed_diagnostics.swap_remove(&key);
977+
let old_err = self.inner.borrow_mut().stashed_diagnostics.get_mut(&key).and_then(
978+
|stashed_diagnostics| stashed_diagnostics.swap_remove(&span.with_parent(None)),
979+
);
972980
match old_err {
973981
Some((old_err, guar)) => {
974982
assert_eq!(old_err.level, Error);
@@ -983,7 +991,14 @@ impl<'a> DiagCtxtHandle<'a> {
983991
}
984992

985993
pub fn has_stashed_diagnostic(&self, span: Span, key: StashKey) -> bool {
986-
self.inner.borrow().stashed_diagnostics.get(&(span.with_parent(None), key)).is_some()
994+
let inner = self.inner.borrow();
995+
if let Some(stashed_diagnostics) = inner.stashed_diagnostics.get(&key)
996+
&& !stashed_diagnostics.is_empty()
997+
{
998+
stashed_diagnostics.contains_key(&span.with_parent(None))
999+
} else {
1000+
false
1001+
}
9871002
}
9881003

9891004
/// Emit all stashed diagnostics.
@@ -997,7 +1012,11 @@ impl<'a> DiagCtxtHandle<'a> {
9971012
let inner = self.inner.borrow();
9981013
inner.err_guars.len()
9991014
+ inner.lint_err_guars.len()
1000-
+ inner.stashed_diagnostics.values().filter(|(_diag, guar)| guar.is_some()).count()
1015+
+ inner
1016+
.stashed_diagnostics
1017+
.values()
1018+
.map(|a| a.values().filter(|(_, guar)| guar.is_some()).count())
1019+
.sum::<usize>()
10011020
}
10021021

10031022
/// This excludes lint errors and delayed bugs. Unless absolutely
@@ -1486,16 +1505,18 @@ impl DiagCtxtInner {
14861505
fn emit_stashed_diagnostics(&mut self) -> Option<ErrorGuaranteed> {
14871506
let mut guar = None;
14881507
let has_errors = !self.err_guars.is_empty();
1489-
for (_, (diag, _guar)) in std::mem::take(&mut self.stashed_diagnostics).into_iter() {
1490-
if !diag.is_error() {
1491-
// Unless they're forced, don't flush stashed warnings when
1492-
// there are errors, to avoid causing warning overload. The
1493-
// stash would've been stolen already if it were important.
1494-
if !diag.is_force_warn() && has_errors {
1495-
continue;
1508+
for (_, stashed_diagnostics) in std::mem::take(&mut self.stashed_diagnostics).into_iter() {
1509+
for (_, (diag, _guar)) in stashed_diagnostics {
1510+
if !diag.is_error() {
1511+
// Unless they're forced, don't flush stashed warnings when
1512+
// there are errors, to avoid causing warning overload. The
1513+
// stash would've been stolen already if it were important.
1514+
if !diag.is_force_warn() && has_errors {
1515+
continue;
1516+
}
14961517
}
1518+
guar = guar.or(self.emit_diagnostic(diag, None));
14971519
}
1498-
guar = guar.or(self.emit_diagnostic(diag, None));
14991520
}
15001521
guar
15011522
}
@@ -1688,6 +1709,7 @@ impl DiagCtxtInner {
16881709
if let Some((_diag, guar)) = self
16891710
.stashed_diagnostics
16901711
.values()
1712+
.flat_map(|stashed_diagnostics| stashed_diagnostics.values())
16911713
.find(|(diag, guar)| guar.is_some() && diag.is_lint.is_none())
16921714
{
16931715
*guar
@@ -1700,13 +1722,9 @@ impl DiagCtxtInner {
17001722
fn has_errors(&self) -> Option<ErrorGuaranteed> {
17011723
self.err_guars.get(0).copied().or_else(|| self.lint_err_guars.get(0).copied()).or_else(
17021724
|| {
1703-
if let Some((_diag, guar)) =
1704-
self.stashed_diagnostics.values().find(|(_diag, guar)| guar.is_some())
1705-
{
1706-
*guar
1707-
} else {
1708-
None
1709-
}
1725+
self.stashed_diagnostics.values().find_map(|stashed_diagnostics| {
1726+
stashed_diagnostics.values().find_map(|(_, guar)| *guar)
1727+
})
17101728
},
17111729
)
17121730
}

compiler/rustc_hir_typeck/src/method/suggest.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use rustc_middle::bug;
2525
use rustc_middle::ty::fast_reject::{DeepRejectCtxt, TreatParams, simplify_type};
2626
use rustc_middle::ty::print::{
2727
PrintTraitRefExt as _, with_crate_prefix, with_forced_trimmed_paths,
28+
with_no_visible_paths_if_doc_hidden,
2829
};
2930
use rustc_middle::ty::{self, GenericArgKind, IsSuggestable, Ty, TyCtxt, TypeVisitableExt};
3031
use rustc_span::def_id::DefIdSet;
@@ -3328,15 +3329,19 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
33283329
let path_strings = candidates.iter().map(|trait_did| {
33293330
format!(
33303331
"{prefix}{}{postfix}\n",
3331-
with_crate_prefix!(self.tcx.def_path_str(*trait_did)),
3332+
with_no_visible_paths_if_doc_hidden!(with_crate_prefix!(
3333+
self.tcx.def_path_str(*trait_did)
3334+
)),
33323335
)
33333336
});
33343337

33353338
let glob_path_strings = globs.iter().map(|trait_did| {
33363339
let parent_did = parent_map.get(trait_did).unwrap();
33373340
format!(
33383341
"{prefix}{}::*{postfix} // trait {}\n",
3339-
with_crate_prefix!(self.tcx.def_path_str(*parent_did)),
3342+
with_no_visible_paths_if_doc_hidden!(with_crate_prefix!(
3343+
self.tcx.def_path_str(*parent_did)
3344+
)),
33403345
self.tcx.item_name(*trait_did),
33413346
)
33423347
});

compiler/rustc_middle/src/ty/print/pretty.rs

+7
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ thread_local! {
6363
static FORCE_TRIMMED_PATH: Cell<bool> = const { Cell::new(false) };
6464
static REDUCED_QUERIES: Cell<bool> = const { Cell::new(false) };
6565
static NO_VISIBLE_PATH: Cell<bool> = const { Cell::new(false) };
66+
static NO_VISIBLE_PATH_IF_DOC_HIDDEN: Cell<bool> = const { Cell::new(false) };
6667
static RTN_MODE: Cell<RtnMode> = const { Cell::new(RtnMode::ForDiagnostic) };
6768
}
6869

@@ -134,6 +135,8 @@ define_helper!(
134135
/// Prevent selection of visible paths. `Display` impl of DefId will prefer
135136
/// visible (public) reexports of types as paths.
136137
fn with_no_visible_paths(NoVisibleGuard, NO_VISIBLE_PATH);
138+
/// Prevent selection of visible paths if the paths are through a doc hidden path.
139+
fn with_no_visible_paths_if_doc_hidden(NoVisibleIfDocHiddenGuard, NO_VISIBLE_PATH_IF_DOC_HIDDEN);
137140
);
138141

139142
#[must_use]
@@ -569,6 +572,10 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
569572
return Ok(false);
570573
};
571574

575+
if self.tcx().is_doc_hidden(visible_parent) && with_no_visible_paths_if_doc_hidden() {
576+
return Ok(false);
577+
}
578+
572579
let actual_parent = self.tcx().opt_parent(def_id);
573580
debug!(
574581
"try_print_visible_def_path: visible_parent={:?} actual_parent={:?}",

library/core/src/array/mod.rs

+27-6
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,16 @@ pub fn repeat<T: Clone, const N: usize>(val: T) -> [T; N] {
5555
from_trusted_iterator(repeat_n(val, N))
5656
}
5757

58-
/// Creates an array of type [T; N], where each element `T` is the returned value from `cb`
59-
/// using that element's index.
58+
/// Creates an array where each element is produced by calling `f` with
59+
/// that element's index while walking forward through the array.
6060
///
61-
/// # Arguments
61+
/// This is essentially the same as writing
62+
/// ```text
63+
/// [f(0), f(1), f(2), …, f(N - 2), f(N - 1)]
64+
/// ```
65+
/// and is similar to `(0..i).map(f)`, just for arrays not iterators.
6266
///
63-
/// * `cb`: Callback where the passed argument is the current array index.
67+
/// If `N == 0`, this produces an empty array without ever calling `f`.
6468
///
6569
/// # Example
6670
///
@@ -82,13 +86,30 @@ pub fn repeat<T: Clone, const N: usize>(val: T) -> [T; N] {
8286
/// // indexes are: 0 1 2 3 4
8387
/// assert_eq!(bool_arr, [true, false, true, false, true]);
8488
/// ```
89+
///
90+
/// You can also capture things, for example to create an array full of clones
91+
/// where you can't just use `[item; N]` because it's not `Copy`:
92+
/// ```
93+
/// # // TBH `array::repeat` would be better for this, but it's not stable yet.
94+
/// let my_string = String::from("Hello");
95+
/// let clones: [String; 42] = std::array::from_fn(|_| my_string.clone());
96+
/// assert!(clones.iter().all(|x| *x == my_string));
97+
/// ```
98+
///
99+
/// The array is generated in ascending index order, starting from the front
100+
/// and going towards the back, so you can use closures with mutable state:
101+
/// ```
102+
/// let mut state = 1;
103+
/// let a = std::array::from_fn(|_| { let x = state; state *= 2; x });
104+
/// assert_eq!(a, [1, 2, 4, 8, 16, 32]);
105+
/// ```
85106
#[inline]
86107
#[stable(feature = "array_from_fn", since = "1.63.0")]
87-
pub fn from_fn<T, const N: usize, F>(cb: F) -> [T; N]
108+
pub fn from_fn<T, const N: usize, F>(f: F) -> [T; N]
88109
where
89110
F: FnMut(usize) -> T,
90111
{
91-
try_from_fn(NeverShortCircuit::wrap_mut_1(cb)).0
112+
try_from_fn(NeverShortCircuit::wrap_mut_1(f)).0
92113
}
93114

94115
/// Creates an array `[T; N]` where each fallible array element `T` is returned by the `cb` call.

library/core/src/macros/mod.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -237,9 +237,10 @@ pub macro assert_matches {
237237
/// ```
238238
#[unstable(feature = "cfg_match", issue = "115585")]
239239
#[rustc_diagnostic_item = "cfg_match"]
240+
#[rustc_macro_transparency = "semitransparent"]
240241
pub macro cfg_match {
241242
({ $($tt:tt)* }) => {{
242-
cfg_match! { $($tt)* }
243+
$crate::cfg_match! { $($tt)* }
243244
}},
244245
(_ => { $($output:tt)* }) => {
245246
$($output)*
@@ -249,10 +250,10 @@ pub macro cfg_match {
249250
$($( $rest:tt )+)?
250251
) => {
251252
#[cfg($cfg)]
252-
cfg_match! { _ => $output }
253+
$crate::cfg_match! { _ => $output }
253254
$(
254255
#[cfg(not($cfg))]
255-
cfg_match! { $($rest)+ }
256+
$crate::cfg_match! { $($rest)+ }
256257
)?
257258
},
258259
}

0 commit comments

Comments
 (0)