Skip to content

Commit 4a5c46f

Browse files
committed
Manually implement Debug for ImportKind.
1 parent 75b7e52 commit 4a5c46f

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

compiler/rustc_resolve/src/imports.rs

+39-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use std::{mem, ptr};
3131
type Res = def::Res<NodeId>;
3232

3333
/// Contains data for specific kinds of imports.
34-
#[derive(Clone, Debug)]
34+
#[derive(Clone)]
3535
pub enum ImportKind<'a> {
3636
Single {
3737
/// `source` in `use prefix::source as target`.
@@ -62,6 +62,44 @@ pub enum ImportKind<'a> {
6262
MacroUse,
6363
}
6464

65+
/// Manually implement `Debug` for `ImportKind` because the `source/target_bindings`
66+
/// contain `Cell`s which can introduce infinite loops while printing.
67+
impl<'a> std::fmt::Debug for ImportKind<'a> {
68+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
69+
use ImportKind::*;
70+
match self {
71+
Single {
72+
ref source,
73+
ref target,
74+
ref type_ns_only,
75+
ref nested,
76+
ref additional_ids,
77+
// Ignore the following to avoid an infinite loop while printing.
78+
source_bindings: _,
79+
target_bindings: _,
80+
} => f
81+
.debug_struct("Single")
82+
.field("source", source)
83+
.field("target", target)
84+
.field("type_ns_only", type_ns_only)
85+
.field("nested", nested)
86+
.field("additional_ids", additional_ids)
87+
.finish(),
88+
Glob { ref is_prelude, ref max_vis } => f
89+
.debug_struct("Glob")
90+
.field("is_prelude", is_prelude)
91+
.field("max_vis", max_vis)
92+
.finish(),
93+
ExternCrate { ref source, ref target } => f
94+
.debug_struct("ExternCrate")
95+
.field("source", source)
96+
.field("target", target)
97+
.finish(),
98+
MacroUse => f.debug_struct("MacroUse").finish(),
99+
}
100+
}
101+
}
102+
65103
/// One import.
66104
#[derive(Debug, Clone)]
67105
pub(crate) struct Import<'a> {

0 commit comments

Comments
 (0)