Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prepare v1.4.33 release #4674

Merged
merged 6 commits into from
Jan 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,25 @@

## [Unreleased]

## [1.4.33] 2021-??-??
## [1.4.33] 2021-01-27

### Changed
- `merge_imports` configuration has been deprecated in favor of the new `imports_granularity` option. Any existing usage of `merge_imports` will be automatically mapped to the corresponding value on `imports_granularity` with a warning message printed to encourage users to update their config files.

### Added
- New `imports_granularity` option has been added which succeeds `merge_imports`. This new option supports several additional variants which allow users to merge imports at different levels (crate or module), and even flatten imports to have a single use statement per item. ([PR #4634](https://github.com/rust-lang/rustfmt/pull/4634), [PR #4639](https://github.com/rust-lang/rustfmt/pull/4639))

See the section on the configuration site for more information
https://rust-lang.github.io/rustfmt/?version=v1.4.33&search=#imports_granularity

### Fixed
- Fix erroneous removal of `const` keyword on const trait impl ([#4084](https://github.com/rust-lang/rustfmt/issues/4084))
- Fix incorrect span usage wit const generics in supertraits ([#4204](https://github.com/rust-lang/rustfmt/issues/4204))
- Use correct span for const generic params ([#4263](https://github.com/rust-lang/rustfmt/issues/4263))
- Correct span on const generics to include type bounds ([#4310](https://github.com/rust-lang/rustfmt/issues/4310))
- Idempotence issue on blocks containing only empty statements ([#4627](https://github.com/rust-lang/rustfmt/issues/4627) and [#3868](https://github.com/rust-lang/rustfmt/issues/3868))
- Fix issue with semicolon placement on required functions that have a trailing comment that ends in a line-style comment before the semicolon ([#4646](https://github.com/rust-lang/rustfmt/issues/4646))
- Avoid shared interned cfg_if symbol since rustfmt can re-initialize the rustc_ast globals on multiple inputs ([#4656](https://github.com/rust-lang/rustfmt/issues/4656))

### Install/Download Options
- **crates.io package** - *pending*
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]

name = "rustfmt-nightly"
version = "1.4.32"
version = "1.4.33"
authors = ["Nicholas Cameron <ncameron@mozilla.com>", "The Rustfmt developers"]
description = "Tool to find and fix Rust formatting issues"
repository = "https://github.com/rust-lang/rustfmt"
Expand Down
17 changes: 16 additions & 1 deletion Configurations.md
Original file line number Diff line number Diff line change
Expand Up @@ -1620,7 +1620,7 @@ pub enum Foo {}
How imports should be grouped into `use` statements. Imports will be merged or split to the configured level of granularity.

- **Default value**: `Preserve`
- **Possible values**: `Preserve`, `Crate`, `Module`
- **Possible values**: `Preserve`, `Crate`, `Module`, `Item`
- **Stable**: No

#### `Preserve` (default):
Expand Down Expand Up @@ -1659,6 +1659,21 @@ use foo::{a, b, c};
use qux::{h, i};
```

#### `Item`:

Flatten imports so that each has its own `use` statement.

```rust
use foo::a;
use foo::b;
use foo::b::f;
use foo::b::g;
use foo::c;
use foo::d::e;
use qux::h;
use qux::i;
```

## `merge_imports`

This option is deprecated. Use `imports_granularity = "Crate"` instead.
Expand Down
2 changes: 2 additions & 0 deletions src/config/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ pub enum ImportGranularity {
Crate,
/// Use one `use` statement per module.
Module,
/// Use one `use` statement per imported item.
Item,
}

#[config_type]
Expand Down
11 changes: 9 additions & 2 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ fn rewrite_empty_block(
prefix: &str,
shape: Shape,
) -> Option<String> {
if !block.stmts.is_empty() {
if block_has_statements(&block) {
return None;
}

Expand Down Expand Up @@ -1173,14 +1173,21 @@ pub(crate) fn is_simple_block_stmt(
&& attrs.map_or(true, |a| a.is_empty())
}

fn block_has_statements(block: &ast::Block) -> bool {
block
.stmts
.iter()
.any(|stmt| !matches!(stmt.kind, ast::StmtKind::Empty))
}

/// Checks whether a block contains no statements, expressions, comments, or
/// inner attributes.
pub(crate) fn is_empty_block(
context: &RewriteContext<'_>,
block: &ast::Block,
attrs: Option<&[ast::Attribute]>,
) -> bool {
block.stmts.is_empty()
!block_has_statements(&block)
&& !block_contains_comment(context, block)
&& attrs.map_or(true, |a| inner_attributes(a).is_empty())
}
Expand Down
37 changes: 37 additions & 0 deletions src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,24 @@ pub(crate) fn merge_use_trees(use_trees: Vec<UseTree>, merge_by: SharedPrefix) -
result
}

pub(crate) fn flatten_use_trees(use_trees: Vec<UseTree>) -> Vec<UseTree> {
use_trees
.into_iter()
.flat_map(UseTree::flatten)
.map(|mut tree| {
// If a path ends in `::self`, rewrite it to `::{self}`.
if let Some(UseSegment::Slf(..)) = tree.path.last() {
let self_segment = tree.path.pop().unwrap();
tree.path.push(UseSegment::List(vec![UseTree::from_path(
vec![self_segment],
DUMMY_SP,
)]));
}
tree
})
.collect()
}

impl fmt::Debug for UseTree {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(self, f)
Expand Down Expand Up @@ -1084,6 +1102,25 @@ mod test {
);
}

#[test]
fn test_flatten_use_trees() {
assert_eq!(
flatten_use_trees(parse_use_trees!["foo::{a::{b, c}, d::e}"]),
parse_use_trees!["foo::a::b", "foo::a::c", "foo::d::e"]
);

assert_eq!(
flatten_use_trees(parse_use_trees!["foo::{self, a, b::{c, d}, e::*}"]),
parse_use_trees![
"foo::{self}",
"foo::a",
"foo::b::c",
"foo::b::d",
"foo::e::*"
]
);
}

#[test]
fn test_use_tree_flatten() {
assert_eq!(
Expand Down
18 changes: 12 additions & 6 deletions src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ impl<'a> FmtVisitor<'a> {
let context = self.get_context();

let mut fn_brace_style = newline_for_brace(self.config, &fn_sig.generics.where_clause);
let (result, force_newline_brace) =
let (result, _, force_newline_brace) =
rewrite_fn_base(&context, indent, ident, fn_sig, span, fn_brace_style)?;

// 2 = ` {`
Expand All @@ -328,7 +328,7 @@ impl<'a> FmtVisitor<'a> {
let span = mk_sp(span.lo(), span.hi() - BytePos(1));
let context = self.get_context();

let (mut result, _) = rewrite_fn_base(
let (mut result, ends_with_comment, _) = rewrite_fn_base(
&context,
indent,
ident,
Expand All @@ -337,6 +337,11 @@ impl<'a> FmtVisitor<'a> {
FnBraceStyle::None,
)?;

// If `result` ends with a comment, then remember to add a newline
if ends_with_comment {
result.push_str(&indent.to_string_with_newline(context.config));
}

// Re-attach semicolon
result.push(';');

Expand Down Expand Up @@ -2142,7 +2147,7 @@ fn rewrite_fn_base(
fn_sig: &FnSig<'_>,
span: Span,
fn_brace_style: FnBraceStyle,
) -> Option<(String, bool)> {
) -> Option<(String, bool, bool)> {
let mut force_new_line_for_brace = false;

let where_clause = &fn_sig.generics.where_clause;
Expand Down Expand Up @@ -2450,10 +2455,11 @@ fn rewrite_fn_base(

result.push_str(&where_clause_str);

force_new_line_for_brace |= last_line_contains_single_line_comment(&result);
let ends_with_comment = last_line_contains_single_line_comment(&result);
force_new_line_for_brace |= ends_with_comment;
force_new_line_for_brace |=
is_params_multi_lined && context.config.where_single_line() && !where_clause_str.is_empty();
Some((result, force_new_line_for_brace))
Some((result, ends_with_comment, force_new_line_for_brace))
}

/// Kind of spaces to put before `where`.
Expand Down Expand Up @@ -3137,7 +3143,7 @@ impl Rewrite for ast::ForeignItem {
span,
FnBraceStyle::None,
)
.map(|(s, _)| format!("{};", s)),
.map(|(s, _, _)| format!("{};", s)),
ast::ForeignItemKind::Static(ref ty, mutability, _) => {
// FIXME(#21): we're dropping potential comments in between the
// function kw here.
Expand Down
15 changes: 7 additions & 8 deletions src/reorder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use rustc_ast::ast;
use rustc_span::{symbol::sym, Span};

use crate::config::{Config, GroupImportsTactic, ImportGranularity};
use crate::imports::{merge_use_trees, SharedPrefix, UseSegment, UseTree};
use crate::imports::{flatten_use_trees, merge_use_trees, SharedPrefix, UseSegment, UseTree};
use crate::items::{is_mod_decl, rewrite_extern_crate, rewrite_mod};
use crate::lists::{itemize_list, write_list, ListFormatting, ListItem};
use crate::rewrite::RewriteContext;
Expand Down Expand Up @@ -107,15 +107,14 @@ fn rewrite_reorderable_or_regroupable_items(
for (item, list_item) in normalized_items.iter_mut().zip(list_items) {
item.list_item = Some(list_item.clone());
}
match context.config.imports_granularity() {
ImportGranularity::Crate => {
normalized_items = merge_use_trees(normalized_items, SharedPrefix::Crate)
}
normalized_items = match context.config.imports_granularity() {
ImportGranularity::Crate => merge_use_trees(normalized_items, SharedPrefix::Crate),
ImportGranularity::Module => {
normalized_items = merge_use_trees(normalized_items, SharedPrefix::Module)
merge_use_trees(normalized_items, SharedPrefix::Module)
}
ImportGranularity::Preserve => {}
}
ImportGranularity::Item => flatten_use_trees(normalized_items),
ImportGranularity::Preserve => normalized_items,
};

let mut regrouped_items = match context.config.group_imports() {
GroupImportsTactic::Preserve => vec![normalized_items],
Expand Down
6 changes: 6 additions & 0 deletions tests/source/imports_granularity_item.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// rustfmt-imports_granularity: Item

use a::{b, c, d};
use a::{f::g, h::{i, j}};
use a::{l::{self, m, n::o, p::*}};
use a::q::{self};
20 changes: 20 additions & 0 deletions tests/source/issue-4646.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
trait Foo {
fn bar(&self)
// where
// Self: Bar
;
}

trait Foo {
fn bar(&self)
// where
// Self: Bar
;
}

trait Foo {
fn bar(&self)
// where
// Self: Bar
;
}
13 changes: 13 additions & 0 deletions tests/source/issue_3868.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
fn foo() {
;
}

fn bar() {
for _ in 0..1 {
;
}
}

fn baz() {
();
}
13 changes: 13 additions & 0 deletions tests/target/imports_granularity_item.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// rustfmt-imports_granularity: Item

use a::b;
use a::c;
use a::d;
use a::f::g;
use a::h::i;
use a::h::j;
use a::l::m;
use a::l::n::o;
use a::l::p::*;
use a::l::{self};
use a::q::{self};
20 changes: 20 additions & 0 deletions tests/target/issue-4646.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
trait Foo {
fn bar(&self)
// where
// Self: Bar
;
}

trait Foo {
fn bar(&self)
// where
// Self: Bar
;
}

trait Foo {
fn bar(&self)
// where
// Self: Bar
;
}
9 changes: 9 additions & 0 deletions tests/target/issue_3868.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
fn foo() {}

fn bar() {
for _ in 0..1 {}
}

fn baz() {
();
}