Skip to content

Commit 82b2753

Browse files
committed
[squashme] fix tests
- we cannot define `new_{display,debug,noop_debug,...}` in a macro because the macro name leaks out in compilation errors arising from formatting macros. - amazingly, this changes the known miscompilation in rust-lang#107975 - formatting no longer changes the result of pointer comparison.
1 parent e5761eb commit 82b2753

File tree

10 files changed

+78
-51
lines changed

10 files changed

+78
-51
lines changed

library/core/src/fmt/rt.rs

+63-36
Original file line numberDiff line numberDiff line change
@@ -62,34 +62,31 @@ pub struct Argument<'a> {
6262
ty: ArgumentType<'a>,
6363
}
6464

65-
macro_rules! define_argument_constructor {
66-
($method:ident, $trait:ident, $fmt:item) => {
67-
#[inline]
68-
pub fn $method<T: $trait>(x: &T) -> Argument<'_> {
69-
#[repr(transparent)]
70-
struct Wrapper<T>(T);
71-
72-
// SAFETY: `Wrapper<T>` has the same memory layout as `T` due to #[repr(transparent)].
73-
let thunk = unsafe { mem::transmute::<&T, &Wrapper<T>>(x) };
74-
75-
impl<T: $trait> FormatThunk for Wrapper<T> {
76-
#[inline]
77-
$fmt
78-
}
65+
macro_rules! implement_argument_constructor {
66+
($trait:ident, $operand:expr, $fmt:item) => {{
67+
#[repr(transparent)]
68+
struct Wrapper<T>(T);
69+
70+
// SAFETY: `Wrapper<T>` has the same memory layout as `T` due to #[repr(transparent)].
71+
let thunk = unsafe { mem::transmute::<&T, &Wrapper<T>>($operand) };
7972

80-
Self::new(thunk)
73+
impl<T: $trait> FormatThunk for Wrapper<T> {
74+
#[inline]
75+
$fmt
8176
}
82-
};
8377

84-
($method:ident, $trait:ident) => {
85-
define_argument_constructor!(
86-
$method,
78+
Self::new(thunk)
79+
}};
80+
81+
($trait:ident, $operand:expr) => {
82+
implement_argument_constructor!(
8783
$trait,
84+
$operand,
8885
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
8986
let Self(inner) = self;
9087
inner.fmt(f)
9188
}
92-
);
89+
)
9390
};
9491
}
9592

@@ -100,22 +97,52 @@ impl Argument<'_> {
10097
Argument { ty: ArgumentType::Placeholder(x) }
10198
}
10299

103-
define_argument_constructor!(new_display, Display);
104-
define_argument_constructor!(new_debug, Debug);
105-
define_argument_constructor!(
106-
new_debug_noop,
107-
Debug,
108-
fn fmt(&self, _: &mut Formatter<'_>) -> Result {
109-
Ok(())
110-
}
111-
);
112-
define_argument_constructor!(new_octal, Octal);
113-
define_argument_constructor!(new_lower_hex, LowerHex);
114-
define_argument_constructor!(new_upper_hex, UpperHex);
115-
define_argument_constructor!(new_pointer, Pointer);
116-
define_argument_constructor!(new_binary, Binary);
117-
define_argument_constructor!(new_lower_exp, LowerExp);
118-
define_argument_constructor!(new_upper_exp, UpperExp);
100+
#[inline]
101+
pub fn new_display<T: Display>(x: &T) -> Argument<'_> {
102+
implement_argument_constructor!(Display, x)
103+
}
104+
#[inline]
105+
pub fn new_debug<T: Debug>(x: &T) -> Argument<'_> {
106+
implement_argument_constructor!(Debug, x)
107+
}
108+
#[inline]
109+
pub fn new_debug_noop<T: Debug>(x: &T) -> Argument<'_> {
110+
implement_argument_constructor!(
111+
Debug,
112+
x,
113+
fn fmt(&self, _: &mut Formatter<'_>) -> Result {
114+
Ok(())
115+
}
116+
)
117+
}
118+
#[inline]
119+
pub fn new_octal<T: Octal>(x: &T) -> Argument<'_> {
120+
implement_argument_constructor!(Octal, x)
121+
}
122+
#[inline]
123+
pub fn new_lower_hex<T: LowerHex>(x: &T) -> Argument<'_> {
124+
implement_argument_constructor!(LowerHex, x)
125+
}
126+
#[inline]
127+
pub fn new_upper_hex<T: UpperHex>(x: &T) -> Argument<'_> {
128+
implement_argument_constructor!(UpperHex, x)
129+
}
130+
#[inline]
131+
pub fn new_pointer<T: Pointer>(x: &T) -> Argument<'_> {
132+
implement_argument_constructor!(Pointer, x)
133+
}
134+
#[inline]
135+
pub fn new_binary<T: Binary>(x: &T) -> Argument<'_> {
136+
implement_argument_constructor!(Binary, x)
137+
}
138+
#[inline]
139+
pub fn new_lower_exp<T: LowerExp>(x: &T) -> Argument<'_> {
140+
implement_argument_constructor!(LowerExp, x)
141+
}
142+
#[inline]
143+
pub fn new_upper_exp<T: UpperExp>(x: &T) -> Argument<'_> {
144+
implement_argument_constructor!(UpperExp, x)
145+
}
119146

120147
#[inline]
121148
#[track_caller]

tests/ui/codegen/equal-pointers-unequal/as-cast/basic.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ fn main() {
1616
assert_ne!(a, b);
1717
// But they are the same number.
1818
assert_eq!(format!("{a}"), format!("{b}"));
19-
// And they are equal.
20-
assert_eq!(a, b);
19+
// And they are still not equal.
20+
assert_ne!(a, b);
2121
}

tests/ui/codegen/equal-pointers-unequal/as-cast/function.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ fn main() {
1717
assert_ne!(a, b);
1818
// But they are the same number.
1919
assert_eq!(format!("{a}"), format!("{b}"));
20-
// And they are equal.
21-
assert_eq!(a, b);
20+
// And they are still not equal.
21+
assert_ne!(a, b);
2222
}

tests/ui/codegen/equal-pointers-unequal/as-cast/print.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@ fn main() {
1616

1717
assert_ne!(a, b);
1818
println!("{a}"); // or b
19-
assert_eq!(a, b);
19+
assert_ne!(a, b);
2020
}

tests/ui/codegen/equal-pointers-unequal/exposed-provenance/basic.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ fn main() {
1818
assert_ne!(a, b);
1919
// But they are the same number.
2020
assert_eq!(format!("{a}"), format!("{b}"));
21-
// And they are equal.
22-
assert_eq!(a, b);
21+
// And they are still not equal.
22+
assert_ne!(a, b);
2323
}

tests/ui/codegen/equal-pointers-unequal/exposed-provenance/function.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ fn main() {
1919
assert_ne!(a, b);
2020
// But they are the same number.
2121
assert_eq!(format!("{a}"), format!("{b}"));
22-
// And they are equal.
23-
assert_eq!(a, b);
22+
// And they are still not equal.
23+
assert_ne!(a, b);
2424
}

tests/ui/codegen/equal-pointers-unequal/exposed-provenance/print.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ fn main() {
1818

1919
assert_ne!(a, b);
2020
println!("{a}"); // or b
21-
assert_eq!(a, b);
21+
assert_ne!(a, b);
2222
}

tests/ui/codegen/equal-pointers-unequal/strict-provenance/basic.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ fn main() {
1818
assert_ne!(a, b);
1919
// But they are the same number.
2020
assert_eq!(format!("{a}"), format!("{b}"));
21-
// And they are equal.
22-
assert_eq!(a, b);
21+
// And they are still not equal.
22+
assert_ne!(a, b);
2323
}

tests/ui/codegen/equal-pointers-unequal/strict-provenance/function.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ fn main() {
1919
assert_ne!(a, b);
2020
// But they are the same number.
2121
assert_eq!(format!("{a}"), format!("{b}"));
22-
// And they are equal.
23-
assert_eq!(a, b);
22+
// And they are still not equal.
23+
assert_ne!(a, b);
2424
}

tests/ui/codegen/equal-pointers-unequal/strict-provenance/print.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ fn main() {
1818

1919
assert_ne!(a, b);
2020
println!("{a}"); // or b
21-
assert_eq!(a, b);
21+
assert_ne!(a, b);
2222
}

0 commit comments

Comments
 (0)