diff --git a/Cargo.lock b/Cargo.lock index db0286c07e1..24681cfffbe 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -24,7 +24,7 @@ dependencies = [ "environment 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "failure 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "failure_derive 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_json 1.0.20 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 1.0.22 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -77,7 +77,7 @@ dependencies = [ "semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_json 1.0.20 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 1.0.22 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -175,7 +175,7 @@ dependencies = [ "atty 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)", "humantime 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", - "regex 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "regex 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "termcolor 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -334,7 +334,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "libc 0.2.42 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", - "smallvec 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)", + "smallvec 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)", "winapi 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -405,7 +405,7 @@ dependencies = [ [[package]] name = "regex" -version = "1.0.0" +version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "aho-corasick 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)", @@ -425,15 +425,15 @@ dependencies = [ [[package]] name = "rustc-ap-arena" -version = "164.0.0" +version = "174.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "rustc-ap-rustc_data_structures 164.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-ap-rustc_data_structures 174.0.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "rustc-ap-rustc_cratesio_shim" -version = "164.0.0" +version = "174.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "bitflags 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)", @@ -442,7 +442,7 @@ dependencies = [ [[package]] name = "rustc-ap-rustc_data_structures" -version = "164.0.0" +version = "174.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "cfg-if 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", @@ -450,8 +450,8 @@ dependencies = [ "log 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", "parking_lot 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)", "parking_lot_core 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)", - "rustc-ap-rustc_cratesio_shim 164.0.0 (registry+https://github.com/rust-lang/crates.io-index)", - "rustc-ap-serialize 164.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-ap-rustc_cratesio_shim 174.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-ap-serialize 174.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-rayon 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-rayon-core 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -460,56 +460,56 @@ dependencies = [ [[package]] name = "rustc-ap-rustc_errors" -version = "164.0.0" +version = "174.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "atty 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)", - "rustc-ap-rustc_data_structures 164.0.0 (registry+https://github.com/rust-lang/crates.io-index)", - "rustc-ap-serialize 164.0.0 (registry+https://github.com/rust-lang/crates.io-index)", - "rustc-ap-syntax_pos 164.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-ap-rustc_data_structures 174.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-ap-serialize 174.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-ap-syntax_pos 174.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "termcolor 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", "unicode-width 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "rustc-ap-rustc_target" -version = "164.0.0" +version = "174.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "bitflags 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", - "rustc-ap-rustc_cratesio_shim 164.0.0 (registry+https://github.com/rust-lang/crates.io-index)", - "rustc-ap-serialize 164.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-ap-rustc_cratesio_shim 174.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-ap-serialize 174.0.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "rustc-ap-serialize" -version = "164.0.0" +version = "174.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "rustc-ap-syntax" -version = "164.0.0" +version = "174.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "bitflags 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", - "rustc-ap-rustc_data_structures 164.0.0 (registry+https://github.com/rust-lang/crates.io-index)", - "rustc-ap-rustc_errors 164.0.0 (registry+https://github.com/rust-lang/crates.io-index)", - "rustc-ap-rustc_target 164.0.0 (registry+https://github.com/rust-lang/crates.io-index)", - "rustc-ap-serialize 164.0.0 (registry+https://github.com/rust-lang/crates.io-index)", - "rustc-ap-syntax_pos 164.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-ap-rustc_data_structures 174.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-ap-rustc_errors 174.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-ap-rustc_target 174.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-ap-serialize 174.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-ap-syntax_pos 174.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "scoped-tls 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "rustc-ap-syntax_pos" -version = "164.0.0" +version = "174.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "rustc-ap-arena 164.0.0 (registry+https://github.com/rust-lang/crates.io-index)", - "rustc-ap-rustc_data_structures 164.0.0 (registry+https://github.com/rust-lang/crates.io-index)", - "rustc-ap-serialize 164.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-ap-arena 174.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-ap-rustc_data_structures 174.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-ap-serialize 174.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "scoped-tls 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "unicode-width 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -563,12 +563,12 @@ dependencies = [ "itertools 0.7.8 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", - "regex 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", - "rustc-ap-rustc_target 164.0.0 (registry+https://github.com/rust-lang/crates.io-index)", - "rustc-ap-syntax 164.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "regex 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-ap-rustc_target 174.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-ap-syntax 174.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_json 1.0.20 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 1.0.22 (registry+https://github.com/rust-lang/crates.io-index)", "term 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", "toml 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "unicode-segmentation 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -615,7 +615,7 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.20" +version = "1.0.22" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "dtoa 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", @@ -625,7 +625,7 @@ dependencies = [ [[package]] name = "smallvec" -version = "0.6.1" +version = "0.6.2" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] @@ -848,16 +848,16 @@ dependencies = [ "checksum rand 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "eba5f8cb59cc50ed56be8880a5c7b496bfd9bd26394e176bc67884094145c2c5" "checksum redox_syscall 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)" = "c214e91d3ecf43e9a4e41e578973adeb14b474f2bee858742d127af75a0112b1" "checksum redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7e891cfe48e9100a70a3b6eb652fef28920c117d366339687bd5576160db0f76" -"checksum regex 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "75ecf88252dce580404a22444fc7d626c01815debba56a7f4f536772a5ff19d3" +"checksum regex 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "13c93d55961981ba9226a213b385216f83ab43bd6ac53ab16b2eeb47e337cf4e" "checksum regex-syntax 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)" = "05b06a75f5217880fc5e905952a42750bf44787e56a6c6d6852ed0992f5e1d54" -"checksum rustc-ap-arena 164.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "6f0687e373d86505f31faeaee87d2be552843a830a0a20e252e76337b9596161" -"checksum rustc-ap-rustc_cratesio_shim 164.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7ab5b83e209f3bcdb3c058a996d54b67db58eed5496bd114a781d9faa021aba7" -"checksum rustc-ap-rustc_data_structures 164.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4bb330c149e9b133d4707718a7981d65ce4eb14f2d59cb487761aa922fefb206" -"checksum rustc-ap-rustc_errors 164.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "6e19ae6a813d5cdd12b8b95cea71438bf8a5fa3505bea1e7d68d438a8ac5ae7b" -"checksum rustc-ap-rustc_target 164.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "867d4a6bc1b62d373fc6ec72632d5cbd36f3cb1f4e51282d0c7b4e771b393031" -"checksum rustc-ap-serialize 164.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e095f23598f115432ffef263201e030626f454d183cf425ef68fcca984f6594b" -"checksum rustc-ap-syntax 164.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ab8f97532dabc3713ac3e8d11a85f1a5b154486e79a0c2643d62078f0f948ce2" -"checksum rustc-ap-syntax_pos 164.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3e098adae207a4b8d470bc5e9565904cfe65dca799ba4c9efc872e7436eb5a67" +"checksum rustc-ap-arena 174.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ea3d63ae8a31549b639dc2d9bfab8cc1f36b4b0d26d3631997a96b8728125357" +"checksum rustc-ap-rustc_cratesio_shim 174.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3331eeec95b71418483fb08661829115c4b52543b54b46954f5598690b28d48a" +"checksum rustc-ap-rustc_data_structures 174.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "36ca6c4b74ca9f4a56c0bd85355969698643ac6e5b275fc321ac98e71404b2aa" +"checksum rustc-ap-rustc_errors 174.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "23e8f5cba1e335ce86f86922e0d713fef107770cdf4f830377dda12c1ec0f43c" +"checksum rustc-ap-rustc_target 174.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "0c90964f9259dd23d574ee0edc0643bcb49b8e0307090ece853670aafd5f9de5" +"checksum rustc-ap-serialize 174.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5d370a086d698a968c97bd89f361f9ec2092cab41f52068ffc6fe070d7b38983" +"checksum rustc-ap-syntax 174.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "625746e5c28b776cdaf28bd12e8c33d97241f08fbf6bb97d05577f4dbb0f8cab" +"checksum rustc-ap-syntax_pos 174.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "41943a782dae68994a1e1b75ed51f5cf5ed2c20e9451dbf993d013e7797d2ede" "checksum rustc-demangle 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "76d7ba1feafada44f2d38eed812bd2489a03c0f5abb975799251518b68848649" "checksum rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7540fc8b0c49f096ee9c961cda096467dce8084bec6bdca2fc83895fd9b28cb8" "checksum rustc-rayon 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "8c6d5a683c6ba4ed37959097e88d71c9e8e26659a3cb5be8b389078e7ad45306" @@ -868,8 +868,8 @@ dependencies = [ "checksum semver-parser 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" "checksum serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)" = "e9a2d9a9ac5120e0f768801ca2b58ad6eec929dc9d1d616c162f208869c2ce95" "checksum serde_derive 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)" = "0a90213fa7e0f5eac3f7afe2d5ff6b088af515052cc7303bd68c7e3b91a3fb79" -"checksum serde_json 1.0.20 (registry+https://github.com/rust-lang/crates.io-index)" = "fc97cccc2959f39984524026d760c08ef0dd5f0f5948c8d31797dbfae458c875" -"checksum smallvec 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)" = "03dab98ab5ded3a8b43b2c80751194608d0b2aa0f1d46cf95d1c35e192844aa7" +"checksum serde_json 1.0.22 (registry+https://github.com/rust-lang/crates.io-index)" = "84b8035cabe9b35878adec8ac5fe03d5f6bc97ff6edd7ccb96b44c1276ba390e" +"checksum smallvec 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "312a7df010092e73d6bbaf141957e868d4f30efd2bfd9bb1028ad91abec58514" "checksum stable_deref_trait 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ffbc596e092fe5f598b12ef46cc03754085ac2f4d8c739ad61c4ae266cc3b3fa" "checksum syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)" = "d3b891b9015c88c576343b9b3e41c2c11a51c219ef067b264bd9c8aa9b441dad" "checksum syn 0.13.11 (registry+https://github.com/rust-lang/crates.io-index)" = "14f9bf6292f3a61d2c716723fdb789a41bbe104168e6f496dc6497e531ea1b9b" diff --git a/Cargo.toml b/Cargo.toml index 33d70c0f0f6..1772207444f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -47,8 +47,8 @@ env_logger = "0.5" getopts = "0.2" derive-new = "0.5" cargo_metadata = "0.5.1" -rustc-ap-rustc_target = "164.0.0" -rustc-ap-syntax = "164.0.0" +rustc-ap-rustc_target = "174.0.0" +rustc-ap-syntax = "174.0.0" failure = "0.1.1" [dev-dependencies] diff --git a/src/chains.rs b/src/chains.rs index c0dafc4f0f2..52999635cf1 100644 --- a/src/chains.rs +++ b/src/chains.rs @@ -70,6 +70,7 @@ use expr::rewrite_call; use macros::convert_try_mac; use rewrite::{Rewrite, RewriteContext}; use shape::Shape; +use spanned::Spanned; use utils::{ first_line_width, last_line_extendable, last_line_width, mk_sp, trimmed_last_line_width, wrap_str, @@ -436,9 +437,9 @@ fn rewrite_chain_subexpr( match expr.node { ast::ExprKind::MethodCall(ref segment, ref expressions) => { - let types = match segment.parameters { + let types = match segment.args { Some(ref params) => match **params { - ast::PathParameters::AngleBracketed(ref data) => &data.types[..], + ast::GenericArgs::AngleBracketed(ref data) => &data.args[..], _ => &[], }, _ => &[], @@ -484,7 +485,7 @@ fn is_try(expr: &ast::Expr) -> bool { fn rewrite_method_call( method_name: ast::Ident, - types: &[ptr::P], + types: &[ast::GenericArg], args: &[ptr::P], span: Span, context: &RewriteContext, @@ -500,7 +501,7 @@ fn rewrite_method_call( let type_str = format!("::<{}>", type_list.join(", ")); - (types.last().unwrap().span.hi(), type_str) + (types.last().unwrap().span().hi(), type_str) }; let callee_str = format!(".{}{}", method_name, type_str); diff --git a/src/expr.rs b/src/expr.rs index 0c2716a2dba..b8a323f73a2 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -2014,8 +2014,8 @@ fn rewrite_assignment( pub enum RhsTactics { /// Use heuristics. Default, - /// Put the rhs on the next line if it uses multiple line. - ForceNextLine, + /// Put the rhs on the next line if it uses multiple line, without extra indentation. + ForceNextLineWithoutIndent, } // The left hand side must contain everything up to, and including, the @@ -2072,11 +2072,12 @@ fn choose_rhs( _ => { // Expression did not fit on the same line as the identifier. // Try splitting the line and see if that works better. - let new_shape = - Shape::indented(shape.indent.block_indent(context.config), context.config) - .sub_width(shape.rhs_overhead(context.config))?; + let new_shape = shape_from_rhs_tactic(context, shape, rhs_tactics)?; let new_rhs = expr.rewrite(context, new_shape); - let new_indent_str = &new_shape.indent.to_string_with_newline(context.config); + let new_indent_str = &shape + .indent + .block_indent(context.config) + .to_string_with_newline(context.config); match (orig_rhs, new_rhs) { (Some(ref orig_rhs), Some(ref new_rhs)) @@ -2098,8 +2099,22 @@ fn choose_rhs( } } +fn shape_from_rhs_tactic( + context: &RewriteContext, + shape: Shape, + rhs_tactic: RhsTactics, +) -> Option { + match rhs_tactic { + RhsTactics::ForceNextLineWithoutIndent => Some(shape.with_max_width(context.config)), + RhsTactics::Default => { + Shape::indented(shape.indent.block_indent(context.config), context.config) + .sub_width(shape.rhs_overhead(context.config)) + } + } +} + pub fn prefer_next_line(orig_rhs: &str, next_line_rhs: &str, rhs_tactics: RhsTactics) -> bool { - rhs_tactics == RhsTactics::ForceNextLine + rhs_tactics == RhsTactics::ForceNextLineWithoutIndent || !next_line_rhs.contains('\n') || count_newlines(orig_rhs) > count_newlines(next_line_rhs) + 1 } diff --git a/src/imports.rs b/src/imports.rs index 7e45bb55f0d..b8639fdd67e 100644 --- a/src/imports.rs +++ b/src/imports.rs @@ -346,7 +346,7 @@ impl UseTree { .collect(), )); } - UseTreeKind::Simple(ref rename) => { + UseTreeKind::Simple(ref rename, ..) => { let mut name = (*path_to_imported_ident(&a.prefix).name.as_str()).to_owned(); let alias = rename.and_then(|ident| { if ident == path_to_imported_ident(&a.prefix) { diff --git a/src/items.rs b/src/items.rs index 507ea09a8fe..05ac3a3fe36 100644 --- a/src/items.rs +++ b/src/items.rs @@ -36,7 +36,6 @@ use overflow; use rewrite::{Rewrite, RewriteContext}; use shape::{Indent, Shape}; use spanned::Spanned; -use types::TraitTyParamBounds; use utils::*; use vertical::rewrite_with_alignment; use visitor::FmtVisitor; @@ -971,7 +970,7 @@ pub fn format_trait(context: &RewriteContext, item: &ast::Item, offset: Indent) is_auto, unsafety, ref generics, - ref type_param_bounds, + ref generic_bounds, ref trait_items, ) = item.node { @@ -997,23 +996,22 @@ pub fn format_trait(context: &RewriteContext, item: &ast::Item, offset: Indent) result.push_str(&generics_str); // FIXME(#2055): rustfmt fails to format when there are comments between trait bounds. - if !type_param_bounds.is_empty() { + if !generic_bounds.is_empty() { let ident_hi = context .snippet_provider .span_after(item.span, &format!("{}", item.ident)); - let bound_hi = type_param_bounds.last().unwrap().span().hi(); + let bound_hi = generic_bounds.last().unwrap().span().hi(); let snippet = context.snippet(mk_sp(ident_hi, bound_hi)); if contains_comment(snippet) { return None; } - } - if !type_param_bounds.is_empty() { + result = rewrite_assign_rhs_with( context, result + ":", - &TraitTyParamBounds::new(type_param_bounds), + generic_bounds, shape, - RhsTactics::ForceNextLine, + RhsTactics::ForceNextLineWithoutIndent, )?; } @@ -1026,10 +1024,10 @@ pub fn format_trait(context: &RewriteContext, item: &ast::Item, offset: Indent) }; let where_budget = context.budget(last_line_width(&result)); - let pos_before_where = if type_param_bounds.is_empty() { + let pos_before_where = if generic_bounds.is_empty() { generics.where_clause.span.lo() } else { - type_param_bounds[type_param_bounds.len() - 1].span().hi() + generic_bounds[generic_bounds.len() - 1].span().hi() }; let option = WhereClauseOption::snuggled(&generics_str); let where_clause_str = rewrite_where_clause( @@ -1134,7 +1132,7 @@ pub fn format_trait_alias( context: &RewriteContext, ident: ast::Ident, generics: &ast::Generics, - ty_param_bounds: &ast::TyParamBounds, + generic_bounds: &ast::GenericBounds, shape: Shape, ) -> Option { let alias = ident.name.as_str(); @@ -1143,7 +1141,7 @@ pub fn format_trait_alias( let generics_str = rewrite_generics(context, &alias, generics, g_shape, generics.span)?; let lhs = format!("trait {} =", generics_str); // 1 = ";" - rewrite_assign_rhs(context, lhs, ty_param_bounds, shape.sub_width(1)?).map(|s| s + ";") + rewrite_assign_rhs(context, lhs, generic_bounds, shape.sub_width(1)?).map(|s| s + ";") } fn format_unit_struct(context: &RewriteContext, p: &StructParts, offset: Indent) -> Option { @@ -1671,13 +1669,13 @@ fn rewrite_static( pub fn rewrite_associated_type( ident: ast::Ident, ty_opt: Option<&ptr::P>, - ty_param_bounds_opt: Option<&ast::TyParamBounds>, + generic_bounds_opt: Option<&ast::GenericBounds>, context: &RewriteContext, indent: Indent, ) -> Option { let prefix = format!("type {}", ident); - let type_bounds_str = if let Some(bounds) = ty_param_bounds_opt { + let type_bounds_str = if let Some(bounds) = generic_bounds_opt { if bounds.is_empty() { String::new() } else { @@ -1703,11 +1701,11 @@ pub fn rewrite_associated_impl_type( ident: ast::Ident, defaultness: ast::Defaultness, ty_opt: Option<&ptr::P>, - ty_param_bounds_opt: Option<&ast::TyParamBounds>, + generic_bounds_opt: Option<&ast::GenericBounds>, context: &RewriteContext, indent: Indent, ) -> Option { - let result = rewrite_associated_type(ident, ty_opt, ty_param_bounds_opt, context, indent)?; + let result = rewrite_associated_type(ident, ty_opt, generic_bounds_opt, context, indent)?; match defaultness { ast::Defaultness::Default => Some(format!("default {}", result)), @@ -2698,7 +2696,7 @@ fn format_generics( } // If the generics are not parameterized then generics.span.hi() == 0, // so we use span.lo(), which is the position after `struct Foo`. - let span_end_before_where = if generics.is_parameterized() { + let span_end_before_where = if !generics.params.is_empty() { generics.span.hi() } else { span.lo() @@ -2804,15 +2802,6 @@ impl Rewrite for ast::ForeignItem { } } -impl Rewrite for ast::GenericParam { - fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option { - match *self { - ast::GenericParam::Lifetime(ref lifetime_def) => lifetime_def.rewrite(context, shape), - ast::GenericParam::Type(ref ty) => ty.rewrite(context, shape), - } - } -} - /// Rewrite an inline mod. pub fn rewrite_mod(item: &ast::Item) -> String { let mut result = String::with_capacity(32); diff --git a/src/spanned.rs b/src/spanned.rs index 237624ab8fe..c2886fcdd8f 100644 --- a/src/spanned.rs +++ b/src/spanned.rs @@ -14,6 +14,8 @@ use syntax::codemap::Span; use macros::MacroArg; use utils::{mk_sp, outer_attributes}; +use std::cmp::max; + /// Spanned returns a span including attributes, if available. pub trait Spanned { fn span(&self) -> Span; @@ -110,10 +112,25 @@ impl Spanned for ast::Arg { impl Spanned for ast::GenericParam { fn span(&self) -> Span { - match *self { - ast::GenericParam::Lifetime(ref lifetime_def) => lifetime_def.span(), - ast::GenericParam::Type(ref ty) => ty.span(), - } + let lo = if self.attrs.is_empty() { + self.ident.span.lo() + } else { + self.attrs[0].span.lo() + }; + let hi = if self.bounds.is_empty() { + self.ident.span.hi() + } else { + self.bounds.last().unwrap().span().hi() + }; + let ty_hi = if let ast::GenericParamKind::Type { + default: Some(ref ty), + } = self.kind + { + ty.span().hi() + } else { + hi + }; + mk_sp(lo, max(hi, ty_hi)) } } @@ -142,45 +159,24 @@ impl Spanned for ast::FunctionRetTy { } } -impl Spanned for ast::TyParam { +impl Spanned for ast::GenericArg { fn span(&self) -> Span { - // Note that ty.span is the span for ty.ident, not the whole item. - let lo = if self.attrs.is_empty() { - self.ident.span.lo() - } else { - self.attrs[0].span.lo() - }; - if let Some(ref def) = self.default { - return mk_sp(lo, def.span.hi()); - } - if self.bounds.is_empty() { - return mk_sp(lo, self.ident.span.hi()); + match *self { + ast::GenericArg::Lifetime(ref lt) => lt.ident.span, + ast::GenericArg::Type(ref ty) => ty.span(), } - let hi = self.bounds[self.bounds.len() - 1].span().hi(); - mk_sp(lo, hi) } } -impl Spanned for ast::TyParamBound { +impl Spanned for ast::GenericBound { fn span(&self) -> Span { match *self { - ast::TyParamBound::TraitTyParamBound(ref ptr, _) => ptr.span, - ast::TyParamBound::RegionTyParamBound(ref l) => l.ident.span, + ast::GenericBound::Trait(ref ptr, _) => ptr.span, + ast::GenericBound::Outlives(ref l) => l.ident.span, } } } -impl Spanned for ast::LifetimeDef { - fn span(&self) -> Span { - let hi = if self.bounds.is_empty() { - self.lifetime.ident.span.hi() - } else { - self.bounds[self.bounds.len() - 1].ident.span.hi() - }; - mk_sp(self.lifetime.ident.span.lo(), hi) - } -} - impl Spanned for MacroArg { fn span(&self) -> Span { match *self { diff --git a/src/types.rs b/src/types.rs index 6e550594680..20c6cebeefd 100644 --- a/src/types.rs +++ b/src/types.rs @@ -148,6 +148,15 @@ enum SegmentParam<'a> { Binding(&'a ast::TypeBinding), } +impl<'a> SegmentParam<'a> { + fn from_generic_arg(arg: &ast::GenericArg) -> SegmentParam { + match arg { + ast::GenericArg::Lifetime(ref lt) => SegmentParam::LifeTime(lt), + ast::GenericArg::Type(ref ty) => SegmentParam::Type(ty), + } + } +} + impl<'a> Spanned for SegmentParam<'a> { fn span(&self) -> Span { match *self { @@ -220,18 +229,15 @@ fn rewrite_segment( shape.shrink_left(ident_len)? }; - if let Some(ref params) = segment.parameters { - match **params { - ast::PathParameters::AngleBracketed(ref data) - if !data.lifetimes.is_empty() - || !data.types.is_empty() - || !data.bindings.is_empty() => + if let Some(ref args) = segment.args { + match **args { + ast::GenericArgs::AngleBracketed(ref data) + if !data.args.is_empty() || !data.bindings.is_empty() => { let param_list = data - .lifetimes + .args .iter() - .map(SegmentParam::LifeTime) - .chain(data.types.iter().map(|x| SegmentParam::Type(&*x))) + .map(SegmentParam::from_generic_arg) .chain(data.bindings.iter().map(|x| SegmentParam::Binding(&*x))) .collect::>(); @@ -257,7 +263,7 @@ fn rewrite_segment( result.push_str(&generics_str) } - ast::PathParameters::Parenthesized(ref data) => { + ast::GenericArgs::Parenthesized(ref data) => { let output = match data.output { Some(ref ty) => FunctionRetTy::Ty(ty.clone()), None => FunctionRetTy::Default(codemap::DUMMY_SP), @@ -457,15 +463,18 @@ impl Rewrite for ast::WherePredicate { } } -impl Rewrite for ast::LifetimeDef { +impl Rewrite for ast::GenericArg { fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option { - rewrite_bounded_lifetime(&self.lifetime, &self.bounds, context, shape) + match *self { + ast::GenericArg::Lifetime(ref lt) => lt.rewrite(context, shape), + ast::GenericArg::Type(ref ty) => ty.rewrite(context, shape), + } } } fn rewrite_bounded_lifetime( lt: &ast::Lifetime, - bounds: &[ast::Lifetime], + bounds: &[ast::GenericBound], context: &RewriteContext, shape: Shape, ) -> Option { @@ -486,45 +495,36 @@ fn rewrite_bounded_lifetime( } } -impl Rewrite for ast::TyParamBound { - fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option { - match *self { - ast::TyParamBound::TraitTyParamBound(ref tref, ast::TraitBoundModifier::None) => { - tref.rewrite(context, shape) - } - ast::TyParamBound::TraitTyParamBound(ref tref, ast::TraitBoundModifier::Maybe) => Some( - format!("?{}", tref.rewrite(context, shape.offset_left(1)?)?), - ), - ast::TyParamBound::RegionTyParamBound(ref l) => l.rewrite(context, shape), - } - } -} - impl Rewrite for ast::Lifetime { fn rewrite(&self, _: &RewriteContext, _: Shape) -> Option { Some(self.ident.to_string()) } } -/// A simple wrapper over type param bounds in trait. -#[derive(new)] -pub struct TraitTyParamBounds<'a> { - inner: &'a ast::TyParamBounds, -} - -impl<'a> Rewrite for TraitTyParamBounds<'a> { +impl Rewrite for ast::GenericBound { fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option { - join_bounds(context, shape, self.inner, false) + match *self { + ast::GenericBound::Trait(ref poly_trait_ref, trait_bound_modifier) => { + match trait_bound_modifier { + ast::TraitBoundModifier::None => poly_trait_ref.rewrite(context, shape), + ast::TraitBoundModifier::Maybe => { + let rw = poly_trait_ref.rewrite(context, shape.offset_left(1)?)?; + Some(format!("?{}", rw)) + } + } + } + ast::GenericBound::Outlives(ref lifetime) => lifetime.rewrite(context, shape), + } } } -impl Rewrite for ast::TyParamBounds { +impl Rewrite for ast::GenericBounds { fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option { join_bounds(context, shape, self, true) } } -impl Rewrite for ast::TyParam { +impl Rewrite for ast::GenericParam { fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option { let mut result = String::with_capacity(128); // FIXME: If there are more than one attributes, this will force multiline. @@ -537,7 +537,10 @@ impl Rewrite for ast::TyParam { result.push_str(type_bound_colon(context)); result.push_str(&self.bounds.rewrite(context, shape)?) } - if let Some(ref def) = self.default { + if let ast::GenericParamKind::Type { + default: Some(ref def), + } = self.kind + { let eq_str = match context.config.type_punctuation_density() { TypeDensity::Compressed => "=", TypeDensity::Wide => " = ", @@ -786,7 +789,10 @@ fn rewrite_lifetime_param( ) -> Option { let result = generic_params .iter() - .filter(|p| p.is_lifetime_param()) + .filter(|p| match p.kind { + ast::GenericParamKind::Lifetime => true, + _ => false, + }) .map(|lt| lt.rewrite(context, shape)) .collect::>>()? .join(", "); diff --git a/src/visitor.rs b/src/visitor.rs index 7d676770421..c58122025de 100644 --- a/src/visitor.rs +++ b/src/visitor.rs @@ -354,13 +354,13 @@ impl<'b, 'a: 'b> FmtVisitor<'a> { let rw = format_trait(&self.get_context(), item, self.block_indent); self.push_rewrite(item.span, rw); } - ast::ItemKind::TraitAlias(ref generics, ref ty_param_bounds) => { + ast::ItemKind::TraitAlias(ref generics, ref generic_bounds) => { let shape = Shape::indented(self.block_indent, self.config); let rw = format_trait_alias( &self.get_context(), item.ident, generics, - ty_param_bounds, + generic_bounds, shape, ); self.push_rewrite(item.span, rw); @@ -461,11 +461,11 @@ impl<'b, 'a: 'b> FmtVisitor<'a> { Some(&inner_attrs), ); } - ast::TraitItemKind::Type(ref type_param_bounds, ref type_default) => { + ast::TraitItemKind::Type(ref generic_bounds, ref type_default) => { let rewrite = rewrite_associated_type( ti.ident, type_default.as_ref(), - Some(type_param_bounds), + Some(generic_bounds), &self.get_context(), self.block_indent, ); diff --git a/tests/source/assignment.rs b/tests/source/assignment.rs index d5860532086..71de325566d 100644 --- a/tests/source/assignment.rs +++ b/tests/source/assignment.rs @@ -13,6 +13,10 @@ fn main() { DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD; single_line_fit = 5;single_lit_fit >>= 10; + + + // #2791 + let x = 2;;;; } fn break_meee() { diff --git a/tests/target/assignment.rs b/tests/target/assignment.rs index 4e8689ed23e..a33f3cb40ab 100644 --- a/tests/target/assignment.rs +++ b/tests/target/assignment.rs @@ -15,6 +15,9 @@ fn main() { single_line_fit = 5; single_lit_fit >>= 10; + + // #2791 + let x = 2;;;; } fn break_meee() {