From 1ec60730fecfc72eb7454f2b8d37f7de7068cd20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Thu, 29 Aug 2019 17:42:45 -0700 Subject: [PATCH] Deduplicate code for formatting `RangeEnd` --- src/librustc/hir/mod.rs | 9 +++++++++ src/librustc_mir/hair/pattern/_match.rs | 7 ++----- src/librustc_mir/hair/pattern/mod.rs | 5 +---- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/librustc/hir/mod.rs b/src/librustc/hir/mod.rs index 7350f89018be..02b391615b62 100644 --- a/src/librustc/hir/mod.rs +++ b/src/librustc/hir/mod.rs @@ -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., `_`). diff --git a/src/librustc_mir/hair/pattern/_match.rs b/src/librustc_mir/hair/pattern/_match.rs index 48ff7fff5edf..636bb2ed6179 100644 --- a/src/librustc_mir/hair/pattern/_match.rs +++ b/src/librustc_mir/hair/pattern/_match.rs @@ -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), ) } diff --git a/src/librustc_mir/hair/pattern/mod.rs b/src/librustc_mir/hair/pattern/mod.rs index 58d741b9295a..7e17162dfb3e 100644 --- a/src/librustc_mir/hair/pattern/mod.rs +++ b/src/librustc_mir/hair/pattern/mod.rs @@ -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 } |