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.25 release #4521

Merged
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
28 changes: 28 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@

## [Unreleased]

## [1.4.25] 2020-11-10

### Changed

- Semicolons are no longer automatically inserted on trailing expressions in macro definition arms ([#4507](https://github.com/rust-lang/rustfmt/pull/4507)). This gives the programmer control and discretion over whether there should be semicolons in these scenarios so that potential expansion issues can be avoided.

### Install/Download Options
- **crates.io package** - *pending*
- **rustup (nightly)** - *pending*
- **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]

## [1.4.24] 2020-11-05

### Changed
Expand All @@ -11,6 +23,12 @@
### Fixed
- Remove useless `deprecated` attribute on a trait impl block in the rustfmt lib, as these now trigger errors ([rust-lang/rust/#78626](https://github.com/rust-lang/rust/pull/78626))

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

## [1.4.23] 2020-10-30

### Changed
Expand All @@ -28,6 +46,13 @@
- Preserve comments in empty statements [#4018](https://github.com/rust-lang/rustfmt/issues/4018))
- Indentation on skipped code [#4398](https://github.com/rust-lang/rustfmt/issues/4398))

### Install/Download Options
- **crates.io package** - *pending*
- **rustup (nightly)** - n/a (superseded by [v1.4.24](#1424-2020-11-05))
- **GitHub Release Binaries** - [Release v1.4.23](https://github.com/rust-lang/rustfmt/releases/tag/v1.4.23)
- **Build from source** - [Tag v1.4.23](https://github.com/rust-lang/rustfmt/tree/v1.4.23), see instructions for how to [install rustfmt from source][install-from-source]



## [1.4.22] 2020-10-04

Expand Down Expand Up @@ -919,3 +944,6 @@ from formatting an attribute #3665
- Handle tabs properly inside macro with braces (#1918).
- Fix a typo in `compute_budgets_for_args()` (#1924).
- Recover comment between keyword (`impl` and `trait`) and `{` which used to get removed (#1925).


[install-from-source]: https://github.com/rust-lang/rustfmt#installing-from-source
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.24"
version = "1.4.25"
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
2 changes: 1 addition & 1 deletion src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ impl<'a> CommentRewrite<'a> {
config.set().wrap_comments(false);
if config.format_code_in_doc_comments() {
if let Some(s) =
crate::format_code_block(&self.code_block_buffer, &config)
crate::format_code_block(&self.code_block_buffer, &config, false)
{
trim_custom_comment_prefix(&s.snippet)
} else {
Expand Down
20 changes: 15 additions & 5 deletions src/formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ pub(crate) type SourceFile = Vec<FileRecord>;
pub(crate) type FileRecord = (FileName, String);

impl<'b, T: Write + 'b> Session<'b, T> {
pub(crate) fn format_input_inner(&mut self, input: Input) -> Result<FormatReport, ErrorKind> {
pub(crate) fn format_input_inner(
&mut self,
input: Input,
is_macro_def: bool,
) -> Result<FormatReport, ErrorKind> {
if !self.config.version_meets_requirement() {
return Err(ErrorKind::VersionMismatch);
}
Expand All @@ -42,7 +46,7 @@ impl<'b, T: Write + 'b> Session<'b, T> {
}

let config = &self.config.clone();
let format_result = format_project(input, config, self);
let format_result = format_project(input, config, self, is_macro_def);

format_result.map(|report| {
self.errors.add(&report.internal.borrow().1);
Expand All @@ -57,6 +61,7 @@ fn format_project<T: FormatHandler>(
input: Input,
config: &Config,
handler: &mut T,
is_macro_def: bool,
) -> Result<FormatReport, ErrorKind> {
let mut timer = Timer::start();

Expand Down Expand Up @@ -103,7 +108,7 @@ fn format_project<T: FormatHandler>(
continue;
}
should_emit_verbose(input_is_stdin, config, || println!("Formatting {}", path));
context.format_file(path, &module)?;
context.format_file(path, &module, is_macro_def)?;
}
timer = timer.done_formatting();

Expand Down Expand Up @@ -134,7 +139,12 @@ impl<'a, T: FormatHandler + 'a> FormatContext<'a, T> {
}

// Formats a single file/module.
fn format_file(&mut self, path: FileName, module: &Module<'_>) -> Result<(), ErrorKind> {
fn format_file(
&mut self,
path: FileName,
module: &Module<'_>,
is_macro_def: bool,
) -> Result<(), ErrorKind> {
let snippet_provider = self.parse_session.snippet_provider(module.as_ref().inner);
let mut visitor = FmtVisitor::from_parse_sess(
&self.parse_session,
Expand All @@ -143,7 +153,7 @@ impl<'a, T: FormatHandler + 'a> FormatContext<'a, T> {
self.report.clone(),
);
visitor.skip_context.update_with_attrs(&self.krate.attrs);

visitor.is_macro_def = is_macro_def;
visitor.last_pos = snippet_provider.start_pos();
visitor.skip_empty_lines(snippet_provider.end_pos());
visitor.format_separate_mod(module, snippet_provider.end_pos());
Expand Down
24 changes: 14 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ impl fmt::Display for FormatReport {

/// Format the given snippet. The snippet is expected to be *complete* code.
/// When we cannot parse the given snippet, this function returns `None`.
fn format_snippet(snippet: &str, config: &Config) -> Option<FormattedSnippet> {
fn format_snippet(snippet: &str, config: &Config, is_macro_def: bool) -> Option<FormattedSnippet> {
let mut config = config.clone();
panic::catch_unwind(|| {
let mut out: Vec<u8> = Vec::with_capacity(snippet.len() * 2);
Expand All @@ -297,7 +297,7 @@ fn format_snippet(snippet: &str, config: &Config) -> Option<FormattedSnippet> {
let (formatting_error, result) = {
let input = Input::Text(snippet.into());
let mut session = Session::new(config, Some(&mut out));
let result = session.format(input);
let result = session.format_input_inner(input, is_macro_def);
(
session.errors.has_macro_format_failure
|| session.out.as_ref().unwrap().is_empty() && !snippet.is_empty()
Expand All @@ -323,7 +323,11 @@ fn format_snippet(snippet: &str, config: &Config) -> Option<FormattedSnippet> {
/// The code block may be incomplete (i.e., parser may be unable to parse it).
/// To avoid panic in parser, we wrap the code block with a dummy function.
/// The returned code block does **not** end with newline.
fn format_code_block(code_snippet: &str, config: &Config) -> Option<FormattedSnippet> {
fn format_code_block(
code_snippet: &str,
config: &Config,
is_macro_def: bool,
) -> Option<FormattedSnippet> {
const FN_MAIN_PREFIX: &str = "fn main() {\n";

fn enclose_in_main_block(s: &str, config: &Config) -> String {
Expand Down Expand Up @@ -356,7 +360,7 @@ fn format_code_block(code_snippet: &str, config: &Config) -> Option<FormattedSni
config_with_unix_newline
.set()
.newline_style(NewlineStyle::Unix);
let mut formatted = format_snippet(&snippet, &config_with_unix_newline)?;
let mut formatted = format_snippet(&snippet, &config_with_unix_newline, is_macro_def)?;
// Remove wrapping main block
formatted.unwrap_code_block();

Expand Down Expand Up @@ -435,7 +439,7 @@ impl<'b, T: Write + 'b> Session<'b, T> {
/// The main entry point for Rustfmt. Formats the given input according to the
/// given config. `out` is only necessary if required by the configuration.
pub fn format(&mut self, input: Input) -> Result<FormatReport, ErrorKind> {
self.format_input_inner(input)
self.format_input_inner(input, false)
}

pub fn override_config<F, U>(&mut self, mut config: Config, f: F) -> U
Expand Down Expand Up @@ -550,15 +554,15 @@ mod unit_tests {
// `format_snippet()` and `format_code_block()` should not panic
// even when we cannot parse the given snippet.
let snippet = "let";
assert!(format_snippet(snippet, &Config::default()).is_none());
assert!(format_code_block(snippet, &Config::default()).is_none());
assert!(format_snippet(snippet, &Config::default(), false).is_none());
assert!(format_code_block(snippet, &Config::default(), false).is_none());
}

fn test_format_inner<F>(formatter: F, input: &str, expected: &str) -> bool
where
F: Fn(&str, &Config) -> Option<FormattedSnippet>,
F: Fn(&str, &Config, bool) -> Option<FormattedSnippet>,
{
let output = formatter(input, &Config::default());
let output = formatter(input, &Config::default(), false);
output.is_some() && output.unwrap().snippet == expected
}

Expand All @@ -580,7 +584,7 @@ mod unit_tests {
fn test_format_code_block_fail() {
#[rustfmt::skip]
let code_block = "this_line_is_100_characters_long_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(x, y, z);";
assert!(format_code_block(code_block, &Config::default()).is_none());
assert!(format_code_block(code_block, &Config::default(), false).is_none());
}

#[test]
Expand Down
4 changes: 2 additions & 2 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1363,12 +1363,12 @@ impl MacroBranch {
config.set().max_width(new_width);

// First try to format as items, then as statements.
let new_body_snippet = match crate::format_snippet(&body_str, &config) {
let new_body_snippet = match crate::format_snippet(&body_str, &config, true) {
Some(new_body) => new_body,
None => {
let new_width = new_width + config.tab_spaces();
config.set().max_width(new_width);
match crate::format_code_block(&body_str, &config) {
match crate::format_code_block(&body_str, &config, true) {
Some(new_body) => new_body,
None => return None,
}
Expand Down
1 change: 1 addition & 0 deletions src/rewrite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub(crate) struct RewriteContext<'a> {
pub(crate) snippet_provider: &'a SnippetProvider,
// Used for `format_snippet`
pub(crate) macro_rewrite_failure: Cell<bool>,
pub(crate) is_macro_def: bool,
pub(crate) report: FormatReport,
pub(crate) skip_context: SkipContext,
pub(crate) skipped_range: Rc<RefCell<Vec<(usize, usize)>>>,
Expand Down
7 changes: 7 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,13 @@ pub(crate) fn contains_skip(attrs: &[Attribute]) -> bool {

#[inline]
pub(crate) fn semicolon_for_expr(context: &RewriteContext<'_>, expr: &ast::Expr) -> bool {
// Never try to insert semicolons on expressions when we're inside
// a macro definition - this can prevent the macro from compiling
// when used in expression position
if context.is_macro_def {
return false;
}

match expr.kind {
ast::ExprKind::Ret(..) | ast::ExprKind::Continue(..) | ast::ExprKind::Break(..) => {
context.config.trailing_semicolon()
Expand Down
3 changes: 3 additions & 0 deletions src/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ pub(crate) struct FmtVisitor<'a> {
pub(crate) macro_rewrite_failure: bool,
pub(crate) report: FormatReport,
pub(crate) skip_context: SkipContext,
pub(crate) is_macro_def: bool,
}

impl<'a> Drop for FmtVisitor<'a> {
Expand Down Expand Up @@ -811,6 +812,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
snippet_provider,
line_number: 0,
skipped_range: Rc::new(RefCell::new(vec![])),
is_macro_def: false,
macro_rewrite_failure: false,
report,
skip_context: Default::default(),
Expand Down Expand Up @@ -1003,6 +1005,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
force_one_line_chain: Cell::new(false),
snippet_provider: self.snippet_provider,
macro_rewrite_failure: Cell::new(false),
is_macro_def: self.is_macro_def,
report: self.report.clone(),
skip_context: self.skip_context.clone(),
skipped_range: self.skipped_range.clone(),
Expand Down
22 changes: 22 additions & 0 deletions tests/target/macro_rules_semi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
macro_rules! expr {
(no_semi) => {
return true
};
(semi) => {
return true;
};
}

fn foo() -> bool {
match true {
true => expr!(no_semi),
false if false => {
expr!(semi)
}
false => {
expr!(semi);
}
}
}

fn main() {}