Skip to content

Commit af745a4

Browse files
committed
Auto merge of rust-lang#144658 - jhpratt:rollup-jdzhz27, r=jhpratt
Rollup of 8 pull requests Successful merges: - rust-lang#144034 (tests: Test line number in debuginfo for diverging function calls) - rust-lang#144510 (Fix Ord, Eq and Hash implementation of panic::Location) - rust-lang#144583 (Enable T-compiler backport nomination) - rust-lang#144586 (Update wasi-sdk to 27.0 in CI) - rust-lang#144605 (Resolve: cachify `ExternPreludeEntry.binding` through a `Cell`) - rust-lang#144632 (Update some tests for LLVM 21) - rust-lang#144639 (Update rustc-perf submodule) - rust-lang#144640 (Add support for the m68k architecture in 'object_architecture') r? `@ghost` `@rustbot` modify labels: rollup
2 parents 2f8dad2 + a003f50 commit af745a4

File tree

5 files changed

+123
-3
lines changed

5 files changed

+123
-3
lines changed

core/src/panic/location.rs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
use crate::cmp::Ordering;
12
use crate::ffi::CStr;
23
use crate::fmt;
4+
use crate::hash::{Hash, Hasher};
35
use crate::marker::PhantomData;
46
use crate::ptr::NonNull;
57

@@ -32,7 +34,7 @@ use crate::ptr::NonNull;
3234
/// Files are compared as strings, not `Path`, which could be unexpected.
3335
/// See [`Location::file`]'s documentation for more discussion.
3436
#[lang = "panic_location"]
35-
#[derive(Copy, Clone, Eq, Hash, Ord, PartialEq, PartialOrd)]
37+
#[derive(Copy, Clone)]
3638
#[stable(feature = "panic_hooks", since = "1.10.0")]
3739
pub struct Location<'a> {
3840
// A raw pointer is used rather than a reference because the pointer is valid for one more byte
@@ -44,6 +46,44 @@ pub struct Location<'a> {
4446
_filename: PhantomData<&'a str>,
4547
}
4648

49+
#[stable(feature = "panic_hooks", since = "1.10.0")]
50+
impl PartialEq for Location<'_> {
51+
fn eq(&self, other: &Self) -> bool {
52+
// Compare col / line first as they're cheaper to compare and more likely to differ,
53+
// while not impacting the result.
54+
self.col == other.col && self.line == other.line && self.file() == other.file()
55+
}
56+
}
57+
58+
#[stable(feature = "panic_hooks", since = "1.10.0")]
59+
impl Eq for Location<'_> {}
60+
61+
#[stable(feature = "panic_hooks", since = "1.10.0")]
62+
impl Ord for Location<'_> {
63+
fn cmp(&self, other: &Self) -> Ordering {
64+
self.file()
65+
.cmp(other.file())
66+
.then_with(|| self.line.cmp(&other.line))
67+
.then_with(|| self.col.cmp(&other.col))
68+
}
69+
}
70+
71+
#[stable(feature = "panic_hooks", since = "1.10.0")]
72+
impl PartialOrd for Location<'_> {
73+
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
74+
Some(self.cmp(other))
75+
}
76+
}
77+
78+
#[stable(feature = "panic_hooks", since = "1.10.0")]
79+
impl Hash for Location<'_> {
80+
fn hash<H: Hasher>(&self, state: &mut H) {
81+
self.file().hash(state);
82+
self.line.hash(state);
83+
self.col.hash(state);
84+
}
85+
}
86+
4787
#[stable(feature = "panic_hooks", since = "1.10.0")]
4888
impl fmt::Debug for Location<'_> {
4989
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {

coretests/tests/panic/location.rs

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,23 @@ use core::panic::Location;
33
// Note: Some of the following tests depend on the source location,
44
// so please be careful when editing this file.
55

6+
mod file_a;
7+
mod file_b;
8+
mod file_c;
9+
10+
// A small shuffled set of locations for testing, along with their true order.
11+
const LOCATIONS: [(usize, &'static Location<'_>); 9] = [
12+
(7, file_c::two()),
13+
(0, file_a::one()),
14+
(3, file_b::one()),
15+
(5, file_b::three()),
16+
(8, file_c::three()),
17+
(6, file_c::one()),
18+
(2, file_a::three()),
19+
(4, file_b::two()),
20+
(1, file_a::two()),
21+
];
22+
623
#[test]
724
fn location_const_caller() {
825
const _CALLER_REFERENCE: &Location<'static> = Location::caller();
@@ -20,7 +37,7 @@ fn location_const_file() {
2037
fn location_const_line() {
2138
const CALLER: &Location<'static> = Location::caller();
2239
const LINE: u32 = CALLER.line();
23-
assert_eq!(LINE, 21);
40+
assert_eq!(LINE, 38);
2441
}
2542

2643
#[test]
@@ -34,6 +51,28 @@ fn location_const_column() {
3451
fn location_debug() {
3552
let f = format!("{:?}", Location::caller());
3653
assert!(f.contains(&format!("{:?}", file!())));
37-
assert!(f.contains("35"));
54+
assert!(f.contains("52"));
3855
assert!(f.contains("29"));
3956
}
57+
58+
#[test]
59+
fn location_eq() {
60+
for (i, a) in LOCATIONS {
61+
for (j, b) in LOCATIONS {
62+
if i == j {
63+
assert_eq!(a, b);
64+
} else {
65+
assert_ne!(a, b);
66+
}
67+
}
68+
}
69+
}
70+
71+
#[test]
72+
fn location_ord() {
73+
let mut locations = LOCATIONS.clone();
74+
locations.sort_by_key(|(_o, l)| **l);
75+
for (correct, (order, _l)) in locations.iter().enumerate() {
76+
assert_eq!(correct, *order);
77+
}
78+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
use core::panic::Location;
2+
3+
// Used for test super::location_{ord, eq}. Must be in a dedicated file.
4+
5+
pub const fn one() -> &'static Location<'static> {
6+
Location::caller()
7+
}
8+
9+
pub const fn two() -> &'static Location<'static> {
10+
Location::caller()
11+
}
12+
13+
pub const fn three() -> &'static Location<'static> {
14+
Location::caller()
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
use core::panic::Location;
2+
3+
// Used for test super::location_{ord, eq}. Must be in a dedicated file.
4+
5+
pub const fn one() -> &'static Location<'static> {
6+
Location::caller()
7+
}
8+
9+
pub const fn two() -> &'static Location<'static> {
10+
Location::caller()
11+
}
12+
13+
pub const fn three() -> &'static Location<'static> {
14+
Location::caller()
15+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Used for test super::location_{ord, eq}. Must be in a dedicated file.
2+
3+
// This is used for testing column ordering of Location, hence this ugly one-liner.
4+
// We must fmt skip the entire containing module or else tidy will still complain.
5+
#[rustfmt::skip]
6+
mod no_fmt {
7+
use core::panic::Location;
8+
pub const fn one() -> &'static Location<'static> { Location::caller() } pub const fn two() -> &'static Location<'static> { Location::caller() } pub const fn three() -> &'static Location<'static> { Location::caller() }
9+
}
10+
11+
pub use no_fmt::*;

0 commit comments

Comments
 (0)