Skip to content

prepare v1.4.26 release #4525

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

Merged
merged 6 commits into from
Nov 14, 2020
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
6 changes: 5 additions & 1 deletion .github/workflows/integration.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
name: integration
on: [push, pull_request]
on:
push:
branches:
- master
pull_request:

jobs:
integration-tests:
Expand Down
6 changes: 5 additions & 1 deletion .github/workflows/linux.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
name: linux
on: [push, pull_request]
on:
push:
branches:
- master
pull_request:

jobs:
test:
Expand Down
6 changes: 5 additions & 1 deletion .github/workflows/mac.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
name: mac
on: [push, pull_request]
on:
push:
branches:
- master
pull_request:

jobs:
test:
Expand Down
6 changes: 5 additions & 1 deletion .github/workflows/windows.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
name: windows
on: [push, pull_request]
on:
push:
branches:
- master
pull_request:

jobs:
test:
Expand Down
51 changes: 50 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,55 @@

## [Unreleased]

## [1.4.26] 2020-11-14

### Changed

- Original comment indentation for trailing comments within an `if` is now taken into account when determining the indentation level to use for the trailing comment in formatted code. This does not modify any existing code formatted with rustfmt; it simply gives the programmer discretion to specify whether the comment is associated to the `else` block, or if the trailing comment is just a member of the `if` block. ([#1575](https://github.com/rust-lang/rustfmt/issues/1575), [#4120](https://github.com/rust-lang/rustfmt/issues/4120), [#4506](https://github.com/rust-lang/rustfmt/issues/4506))

In this example the `// else comment` refers to the `else`:
```rust
// if comment
if cond {
"if"
// else comment
} else {
"else"
}
```

Whereas in this case the `// continue` comments are members of their respective blocks and do not refer to the `else` below.
```rust
if toks.eat_token(Token::Word("modify"))? && toks.eat_token(Token::Word("labels"))? {
if toks.eat_token(Token::Colon)? {
// ate the token
} else if toks.eat_token(Token::Word("to"))? {
// optionally eat the colon after to, e.g.:
// @rustbot modify labels to: -S-waiting-on-author, +S-waiting-on-review
toks.eat_token(Token::Colon)?;
} else {
// It's okay if there's no to or colon, we can just eat labels
// afterwards.
}
1 + 2;
// continue
} else if toks.eat_token(Token::Word("label"))? {
// continue
} else {
return Ok(None);
}
```

### Fixed
- Formatting of empty blocks with attributes which only contained comments is no longer butchered.([#4475](https://github.com/rust-lang/rustfmt/issues/4475), [#4467](https://github.com/rust-lang/rustfmt/issues/4467), [#4452](https://github.com/rust-lang/rustfmt/issues/4452#issuecomment-705886282), [#4522](https://github.com/rust-lang/rustfmt/issues/4522))
- Indentation of trailing comments in non-empty extern blocks is now correct. ([#4120](https://github.com/rust-lang/rustfmt/issues/4120#issuecomment-696491872))

### Install/Download Options
- **crates.io package** - *pending*
- **rustup (nightly)** - *pending*
- **GitHub Release Binaries** - [Release v1.4.26](https://github.com/rust-lang/rustfmt/releases/tag/v1.4.26)
- **Build from source** - [Tag v1.4.26](https://github.com/rust-lang/rustfmt/tree/v1.4.26), see instructions for how to [install rustfmt from source][install-from-source]

## [1.4.25] 2020-11-10

### Changed
Expand All @@ -10,7 +59,7 @@

### Install/Download Options
- **crates.io package** - *pending*
- **rustup (nightly)** - *pending*
- **rustup (nightly)** - Starting in `2020-11-14`
- **GitHub Release Binaries** - [Release v1.4.25](https://github.com/rust-lang/rustfmt/releases/tag/v1.4.25)
- **Build from source** - [Tag v1.4.25](https://github.com/rust-lang/rustfmt/tree/v1.4.25), see instructions for how to [install rustfmt from source][install-from-source]

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.25"
version = "1.4.26"
authors = ["Nicholas Cameron <[email protected]>", "The Rustfmt developers"]
description = "Tool to find and fix Rust formatting issues"
repository = "https://github.com/rust-lang/rustfmt"
Expand Down
7 changes: 6 additions & 1 deletion src/closures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ fn veto_block(e: &ast::Expr) -> bool {
}

// Rewrite closure with a single expression wrapping its body with block.
// || { #[attr] foo() } -> Block { #[attr] foo() }
fn rewrite_closure_with_block(
body: &ast::Expr,
prefix: &str,
Expand All @@ -154,8 +155,12 @@ fn rewrite_closure_with_block(
}],
id: ast::NodeId::root(),
rules: ast::BlockCheckMode::Default,
span: body.span,
tokens: None,
span: body
.attrs
.first()
.map(|attr| attr.span.to(body.span))
.unwrap_or(body.span),
};
let block = crate::expr::rewrite_block_with_visitor(
context,
Expand Down
12 changes: 1 addition & 11 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -528,17 +528,7 @@ pub(crate) fn rewrite_block_with_visitor(
let open_pos = snippet.find_uncommented("{")?;
visitor.last_pos = block.span.lo() + BytePos(open_pos as u32)
}
(ast::BlockCheckMode::Default, None) => {
visitor.last_pos = block.span.lo();
if let Some(attrs) = attrs {
if let Some(first) = attrs.first() {
let first_lo_span = first.span.lo();
if first_lo_span < visitor.last_pos {
visitor.last_pos = first_lo_span;
}
}
}
}
(ast::BlockCheckMode::Default, None) => visitor.last_pos = block.span.lo(),
}

let inner_attrs = attrs.map(inner_attributes);
Expand Down
26 changes: 17 additions & 9 deletions src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,14 @@ enum BodyElement<'a> {
ForeignItem(&'a ast::ForeignItem),
}

impl BodyElement<'_> {
pub(crate) fn span(&self) -> Span {
match self {
BodyElement::ForeignItem(fi) => fi.span(),
}
}
}

/// Represents a fn's signature.
pub(crate) struct FnSig<'a> {
decl: &'a ast::FnDecl,
Expand Down Expand Up @@ -268,19 +276,19 @@ impl<'a> FmtVisitor<'a> {
self.last_pos = item.span.lo() + BytePos(brace_pos as u32 + 1);
self.block_indent = self.block_indent.block_indent(self.config);

if item.body.is_empty() {
self.format_missing_no_indent(item.span.hi() - BytePos(1));
self.block_indent = self.block_indent.block_unindent(self.config);
let indent_str = self.block_indent.to_string(self.config);
self.push_str(&indent_str);
} else {
if !item.body.is_empty() {
// Advance to first item (statement or inner attribute)
// within the block.
self.last_pos = item.body[0].span().lo();
for item in &item.body {
self.format_body_element(item);
}

self.block_indent = self.block_indent.block_unindent(self.config);
self.format_missing_with_indent(item.span.hi() - BytePos(1));
}

self.format_missing_no_indent(item.span.hi() - BytePos(1));
self.block_indent = self.block_indent.block_unindent(self.config);
let indent_str = self.block_indent.to_string(self.config);
self.push_str(&indent_str);
}

self.push_str("}");
Expand Down
15 changes: 12 additions & 3 deletions src/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use rustc_ast::{ast, attr::HasAttrs, token::DelimToken, visit};
use rustc_span::{symbol, BytePos, Pos, Span, DUMMY_SP};

use crate::attr::*;
use crate::comment::{rewrite_comment, CodeCharKind, CommentCodeSlices};
use crate::comment::{contains_comment, rewrite_comment, CodeCharKind, CommentCodeSlices};
use crate::config::Version;
use crate::config::{BraceStyle, Config};
use crate::coverage::transform_missing_snippet;
Expand Down Expand Up @@ -261,14 +261,23 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
trimmed.is_empty() || trimmed.chars().all(|c| c == ';')
};

for (kind, offset, sub_slice) in CommentCodeSlices::new(self.snippet(span)) {
let comment_snippet = self.snippet(span);

let align_to_right = if unindent_comment && contains_comment(&comment_snippet) {
let first_lines = comment_snippet.splitn(2, '/').next().unwrap_or("");
last_line_width(first_lines) > last_line_width(&comment_snippet)
} else {
false
};

for (kind, offset, sub_slice) in CommentCodeSlices::new(comment_snippet) {
let sub_slice = transform_missing_snippet(config, sub_slice);

debug!("close_block: {:?} {:?} {:?}", kind, offset, sub_slice);

match kind {
CodeCharKind::Comment => {
if !unindented && unindent_comment {
if !unindented && unindent_comment && !align_to_right {
unindented = true;
self.block_indent = self.block_indent.block_unindent(config);
}
Expand Down
85 changes: 85 additions & 0 deletions tests/source/issue-4120.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
fn main() {
let x = if true {
1
// In if
} else {
0
// In else
};

let x = if true {
1
/* In if */
} else {
0
/* In else */
};

let z = if true {
if true {
1

// In if level 2
} else {
2
}
} else {
3
};

let a = if true {
1
// In if
} else {
0
// In else
};

let a = if true {
1

// In if
} else {
0
// In else
};

let b = if true {
1

// In if
} else {
0
// In else
};

let c = if true {
1

// In if
} else {
0
// In else
};
for i in 0..2 {
println!("Something");
// In for
}

for i in 0..2 {
println!("Something");
/* In for */
}

extern "C" {
fn first();

// In foreign mod
}

extern "C" {
fn first();

/* In foreign mod */
}
}
4 changes: 4 additions & 0 deletions tests/source/issue-4382.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pub const NAME_MAX: usize = {
#[cfg(target_os = "linux")] { 1024 }
#[cfg(target_os = "freebsd")] { 255 }
};
27 changes: 27 additions & 0 deletions tests/source/issue_4475.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
fn main() {
#[cfg(debug_assertions)]
{ println!("DEBUG"); }
}

fn main() {
#[cfg(feature = "foo")]
{
/*
let foo = 0
*/
}
}

fn main() {
#[cfg(feature = "foo")]
{ /* let foo = 0; */ }
}

fn main() {
#[foo]
#[bar]
#[baz]
{
// let foo = 0;
}
}
Loading