Skip to content

Commit 04ba50e

Browse files
committed
Auto merge of #128927 - GuillaumeGomez:rollup-ei2lr0f, r=GuillaumeGomez
Rollup of 8 pull requests Successful merges: - #128273 (Improve `Ord` violation help) - #128807 (run-make: explaing why fmt-write-bloat is ignore-windows) - #128903 (rustdoc-json-types `Discriminant`: fix typo) - #128905 (gitignore: Add Zed and Helix editors) - #128908 (diagnostics: do not warn when a lifetime bound infers itself) - #128909 (Fix dump-ice-to-disk for RUSTC_ICE=0 users) - #128910 (Differentiate between methods and associated functions in diagnostics) - #128923 ([rustdoc] Stop showing impl items for negative impls) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 8291d68 + 893b9f3 commit 04ba50e

File tree

52 files changed

+368
-191
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+368
-191
lines changed

Diff for: .gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ Session.vim
2020
.vscode
2121
.project
2222
.vim/
23+
.helix/
24+
.zed/
2325
.favorites.json
2426
.settings/
2527
.vs/

Diff for: compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

+3-11
Original file line numberDiff line numberDiff line change
@@ -1117,7 +1117,7 @@ fn check_region_bounds_on_impl_item<'tcx>(
11171117
.dcx()
11181118
.create_err(LifetimesOrBoundsMismatchOnTrait {
11191119
span,
1120-
item_kind: assoc_item_kind_str(&impl_m),
1120+
item_kind: impl_m.descr(),
11211121
ident: impl_m.ident(tcx),
11221122
generics_span,
11231123
bounds_span,
@@ -1294,7 +1294,7 @@ fn compare_number_of_generics<'tcx>(
12941294
("const", trait_own_counts.consts, impl_own_counts.consts),
12951295
];
12961296

1297-
let item_kind = assoc_item_kind_str(&impl_);
1297+
let item_kind = impl_.descr();
12981298

12991299
let mut err_occurred = None;
13001300
for (kind, trait_count, impl_count) in matchings {
@@ -1676,7 +1676,7 @@ fn compare_generic_param_kinds<'tcx>(
16761676
param_impl_span,
16771677
E0053,
16781678
"{} `{}` has an incompatible generic parameter for trait `{}`",
1679-
assoc_item_kind_str(&impl_item),
1679+
impl_item.descr(),
16801680
trait_item.name,
16811681
&tcx.def_path_str(tcx.parent(trait_item.def_id))
16821682
);
@@ -2249,14 +2249,6 @@ fn param_env_with_gat_bounds<'tcx>(
22492249
ty::ParamEnv::new(tcx.mk_clauses(&predicates), Reveal::UserFacing)
22502250
}
22512251

2252-
fn assoc_item_kind_str(impl_item: &ty::AssocItem) -> &'static str {
2253-
match impl_item.kind {
2254-
ty::AssocKind::Const => "const",
2255-
ty::AssocKind::Fn => "method",
2256-
ty::AssocKind::Type => "type",
2257-
}
2258-
}
2259-
22602252
/// Manually check here that `async fn foo()` wasn't matched against `fn foo()`,
22612253
/// and extract a better error if so.
22622254
fn try_report_async_mismatch<'tcx>(

Diff for: compiler/rustc_lint/src/builtin.rs

+15-6
Original file line numberDiff line numberDiff line change
@@ -1924,14 +1924,13 @@ declare_lint_pass!(ExplicitOutlivesRequirements => [EXPLICIT_OUTLIVES_REQUIREMEN
19241924
impl ExplicitOutlivesRequirements {
19251925
fn lifetimes_outliving_lifetime<'tcx>(
19261926
tcx: TyCtxt<'tcx>,
1927-
inferred_outlives: &'tcx [(ty::Clause<'tcx>, Span)],
1927+
inferred_outlives: impl Iterator<Item = &'tcx (ty::Clause<'tcx>, Span)>,
19281928
item: DefId,
19291929
lifetime: DefId,
19301930
) -> Vec<ty::Region<'tcx>> {
19311931
let item_generics = tcx.generics_of(item);
19321932

19331933
inferred_outlives
1934-
.iter()
19351934
.filter_map(|(clause, _)| match clause.kind().skip_binder() {
19361935
ty::ClauseKind::RegionOutlives(ty::OutlivesPredicate(a, b)) => match *a {
19371936
ty::ReEarlyParam(ebr)
@@ -1947,11 +1946,10 @@ impl ExplicitOutlivesRequirements {
19471946
}
19481947

19491948
fn lifetimes_outliving_type<'tcx>(
1950-
inferred_outlives: &'tcx [(ty::Clause<'tcx>, Span)],
1949+
inferred_outlives: impl Iterator<Item = &'tcx (ty::Clause<'tcx>, Span)>,
19511950
index: u32,
19521951
) -> Vec<ty::Region<'tcx>> {
19531952
inferred_outlives
1954-
.iter()
19551953
.filter_map(|(clause, _)| match clause.kind().skip_binder() {
19561954
ty::ClauseKind::TypeOutlives(ty::OutlivesPredicate(a, b)) => {
19571955
a.is_param(index).then_some(b)
@@ -2094,7 +2092,11 @@ impl<'tcx> LateLintPass<'tcx> for ExplicitOutlivesRequirements {
20942092
(
20952093
Self::lifetimes_outliving_lifetime(
20962094
cx.tcx,
2097-
inferred_outlives,
2095+
// don't warn if the inferred span actually came from the predicate we're looking at
2096+
// this happens if the type is recursively defined
2097+
inferred_outlives
2098+
.iter()
2099+
.filter(|(_, span)| !predicate.span.contains(*span)),
20982100
item.owner_id.to_def_id(),
20992101
region_def_id,
21002102
),
@@ -2116,7 +2118,14 @@ impl<'tcx> LateLintPass<'tcx> for ExplicitOutlivesRequirements {
21162118
};
21172119
let index = ty_generics.param_def_id_to_index[&def_id];
21182120
(
2119-
Self::lifetimes_outliving_type(inferred_outlives, index),
2121+
Self::lifetimes_outliving_type(
2122+
// don't warn if the inferred span actually came from the predicate we're looking at
2123+
// this happens if the type is recursively defined
2124+
inferred_outlives.iter().filter(|(_, span)| {
2125+
!predicate.span.contains(*span)
2126+
}),
2127+
index,
2128+
),
21202129
&predicate.bounds,
21212130
predicate.span,
21222131
predicate.origin == PredicateOrigin::WhereClause,

Diff for: compiler/rustc_middle/src/hir/map/mod.rs

+12-6
Original file line numberDiff line numberDiff line change
@@ -1165,17 +1165,23 @@ fn hir_id_to_string(map: Map<'_>, id: HirId) -> String {
11651165
}
11661166
Node::ImplItem(ii) => {
11671167
let kind = match ii.kind {
1168-
ImplItemKind::Const(..) => "assoc const",
1169-
ImplItemKind::Fn(..) => "method",
1170-
ImplItemKind::Type(_) => "assoc type",
1168+
ImplItemKind::Const(..) => "associated constant",
1169+
ImplItemKind::Fn(fn_sig, _) => match fn_sig.decl.implicit_self {
1170+
ImplicitSelfKind::None => "associated function",
1171+
_ => "method",
1172+
},
1173+
ImplItemKind::Type(_) => "associated type",
11711174
};
11721175
format!("{id} ({kind} `{}` in {})", ii.ident, path_str(ii.owner_id.def_id))
11731176
}
11741177
Node::TraitItem(ti) => {
11751178
let kind = match ti.kind {
1176-
TraitItemKind::Const(..) => "assoc constant",
1177-
TraitItemKind::Fn(..) => "trait method",
1178-
TraitItemKind::Type(..) => "assoc type",
1179+
TraitItemKind::Const(..) => "associated constant",
1180+
TraitItemKind::Fn(fn_sig, _) => match fn_sig.decl.implicit_self {
1181+
ImplicitSelfKind::None => "associated function",
1182+
_ => "trait method",
1183+
},
1184+
TraitItemKind::Type(..) => "associated type",
11791185
};
11801186

11811187
format!("{id} ({kind} `{}` in {})", ti.ident, path_str(ti.owner_id.def_id))

Diff for: compiler/rustc_middle/src/ty/assoc.rs

+9
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,15 @@ impl AssocItem {
9898
}
9999
}
100100

101+
pub fn descr(&self) -> &'static str {
102+
match self.kind {
103+
ty::AssocKind::Const => "const",
104+
ty::AssocKind::Fn if self.fn_has_self_parameter => "method",
105+
ty::AssocKind::Fn => "associated function",
106+
ty::AssocKind::Type => "type",
107+
}
108+
}
109+
101110
pub fn is_impl_trait_in_trait(&self) -> bool {
102111
self.opt_rpitit_info.is_some()
103112
}

Diff for: library/alloc/src/slice.rs

+56-40
Original file line numberDiff line numberDiff line change
@@ -178,15 +178,25 @@ impl<T> [T] {
178178
/// This sort is stable (i.e., does not reorder equal elements) and *O*(*n* \* log(*n*))
179179
/// worst-case.
180180
///
181-
/// If `T: Ord` does not implement a total order the resulting order is unspecified. All
182-
/// original elements will remain in the slice and any possible modifications via interior
183-
/// mutability are observed in the input. Same is true if `T: Ord` panics.
181+
/// If the implementation of [`Ord`] for `T` does not implement a [total order] the resulting
182+
/// order of elements in the slice is unspecified. All original elements will remain in the
183+
/// slice and any possible modifications via interior mutability are observed in the input. Same
184+
/// is true if the implementation of [`Ord`] for `T` panics.
184185
///
185186
/// When applicable, unstable sorting is preferred because it is generally faster than stable
186187
/// sorting and it doesn't allocate auxiliary memory. See
187188
/// [`sort_unstable`](slice::sort_unstable). The exception are partially sorted slices, which
188189
/// may be better served with `slice::sort`.
189190
///
191+
/// Sorting types that only implement [`PartialOrd`] such as [`f32`] and [`f64`] require
192+
/// additional precautions. For example, `f32::NAN != f32::NAN`, which doesn't fulfill the
193+
/// reflexivity requirement of [`Ord`]. By using an alternative comparison function with
194+
/// `slice::sort_by` such as [`f32::total_cmp`] or [`f64::total_cmp`] that defines a [total
195+
/// order] users can sort slices containing floating-point values. Alternatively, if all values
196+
/// in the slice are guaranteed to be in a subset for which [`PartialOrd::partial_cmp`] forms a
197+
/// [total order], it's possible to sort the slice with `sort_by(|a, b|
198+
/// a.partial_cmp(b).unwrap())`.
199+
///
190200
/// # Current implementation
191201
///
192202
/// The current implementation is based on [driftsort] by Orson Peters and Lukas Bergdoll, which
@@ -198,18 +208,21 @@ impl<T> [T] {
198208
/// handled without allocation, medium sized slices allocate `self.len()` and beyond that it
199209
/// clamps at `self.len() / 2`.
200210
///
201-
/// If `T: Ord` does not implement a total order, the implementation may panic.
211+
/// # Panics
212+
///
213+
/// May panic if the implementation of [`Ord`] for `T` does not implement a [total order].
202214
///
203215
/// # Examples
204216
///
205217
/// ```
206-
/// let mut v = [-5, 4, 1, -3, 2];
218+
/// let mut v = [4, -5, 1, -3, 2];
207219
///
208220
/// v.sort();
209-
/// assert!(v == [-5, -3, 1, 2, 4]);
221+
/// assert_eq!(v, [-5, -3, 1, 2, 4]);
210222
/// ```
211223
///
212224
/// [driftsort]: https://github.com/Voultapher/driftsort
225+
/// [total order]: https://en.wikipedia.org/wiki/Total_order
213226
#[cfg(not(no_global_oom_handling))]
214227
#[rustc_allow_incoherent_impl]
215228
#[stable(feature = "rust1", since = "1.0.0")]
@@ -221,30 +234,19 @@ impl<T> [T] {
221234
stable_sort(self, T::lt);
222235
}
223236

224-
/// Sorts the slice with a comparator function, preserving initial order of equal elements.
237+
/// Sorts the slice with a comparison function, preserving initial order of equal elements.
225238
///
226239
/// This sort is stable (i.e., does not reorder equal elements) and *O*(*n* \* log(*n*))
227240
/// worst-case.
228241
///
229-
/// The comparator function should define a total ordering for the elements in the slice. If the
230-
/// ordering is not total, the order of the elements is unspecified.
231-
///
232-
/// If the comparator function does not implement a total order the resulting order is
233-
/// unspecified. All original elements will remain in the slice and any possible modifications
234-
/// via interior mutability are observed in the input. Same is true if the comparator function
235-
/// panics. A total order (for all `a`, `b` and `c`):
242+
/// If the comparison function `compare` does not implement a [total order] the resulting order
243+
/// of elements in the slice is unspecified. All original elements will remain in the slice and
244+
/// any possible modifications via interior mutability are observed in the input. Same is true
245+
/// if `compare` panics.
236246
///
237-
/// * total and antisymmetric: exactly one of `a < b`, `a == b` or `a > b` is true, and
238-
/// * transitive, `a < b` and `b < c` implies `a < c`. The same must hold for both `==` and `>`.
239-
///
240-
/// For example, while [`f64`] doesn't implement [`Ord`] because `NaN != NaN`, we can use
241-
/// `partial_cmp` as our sort function when we know the slice doesn't contain a `NaN`.
242-
///
243-
/// ```
244-
/// let mut floats = [5f64, 4.0, 1.0, 3.0, 2.0];
245-
/// floats.sort_unstable_by(|a, b| a.partial_cmp(b).unwrap());
246-
/// assert_eq!(floats, [1.0, 2.0, 3.0, 4.0, 5.0]);
247-
/// ```
247+
/// For example `|a, b| (a - b).cmp(a)` is a comparison function that is neither transitive nor
248+
/// reflexive nor total, `a < b < c < a` with `a = 1, b = 2, c = 3`. For more information and
249+
/// examples see the [`Ord`] documentation.
248250
///
249251
/// # Current implementation
250252
///
@@ -257,21 +259,24 @@ impl<T> [T] {
257259
/// handled without allocation, medium sized slices allocate `self.len()` and beyond that it
258260
/// clamps at `self.len() / 2`.
259261
///
260-
/// If `T: Ord` does not implement a total order, the implementation may panic.
262+
/// # Panics
263+
///
264+
/// May panic if `compare` does not implement a [total order].
261265
///
262266
/// # Examples
263267
///
264268
/// ```
265-
/// let mut v = [5, 4, 1, 3, 2];
269+
/// let mut v = [4, -5, 1, -3, 2];
266270
/// v.sort_by(|a, b| a.cmp(b));
267-
/// assert!(v == [1, 2, 3, 4, 5]);
271+
/// assert_eq!(v, [-5, -3, 1, 2, 4]);
268272
///
269273
/// // reverse sorting
270274
/// v.sort_by(|a, b| b.cmp(a));
271-
/// assert!(v == [5, 4, 3, 2, 1]);
275+
/// assert_eq!(v, [4, 2, 1, -3, -5]);
272276
/// ```
273277
///
274278
/// [driftsort]: https://github.com/Voultapher/driftsort
279+
/// [total order]: https://en.wikipedia.org/wiki/Total_order
275280
#[cfg(not(no_global_oom_handling))]
276281
#[rustc_allow_incoherent_impl]
277282
#[stable(feature = "rust1", since = "1.0.0")]
@@ -288,9 +293,10 @@ impl<T> [T] {
288293
/// This sort is stable (i.e., does not reorder equal elements) and *O*(*m* \* *n* \* log(*n*))
289294
/// worst-case, where the key function is *O*(*m*).
290295
///
291-
/// If `K: Ord` does not implement a total order the resulting order is unspecified.
292-
/// All original elements will remain in the slice and any possible modifications via interior
293-
/// mutability are observed in the input. Same is true if `K: Ord` panics.
296+
/// If the implementation of [`Ord`] for `K` does not implement a [total order] the resulting
297+
/// order of elements in the slice is unspecified. All original elements will remain in the
298+
/// slice and any possible modifications via interior mutability are observed in the input. Same
299+
/// is true if the implementation of [`Ord`] for `K` panics.
294300
///
295301
/// # Current implementation
296302
///
@@ -303,18 +309,21 @@ impl<T> [T] {
303309
/// handled without allocation, medium sized slices allocate `self.len()` and beyond that it
304310
/// clamps at `self.len() / 2`.
305311
///
306-
/// If `K: Ord` does not implement a total order, the implementation may panic.
312+
/// # Panics
313+
///
314+
/// May panic if the implementation of [`Ord`] for `K` does not implement a [total order].
307315
///
308316
/// # Examples
309317
///
310318
/// ```
311-
/// let mut v = [-5i32, 4, 1, -3, 2];
319+
/// let mut v = [4i32, -5, 1, -3, 2];
312320
///
313321
/// v.sort_by_key(|k| k.abs());
314-
/// assert!(v == [1, 2, -3, 4, -5]);
322+
/// assert_eq!(v, [1, 2, -3, 4, -5]);
315323
/// ```
316324
///
317325
/// [driftsort]: https://github.com/Voultapher/driftsort
326+
/// [total order]: https://en.wikipedia.org/wiki/Total_order
318327
#[cfg(not(no_global_oom_handling))]
319328
#[rustc_allow_incoherent_impl]
320329
#[stable(feature = "slice_sort_by_key", since = "1.7.0")]
@@ -336,9 +345,10 @@ impl<T> [T] {
336345
/// storage to remember the results of key evaluation. The order of calls to the key function is
337346
/// unspecified and may change in future versions of the standard library.
338347
///
339-
/// If `K: Ord` does not implement a total order the resulting order is unspecified.
340-
/// All original elements will remain in the slice and any possible modifications via interior
341-
/// mutability are observed in the input. Same is true if `K: Ord` panics.
348+
/// If the implementation of [`Ord`] for `K` does not implement a [total order] the resulting
349+
/// order of elements in the slice is unspecified. All original elements will remain in the
350+
/// slice and any possible modifications via interior mutability are observed in the input. Same
351+
/// is true if the implementation of [`Ord`] for `K` panics.
342352
///
343353
/// For simple key functions (e.g., functions that are property accesses or basic operations),
344354
/// [`sort_by_key`](slice::sort_by_key) is likely to be faster.
@@ -355,16 +365,22 @@ impl<T> [T] {
355365
/// In the worst case, the algorithm allocates temporary storage in a `Vec<(K, usize)>` the
356366
/// length of the slice.
357367
///
368+
/// # Panics
369+
///
370+
/// May panic if the implementation of [`Ord`] for `K` does not implement a [total order].
371+
///
358372
/// # Examples
359373
///
360374
/// ```
361-
/// let mut v = [-5i32, 4, 32, -3, 2];
375+
/// let mut v = [4i32, -5, 1, -3, 2, 10];
362376
///
377+
/// // Strings are sorted by lexicographical order.
363378
/// v.sort_by_cached_key(|k| k.to_string());
364-
/// assert!(v == [-3, -5, 2, 32, 4]);
379+
/// assert_eq!(v, [-3, -5, 1, 10, 2, 4]);
365380
/// ```
366381
///
367382
/// [ipnsort]: https://github.com/Voultapher/sort-research-rs/tree/main/ipnsort
383+
/// [total order]: https://en.wikipedia.org/wiki/Total_order
368384
#[cfg(not(no_global_oom_handling))]
369385
#[rustc_allow_incoherent_impl]
370386
#[stable(feature = "slice_sort_by_cached_key", since = "1.34.0")]

0 commit comments

Comments
 (0)