From 9eee93306a001ef7eb2ee74183e534e62e77d8e4 Mon Sep 17 00:00:00 2001 From: Marcus Klaas Date: Tue, 29 Mar 2016 23:46:55 +0200 Subject: [PATCH 1/2] Format closed ranges --- Cargo.lock | 16 +++++++++++----- src/expr.rs | 40 +++++++++++++++++++++------------------- tests/source/expr.rs | 12 ++++++++++++ tests/target/expr.rs | 13 +++++++++++++ 4 files changed, 57 insertions(+), 24 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 853b2f4481b..813c5359c79 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6,7 +6,7 @@ dependencies = [ "env_logger 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "getopts 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", - "regex 0.1.58 (registry+https://github.com/rust-lang/crates.io-index)", + "regex 0.1.60 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-serialize 0.3.18 (registry+https://github.com/rust-lang/crates.io-index)", "strings 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "syntex_syntax 0.30.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -39,7 +39,7 @@ version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "log 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", - "regex 0.1.58 (registry+https://github.com/rust-lang/crates.io-index)", + "regex 0.1.60 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -77,20 +77,26 @@ dependencies = [ "libc 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "mempool" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "regex" -version = "0.1.58" +version = "0.1.60" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "aho-corasick 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", "memchr 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", - "regex-syntax 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "mempool 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "regex-syntax 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "utf8-ranges 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "regex-syntax" -version = "0.3.0" +version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] diff --git a/src/expr.rs b/src/expr.rs index a1e8a17c091..968eac96806 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -185,25 +185,27 @@ impl Rewrite for ast::Expr { ast::ExprKind::Repeat(ref expr, ref repeats) => { rewrite_pair(&**expr, &**repeats, "[", "; ", "]", context, width, offset) } - // TODO(#890): Handle closed ranges; rust tracking issue - // https://github.com/rust-lang/rust/issues/28237 - ast::ExprKind::Range(Some(ref lhs), Some(ref rhs), _range_limits) => { - rewrite_pair(&**lhs, &**rhs, "", "..", "", context, width, offset) - } - ast::ExprKind::Range(None, Some(ref rhs), _range_limits) => { - rewrite_unary_prefix(context, "..", &**rhs, width, offset) - } - ast::ExprKind::Range(Some(ref lhs), None, _range_limits) => { - Some(format!("{}..", - try_opt!(lhs.rewrite(context, - try_opt!(width.checked_sub(2)), - offset)))) - } - ast::ExprKind::Range(None, None, _range_limits) => { - if width >= 2 { - Some("..".into()) - } else { - None + ast::ExprKind::Range(ref lhs, ref rhs, limits) => { + let delim = match limits { + ast::RangeLimits::HalfOpen => "..", + ast::RangeLimits::Closed => "...", + }; + + match (lhs.as_ref().map(|x| &**x), rhs.as_ref().map(|x| &**x)) { + (Some(ref lhs), Some(ref rhs)) => { + rewrite_pair(&**lhs, &**rhs, "", delim, "", context, width, offset) + } + (None, Some(ref rhs)) => { + rewrite_unary_prefix(context, delim, &**rhs, width, offset) + } + (Some(ref lhs), None) => { + Some(format!("{}{}", + try_opt!(lhs.rewrite(context, + try_opt!(width.checked_sub(delim.len())), + offset)), + delim)) + } + (None, None) => wrap_str(delim.into(), context.config.max_width, width, offset), } } // We do not format these expressions yet, but they should still diff --git a/tests/source/expr.rs b/tests/source/expr.rs index c5002f5ec15..16953a932a3 100644 --- a/tests/source/expr.rs +++ b/tests/source/expr.rs @@ -244,3 +244,15 @@ fn issue767() { } else if let false = false { } } + +fn ranges() { + let x = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa .. bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; + let y = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ... bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; + let z = ... x ; + let infi_range_2 = ... ; + + a ... b + + // the expr below won't compile for some reason... + // let a = 0 ... ; +} diff --git a/tests/target/expr.rs b/tests/target/expr.rs index 9c9c65a690c..e9a051711f5 100644 --- a/tests/target/expr.rs +++ b/tests/target/expr.rs @@ -267,3 +267,16 @@ fn issue767() { } else if let false = false { } } + +fn ranges() { + let x = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa..bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; + let y = + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; + let z = ...x; + let infi_range_2 = ...; + + a...b + + // the expr below won't compile for some reason... + // let a = 0 ... ; +} From f94164611880d85f26accf8b422ee2e7609d81dd Mon Sep 17 00:00:00 2001 From: Marcus Klaas Date: Wed, 30 Mar 2016 00:01:54 +0200 Subject: [PATCH 2/2] Do not panic on type ascription or try shorthand Instead, simply format expressions involving these unstable features as they were found. --- src/expr.rs | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/expr.rs b/src/expr.rs index 968eac96806..5c4a236705f 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -176,9 +176,6 @@ impl Rewrite for ast::Expr { ast::ExprKind::Cast(ref expr, ref ty) => { rewrite_pair(&**expr, &**ty, "", " as ", "", context, width, offset) } - // TODO(#848): Handle type ascription; rust tracking issue - // https://github.com/rust-lang/rust/issues/23416 - ast::ExprKind::Type(_, _) => unimplemented!(), ast::ExprKind::Index(ref expr, ref index) => { rewrite_pair(&**expr, &**index, "", "[", "]", context, width, offset) } @@ -211,15 +208,16 @@ impl Rewrite for ast::Expr { // We do not format these expressions yet, but they should still // satisfy our width restrictions. ast::ExprKind::InPlace(..) | - ast::ExprKind::InlineAsm(..) => { + ast::ExprKind::InlineAsm(..) | + // TODO(#848): Handle type ascription + ast::ExprKind::Type(_, _) | + // TODO(#867): Handle try shorthand + ast::ExprKind::Try(_) => { wrap_str(context.snippet(self.span), context.config.max_width, width, offset) } - // TODO(#867): Handle type ascription; rust tracking issue - // https://github.com/rust-lang/rust/issues/31436 - ast::ExprKind::Try(_) => unimplemented!(), }; result.and_then(|res| recover_comment_removed(res, self.span, context, width, offset)) }