Skip to content

Commit a9ea85e

Browse files
committed
Revert "Make thir::Pat not implement fmt::Display directly"
This reverts commit ae0ec73. The original change in rust-lang#128304 was intended to be a step towards being able to print `thir::Pat` even after switching to `PatId`. But because the only patterns that need to be printed are the synthetic ones created by pattern analysis (for diagnostic purposes only), it makes more sense to completely separate the printable patterns from the real THIR patterns.
1 parent a2b3256 commit a9ea85e

File tree

1 file changed

+19
-46
lines changed

1 file changed

+19
-46
lines changed

compiler/rustc_middle/src/thir.rs

+19-46
Original file line numberDiff line numberDiff line change
@@ -1073,33 +1073,8 @@ impl<'tcx> PatRangeBoundary<'tcx> {
10731073
}
10741074
}
10751075

1076-
impl<'tcx> Pat<'tcx> {
1077-
/// Prints a [`Pat`] to an owned string, for user-facing diagnostics.
1078-
///
1079-
/// If we ever switch over to storing subpatterns as `PatId`, this will also
1080-
/// need to take a context that can resolve IDs to subpatterns.
1081-
pub fn to_string(&self) -> String {
1082-
format!("{}", self.display())
1083-
}
1084-
1085-
/// Used internally by [`fmt::Display`] for [`PatDisplay`].
1086-
fn display(&self) -> PatDisplay<'_, 'tcx> {
1087-
PatDisplay { pat: self }
1088-
}
1089-
}
1090-
1091-
/// Wrapper around [`&Pat<'tcx>`][`Pat`] that implements [`fmt::Display`].
1092-
///
1093-
/// If we ever switch over to storing subpatterns as `PatId`, this will also
1094-
/// need to hold a context that can resolve IDs to subpatterns.
1095-
struct PatDisplay<'pat, 'tcx> {
1096-
pat: &'pat Pat<'tcx>,
1097-
}
1098-
1099-
impl<'pat, 'tcx> fmt::Display for PatDisplay<'pat, 'tcx> {
1076+
impl<'tcx> fmt::Display for Pat<'tcx> {
11001077
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1101-
let &Self { pat } = self;
1102-
11031078
// Printing lists is a chore.
11041079
let mut first = true;
11051080
let mut start_or_continue = |s| {
@@ -1112,22 +1087,20 @@ impl<'pat, 'tcx> fmt::Display for PatDisplay<'pat, 'tcx> {
11121087
};
11131088
let mut start_or_comma = || start_or_continue(", ");
11141089

1115-
match pat.kind {
1090+
match self.kind {
11161091
PatKind::Wild => write!(f, "_"),
11171092
PatKind::Never => write!(f, "!"),
1118-
PatKind::AscribeUserType { ref subpattern, .. } => {
1119-
write!(f, "{}: _", subpattern.display())
1120-
}
1093+
PatKind::AscribeUserType { ref subpattern, .. } => write!(f, "{subpattern}: _"),
11211094
PatKind::Binding { name, mode, ref subpattern, .. } => {
11221095
f.write_str(mode.prefix_str())?;
11231096
write!(f, "{name}")?;
11241097
if let Some(ref subpattern) = *subpattern {
1125-
write!(f, " @ {}", subpattern.display())?;
1098+
write!(f, " @ {subpattern}")?;
11261099
}
11271100
Ok(())
11281101
}
11291102
PatKind::Variant { ref subpatterns, .. } | PatKind::Leaf { ref subpatterns } => {
1130-
let variant_and_name = match pat.kind {
1103+
let variant_and_name = match self.kind {
11311104
PatKind::Variant { adt_def, variant_index, .. } => ty::tls::with(|tcx| {
11321105
let variant = adt_def.variant(variant_index);
11331106
let adt_did = adt_def.did();
@@ -1140,7 +1113,7 @@ impl<'pat, 'tcx> fmt::Display for PatDisplay<'pat, 'tcx> {
11401113
};
11411114
Some((variant, name))
11421115
}),
1143-
_ => pat.ty.ty_adt_def().and_then(|adt_def| {
1116+
_ => self.ty.ty_adt_def().and_then(|adt_def| {
11441117
if !adt_def.is_enum() {
11451118
ty::tls::with(|tcx| {
11461119
Some((adt_def.non_enum_variant(), tcx.def_path_str(adt_def.did())))
@@ -1165,11 +1138,11 @@ impl<'pat, 'tcx> fmt::Display for PatDisplay<'pat, 'tcx> {
11651138
continue;
11661139
}
11671140
let name = variant.fields[p.field].name;
1168-
write!(f, "{}{}: {}", start_or_comma(), name, p.pattern.display())?;
1141+
write!(f, "{}{}: {}", start_or_comma(), name, p.pattern)?;
11691142
printed += 1;
11701143
}
11711144

1172-
let is_union = pat.ty.ty_adt_def().is_some_and(|adt| adt.is_union());
1145+
let is_union = self.ty.ty_adt_def().is_some_and(|adt| adt.is_union());
11731146
if printed < variant.fields.len() && (!is_union || printed == 0) {
11741147
write!(f, "{}..", start_or_comma())?;
11751148
}
@@ -1188,14 +1161,14 @@ impl<'pat, 'tcx> fmt::Display for PatDisplay<'pat, 'tcx> {
11881161
// Common case: the field is where we expect it.
11891162
if let Some(p) = subpatterns.get(i) {
11901163
if p.field.index() == i {
1191-
write!(f, "{}", p.pattern.display())?;
1164+
write!(f, "{}", p.pattern)?;
11921165
continue;
11931166
}
11941167
}
11951168

11961169
// Otherwise, we have to go looking for it.
11971170
if let Some(p) = subpatterns.iter().find(|p| p.field.index() == i) {
1198-
write!(f, "{}", p.pattern.display())?;
1171+
write!(f, "{}", p.pattern)?;
11991172
} else {
12001173
write!(f, "_")?;
12011174
}
@@ -1206,45 +1179,45 @@ impl<'pat, 'tcx> fmt::Display for PatDisplay<'pat, 'tcx> {
12061179
Ok(())
12071180
}
12081181
PatKind::Deref { ref subpattern } => {
1209-
match pat.ty.kind() {
1182+
match self.ty.kind() {
12101183
ty::Adt(def, _) if def.is_box() => write!(f, "box ")?,
12111184
ty::Ref(_, _, mutbl) => {
12121185
write!(f, "&{}", mutbl.prefix_str())?;
12131186
}
1214-
_ => bug!("{} is a bad Deref pattern type", pat.ty),
1187+
_ => bug!("{} is a bad Deref pattern type", self.ty),
12151188
}
1216-
write!(f, "{}", subpattern.display())
1189+
write!(f, "{subpattern}")
12171190
}
12181191
PatKind::DerefPattern { ref subpattern, .. } => {
1219-
write!(f, "deref!({})", subpattern.display())
1192+
write!(f, "deref!({subpattern})")
12201193
}
12211194
PatKind::Constant { value } => write!(f, "{value}"),
12221195
PatKind::InlineConstant { def: _, ref subpattern } => {
1223-
write!(f, "{} (from inline const)", subpattern.display())
1196+
write!(f, "{} (from inline const)", subpattern)
12241197
}
12251198
PatKind::Range(ref range) => write!(f, "{range}"),
12261199
PatKind::Slice { ref prefix, ref slice, ref suffix }
12271200
| PatKind::Array { ref prefix, ref slice, ref suffix } => {
12281201
write!(f, "[")?;
12291202
for p in prefix.iter() {
1230-
write!(f, "{}{}", start_or_comma(), p.display())?;
1203+
write!(f, "{}{}", start_or_comma(), p)?;
12311204
}
12321205
if let Some(ref slice) = *slice {
12331206
write!(f, "{}", start_or_comma())?;
12341207
match slice.kind {
12351208
PatKind::Wild => {}
1236-
_ => write!(f, "{}", slice.display())?,
1209+
_ => write!(f, "{slice}")?,
12371210
}
12381211
write!(f, "..")?;
12391212
}
12401213
for p in suffix.iter() {
1241-
write!(f, "{}{}", start_or_comma(), p.display())?;
1214+
write!(f, "{}{}", start_or_comma(), p)?;
12421215
}
12431216
write!(f, "]")
12441217
}
12451218
PatKind::Or { ref pats } => {
12461219
for pat in pats.iter() {
1247-
write!(f, "{}{}", start_or_continue(" | "), pat.display())?;
1220+
write!(f, "{}{}", start_or_continue(" | "), pat)?;
12481221
}
12491222
Ok(())
12501223
}

0 commit comments

Comments
 (0)