Skip to content

Commit 42dead4

Browse files
committed
Auto merge of #115889 - matthiaskrgr:rollup-kfmauvh, r=matthiaskrgr
Rollup of 2 pull requests Successful merges: - #115607 (clarify that unsafe code must not rely on our safe traits) - #115866 (make interpreter and TyAndLayout type Debug impl independent of Ty debug impl) Failed merges: - #115873 (Make `TyKind::Adt`'s `Debug` impl be more pretty) - #115884 (make ty::Const debug printing less verbose) r? `@ghost` `@rustbot` modify labels: rollup
2 parents fd70f7e + 53cd978 commit 42dead4

File tree

9 files changed

+105
-23
lines changed

9 files changed

+105
-23
lines changed

compiler/rustc_const_eval/src/interpret/operand.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,11 @@ impl<Prov: Provenance> std::fmt::Display for ImmTy<'_, Prov> {
136136

137137
impl<Prov: Provenance> std::fmt::Debug for ImmTy<'_, Prov> {
138138
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
139-
f.debug_struct("ImmTy").field("imm", &self.imm).field("ty", &self.layout.ty).finish()
139+
// Printing `layout` results in too much noise; just print a nice version of the type.
140+
f.debug_struct("ImmTy")
141+
.field("imm", &self.imm)
142+
.field("ty", &format_args!("{}", self.layout.ty))
143+
.finish()
140144
}
141145
}
142146

@@ -305,7 +309,11 @@ pub struct OpTy<'tcx, Prov: Provenance = AllocId> {
305309

306310
impl<Prov: Provenance> std::fmt::Debug for OpTy<'_, Prov> {
307311
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
308-
f.debug_struct("OpTy").field("op", &self.op).field("ty", &self.layout.ty).finish()
312+
// Printing `layout` results in too much noise; just print a nice version of the type.
313+
f.debug_struct("OpTy")
314+
.field("op", &self.op)
315+
.field("ty", &format_args!("{}", self.layout.ty))
316+
.finish()
309317
}
310318
}
311319

compiler/rustc_const_eval/src/interpret/place.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,10 @@ pub struct MPlaceTy<'tcx, Prov: Provenance = AllocId> {
117117

118118
impl<Prov: Provenance> std::fmt::Debug for MPlaceTy<'_, Prov> {
119119
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
120+
// Printing `layout` results in too much noise; just print a nice version of the type.
120121
f.debug_struct("MPlaceTy")
121122
.field("mplace", &self.mplace)
122-
.field("ty", &self.layout.ty)
123+
.field("ty", &format_args!("{}", self.layout.ty))
123124
.finish()
124125
}
125126
}
@@ -237,7 +238,11 @@ pub struct PlaceTy<'tcx, Prov: Provenance = AllocId> {
237238

238239
impl<Prov: Provenance> std::fmt::Debug for PlaceTy<'_, Prov> {
239240
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
240-
f.debug_struct("PlaceTy").field("place", &self.place).field("ty", &self.layout.ty).finish()
241+
// Printing `layout` results in too much noise; just print a nice version of the type.
242+
f.debug_struct("PlaceTy")
243+
.field("place", &self.place)
244+
.field("ty", &format_args!("{}", self.layout.ty))
245+
.finish()
241246
}
242247
}
243248

compiler/rustc_target/src/abi/call/mod.rs

+26-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::abi::{self, Abi, Align, FieldsShape, Size};
22
use crate::abi::{HasDataLayout, TyAbiInterface, TyAndLayout};
33
use crate::spec::{self, HasTargetSpec};
44
use rustc_span::Symbol;
5+
use std::fmt;
56
use std::str::FromStr;
67

78
mod aarch64;
@@ -515,12 +516,20 @@ impl<'a, Ty> TyAndLayout<'a, Ty> {
515516

516517
/// Information about how to pass an argument to,
517518
/// or return a value from, a function, under some ABI.
518-
#[derive(Clone, PartialEq, Eq, Hash, Debug, HashStable_Generic)]
519+
#[derive(Clone, PartialEq, Eq, Hash, HashStable_Generic)]
519520
pub struct ArgAbi<'a, Ty> {
520521
pub layout: TyAndLayout<'a, Ty>,
521522
pub mode: PassMode,
522523
}
523524

525+
// Needs to be a custom impl because of the bounds on the `TyAndLayout` debug impl.
526+
impl<'a, Ty: fmt::Display> fmt::Debug for ArgAbi<'a, Ty> {
527+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
528+
let ArgAbi { layout, mode } = self;
529+
f.debug_struct("ArgAbi").field("layout", layout).field("mode", mode).finish()
530+
}
531+
}
532+
524533
impl<'a, Ty> ArgAbi<'a, Ty> {
525534
/// This defines the "default ABI" for that type, that is then later adjusted in `fn_abi_adjust_for_abi`.
526535
pub fn new(
@@ -694,7 +703,7 @@ impl RiscvInterruptKind {
694703
///
695704
/// I will do my best to describe this structure, but these
696705
/// comments are reverse-engineered and may be inaccurate. -NDM
697-
#[derive(Clone, PartialEq, Eq, Hash, Debug, HashStable_Generic)]
706+
#[derive(Clone, PartialEq, Eq, Hash, HashStable_Generic)]
698707
pub struct FnAbi<'a, Ty> {
699708
/// The LLVM types of each argument.
700709
pub args: Box<[ArgAbi<'a, Ty>]>,
@@ -715,6 +724,21 @@ pub struct FnAbi<'a, Ty> {
715724
pub can_unwind: bool,
716725
}
717726

727+
// Needs to be a custom impl because of the bounds on the `TyAndLayout` debug impl.
728+
impl<'a, Ty: fmt::Display> fmt::Debug for FnAbi<'a, Ty> {
729+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
730+
let FnAbi { args, ret, c_variadic, fixed_count, conv, can_unwind } = self;
731+
f.debug_struct("FnAbi")
732+
.field("args", args)
733+
.field("ret", ret)
734+
.field("c_variadic", c_variadic)
735+
.field("fixed_count", fixed_count)
736+
.field("conv", conv)
737+
.field("can_unwind", can_unwind)
738+
.finish()
739+
}
740+
}
741+
718742
/// Error produced by attempting to adjust a `FnAbi`, for a "foreign" ABI.
719743
#[derive(Copy, Clone, Debug, HashStable_Generic)]
720744
pub enum AdjustForForeignAbiError {

compiler/rustc_target/src/abi/mod.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ pub use Primitive::*;
33

44
use crate::json::{Json, ToJson};
55

6+
use std::fmt;
67
use std::ops::Deref;
78

89
use rustc_macros::HashStable_Generic;
@@ -24,12 +25,22 @@ impl ToJson for Endian {
2425
/// to that obtained from `layout_of(ty)`, as we need to produce
2526
/// layouts for which Rust types do not exist, such as enum variants
2627
/// or synthetic fields of enums (i.e., discriminants) and fat pointers.
27-
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, HashStable_Generic)]
28+
#[derive(Copy, Clone, PartialEq, Eq, Hash, HashStable_Generic)]
2829
pub struct TyAndLayout<'a, Ty> {
2930
pub ty: Ty,
3031
pub layout: Layout<'a>,
3132
}
3233

34+
impl<'a, Ty: fmt::Display> fmt::Debug for TyAndLayout<'a, Ty> {
35+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
36+
// Print the type in a readable way, not its debug representation.
37+
f.debug_struct("TyAndLayout")
38+
.field("ty", &format_args!("{}", self.ty))
39+
.field("layout", &self.layout)
40+
.finish()
41+
}
42+
}
43+
3344
impl<'a, Ty> Deref for TyAndLayout<'a, Ty> {
3445
type Target = &'a LayoutS;
3546
fn deref(&self) -> &&'a LayoutS {

library/core/src/cmp.rs

+20
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ use self::Ordering::*;
6363
/// (transitive) impls are not forced to exist, but these requirements apply
6464
/// whenever they do exist.
6565
///
66+
/// Violating these requirements is a logic error. The behavior resulting from a logic error is not
67+
/// specified, but users of the trait must ensure that such logic errors do *not* result in
68+
/// undefined behavior. This means that `unsafe` code **must not** rely on the correctness of these
69+
/// methods.
70+
///
6671
/// ## Derivable
6772
///
6873
/// This trait can be used with `#[derive]`. When `derive`d on structs, two
@@ -250,6 +255,11 @@ pub macro PartialEq($item:item) {
250255
/// This property cannot be checked by the compiler, and therefore `Eq` implies
251256
/// [`PartialEq`], and has no extra methods.
252257
///
258+
/// Violating this property is a logic error. The behavior resulting from a logic error is not
259+
/// specified, but users of the trait must ensure that such logic errors do *not* result in
260+
/// undefined behavior. This means that `unsafe` code **must not** rely on the correctness of these
261+
/// methods.
262+
///
253263
/// ## Derivable
254264
///
255265
/// This trait can be used with `#[derive]`. When `derive`d, because `Eq` has
@@ -659,6 +669,11 @@ impl<T: Clone> Clone for Reverse<T> {
659669
/// It's easy to accidentally make `cmp` and `partial_cmp` disagree by
660670
/// deriving some of the traits and manually implementing others.
661671
///
672+
/// Violating these requirements is a logic error. The behavior resulting from a logic error is not
673+
/// specified, but users of the trait must ensure that such logic errors do *not* result in
674+
/// undefined behavior. This means that `unsafe` code **must not** rely on the correctness of these
675+
/// methods.
676+
///
662677
/// ## Corollaries
663678
///
664679
/// From the above and the requirements of `PartialOrd`, it follows that `<` defines a strict total order.
@@ -892,6 +907,11 @@ pub macro Ord($item:item) {
892907
/// transitively: if `T: PartialOrd<U>` and `U: PartialOrd<V>` then `U: PartialOrd<T>` and `T:
893908
/// PartialOrd<V>`.
894909
///
910+
/// Violating these requirements is a logic error. The behavior resulting from a logic error is not
911+
/// specified, but users of the trait must ensure that such logic errors do *not* result in
912+
/// undefined behavior. This means that `unsafe` code **must not** rely on the correctness of these
913+
/// methods.
914+
///
895915
/// ## Corollaries
896916
///
897917
/// The following corollaries follow from the above requirements:

library/core/src/hash/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,11 @@ mod sip;
153153
/// Thankfully, you won't need to worry about upholding this property when
154154
/// deriving both [`Eq`] and `Hash` with `#[derive(PartialEq, Eq, Hash)]`.
155155
///
156+
/// Violating this property is a logic error. The behavior resulting from a logic error is not
157+
/// specified, but users of the trait must ensure that such logic errors do *not* result in
158+
/// undefined behavior. This means that `unsafe` code **must not** rely on the correctness of these
159+
/// methods.
160+
///
156161
/// ## Prefix collisions
157162
///
158163
/// Implementations of `hash` should ensure that the data they

library/core/src/ops/deref.rs

+10
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
/// For similar reasons, **this trait should never fail**. Failure during
1515
/// dereferencing can be extremely confusing when `Deref` is invoked implicitly.
1616
///
17+
/// Violating these requirements is a logic error. The behavior resulting from a logic error is not
18+
/// specified, but users of the trait must ensure that such logic errors do *not* result in
19+
/// undefined behavior. This means that `unsafe` code **must not** rely on the correctness of this
20+
/// method.
21+
///
1722
/// # More on `Deref` coercion
1823
///
1924
/// If `T` implements `Deref<Target = U>`, and `x` is a value of type `T`, then:
@@ -114,6 +119,11 @@ impl<T: ?Sized> Deref for &mut T {
114119
/// dereferencing can be extremely confusing when `DerefMut` is invoked
115120
/// implicitly.
116121
///
122+
/// Violating these requirements is a logic error. The behavior resulting from a logic error is not
123+
/// specified, but users of the trait must ensure that such logic errors do *not* result in
124+
/// undefined behavior. This means that `unsafe` code **must not** rely on the correctness of this
125+
/// method.
126+
///
117127
/// # More on `Deref` coercion
118128
///
119129
/// If `T` implements `DerefMut<Target = U>`, and `x` is a value of type `T`,

tests/ui/abi/debug.rs

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
// normalize-stderr-test "(valid_range): 0\.\.=(4294967295|18446744073709551615)" -> "$1: $$FULL"
55
// This pattern is prepared for when we account for alignment in the niche.
66
// normalize-stderr-test "(valid_range): [1-9]\.\.=(429496729[0-9]|1844674407370955161[0-9])" -> "$1: $$NON_NULL"
7-
// normalize-stderr-test "Leaf\(0x0*20\)" -> "Leaf(0x0...20)"
87
// Some attributes are only computed for release builds:
98
// compile-flags: -O
109
#![feature(rustc_attrs)]

tests/ui/abi/debug.stderr

+15-15
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ error: fn_abi_of(test) = FnAbi {
8787
conv: Rust,
8888
can_unwind: $SOME_BOOL,
8989
}
90-
--> $DIR/debug.rs:16:1
90+
--> $DIR/debug.rs:15:1
9191
|
9292
LL | fn test(_x: u8) -> bool { true }
9393
| ^^^^^^^^^^^^^^^^^^^^^^^
@@ -181,7 +181,7 @@ error: fn_abi_of(TestFnPtr) = FnAbi {
181181
conv: Rust,
182182
can_unwind: $SOME_BOOL,
183183
}
184-
--> $DIR/debug.rs:19:1
184+
--> $DIR/debug.rs:18:1
185185
|
186186
LL | type TestFnPtr = fn(bool) -> u8;
187187
| ^^^^^^^^^^^^^^
@@ -190,7 +190,7 @@ error: fn_abi_of(test_generic) = FnAbi {
190190
args: [
191191
ArgAbi {
192192
layout: TyAndLayout {
193-
ty: *const T/#0,
193+
ty: *const T,
194194
layout: Layout {
195195
size: $SOME_SIZE,
196196
align: AbiAndPrefAlign {
@@ -257,13 +257,13 @@ error: fn_abi_of(test_generic) = FnAbi {
257257
conv: Rust,
258258
can_unwind: $SOME_BOOL,
259259
}
260-
--> $DIR/debug.rs:22:1
260+
--> $DIR/debug.rs:21:1
261261
|
262262
LL | fn test_generic<T>(_x: *const T) { }
263263
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
264264

265265
error: `#[rustc_abi]` can only be applied to function items, type aliases, and associated functions
266-
--> $DIR/debug.rs:25:1
266+
--> $DIR/debug.rs:24:1
267267
|
268268
LL | const C: () = ();
269269
| ^^^^^^^^^^^
@@ -409,7 +409,7 @@ error: ABIs are not compatible
409409
conv: Rust,
410410
can_unwind: $SOME_BOOL,
411411
}
412-
--> $DIR/debug.rs:41:1
412+
--> $DIR/debug.rs:40:1
413413
|
414414
LL | type TestAbiNe = (fn(u8), fn(u32));
415415
| ^^^^^^^^^^^^^^
@@ -419,7 +419,7 @@ error: ABIs are not compatible
419419
args: [
420420
ArgAbi {
421421
layout: TyAndLayout {
422-
ty: [u8; Const { ty: usize, kind: Leaf(0x0...20) }],
422+
ty: [u8; 32],
423423
layout: Layout {
424424
size: Size(32 bytes),
425425
align: AbiAndPrefAlign {
@@ -490,7 +490,7 @@ error: ABIs are not compatible
490490
args: [
491491
ArgAbi {
492492
layout: TyAndLayout {
493-
ty: [u32; Const { ty: usize, kind: Leaf(0x0...20) }],
493+
ty: [u32; 32],
494494
layout: Layout {
495495
size: Size(128 bytes),
496496
align: AbiAndPrefAlign {
@@ -557,7 +557,7 @@ error: ABIs are not compatible
557557
conv: Rust,
558558
can_unwind: $SOME_BOOL,
559559
}
560-
--> $DIR/debug.rs:44:1
560+
--> $DIR/debug.rs:43:1
561561
|
562562
LL | type TestAbiNeLarger = (fn([u8; 32]), fn([u32; 32]));
563563
| ^^^^^^^^^^^^^^^^^^^^
@@ -700,7 +700,7 @@ error: ABIs are not compatible
700700
conv: Rust,
701701
can_unwind: $SOME_BOOL,
702702
}
703-
--> $DIR/debug.rs:47:1
703+
--> $DIR/debug.rs:46:1
704704
|
705705
LL | type TestAbiNeFloat = (fn(f32), fn(u32));
706706
| ^^^^^^^^^^^^^^^^^^^
@@ -846,13 +846,13 @@ error: ABIs are not compatible
846846
conv: Rust,
847847
can_unwind: $SOME_BOOL,
848848
}
849-
--> $DIR/debug.rs:51:1
849+
--> $DIR/debug.rs:50:1
850850
|
851851
LL | type TestAbiNeSign = (fn(i32), fn(u32));
852852
| ^^^^^^^^^^^^^^^^^^
853853

854854
error[E0277]: the size for values of type `str` cannot be known at compilation time
855-
--> $DIR/debug.rs:54:46
855+
--> $DIR/debug.rs:53:46
856856
|
857857
LL | type TestAbiEqNonsense = (fn((str, str)), fn((str, str)));
858858
| ^^^^^^^^^^ doesn't have a size known at compile-time
@@ -861,7 +861,7 @@ LL | type TestAbiEqNonsense = (fn((str, str)), fn((str, str)));
861861
= note: only the last element of a tuple may have a dynamically sized type
862862

863863
error: `#[rustc_abi]` can only be applied to function items, type aliases, and associated functions
864-
--> $DIR/debug.rs:29:5
864+
--> $DIR/debug.rs:28:5
865865
|
866866
LL | const C: () = ();
867867
| ^^^^^^^^^^^
@@ -870,7 +870,7 @@ error: fn_abi_of(assoc_test) = FnAbi {
870870
args: [
871871
ArgAbi {
872872
layout: TyAndLayout {
873-
ty: &ReErased Adt(S, []),
873+
ty: &S,
874874
layout: Layout {
875875
size: $SOME_SIZE,
876876
align: AbiAndPrefAlign {
@@ -949,7 +949,7 @@ error: fn_abi_of(assoc_test) = FnAbi {
949949
conv: Rust,
950950
can_unwind: $SOME_BOOL,
951951
}
952-
--> $DIR/debug.rs:34:5
952+
--> $DIR/debug.rs:33:5
953953
|
954954
LL | fn assoc_test(&self) { }
955955
| ^^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)