Skip to content

Commit 680cfe6

Browse files
committed
Also diff types inside-out
1 parent 923b53d commit 680cfe6

File tree

1 file changed

+30
-8
lines changed

1 file changed

+30
-8
lines changed

src/ast/printer.rs

+30-8
Original file line numberDiff line numberDiff line change
@@ -141,18 +141,40 @@ impl<'d> ToDisplayTree<'d> for Pattern<'_> {
141141

142142
impl<'d> ToDisplayTree<'d> for Type<'_> {
143143
fn to_display_tree(&self, a: &'d Arenas<'d>) -> DisplayTree<'d> {
144-
match self {
145-
Self::Tuple(tys) => DisplayTree::sep_by(a, ", ", tys.iter())
144+
/// Remove the refs in front of this type.
145+
fn strip_refs<'a>(ty: &'a Type<'a>, mutabilities: &mut Vec<Mutability>) -> &'a Type<'a> {
146+
match ty {
147+
Type::Ref(mtbl, ty) => {
148+
mutabilities.push(*mtbl);
149+
strip_refs(ty, mutabilities)
150+
}
151+
_ => &ty,
152+
}
153+
}
154+
let mut mutabilities = Vec::new();
155+
let ty = strip_refs(self, &mut mutabilities);
156+
let leaf = match ty {
157+
Type::Tuple(tys) => DisplayTree::sep_by(a, ", ", tys.iter())
146158
.surrounded(a, "[", "]")
147159
.tag("ty_list"),
148-
Self::Ref(mutable, ty) => format!("&{mutable}")
149-
.to_display_tree(a)
150-
.then(a, ty)
151-
.tag("ty_ref"),
152-
Self::OtherNonRef(name) | Self::AbstractNonRef(name) | Self::Abstract(name) => {
160+
Type::OtherNonRef(name) | Type::AbstractNonRef(name) | Type::Abstract(name) => {
153161
name.to_display_tree(a)
154162
}
155-
}
163+
Type::Ref(..) => unreachable!(),
164+
};
165+
// Types tend be the same on the inside; so we want to show that the innermost types are
166+
// the same and the surrounding refs differ. To do this, we extract the list of refs and
167+
// add them to the same list, with `Suffix` compare mode.
168+
DisplayTree::sep_by_compare_mode(
169+
a,
170+
"",
171+
mutabilities
172+
.iter()
173+
.map(|mutable| format!("&{mutable}").to_display_tree(a))
174+
.chain([leaf]),
175+
CompareMode::Suffix,
176+
)
177+
.tag("ty_refs")
156178
}
157179
}
158180

0 commit comments

Comments
 (0)