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

Sync rustfmt subtree #95458

Merged
merged 75 commits into from
Mar 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
75 commits
Select commit Hold shift + click to select a range
6db6baf
Merge commit '4a053f206fd6799a25823c307f7d7f9d897be118' into sync-rus…
calebcartwright Dec 30, 2021
737e6f7
Improve out of line module resolution
ytmimi Dec 20, 2021
93b7de5
Make `--check` work when running from stdin. (#3896)
jugglerchris Nov 6, 2019
34263cd
Fix --check -l with stdin. (#3910)
jugglerchris Nov 12, 2019
776baf9
refactor: update json emitter to better handle errors (#3953)
calebcartwright Dec 9, 2019
894a3c0
Fix newlines in JSON output (#4262)
Timmmm Jun 23, 2020
34d374e
Use <stdin> when emitting stdin as filename (#4298)
topecongiro Jul 3, 2020
5056f4c
some minor clippy fixes
matthiaskrgr Jan 5, 2022
6e63002
Compute most of Public/Exported access level in rustc_resolve
lambinoo Jul 26, 2021
04670a1
Remove LLVM-style inline assembly from rustfmt
tmiasko Jan 12, 2022
bfbf42c
fix(rustfmt): resolve generated file formatting issue
calebcartwright Jan 15, 2022
8ae2312
Auto merge of #92816 - tmiasko:rm-llvm-asm, r=Amanieu
bors Jan 17, 2022
f5ce84e
add eq constraints on associated constants
JulianKnodt Jul 30, 2021
cf86d53
Add term
JulianKnodt Jan 7, 2022
7913f13
Add term to ExistentialProjection
JulianKnodt Jan 13, 2022
9b454a5
Merge remote-tracking branch 'upstream/master' into subtree-sync-2022…
calebcartwright Jan 23, 2022
9e1973f
chore: bump toolchain, update test
calebcartwright Jan 23, 2022
5c558e2
Merge pull request #5186 from calebcartwright/subtree-sync-2022-01-23
calebcartwright Jan 23, 2022
3572c54
rustc_errors: only box the `diagnostic` field in `DiagnosticBuilder`.
eddyb Jan 24, 2022
b4a4bf0
Fix formatting of comments in empty structs (#5171)
Szymongib Jan 29, 2022
8b0b213
Prevent adding trailing whitespace when rewriting ast::Param
ytmimi Dec 10, 2021
368a9b7
Handle non-ascii character at boundary (#5089)
scampi Feb 2, 2022
606894e
Retain trailing separator when extracting the last inline post comment
ytmimi Dec 19, 2021
b2c7a52
Fix import_granularity option when the use tree has an alias
scampi Jan 31, 2022
fd6e11c
Add tests for the One and Crate variants
scampi Feb 2, 2022
5df8c8f
Fix doc of generic items formmating error (#5124)
frank-king Feb 7, 2022
ace7241
Fix incorrect string indentation in macro defs with `format_strings`
yodaldevoid Jan 29, 2022
813d127
Clarify generated marker requires a config option
stepancheg Feb 7, 2022
b05b313
chore(rustfmt): remove executable path from usage string (#5216)
tharun208 Feb 11, 2022
1e78a2b
Leverage itemized blocks to support formatting markdown block quotes
ytmimi Dec 30, 2021
6c47612
Add context to get_toml_path() error (#5207)
tmfink Feb 15, 2022
d5aabcc
Format code
camsteffen Feb 1, 2022
c63d42e
Use cargo-fmt in self_tests
camsteffen Feb 1, 2022
281bf03
fix: formatting in new test
calebcartwright Feb 16, 2022
7592663
rustc_errors: add `downgrade_to_delayed_bug` to `Diagnostic` itself.
eddyb Jan 23, 2022
5723946
rustc_errors: take `self` by value in `DiagnosticBuilder::cancel`.
eddyb Jan 26, 2022
89ca3f3
fix: unused test imports on non-nightly, prevent regression
calebcartwright Feb 24, 2022
de1ac37
Enable rustc_pass_by_value for Span
Mark-Simulacrum Feb 23, 2022
12048e4
fallback to dir_path when relative external mod resolution fails
ytmimi Jan 30, 2022
272fb42
Prevent wrapping markdown headers in doc comments
ytmimi Feb 25, 2022
4edb757
refactor: prepare to associate multiple spans with a module.
pnkfelix Jan 20, 2022
74876ef
Associate multiple with a crate too.
pnkfelix Mar 3, 2022
651f463
Adjusted diagnostic output so that if there is no `use` in a item seq…
pnkfelix Jan 20, 2022
0be8931
Update `itertools`
pierwill Mar 4, 2022
ce301d9
Placate tidy in submodule.
pnkfelix Mar 4, 2022
1212c94
Change syntax for TyAlias where clauses
jackh726 Oct 19, 2021
6a2dae6
Rollup merge of #94617 - pierwill:update-itertools, r=Mark-Simulacrum
matthiaskrgr Mar 6, 2022
ee13051
Fix missing struct field separators under certain conditions
cassaundra Dec 29, 2021
ab9f2a8
Add more tests for struct_field_align_threshold and trailing_comma
cassaundra Jan 15, 2022
58de414
Add test for issue #4791
cassaundra Mar 5, 2022
003eaf8
placate rustfmt in rustfmt.
pnkfelix Mar 7, 2022
18c0369
Improve mod resolution error for mods with multiple candidate files
ytmimi Jan 11, 2022
b4de150
fix: imports_granularity module with path containing self
tommilligan Mar 4, 2022
a918d8b
Auto merge of #94584 - pnkfelix:inject-use-suggestion-sites, r=ekuber
bors Mar 15, 2022
9c65db6
Correct tracking issue link for `skip_children`
ytmimi Mar 12, 2022
5696e38
Add test to verify tracking issue links
ytmimi Mar 12, 2022
1bb85bd
chore: add utility function for relative span positions
calebcartwright Mar 10, 2022
c0861d3
resolve the conflict in compiler/rustc_session/src/parse.rs
codehorseman Mar 16, 2022
c1d351f
rustc_error: make ErrorReported impossible to construct
mark-i-m Jan 23, 2022
c801367
Merge remote-tracking branch 'upstream/master' into subtree-sync-2022…
calebcartwright Mar 17, 2022
cce0d50
Rollup merge of #94960 - codehorseman:master, r=oli-obk
Dylan-DPC Mar 17, 2022
432b8de
chore: bump toolchain
calebcartwright Mar 17, 2022
a36dc36
Merge pull request #5268 from calebcartwright/subtree-sync-2022-03-16
calebcartwright Mar 18, 2022
4f89c51
Take &mut Diagnostic in emit_diagnostic.
cjgillot Mar 20, 2022
e41329c
Search for struct body span after any generic arguments
ytmimi Mar 20, 2022
0dba01a
Add `short_item_threshold` config option
123vivekr Feb 13, 2022
8984438
Honor `#[rustfmt::skip::attributes(derive)]` attribute
ytmimi Mar 18, 2022
5731f1d
Merge remote-tracking branch 'upstream/master' into subtree-sync-2022…
calebcartwright Mar 28, 2022
c2039d9
chore: bump toolchain
calebcartwright Mar 28, 2022
63acf90
Merge pull request #5276 from calebcartwright/subtree-sync-2022-03-27
calebcartwright Mar 28, 2022
4fecede
Revert "Use cargo-fmt in self_tests"
calebcartwright Mar 29, 2022
e0c7b7d
tests: ignore cargo fmt test for rust-lang/rust runs
calebcartwright Mar 29, 2022
8e94761
Add test of macro calls inside extern block
dtolnay Mar 29, 2022
5ff7b63
Preserve semicolon after macro call inside foreign mod
dtolnay Mar 29, 2022
419df99
Merge commit '5ff7b632a95bac6955611d85040859128902c580' into sync-rus…
calebcartwright Mar 30, 2022
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
4 changes: 4 additions & 0 deletions src/tools/rustfmt/.github/workflows/linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ jobs:
rustc -Vv
cargo -V
cargo build
env:
RUSTFLAGS: '-D warnings'

- name: test
run: cargo test
env:
RUSTFLAGS: '-D warnings'
5 changes: 5 additions & 0 deletions src/tools/rustfmt/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## [Unreleased]

### Fixed

- Fixes issue where wrapped strings would be incorrectly indented in macro defs when `format_strings` was enabled [#4036](https://github.com/rust-lang/rustfmt/issues/4036)

## [1.4.38] 2021-10-20

### Changed
Expand Down Expand Up @@ -57,6 +61,7 @@ Note this hit the rustup distributions prior to the v1.4.38 release as part of a

- New `One` variant added to `imports_granularity` configuration option which can be used to reformat all imports into a single use statement [#4669](https://github.com/rust-lang/rustfmt/issues/4669)
- rustfmt will now skip files that are annotated with `@generated` at the top of the file [#3958](https://github.com/rust-lang/rustfmt/issues/3958)
if `format_generated_files` option is set to `false` (by default `@generated` files are formatted)
- New configuration option `hex_literal_case` that allows user to control the casing utilized for hex literals [PR #4903](https://github.com/rust-lang/rustfmt/pull/4903)

See the section on the configuration site for more information
Expand Down
4 changes: 2 additions & 2 deletions src/tools/rustfmt/Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,9 @@ dependencies = [

[[package]]
name = "itertools"
version = "0.9.0"
version = "0.10.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "284f18f85651fe11e8a991b2adb42cb078325c996ed026d994719efcfca1d54b"
checksum = "a9a9d19fa1e79b6215ff29b9d6880b706147f16e9b1dbb1e4e5947b5b02bc5e3"
dependencies = [
"either",
]
Expand Down
38 changes: 37 additions & 1 deletion src/tools/rustfmt/Configurations.md
Original file line number Diff line number Diff line change
Expand Up @@ -930,6 +930,8 @@ fn add_one(x: i32) -> i32 {

Format generated files. A file is considered generated
if any of the first five lines contain a `@generated` comment marker.
By default, generated files are reformatted, i. e. `@generated` marker is ignored.
This option is currently ignored for stdin (`@generated` in stdin is ignored.)

- **Default value**: `true`
- **Possible values**: `true`, `false`
Expand Down Expand Up @@ -2198,13 +2200,47 @@ specific version of rustfmt is used in your CI, use this option.
- **Possible values**: any published version (e.g. `"0.3.8"`)
- **Stable**: No (tracking issue: [#3386](https://github.com/rust-lang/rustfmt/issues/3386))

## `short_array_element_width_threshold`

The width threshold for an array element to be considered "short".

The layout of an array is dependent on the length of each of its elements.
If the length of every element in an array is below this threshold (all elements are "short") then the array can be formatted in the mixed/compressed style, but if any one element has a length that exceeds this threshold then the array elements will have to be formatted vertically.

- **Default value**: `10`
- **Possible values**: any positive integer that is less than or equal to the value specified for [`max_width`](#max_width)
- **Stable**: Yes

#### `10` (default):
```rust
fn main() {
pub const FORMAT_TEST: [u64; 5] = [
0x0000000000000000,
0xaaaaaaaaaaaaaaaa,
0xbbbbbbbbbbbbbbbb,
0xcccccccccccccccc,
0xdddddddddddddddd,
];
}
```
#### `20`:
```rust
fn main() {
pub const FORMAT_TEST: [u64; 5] = [
0x0000000000000000, 0xaaaaaaaaaaaaaaaa, 0xbbbbbbbbbbbbbbbb, 0xcccccccccccccccc,
0xdddddddddddddddd,
];
}
```
See also [`max_width`](#max_width).

## `skip_children`

Don't reformat out of line modules

- **Default value**: `false`
- **Possible values**: `true`, `false`
- **Stable**: No (tracking issue: [#3389](https://github.com/rust-lang/rustfmt/issues/3386))
- **Stable**: No (tracking issue: [#3389](https://github.com/rust-lang/rustfmt/issues/3389))

## `single_line_if_else_max_width`

Expand Down
4 changes: 2 additions & 2 deletions src/tools/rustfmt/config_proc_macro/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ pub fn is_unit(v: &syn::Variant) -> bool {
#[cfg(feature = "debug-with-rustfmt")]
/// Pretty-print the output of proc macro using rustfmt.
pub fn debug_with_rustfmt(input: &TokenStream) {
use std::io::Write;
use std::process::{Command, Stdio};
use std::env;
use std::ffi::OsStr;
use std::io::Write;
use std::process::{Command, Stdio};

let rustfmt_var = env::var_os("RUSTFMT");
let rustfmt = match &rustfmt_var {
Expand Down
2 changes: 1 addition & 1 deletion src/tools/rustfmt/rust-toolchain
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[toolchain]
channel = "nightly-2021-12-29"
channel = "nightly-2022-03-27"
components = ["rustc-dev"]
6 changes: 5 additions & 1 deletion src/tools/rustfmt/src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,10 @@ impl Rewrite for [ast::Attribute] {
let mut attrs = self;
let mut result = String::new();

// Determine if the source text is annotated with `#[rustfmt::skip::attributes(derive)]`
// or `#![rustfmt::skip::attributes(derive)]`
let skip_derives = context.skip_context.skip_attribute("derive");

// This is not just a simple map because we need to handle doc comments
// (where we take as many doc comment attributes as possible) and possibly
// merging derives into a single attribute.
Expand Down Expand Up @@ -431,7 +435,7 @@ impl Rewrite for [ast::Attribute] {
}

// Handle derives if we will merge them.
if context.config.merge_derives() && is_derive(&attrs[0]) {
if !skip_derives && context.config.merge_derives() && is_derive(&attrs[0]) {
let derives = take_while_with_pred(context, attrs, is_derive);
let derive_str = format_derive(derives, shape, context)?;
result.push_str(&derive_str);
Expand Down
38 changes: 19 additions & 19 deletions src/tools/rustfmt/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fn main() {
let exit_code = match execute(&opts) {
Ok(code) => code,
Err(e) => {
eprintln!("{}", e);
eprintln!("{:#}", e);
1
}
};
Expand Down Expand Up @@ -74,14 +74,10 @@ pub enum OperationError {
/// An io error during reading or writing.
#[error("{0}")]
IoError(IoError),
/// Attempt to use --check with stdin, which isn't currently
/// supported.
#[error("The `--check` option is not supported with standard input.")]
CheckWithStdin,
/// Attempt to use --emit=json with stdin, which isn't currently
/// supported.
#[error("Using `--emit` other than stdout is not supported with standard input.")]
EmitWithStdin,
/// Attempt to use --emit with a mode which is not currently
/// supported with stdandard input.
#[error("Emit mode {0} not supported with standard output.")]
StdinBadEmit(EmitMode),
}

impl From<IoError> for OperationError {
Expand Down Expand Up @@ -255,15 +251,20 @@ fn format_string(input: String, options: GetOptsOptions) -> Result<i32> {
let (mut config, _) = load_config(Some(Path::new(".")), Some(options.clone()))?;

if options.check {
return Err(OperationError::CheckWithStdin.into());
}
if let Some(emit_mode) = options.emit_mode {
if emit_mode != EmitMode::Stdout {
return Err(OperationError::EmitWithStdin.into());
config.set().emit_mode(EmitMode::Diff);
} else {
match options.emit_mode {
// Emit modes which work with standard input
// None means default, which is Stdout.
None | Some(EmitMode::Stdout) | Some(EmitMode::Checkstyle) | Some(EmitMode::Json) => {}
Some(emit_mode) => {
return Err(OperationError::StdinBadEmit(emit_mode).into());
}
}
config
.set()
.emit_mode(options.emit_mode.unwrap_or(EmitMode::Stdout));
}
// emit mode is always Stdout for Stdin.
config.set().emit_mode(EmitMode::Stdout);
config.set().verbose(Verbosity::Quiet);

// parse file_lines
Expand Down Expand Up @@ -393,9 +394,8 @@ fn print_usage_to_stdout(opts: &Options, reason: &str) {
format!("{}\n\n", reason)
};
let msg = format!(
"{}Format Rust code\n\nusage: {} [options] <file>...",
sep,
env::args_os().next().unwrap().to_string_lossy()
"{}Format Rust code\n\nusage: rustfmt [options] <file>...",
sep
);
println!("{}", opts.usage(&msg));
}
Expand Down
3 changes: 1 addition & 2 deletions src/tools/rustfmt/src/cargo-fmt/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,8 +387,7 @@ fn get_targets_root_only(
.unwrap_or_default()
== current_dir_manifest
})
.map(|p| p.targets)
.flatten()
.flat_map(|p| p.targets)
.collect(),
};

Expand Down
64 changes: 53 additions & 11 deletions src/tools/rustfmt/src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,36 +432,49 @@ impl CodeBlockAttribute {

/// Block that is formatted as an item.
///
/// An item starts with either a star `*` or a dash `-`. Different level of indentation are
/// handled by shrinking the shape accordingly.
/// An item starts with either a star `*` a dash `-` or a greater-than `>`.
/// Different level of indentation are handled by shrinking the shape accordingly.
struct ItemizedBlock {
/// the lines that are identified as part of an itemized block
lines: Vec<String>,
/// the number of whitespaces up to the item sigil
/// the number of characters (typically whitespaces) up to the item sigil
indent: usize,
/// the string that marks the start of an item
opener: String,
/// sequence of whitespaces to prefix new lines that are part of the item
/// sequence of characters (typically whitespaces) to prefix new lines that are part of the item
line_start: String,
}

impl ItemizedBlock {
/// Returns `true` if the line is formatted as an item
fn is_itemized_line(line: &str) -> bool {
let trimmed = line.trim_start();
trimmed.starts_with("* ") || trimmed.starts_with("- ")
trimmed.starts_with("* ") || trimmed.starts_with("- ") || trimmed.starts_with("> ")
}

/// Creates a new ItemizedBlock described with the given line.
/// The `is_itemized_line` needs to be called first.
fn new(line: &str) -> ItemizedBlock {
let space_to_sigil = line.chars().take_while(|c| c.is_whitespace()).count();
let indent = space_to_sigil + 2;
// +2 = '* ', which will add the appropriate amount of whitespace to keep itemized
// content formatted correctly.
let mut indent = space_to_sigil + 2;
let mut line_start = " ".repeat(indent);

// Markdown blockquote start with a "> "
if line.trim_start().starts_with(">") {
// remove the original +2 indent because there might be multiple nested block quotes
// and it's easier to reason about the final indent by just taking the length
// of th new line_start. We update the indent because it effects the max width
// of each formatted line.
line_start = itemized_block_quote_start(line, line_start, 2);
indent = line_start.len();
}
ItemizedBlock {
lines: vec![line[indent..].to_string()],
indent,
opener: line[..indent].to_string(),
line_start: " ".repeat(indent),
line_start,
}
}

Expand Down Expand Up @@ -504,6 +517,25 @@ impl ItemizedBlock {
}
}

/// Determine the line_start when formatting markdown block quotes.
/// The original line_start likely contains indentation (whitespaces), which we'd like to
/// replace with '> ' characters.
fn itemized_block_quote_start(line: &str, mut line_start: String, remove_indent: usize) -> String {
let quote_level = line
.chars()
.take_while(|c| !c.is_alphanumeric())
.fold(0, |acc, c| if c == '>' { acc + 1 } else { acc });

for _ in 0..remove_indent {
line_start.pop();
}

for _ in 0..quote_level {
line_start.push_str("> ")
}
line_start
}

struct CommentRewrite<'a> {
result: String,
code_block_buffer: String,
Expand Down Expand Up @@ -651,6 +683,7 @@ impl<'a> CommentRewrite<'a> {
i: usize,
line: &'a str,
has_leading_whitespace: bool,
is_doc_comment: bool,
) -> bool {
let num_newlines = count_newlines(orig);
let is_last = i == num_newlines;
Expand Down Expand Up @@ -757,10 +790,19 @@ impl<'a> CommentRewrite<'a> {
}
}

if self.fmt.config.wrap_comments()
let is_markdown_header_doc_comment = is_doc_comment && line.starts_with("#");

// We only want to wrap the comment if:
// 1) wrap_comments = true is configured
// 2) The comment is not the start of a markdown header doc comment
// 3) The comment width exceeds the shape's width
// 4) No URLS were found in the commnet
let should_wrap_comment = self.fmt.config.wrap_comments()
&& !is_markdown_header_doc_comment
&& unicode_str_width(line) > self.fmt.shape.width
&& !has_url(line)
{
&& !has_url(line);

if should_wrap_comment {
match rewrite_string(line, &self.fmt, self.max_width) {
Some(ref s) => {
self.is_prev_line_multi_line = s.contains('\n');
Expand Down Expand Up @@ -850,7 +892,7 @@ fn rewrite_comment_inner(
});

for (i, (line, has_leading_whitespace)) in lines.enumerate() {
if rewriter.handle_line(orig, i, line, has_leading_whitespace) {
if rewriter.handle_line(orig, i, line, has_leading_whitespace, is_doc_comment) {
break;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/tools/rustfmt/src/config/file_lines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl fmt::Display for FileName {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
FileName::Real(p) => write!(f, "{}", p.to_str().unwrap()),
FileName::Stdin => write!(f, "stdin"),
FileName::Stdin => write!(f, "<stdin>"),
}
}
}
Expand Down
7 changes: 6 additions & 1 deletion src/tools/rustfmt/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ create_config! {
// Misc.
remove_nested_parens: bool, true, true, "Remove nested parens";
combine_control_expr: bool, true, false, "Combine control expressions with function calls";
short_array_element_width_threshold: usize, 10, true,
"Width threshold for an array element to be considered short";
overflow_delimited_expr: bool, false, false,
"Allow trailing bracket/brace delimited expressions to overflow";
struct_field_align_threshold: usize, 0, false,
Expand Down Expand Up @@ -364,7 +366,9 @@ fn get_toml_path(dir: &Path) -> Result<Option<PathBuf>, Error> {
// find the project file yet, and continue searching.
Err(e) => {
if e.kind() != ErrorKind::NotFound {
return Err(e);
let ctx = format!("Failed to get metadata for config file {:?}", &config_file);
let err = anyhow::Error::new(e).context(ctx);
return Err(Error::new(ErrorKind::Other, err));
}
}
_ => {}
Expand Down Expand Up @@ -589,6 +593,7 @@ spaces_around_ranges = false
binop_separator = "Front"
remove_nested_parens = true
combine_control_expr = true
short_array_element_width_threshold = 10
overflow_delimited_expr = false
struct_field_align_threshold = 0
enum_discrim_align_threshold = 0
Expand Down
Loading