Skip to content

Commit 73b3d58

Browse files
authored
Merge branch 'rust-lang:master' into subtree_push_automation
2 parents c2bdaec + 182a203 commit 73b3d58

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+1904
-628
lines changed

.github/workflows/check_diff.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121

2222
steps:
2323
- name: checkout
24-
uses: actions/checkout@v3
24+
uses: actions/checkout@v4
2525

2626
- name: install rustup
2727
run: |

.github/workflows/integration.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ jobs:
6464

6565
steps:
6666
- name: checkout
67-
uses: actions/checkout@v3
67+
uses: actions/checkout@v4
6868

6969
# Run build
7070
- name: install rustup

.github/workflows/linux.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626

2727
steps:
2828
- name: checkout
29-
uses: actions/checkout@v3
29+
uses: actions/checkout@v4
3030

3131
# Run build
3232
- name: install rustup

.github/workflows/mac.yml

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ on:
88
jobs:
99
test:
1010
# https://help.github.com/en/actions/automating-your-workflow-with-github-actions/virtual-environments-for-github-hosted-runners#supported-runners-and-hardware-resources
11-
# macOS Catalina 10.15
1211
runs-on: macos-latest
1312
name: (${{ matrix.target }}, ${{ matrix.cfg_release_channel }})
1413
env:
@@ -23,7 +22,7 @@ jobs:
2322

2423
steps:
2524
- name: checkout
26-
uses: actions/checkout@v3
25+
uses: actions/checkout@v4
2726

2827
# Run build
2928
- name: install rustup

.github/workflows/rustdoc_check.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
name: rustdoc check
1212
steps:
1313
- name: checkout
14-
uses: actions/checkout@v3
14+
uses: actions/checkout@v4
1515

1616
- name: install rustup
1717
run: |

.github/workflows/upload-assets.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ jobs:
3131
target: x86_64-pc-windows-msvc
3232
runs-on: ${{ matrix.os }}
3333
steps:
34-
- uses: actions/checkout@v3
34+
- uses: actions/checkout@v4
3535

3636
# Run build
3737
- name: install rustup

.github/workflows/windows.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ jobs:
3333
- name: disable git eol translation
3434
run: git config --global core.autocrlf false
3535
- name: checkout
36-
uses: actions/checkout@v3
36+
uses: actions/checkout@v4
3737

3838
# Run build
3939
- name: Install Rustup using win.rustup.rs

config_proc_macro/src/utils.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use proc_macro2::TokenStream;
2-
use quote::{quote, ToTokens};
2+
use quote::{ToTokens, quote};
33

44
pub fn fold_quote<F, I, T>(input: impl Iterator<Item = I>, f: F) -> TokenStream
55
where

rust-toolchain

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[toolchain]
2-
channel = "nightly-2024-06-25"
2+
channel = "nightly-2024-08-17"
33
components = ["llvm-tools", "rustc-dev"]

rustfmt.toml

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
error_on_line_overflow = true
22
error_on_unformatted = true
33
style_edition = "2024"
4+
overflow_delimited_expr = false

src/attr.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
//! Format attributes and meta items.
22
3-
use rustc_ast::ast;
43
use rustc_ast::HasAttrs;
5-
use rustc_span::{symbol::sym, Span};
4+
use rustc_ast::ast;
5+
use rustc_span::{Span, symbol::sym};
66

77
use self::doc_comment::DocCommentFormatter;
8-
use crate::comment::{contains_comment, rewrite_doc_comment, CommentStyle};
9-
use crate::config::lists::*;
8+
use crate::comment::{CommentStyle, contains_comment, rewrite_doc_comment};
109
use crate::config::IndentStyle;
10+
use crate::config::lists::*;
1111
use crate::expr::rewrite_literal;
12-
use crate::lists::{definitive_tactic, itemize_list, write_list, ListFormatting, Separator};
12+
use crate::lists::{ListFormatting, Separator, definitive_tactic, itemize_list, write_list};
1313
use crate::overflow;
1414
use crate::rewrite::{Rewrite, RewriteContext, RewriteError, RewriteErrorExt, RewriteResult};
1515
use crate::shape::Shape;
1616
use crate::source_map::SpanUtils;
17-
use crate::types::{rewrite_path, PathContext};
17+
use crate::types::{PathContext, rewrite_path};
1818
use crate::utils::{count_newlines, mk_sp};
1919

2020
mod doc_comment;

src/bin/main.rs

+75-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![feature(rustc_private)]
22

3-
use anyhow::{format_err, Result};
3+
use anyhow::{Result, format_err};
44

55
use io::Error as IoError;
66
use thiserror::Error;
@@ -11,15 +11,15 @@ use tracing_subscriber::EnvFilter;
1111
use std::collections::HashMap;
1212
use std::env;
1313
use std::fs::File;
14-
use std::io::{self, stdout, Read, Write};
14+
use std::io::{self, Read, Write, stdout};
1515
use std::path::{Path, PathBuf};
1616
use std::str::FromStr;
1717

1818
use getopts::{Matches, Options};
1919

2020
use crate::rustfmt::{
21-
load_config, CliOptions, Color, Config, Edition, EmitMode, FileLines, FileName,
22-
FormatReportFormatterBuilder, Input, Session, StyleEdition, Verbosity,
21+
CliOptions, Color, Config, Edition, EmitMode, FileLines, FileName,
22+
FormatReportFormatterBuilder, Input, Session, StyleEdition, Verbosity, Version, load_config,
2323
};
2424

2525
const BUG_REPORT_URL: &str = "https://github.com/rust-lang/rustfmt/issues/new?labels=bug";
@@ -462,16 +462,15 @@ fn print_version() {
462462

463463
fn determine_operation(matches: &Matches) -> Result<Operation, OperationError> {
464464
if matches.opt_present("h") {
465-
let topic = matches.opt_str("h");
466-
if topic.is_none() {
465+
let Some(topic) = matches.opt_str("h") else {
467466
return Ok(Operation::Help(HelpOp::None));
468-
} else if topic == Some("config".to_owned()) {
469-
return Ok(Operation::Help(HelpOp::Config));
470-
} else if topic == Some("file-lines".to_owned()) && is_nightly() {
471-
return Ok(Operation::Help(HelpOp::FileLines));
472-
} else {
473-
return Err(OperationError::UnknownHelpTopic(topic.unwrap()));
474-
}
467+
};
468+
469+
return match topic.as_str() {
470+
"config" => Ok(Operation::Help(HelpOp::Config)),
471+
"file-lines" if is_nightly() => Ok(Operation::Help(HelpOp::FileLines)),
472+
_ => Err(OperationError::UnknownHelpTopic(topic)),
473+
};
475474
}
476475
let mut free_matches = matches.free.iter();
477476

@@ -734,6 +733,25 @@ impl CliOptions for GetOptsOptions {
734733
fn config_path(&self) -> Option<&Path> {
735734
self.config_path.as_deref()
736735
}
736+
737+
fn edition(&self) -> Option<Edition> {
738+
self.inline_config
739+
.get("edition")
740+
.map_or(self.edition, |e| Edition::from_str(e).ok())
741+
}
742+
743+
fn style_edition(&self) -> Option<StyleEdition> {
744+
self.inline_config
745+
.get("style_edition")
746+
.map_or(self.style_edition, |se| StyleEdition::from_str(se).ok())
747+
}
748+
749+
fn version(&self) -> Option<Version> {
750+
self.inline_config
751+
.get("version")
752+
.map(|version| Version::from_str(version).ok())
753+
.flatten()
754+
}
737755
}
738756

739757
fn edition_from_edition_str(edition_str: &str) -> Result<Edition> {
@@ -802,6 +820,17 @@ mod test {
802820
options.inline_config = HashMap::from([("version".to_owned(), "Two".to_owned())]);
803821
let config = get_config(None, Some(options));
804822
assert_eq!(config.style_edition(), StyleEdition::Edition2024);
823+
assert_eq!(config.overflow_delimited_expr(), true);
824+
}
825+
826+
#[nightly_only_test]
827+
#[test]
828+
fn version_config_file_sets_style_edition_override_correctly() {
829+
let options = GetOptsOptions::default();
830+
let config_file = Some(Path::new("tests/config/style-edition/just-version"));
831+
let config = get_config(config_file, Some(options));
832+
assert_eq!(config.style_edition(), StyleEdition::Edition2024);
833+
assert_eq!(config.overflow_delimited_expr(), true);
805834
}
806835

807836
#[nightly_only_test]
@@ -846,6 +875,7 @@ mod test {
846875
]);
847876
let config = get_config(None, Some(options));
848877
assert_eq!(config.style_edition(), StyleEdition::Edition2024);
878+
assert_eq!(config.overflow_delimited_expr(), true);
849879
}
850880

851881
#[nightly_only_test]
@@ -903,4 +933,36 @@ mod test {
903933
let config = get_config(config_file, Some(options));
904934
assert_eq!(config.style_edition(), StyleEdition::Edition2021);
905935
}
936+
937+
#[nightly_only_test]
938+
#[test]
939+
fn correct_defaults_for_style_edition_loaded() {
940+
let mut options = GetOptsOptions::default();
941+
options.style_edition = Some(StyleEdition::Edition2024);
942+
let config = get_config(None, Some(options));
943+
assert_eq!(config.style_edition(), StyleEdition::Edition2024);
944+
assert_eq!(config.overflow_delimited_expr(), true);
945+
}
946+
947+
#[nightly_only_test]
948+
#[test]
949+
fn style_edition_defaults_overridden_from_config() {
950+
let options = GetOptsOptions::default();
951+
let config_file = Some(Path::new("tests/config/style-edition/overrides"));
952+
let config = get_config(config_file, Some(options));
953+
assert_eq!(config.style_edition(), StyleEdition::Edition2024);
954+
assert_eq!(config.overflow_delimited_expr(), false);
955+
}
956+
957+
#[nightly_only_test]
958+
#[test]
959+
fn style_edition_defaults_overridden_from_cli() {
960+
let mut options = GetOptsOptions::default();
961+
let config_file = Some(Path::new("tests/config/style-edition/just-style-edition"));
962+
options.inline_config =
963+
HashMap::from([("overflow_delimited_expr".to_owned(), "false".to_owned())]);
964+
let config = get_config(config_file, Some(options));
965+
assert_eq!(config.style_edition(), StyleEdition::Edition2024);
966+
assert_eq!(config.overflow_delimited_expr(), false);
967+
}
906968
}

src/chains.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ use std::borrow::Cow;
5959
use std::cmp::min;
6060

6161
use rustc_ast::{ast, ptr};
62-
use rustc_span::{symbol, BytePos, Span};
62+
use rustc_span::{BytePos, Span, symbol};
6363

64-
use crate::comment::{rewrite_comment, CharClasses, FullCodeCharKind, RichChar};
64+
use crate::comment::{CharClasses, FullCodeCharKind, RichChar, rewrite_comment};
6565
use crate::config::{IndentStyle, StyleEdition};
6666
use crate::expr::rewrite_call;
6767
use crate::lists::extract_pre_comment;
@@ -209,7 +209,7 @@ impl ChainItemKind {
209209
fn is_tup_field_access(expr: &ast::Expr) -> bool {
210210
match expr.kind {
211211
ast::ExprKind::Field(_, ref field) => {
212-
field.name.to_string().chars().all(|c| c.is_digit(10))
212+
field.name.as_str().chars().all(|c| c.is_digit(10))
213213
}
214214
_ => false,
215215
}

src/closures.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@ use rustc_span::Span;
33
use thin_vec::thin_vec;
44

55
use crate::attr::get_attrs_from_stmt;
6-
use crate::config::lists::*;
76
use crate::config::StyleEdition;
7+
use crate::config::lists::*;
88
use crate::expr::{block_contains_comment, is_simple_block, is_unsafe_block, rewrite_cond};
99
use crate::items::{span_hi_for_param, span_lo_for_param};
10-
use crate::lists::{definitive_tactic, itemize_list, write_list, ListFormatting, Separator};
10+
use crate::lists::{ListFormatting, Separator, definitive_tactic, itemize_list, write_list};
1111
use crate::overflow::OverflowableItem;
1212
use crate::rewrite::{Rewrite, RewriteContext, RewriteError, RewriteErrorExt, RewriteResult};
1313
use crate::shape::Shape;
1414
use crate::source_map::SpanUtils;
1515
use crate::types::rewrite_bound_params;
16-
use crate::utils::{last_line_width, left_most_sub_expr, stmt_expr, NodeIdExt};
16+
use crate::utils::{NodeIdExt, last_line_width, left_most_sub_expr, stmt_expr};
1717

1818
// This module is pretty messy because of the rules around closures and blocks:
1919
// FIXME - the below is probably no longer true in full.

src/comment.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
use std::{borrow::Cow, iter};
44

5-
use itertools::{multipeek, MultiPeek};
5+
use itertools::{MultiPeek, multipeek};
66
use rustc_span::Span;
77

88
use crate::config::Config;
99
use crate::rewrite::{RewriteContext, RewriteErrorExt, RewriteResult};
1010
use crate::shape::{Indent, Shape};
11-
use crate::string::{rewrite_string, StringFormat};
11+
use crate::string::{StringFormat, rewrite_string};
1212
use crate::utils::{
1313
count_newlines, first_line_width, last_line_width, trim_left_preserve_layout,
1414
trimmed_last_line_width, unicode_str_width,
@@ -533,10 +533,11 @@ impl ItemizedBlock {
533533

534534
/// Returns the block as a string, with each line trimmed at the start.
535535
fn trimmed_block_as_string(&self) -> String {
536-
self.lines
537-
.iter()
538-
.map(|line| format!("{} ", line.trim_start()))
539-
.collect::<String>()
536+
self.lines.iter().fold(String::new(), |mut acc, line| {
537+
acc.push_str(line.trim_start());
538+
acc.push(' ');
539+
acc
540+
})
540541
}
541542

542543
/// Returns the block as a string under its original form.
@@ -1703,12 +1704,11 @@ impl<'a> Iterator for CommentCodeSlices<'a> {
17031704
}
17041705

17051706
/// Checks is `new` didn't miss any comment from `span`, if it removed any, return previous text
1706-
/// (if it fits in the width/offset, else return `None`), else return `new`
17071707
pub(crate) fn recover_comment_removed(
17081708
new: String,
17091709
span: Span,
17101710
context: &RewriteContext<'_>,
1711-
) -> Option<String> {
1711+
) -> String {
17121712
let snippet = context.snippet(span);
17131713
if snippet != new && changed_comment_content(snippet, &new) {
17141714
// We missed some comments. Warn and keep the original text.
@@ -1722,9 +1722,9 @@ pub(crate) fn recover_comment_removed(
17221722
)],
17231723
);
17241724
}
1725-
Some(snippet.to_owned())
1725+
snippet.to_owned()
17261726
} else {
1727-
Some(new)
1727+
new
17281728
}
17291729
}
17301730

0 commit comments

Comments
 (0)