Skip to content

Commit

Permalink
Deduplicate code for formatting RangeEnd
Browse files Browse the repository at this point in the history
  • Loading branch information
estebank committed Oct 16, 2019
1 parent be9e6af commit 1ec6073
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
9 changes: 9 additions & 0 deletions src/librustc/hir/mod.rs
Expand Up @@ -989,6 +989,15 @@ pub enum RangeEnd {
Excluded,
}

impl fmt::Display for RangeEnd {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
RangeEnd::Included => "..=",
RangeEnd::Excluded => "..",
})
}
}

#[derive(RustcEncodable, RustcDecodable, Debug, HashStable)]
pub enum PatKind {
/// Represents a wildcard pattern (i.e., `_`).
Expand Down
7 changes: 2 additions & 5 deletions src/librustc_mir/hair/pattern/_match.rs
Expand Up @@ -482,12 +482,9 @@ impl<'tcx> Constructor<'tcx> {
// Get the right sign on the output:
let ty = ty::ParamEnv::empty().and(*ty);
format!(
"{}..{}{}",
"{}{}{}",
ty::Const::from_bits(tcx, *lo, ty),
match range_end {
RangeEnd::Included => "=",
RangeEnd::Excluded => "",
},
range_end,
ty::Const::from_bits(tcx, *hi, ty),
)
}
Expand Down
5 changes: 1 addition & 4 deletions src/librustc_mir/hair/pattern/mod.rs
Expand Up @@ -312,10 +312,7 @@ impl<'tcx> fmt::Display for Pat<'tcx> {
}
PatKind::Range(PatRange { lo, hi, end }) => {
write!(f, "{}", lo)?;
match end {
RangeEnd::Included => write!(f, "..=")?,
RangeEnd::Excluded => write!(f, "..")?,
}
write!(f, "{}", end)?;
write!(f, "{}", hi)
}
PatKind::Slice { ref prefix, ref slice, ref suffix } |
Expand Down

0 comments on commit 1ec6073

Please sign in to comment.