diff --git a/Cargo.lock b/Cargo.lock index 2d34a8f7cea..161a26ad916 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3,18 +3,18 @@ name = "rustfmt" version = "0.8.4" dependencies = [ "diff 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", - "env_logger 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", + "env_logger 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)", "getopts 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)", "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.23 (registry+https://github.com/rust-lang/crates.io-index)", - "log 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", "regex 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_derive 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", "strings 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", - "syntex_errors 0.58.1 (registry+https://github.com/rust-lang/crates.io-index)", - "syntex_syntax 0.58.1 (registry+https://github.com/rust-lang/crates.io-index)", + "syntex_errors 0.59.0 (registry+https://github.com/rust-lang/crates.io-index)", + "syntex_syntax 0.59.0 (registry+https://github.com/rust-lang/crates.io-index)", "term 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)", "toml 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", "unicode-segmentation 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -31,7 +31,7 @@ dependencies = [ [[package]] name = "bitflags" -version = "0.8.2" +version = "0.9.1" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] @@ -46,13 +46,23 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "env_logger" -version = "0.4.2" +version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "log 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", "regex 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "extprim" +version = "1.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "num-traits 0.1.37 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc_version 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "getopts" version = "0.2.14" @@ -79,7 +89,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "log" -version = "0.3.7" +version = "0.3.8" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] @@ -100,6 +110,14 @@ name = "quote" version = "0.3.15" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "rand" +version = "0.3.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.23 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "regex" version = "0.2.2" @@ -118,28 +136,44 @@ version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] -name = "rustc-serialize" -version = "0.3.24" +name = "rustc_version" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "semver 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "semver" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "semver-parser 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "semver-parser" +version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "serde" -version = "1.0.7" +version = "1.0.8" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "serde_derive" -version = "1.0.7" +version = "1.0.8" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_derive_internals 0.15.0 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive_internals 0.15.1 (registry+https://github.com/rust-lang/crates.io-index)", "syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "serde_derive_internals" -version = "0.15.0" +version = "0.15.1" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)", @@ -154,7 +188,7 @@ dependencies = [ "dtoa 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", "itoa 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "num-traits 0.1.37 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -162,7 +196,7 @@ name = "strings" version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "log 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -185,35 +219,40 @@ dependencies = [ [[package]] name = "syntex_errors" -version = "0.58.1" +version = "0.59.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "libc 0.2.23 (registry+https://github.com/rust-lang/crates.io-index)", - "rustc-serialize 0.3.24 (registry+https://github.com/rust-lang/crates.io-index)", - "syntex_pos 0.58.1 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)", + "syntex_pos 0.59.0 (registry+https://github.com/rust-lang/crates.io-index)", "term 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)", - "unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "syntex_pos" -version = "0.58.1" +version = "0.59.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "rustc-serialize 0.3.24 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "syntex_syntax" -version = "0.58.1" +version = "0.59.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "bitflags 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)", - "log 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", - "rustc-serialize 0.3.24 (registry+https://github.com/rust-lang/crates.io-index)", - "syntex_errors 0.58.1 (registry+https://github.com/rust-lang/crates.io-index)", - "syntex_pos 0.58.1 (registry+https://github.com/rust-lang/crates.io-index)", - "unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)", + "bitflags 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)", + "extprim 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "syntex_errors 0.59.0 (registry+https://github.com/rust-lang/crates.io-index)", + "syntex_pos 0.59.0 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -248,7 +287,7 @@ name = "toml" version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "serde 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -261,6 +300,11 @@ name = "unicode-xid" version = "0.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "unicode-xid" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "unreachable" version = "0.1.1" @@ -291,37 +335,42 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [metadata] "checksum aho-corasick 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)" = "500909c4f87a9e52355b26626d890833e9e1d53ac566db76c36faa984b889699" -"checksum bitflags 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)" = "1370e9fc2a6ae53aea8b7a5110edbd08836ed87c88736dfabccade1c2b44bff4" +"checksum bitflags 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4efd02e230a02e18f92fc2735f44597385ed02ad8f831e7c1c1156ee5e1ab3a5" "checksum diff 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = "0a515461b6c8c08419850ced27bc29e86166dcdcde8fbe76f8b1f0589bb49472" "checksum dtoa 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "80c8b71fd71146990a9742fc06dcbbde19161a267e0ad4e572c35162f4578c90" -"checksum env_logger 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e3856f1697098606fc6cb97a93de88ca3f3bc35bb878c725920e6e82ecf05e83" +"checksum env_logger 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3ddf21e73e016298f5cb37d6ef8e8da8e39f91f9ec8b0df44b7deb16a9f8cd5b" +"checksum extprim 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d9f92dbf3843b6b56a224bce6aacd734feea193013d6ee1e18f03dcb36a93911" "checksum getopts 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)" = "d9047cfbd08a437050b363d35ef160452c5fe8ea5187ae0a624708c91581d685" "checksum itoa 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "eb2f404fbc66fd9aac13e998248505e7ecb2ad8e44ab6388684c5fb11c6c251c" "checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d" "checksum libc 0.2.23 (registry+https://github.com/rust-lang/crates.io-index)" = "e7eb6b826bfc1fdea7935d46556250d1799b7fe2d9f7951071f4291710665e3e" -"checksum log 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)" = "5141eca02775a762cc6cd564d8d2c50f67c0ea3a372cbf1c51592b3e029e10ad" +"checksum log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "880f77541efa6e5cc74e76910c9884d9859683118839d6a1dc3b11e63512565b" "checksum memchr 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "1dbccc0e46f1ea47b9f17e6d67c5a96bd27030519c519c9c91327e31275a47b4" "checksum num-traits 0.1.37 (registry+https://github.com/rust-lang/crates.io-index)" = "e1cbfa3781f3fe73dc05321bed52a06d2d491eaa764c52335cf4399f046ece99" "checksum quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e920b65c65f10b2ae65c831a81a073a89edd28c7cce89475bff467ab4167a" +"checksum rand 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "022e0636ec2519ddae48154b028864bdce4eaf7d35226ab8e65c611be97b189d" "checksum regex 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "1731164734096285ec2a5ec7fea5248ae2f5485b3feeb0115af4fda2183b2d1b" "checksum regex-syntax 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ad890a5eef7953f55427c50575c680c42841653abd2b028b68cd223d157f62db" -"checksum rustc-serialize 0.3.24 (registry+https://github.com/rust-lang/crates.io-index)" = "dcf128d1287d2ea9d80910b5f1120d0b8eede3fbf1abe91c40d39ea7d51e6fda" -"checksum serde 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)" = "c0c3d79316a6051231925504f6ef893d45088e8823c77a8331a3dcf427ee9087" -"checksum serde_derive 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)" = "0019cd5b9f0529a1a0e145a912e9a2d60c325c58f7f260fc36c71976e9d76aee" -"checksum serde_derive_internals 0.15.0 (registry+https://github.com/rust-lang/crates.io-index)" = "021c338d22c7e30f957a6ab7e388cb6098499dda9fd4ba1661ee074ca7a180d1" +"checksum rustc_version 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b9743a7670d88d5d52950408ecdb7c71d8986251ab604d4689dd2ca25c9bca69" +"checksum semver 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7a3186ec9e65071a2095434b1f5bb24838d4e8e130f584c790f6033c79943537" +"checksum semver-parser 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" +"checksum serde 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)" = "c2f530d36fb84ec48fb7146936881f026cdbf4892028835fd9398475f82c1bb4" +"checksum serde_derive 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)" = "10552fad5500771f3902d0c5ba187c5881942b811b7ba0d8fbbfbf84d80806d3" +"checksum serde_derive_internals 0.15.1 (registry+https://github.com/rust-lang/crates.io-index)" = "37aee4e0da52d801acfbc0cc219eb1eda7142112339726e427926a6f6ee65d3a" "checksum serde_json 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "48b04779552e92037212c3615370f6bd57a40ebba7f20e554ff9f55e41a69a7b" "checksum strings 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "da75d8bf2c4d210d63dd09581a041b036001f9f6e03d9b151dbff810fb7ba26a" "checksum syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)" = "d3b891b9015c88c576343b9b3e41c2c11a51c219ef067b264bd9c8aa9b441dad" "checksum synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a393066ed9010ebaed60b9eafa373d4b1baac186dd7e008555b0f702b51945b6" -"checksum syntex_errors 0.58.1 (registry+https://github.com/rust-lang/crates.io-index)" = "867cc5c2d7140ae7eaad2ae9e8bf39cb18a67ca651b7834f88d46ca98faadb9c" -"checksum syntex_pos 0.58.1 (registry+https://github.com/rust-lang/crates.io-index)" = "13ad4762fe52abc9f4008e85c4fb1b1fe3aa91ccb99ff4826a439c7c598e1047" -"checksum syntex_syntax 0.58.1 (registry+https://github.com/rust-lang/crates.io-index)" = "6e0e4dbae163dd98989464c23dd503161b338790640e11537686f2ef0f25c791" +"checksum syntex_errors 0.59.0 (registry+https://github.com/rust-lang/crates.io-index)" = "0f769b93e132914e999edb13018e192bd5f0296c2886fb6d5473ee6e055acb1c" +"checksum syntex_pos 0.59.0 (registry+https://github.com/rust-lang/crates.io-index)" = "56c108cb745a38857097c1662a9d513594486acaf1d508831201fd122f83ba44" +"checksum syntex_syntax 0.59.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a2c815f257bdfb6f8b8876bb2937438a5365f2884f268c312f864cd002abace6" "checksum term 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)" = "d168af3930b369cfe245132550579d47dfd873d69470755a19c2c6568dbbd989" "checksum thread-id 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8df7875b676fddfadffd96deea3b1124e5ede707d4884248931077518cf1f773" "checksum thread_local 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "c85048c6260d17cf486ceae3282d9fb6b90be220bf5b28c400f5485ffc29f0c7" "checksum toml 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4cc5dbfb20a481e64b99eb7ae280859ec76730c7191570ba5edaa962394edb0a" "checksum unicode-segmentation 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a8083c594e02b8ae1654ae26f0ade5158b119bd88ad0e8227a5d8fcd72407946" "checksum unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8c1f860d7d29cf02cb2f3f359fd35991af3d30bac52c57d265a3c461074cb4dc" +"checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc" "checksum unreachable 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "1f2ae5ddb18e1c92664717616dd9549dde73f539f01bd7b77c2edb2446bdff91" "checksum utf8-ranges 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "662fab6525a98beff2921d7f61a39e7d59e0b425ebc7d0d9e66d316e55124122" "checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" diff --git a/Cargo.toml b/Cargo.toml index 4f5eaa7f018..083dfd787fa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,8 +28,8 @@ regex = "0.2" term = "0.4" strings = "0.1" diff = "0.1" -syntex_syntax = "0.58" -syntex_errors = "0.58" +syntex_syntax = "0.59" +syntex_errors = "0.59" log = "0.3" env_logger = "0.4" getopts = "0.2" diff --git a/src/chains.rs b/src/chains.rs index 204f0b80423..b84a7e469f2 100644 --- a/src/chains.rs +++ b/src/chains.rs @@ -78,7 +78,7 @@ use Shape; use rewrite::{Rewrite, RewriteContext}; -use utils::{wrap_str, first_line_width, last_line_width}; +use utils::{wrap_str, first_line_width, last_line_width, mk_sp}; use expr::rewrite_call; use config::IndentStyle; use macros::convert_try_mac; @@ -86,7 +86,7 @@ use std::cmp::min; use std::iter; use syntax::{ast, ptr}; -use syntax::codemap::{mk_sp, Span}; +use syntax::codemap::Span; pub fn rewrite_chain(expr: &ast::Expr, context: &RewriteContext, shape: Shape) -> Option { debug!("rewrite_chain {:?}", shape); diff --git a/src/expr.rs b/src/expr.rs index 72c10619ea2..aa5913e5f0f 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -22,7 +22,7 @@ use string::{StringFormat, rewrite_string}; use utils::{extra_offset, last_line_width, wrap_str, binary_search, first_line_width, semicolon_for_stmt, trimmed_last_line_width, left_most_sub_expr, stmt_expr, - colon_spaces, contains_skip}; + colon_spaces, contains_skip, mk_sp}; use visitor::FmtVisitor; use config::{Config, IndentStyle, MultilineStyle, ControlBraceStyle, Style}; use comment::{FindUncommented, rewrite_comment, contains_comment, recover_comment_removed}; @@ -32,7 +32,7 @@ use macros::{rewrite_macro, MacroPosition}; use syntax::{ast, ptr}; -use syntax::codemap::{CodeMap, Span, BytePos, mk_sp}; +use syntax::codemap::{CodeMap, Span, BytePos}; use syntax::parse::classify; impl Rewrite for ast::Expr { @@ -253,6 +253,16 @@ fn format_expr(expr: &ast::Expr, context.config.max_width(), shape) } + ast::ExprKind::Catch(ref block) => { + if let rewrite @ Some(_) = try_one_line_block(context, shape, "do catch ", block) { + return rewrite; + } + // 9 = `do catch ` + let budget = shape.width.checked_sub(9).unwrap_or(0); + Some(format!("{}{}", + "do catch ", + try_opt!(block.rewrite(&context, Shape::legacy(budget, shape.indent))))) + } }; match (attr_rw, expr_rw) { (Some(attr_str), Some(expr_str)) => { @@ -266,6 +276,22 @@ fn format_expr(expr: &ast::Expr, } } +fn try_one_line_block(context: &RewriteContext, + shape: Shape, + prefix: &str, + block: &ast::Block) + -> Option { + if is_simple_block(block, context.codemap) { + let expr_shape = Shape::legacy(shape.width - prefix.len(), shape.indent); + let expr_str = try_opt!(block.stmts[0].rewrite(context, expr_shape)); + let result = format!("{}{{ {} }}", prefix, expr_str); + if result.len() <= shape.width && !result.contains('\n') { + return Some(result); + } + } + None +} + pub fn rewrite_pair(lhs: &LHS, rhs: &RHS, prefix: &str, @@ -620,9 +646,7 @@ fn rewrite_closure_block(block: &ast::Block, // means we must re-format. let block_shape = shape.block().with_max_width(context.config); let block_str = try_opt!(block.rewrite(&context, block_shape)); - Some(format!("{} {}", - prefix, - try_opt!(block_str.rewrite(context, block_shape)))) + Some(format!("{} {}", prefix, block_str)) } } @@ -687,24 +711,13 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option { } else { "unsafe ".to_owned() }; - - if is_simple_block(self, context.codemap) && prefix.len() < shape.width { - let expr_str = - self.stmts[0].rewrite(context, - Shape::legacy(shape.width - prefix.len(), - shape.indent)); - let expr_str = try_opt!(expr_str); - let result = format!("{}{{ {} }}", prefix, expr_str); - if result.len() <= shape.width && !result.contains('\n') { - return Some(result); - } + if let result @ Some(_) = try_one_line_block(context, shape, &prefix, self) { + return result; } - prefix } ast::BlockCheckMode::Default => { visitor.last_pos = self.span.lo; - String::new() } }; diff --git a/src/imports.rs b/src/imports.rs index 46ce14f47b7..83e7661aad9 100644 --- a/src/imports.rs +++ b/src/imports.rs @@ -10,7 +10,7 @@ use Shape; use utils; -use syntax::codemap::{self, BytePos, Span}; +use syntax::codemap::{BytePos, Span}; use codemap::SpanUtils; use lists::{write_list, itemize_list, ListItem, ListFormatting, SeparatorTactic, definitive_tactic}; use types::{rewrite_path, PathContext}; @@ -202,7 +202,7 @@ pub fn format_imports(&mut self, use_items: &[ptr::P]) { // Order the imports by view-path & other import path properties ordered_use_items.sort_by(|a, b| compare_use_items(a.0, b.0).unwrap()); // First, output the span before the first import - let prev_span_str = self.snippet(codemap::mk_sp(self.last_pos, pos_before_first_use_item)); + let prev_span_str = self.snippet(utils::mk_sp(self.last_pos, pos_before_first_use_item)); // Look for purely trailing space at the start of the prefix snippet before a linefeed, or // a prefix that's entirely horizontal whitespace. let prefix_span_start = match prev_span_str.find('\n') { @@ -241,7 +241,7 @@ pub fn format_import(&mut self, vis: &ast::Visibility, vp: &ast::ViewPath, span: Shape::legacy(self.config.max_width() - offset.width() - 1, offset)) { Some(ref s) if s.is_empty() => { // Format up to last newline - let prev_span = codemap::mk_sp(self.last_pos, source!(self, span).lo); + let prev_span = utils::mk_sp(self.last_pos, source!(self, span).lo); let span_end = match self.snippet(prev_span).rfind('\n') { Some(offset) => self.last_pos + BytePos(offset as u32), None => source!(self, span).lo, diff --git a/src/items.rs b/src/items.rs index c9d9f7a12fc..bbbe75ddf78 100644 --- a/src/items.rs +++ b/src/items.rs @@ -14,7 +14,7 @@ use codemap::SpanUtils; use utils::{format_mutability, format_visibility, contains_skip, end_typaram, wrap_str, last_line_width, format_unsafety, trim_newlines, stmt_expr, semicolon_for_expr, - trimmed_last_line_width, colon_spaces}; + trimmed_last_line_width, colon_spaces, mk_sp}; use lists::{write_list, itemize_list, ListItem, ListFormatting, SeparatorTactic, list_helper, DefinitiveListTactic, ListTactic, definitive_tactic, format_item_list}; use expr::{is_empty_block, is_simple_block_stmt, rewrite_assign_rhs}; @@ -23,8 +23,8 @@ use rewrite::{Rewrite, RewriteContext}; use config::{Config, IndentStyle, Density, ReturnIndent, BraceStyle, Style, TypeDensity}; -use syntax::{ast, abi, codemap, ptr, symbol}; -use syntax::codemap::{Span, BytePos, mk_sp}; +use syntax::{ast, abi, ptr, symbol}; +use syntax::codemap::{Span, BytePos}; use syntax::ast::ImplItem; fn type_annotation_separator(config: &Config) -> &str { @@ -246,7 +246,7 @@ pub fn rewrite_fn(&mut self, let mut newline_brace = newline_for_brace(self.config, &generics.where_clause); let context = self.get_context(); - let block_snippet = self.snippet(codemap::mk_sp(block.span.lo, block.span.hi)); + let block_snippet = self.snippet(mk_sp(block.span.lo, block.span.hi)); let has_body = !block_snippet[1..block_snippet.len() - 1].trim().is_empty() || !context.config.fn_empty_single_line(); @@ -527,7 +527,7 @@ pub fn format_impl(context: &RewriteContext, offset: Indent, where_span_end: Option) -> Option { - if let ast::ItemKind::Impl(_, _, ref generics, ref trait_ref, _, ref items) = item.node { + if let ast::ItemKind::Impl(_, _, _, ref generics, ref trait_ref, _, ref items) = item.node { let mut result = String::new(); // First try to format the ref and type without a split at the 'for'. let mut ref_and_type = try_opt!(format_impl_ref_and_type(context, item, offset, false)); @@ -647,8 +647,13 @@ fn format_impl_ref_and_type(context: &RewriteContext, offset: Indent, split_at_for: bool) -> Option { - if let ast::ItemKind::Impl(unsafety, polarity, ref generics, ref trait_ref, ref self_ty, _) = - item.node { + if let ast::ItemKind::Impl(unsafety, + polarity, + _, + ref generics, + ref trait_ref, + ref self_ty, + _) = item.node { let mut result = String::new(); result.push_str(&format_visibility(&item.vis)); diff --git a/src/lib.rs b/src/lib.rs index 1635553eccd..b7edc183618 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -33,7 +33,7 @@ use errors::{Handler, DiagnosticBuilder}; use errors::emitter::{ColorConfig, EmitterWriter}; use syntax::ast; -use syntax::codemap::{mk_sp, CodeMap, Span}; +use syntax::codemap::{CodeMap, Span, FilePathMapping}; use syntax::parse::{self, ParseSess}; use strings::string_buffer::StringBuffer; @@ -107,7 +107,7 @@ fn span(&self) -> Span { impl Spanned for ast::Arg { fn span(&self) -> Span { if items::is_named_arg(self) { - mk_sp(self.pat.span.lo, self.ty.span.hi) + utils::mk_sp(self.pat.span.lo, self.ty.span.hi) } else { self.ty.span } @@ -578,7 +578,7 @@ pub fn format_input(input: Input, if config.disable_all_formatting() { return Ok((summary, FileMap::new(), FormatReport::new())); } - let codemap = Rc::new(CodeMap::new()); + let codemap = Rc::new(CodeMap::new(FilePathMapping::empty())); let tty_handler = Handler::with_tty_emitter(ColorConfig::Auto, true, false, Some(codemap.clone())); diff --git a/src/lists.rs b/src/lists.rs index 3448f8a446a..978d9dbaceb 100644 --- a/src/lists.rs +++ b/src/lists.rs @@ -11,12 +11,13 @@ use std::cmp; use std::iter::Peekable; -use syntax::codemap::{self, CodeMap, BytePos}; +use syntax::codemap::{CodeMap, BytePos}; use {Indent, Shape}; use comment::{FindUncommented, rewrite_comment, find_comment_end}; use config::{Config, IndentStyle}; use rewrite::RewriteContext; +use utils::mk_sp; #[derive(Eq, PartialEq, Debug, Copy, Clone)] /// Formatting tactic for lists. This will be cast down to a @@ -344,7 +345,7 @@ fn next(&mut self) -> Option { let mut new_lines = false; // Pre-comment let pre_snippet = self.codemap - .span_to_snippet(codemap::mk_sp(self.prev_span_end, (self.get_lo)(&item))) + .span_to_snippet(mk_sp(self.prev_span_end, (self.get_lo)(&item))) .unwrap(); let trimmed_pre_snippet = pre_snippet.trim(); let has_pre_comment = trimmed_pre_snippet.contains("//") || @@ -361,7 +362,7 @@ fn next(&mut self) -> Option { None => self.next_span_start, }; let post_snippet = self.codemap - .span_to_snippet(codemap::mk_sp((self.get_hi)(&item), next_start)) + .span_to_snippet(mk_sp((self.get_hi)(&item), next_start)) .unwrap(); let comment_end = match self.inner.peek() { diff --git a/src/macros.rs b/src/macros.rs index f99e236ac88..eca89a4b609 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -20,9 +20,10 @@ // and those with brackets will be formatted as array literals. use syntax::ast; -use syntax::codemap::{mk_sp, BytePos}; +use syntax::codemap::BytePos; use syntax::parse::token::Token; -use syntax::parse::tts_to_parser; +use syntax::parse::new_parser_from_tts; +use syntax::tokenstream::TokenStream; use syntax::symbol; use syntax::util::ThinVec; @@ -31,6 +32,7 @@ use rewrite::{Rewrite, RewriteContext}; use expr::{rewrite_call, rewrite_array, rewrite_pair}; use comment::{FindUncommented, contains_comment}; +use utils::mk_sp; const FORCED_BRACKET_MACROS: &'static [&'static str] = &["vec!"]; @@ -92,7 +94,8 @@ pub fn rewrite_macro(mac: &ast::Mac, original_style }; - if mac.node.tts.is_empty() && !contains_comment(&context.snippet(mac.span)) { + let ts: TokenStream = mac.node.tts.clone().into(); + if ts.is_empty() && !contains_comment(&context.snippet(mac.span)) { return match style { MacroStyle::Parens if position == MacroPosition::Item => { Some(format!("{}();", macro_name)) @@ -103,7 +106,7 @@ pub fn rewrite_macro(mac: &ast::Mac, }; } - let mut parser = tts_to_parser(context.parse_session, mac.node.tts.clone()); + let mut parser = new_parser_from_tts(context.parse_session, ts.trees().collect()); let mut expr_vec = Vec::new(); let mut vec_with_semi = false; @@ -222,7 +225,8 @@ pub fn rewrite_macro(mac: &ast::Mac, /// failed). pub fn convert_try_mac(mac: &ast::Mac, context: &RewriteContext) -> Option { if &format!("{}", mac.node.path)[..] == "try" { - let mut parser = tts_to_parser(context.parse_session, mac.node.tts.clone()); + let ts: TokenStream = mac.node.tts.clone().into(); + let mut parser = new_parser_from_tts(context.parse_session, ts.trees().collect()); Some(ast::Expr { id: ast::NodeId::new(0), // dummy value diff --git a/src/missed_spans.rs b/src/missed_spans.rs index 57ebc332ac4..90a13277865 100644 --- a/src/missed_spans.rs +++ b/src/missed_spans.rs @@ -10,9 +10,10 @@ use config::WriteMode; use visitor::FmtVisitor; -use syntax::codemap::{self, BytePos, Span, Pos}; +use syntax::codemap::{BytePos, Span, Pos}; use comment::{CodeCharKind, CommentCodeSlices, rewrite_comment}; use Shape; +use utils::mk_sp; impl<'a> FmtVisitor<'a> { fn output_at_start(&self) -> bool { @@ -65,7 +66,7 @@ fn format_missing_inner(&mut self, self.codemap.lookup_char_pos(end)); self.last_pos = end; - let span = codemap::mk_sp(start, end); + let span = mk_sp(start, end); self.write_snippet(span, &process_last_snippet); } diff --git a/src/patterns.rs b/src/patterns.rs index 312f2b0ace9..eb100abd664 100644 --- a/src/patterns.rs +++ b/src/patterns.rs @@ -12,7 +12,7 @@ use codemap::SpanUtils; use config::{IndentStyle, MultilineStyle}; use rewrite::{Rewrite, RewriteContext}; -use utils::{wrap_str, format_mutability}; +use utils::{wrap_str, format_mutability, mk_sp}; use lists::{DefinitiveListTactic, SeparatorTactic, format_item_list, itemize_list, ListItem, struct_lit_shape, struct_lit_tactic, shape_for_tactic, struct_lit_formatting, write_list}; @@ -261,14 +261,14 @@ fn rewrite_tuple_pat(pats: &[ptr::P], } else { pats[pos + 1].span().lo }; - let dot_span = codemap::mk_sp(prev, next); + let dot_span = mk_sp(prev, next); let snippet = context.snippet(dot_span); let lo = dot_span.lo + BytePos(snippet.find_uncommented("..").unwrap() as u32); let span = Span { lo: lo, // 2 == "..".len() hi: lo + BytePos(2), - expn_id: codemap::NO_EXPANSION, + ctxt: codemap::NO_EXPANSION, }; let dotdot = TuplePatField::Dotdot(span); pat_vec.insert(pos, dotdot); diff --git a/src/types.rs b/src/types.rs index 17e9a467d1b..7dfc4337178 100644 --- a/src/types.rs +++ b/src/types.rs @@ -21,7 +21,7 @@ use codemap::SpanUtils; use lists::{format_item_list, itemize_list, format_fn_args}; use rewrite::{Rewrite, RewriteContext}; -use utils::{extra_offset, format_mutability, colon_spaces, wrap_str}; +use utils::{extra_offset, format_mutability, colon_spaces, wrap_str, mk_sp}; use expr::{rewrite_unary_prefix, rewrite_pair, rewrite_tuple_type}; use config::TypeDensity; @@ -206,9 +206,7 @@ fn rewrite_segment(path_context: PathContext, .collect::>(); let next_span_lo = param_list.last().unwrap().get_span().hi + BytePos(1); - let list_lo = context - .codemap - .span_after(codemap::mk_sp(*span_lo, span_hi), "<"); + let list_lo = context.codemap.span_after(mk_sp(*span_lo, span_hi), "<"); let separator = if path_context == PathContext::Expr { "::" } else { @@ -686,6 +684,7 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option { it.rewrite(context, shape) .map(|it_str| format!("impl {}", it_str)) } + ast::TyKind::Err | ast::TyKind::Typeof(..) => unreachable!(), } } diff --git a/src/utils.rs b/src/utils.rs index e80c558e51e..b4095978b8a 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -13,7 +13,7 @@ use syntax::ast::{self, Visibility, Attribute, MetaItem, MetaItemKind, NestedMetaItem, NestedMetaItemKind, Path}; -use syntax::codemap::BytePos; +use syntax::codemap::{BytePos, Span, NO_EXPANSION}; use syntax::abi; use Shape; @@ -48,8 +48,11 @@ pub fn format_visibility(vis: &Visibility) -> Cow<'static, str> { .next() .expect("Non-global path in pub(restricted)?"); } + let is_keyword = |s: &str| s == "self" || s == "super"; + let path = segments_iter.collect::>().join("::"); + let in_str = if is_keyword(&path) { "" } else { "in " }; - Cow::from(format!("pub({}) ", segments_iter.collect::>().join("::"))) + Cow::from(format!("pub({}{}) ", in_str, path)) } } } @@ -125,7 +128,9 @@ fn is_skip_nested(meta_item: &NestedMetaItem) -> bool { #[inline] pub fn contains_skip(attrs: &[Attribute]) -> bool { - attrs.iter().any(|a| is_skip(&a.value)) + attrs + .iter() + .any(|a| a.meta().map_or(false, |a| is_skip(&a))) } // Find the end of a TyParam @@ -287,7 +292,15 @@ macro_rules! msg { // Required as generated code spans aren't guaranteed to follow on from the last span. macro_rules! source { ($this:ident, $sp: expr) => { - $this.codemap.source_callsite($sp) + $sp.source_callsite() + } +} + +pub fn mk_sp(lo: BytePos, hi: BytePos) -> Span { + Span { + lo, + hi, + ctxt: NO_EXPANSION, } } diff --git a/src/visitor.rs b/src/visitor.rs index d3ab3d2d96d..d01e8e02ead 100644 --- a/src/visitor.rs +++ b/src/visitor.rs @@ -11,13 +11,13 @@ use std::cmp; use syntax::{ast, ptr, visit}; -use syntax::codemap::{self, CodeMap, Span, BytePos}; +use syntax::codemap::{CodeMap, Span, BytePos}; use syntax::parse::ParseSess; use strings::string_buffer::StringBuffer; use {Indent, Shape}; -use utils; +use utils::{self, mk_sp}; use codemap::{LineRangeUtils, SpanUtils}; use comment::FindUncommented; use config::Config; @@ -43,7 +43,7 @@ fn item_bound(item: &ast::Item) -> Span { Span { lo: cmp::min(bound.lo, span.lo), hi: cmp::max(bound.hi, span.hi), - expn_id: span.expn_id, + ctxt: span.ctxt, } }) } @@ -122,8 +122,7 @@ pub fn visit_block(&mut self, b: &ast::Block) { let mut unindent_comment = self.is_if_else_block && !b.stmts.is_empty(); if unindent_comment { let end_pos = source!(self, b.span).hi - brace_compensation; - let snippet = self.get_context() - .snippet(codemap::mk_sp(self.last_pos, end_pos)); + let snippet = self.get_context().snippet(mk_sp(self.last_pos, end_pos)); unindent_comment = snippet.contains("//") || snippet.contains("/*"); } // FIXME: we should compress any newlines here to just one @@ -178,7 +177,7 @@ fn visit_fn(&mut self, defaultness, abi, vis, - codemap::mk_sp(s.lo, b.span.lo), + mk_sp(s.lo, b.span.lo), &b) } visit::FnKind::Method(ident, sig, vis, b) => { @@ -192,7 +191,7 @@ fn visit_fn(&mut self, defaultness, sig.abi, vis.unwrap_or(&ast::Visibility::Inherited), - codemap::mk_sp(s.lo, b.span.lo), + mk_sp(s.lo, b.span.lo), &b) } visit::FnKind::Closure(_) => unreachable!(), @@ -384,6 +383,15 @@ pub fn visit_item(&mut self, item: &ast::Item) { ast::ItemKind::Union(..) => { // FIXME(#1157): format union definitions. } + ast::ItemKind::GlobalAsm(..) => { + let snippet = Some(self.snippet(item.span)); + self.push_rewrite(item.span, snippet); + } + ast::ItemKind::MacroDef(..) => { + // FIXME(#1539): macros 2.0 + let snippet = Some(self.snippet(item.span)); + self.push_rewrite(item.span, snippet); + } } } @@ -599,8 +607,7 @@ fn format_mod(&mut self, m: &ast::Mod, vis: &ast::Visibility, s: Span, ident: as self.buffer.push_str(" {"); // Hackery to account for the closing }. let mod_lo = self.codemap.span_after(source!(self, s), "{"); - let body_snippet = - self.snippet(codemap::mk_sp(mod_lo, source!(self, m.inner).hi - BytePos(1))); + let body_snippet = self.snippet(mk_sp(mod_lo, source!(self, m.inner).hi - BytePos(1))); let body_snippet = body_snippet.trim(); if body_snippet.is_empty() { self.buffer.push_str("}"); @@ -704,7 +711,7 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option { impl Rewrite for ast::Attribute { fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option { - self.value + try_opt!(self.meta()) .rewrite(context, shape) .map(|rw| if rw.starts_with("///") { rw @@ -727,7 +734,7 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option { // Write comments and blank lines between attributes. if i > 0 { - let comment = context.snippet(codemap::mk_sp(self[i - 1].span.hi, a.span.lo)); + let comment = context.snippet(mk_sp(self[i - 1].span.hi, a.span.lo)); // This particular horror show is to preserve line breaks in between doc // comments. An alternative would be to force such line breaks to start // with the usual doc comment token. diff --git a/tests/source/catch.rs b/tests/source/catch.rs new file mode 100644 index 00000000000..64cc9e7a207 --- /dev/null +++ b/tests/source/catch.rs @@ -0,0 +1,27 @@ +#![feature(catch_expr)] + +fn main() { + let x = do catch { + foo()? + }; + + let x = do catch /* Invisible comment */ { foo()? }; + + let x = do catch { + unsafe { foo()? } + }; + + let y = match (do catch { + foo()? + }) { + _ => (), + }; + + do catch { + foo()?; + }; + + do catch { + // Regular do catch block + }; +} diff --git a/tests/source/pub-restricted.rs b/tests/source/pub-restricted.rs index 77b19b8e746..5683acbf3aa 100644 --- a/tests/source/pub-restricted.rs +++ b/tests/source/pub-restricted.rs @@ -24,7 +24,7 @@ pub( crate ) enum WriteState { WriteData(Writer), } -pub( ::global:: path :: to::some_mod ) enum WriteState { +pub(in ::global:: path :: to::some_mod ) enum WriteState { WriteId { id: U64Writer, size: U64Writer, @@ -37,7 +37,7 @@ pub( ::global:: path :: to::some_mod ) enum WriteState { WriteData(Writer), } -pub( local:: path :: to::some_mod ) enum WriteState { +pub( in local:: path :: to::some_mod ) enum WriteState { WriteId { id: U64Writer, size: U64Writer, diff --git a/tests/target/catch.rs b/tests/target/catch.rs new file mode 100644 index 00000000000..640f9bade49 --- /dev/null +++ b/tests/target/catch.rs @@ -0,0 +1,21 @@ +#![feature(catch_expr)] + +fn main() { + let x = do catch { foo()? }; + + let x = do catch /* Invisible comment */ { foo()? }; + + let x = do catch { unsafe { foo()? } }; + + let y = match (do catch { foo()? }) { + _ => (), + }; + + do catch { + foo()?; + }; + + do catch { + // Regular do catch block + }; +} diff --git a/tests/target/match.rs b/tests/target/match.rs index 09df7ebd55f..6acab043e67 100644 --- a/tests/target/match.rs +++ b/tests/target/match.rs @@ -326,11 +326,10 @@ fn issue1371() { sfEvtGainedFocus => GainedFocus, sfEvtTextEntered => { TextEntered { - unicode: - unsafe { - ::std::char::from_u32((*event.text.as_ref()).unicode) - .expect("Invalid unicode encountered on TextEntered event") - }, + unicode: unsafe { + ::std::char::from_u32((*event.text.as_ref()).unicode) + .expect("Invalid unicode encountered on TextEntered event") + }, } } sfEvtKeyPressed => { diff --git a/tests/target/pub-restricted.rs b/tests/target/pub-restricted.rs index 88053173704..0e178ef1013 100644 --- a/tests/target/pub-restricted.rs +++ b/tests/target/pub-restricted.rs @@ -24,7 +24,7 @@ pub(crate) enum WriteState { WriteData(Writer), } -pub(global::path::to::some_mod) enum WriteState { +pub(in global::path::to::some_mod) enum WriteState { WriteId { id: U64Writer, size: U64Writer, @@ -37,7 +37,7 @@ pub(global::path::to::some_mod) enum WriteState { WriteData(Writer), } -pub(local::path::to::some_mod) enum WriteState { +pub(in local::path::to::some_mod) enum WriteState { WriteId { id: U64Writer, size: U64Writer,