Skip to content

Commit

Permalink
Merge pull request rust-lang#5944 from calebcartwright/subtree-sync-2…
Browse files Browse the repository at this point in the history
…023-10-22

sync subtree
  • Loading branch information
calebcartwright authored Oct 22, 2023
2 parents 547577f + c2515df commit 0bb2acf
Show file tree
Hide file tree
Showing 20 changed files with 111 additions and 85 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@
- Support for formatting let-else statements [#5690]
- New config option, `single_line_let_else_max_width`, that allows users to configure the maximum length of single line `let-else` statements. `let-else` statements that otherwise meet the requirements to be formatted on a single line will have their divergent`else` block formatted over multiple lines if they exceed this length [#5684]

<<<<<<< HEAD
[#5690]: (https://github.com/rust-lang/rustfmt/pulls/5690)
=======
[#5690]: https://github.com/rust-lang/rustfmt/pull/5690
>>>>>>> upstream/master
[#5684]: https://github.com/rust-lang/rustfmt/issues/5684

## [1.5.3] 2023-06-20
Expand All @@ -90,7 +94,11 @@

- When formatting doc comments with `wrap_comments = true` rustfmt will no longer wrap markdown tables [#4210](https://github.com/rust-lang/rustfmt/issues/4210)
- Properly handle wrapping comments that include a numbered list in markdown [#5416](https://github.com/rust-lang/rustfmt/issues/5416)
<<<<<<< HEAD
- Properly handle markdown sublists that utilize a `+` [#4041](https://github.com/rust-lang/rustfmt/issues/4210)
=======
- Properly handle markdown sublists that utilize a `+` [#4041](https://github.com/rust-lang/rustfmt/issues/4041)
>>>>>>> upstream/master
- rustfmt will no longer use shorthand initialization when rewriting a tuple struct even when `use_field_init_shorthand = true` as this leads to code that could no longer compile.
Take the following struct as an example `struct MyStruct(u64);`. rustfmt will no longer format `MyStruct { 0: 0 }` as `MyStruct { 0 }` [#5488](https://github.com/rust-lang/rustfmt/issues/5488)
- rustfmt no longer panics when formatting an empty code block in a doc comment with `format_code_in_doc_comments = true` [#5234](https://github.com/rust-lang/rustfmt/issues/5234). For example:
Expand Down
28 changes: 19 additions & 9 deletions 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
Expand Up @@ -35,7 +35,7 @@ generic-simd = ["bytecount/generic-simd"]
[dependencies]
annotate-snippets = { version = "0.9", features = ["color"] }
anyhow = "1.0"
bytecount = "0.6.3"
bytecount = "0.6.4"
cargo_metadata = "0.15.4"
clap = { version = "4.4.2", features = ["derive"] }
clap-cargo = "0.12.0"
Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[toolchain]
channel = "nightly-2023-07-01"
channel = "nightly-2023-10-22"
components = ["llvm-tools", "rustc-dev"]
16 changes: 6 additions & 10 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ pub(crate) fn format_expr(
ast::ExprKind::Tup(ref items) => {
rewrite_tuple(context, items.iter(), expr.span, shape, items.len() == 1)
}
ast::ExprKind::Let(ref pat, ref expr, _span) => rewrite_let(context, shape, pat, expr),
ast::ExprKind::Let(ref pat, ref expr, _span, _) => rewrite_let(context, shape, pat, expr),
ast::ExprKind::If(..)
| ast::ExprKind::ForLoop(..)
| ast::ExprKind::Loop(..)
Expand Down Expand Up @@ -261,7 +261,7 @@ pub(crate) fn format_expr(
shape,
SeparatorPlace::Back,
),
ast::ExprKind::Index(ref expr, ref index) => {
ast::ExprKind::Index(ref expr, ref index, _) => {
rewrite_index(&**expr, &**index, context, shape)
}
ast::ExprKind::Repeat(ref expr, ref repeats) => rewrite_pair(
Expand Down Expand Up @@ -662,7 +662,7 @@ struct ControlFlow<'a> {

fn extract_pats_and_cond(expr: &ast::Expr) -> (Option<&ast::Pat>, &ast::Expr) {
match expr.kind {
ast::ExprKind::Let(ref pat, ref cond, _) => (Some(pat), cond),
ast::ExprKind::Let(ref pat, ref cond, _, _) => (Some(pat), cond),
_ => (None, expr),
}
}
Expand Down Expand Up @@ -1339,7 +1339,7 @@ pub(crate) fn is_simple_expr(expr: &ast::Expr) -> bool {
| ast::ExprKind::Field(ref expr, _)
| ast::ExprKind::Try(ref expr)
| ast::ExprKind::Unary(_, ref expr) => is_simple_expr(expr),
ast::ExprKind::Index(ref lhs, ref rhs) => is_simple_expr(lhs) && is_simple_expr(rhs),
ast::ExprKind::Index(ref lhs, ref rhs, _) => is_simple_expr(lhs) && is_simple_expr(rhs),
ast::ExprKind::Repeat(ref lhs, ref rhs) => {
is_simple_expr(lhs) && is_simple_expr(&*rhs.value)
}
Expand Down Expand Up @@ -1379,12 +1379,8 @@ pub(crate) fn can_be_overflowed_expr(
|| (context.use_block_indent() && args_len == 1)
}
ast::ExprKind::MacCall(ref mac) => {
match (
rustc_ast::ast::MacDelimiter::from_token(mac.args.delim.to_token()),
context.config.overflow_delimited_expr(),
) {
(Some(ast::MacDelimiter::Bracket), true)
| (Some(ast::MacDelimiter::Brace), true) => true,
match (mac.args.delim, context.config.overflow_delimited_expr()) {
(Delimiter::Bracket, true) | (Delimiter::Brace, true) => true,
_ => context.use_block_indent() && args_len == 1,
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2608,7 +2608,8 @@ fn rewrite_fn_base(
if where_clause_str.is_empty() {
if let ast::FnRetTy::Default(ret_span) = fd.output {
match recover_missing_comment_in_span(
mk_sp(params_span.hi(), ret_span.hi()),
// from after the closing paren to right before block or semicolon
mk_sp(ret_span.lo(), span.hi()),
shape,
context,
last_line_width(&result),
Expand Down
28 changes: 14 additions & 14 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use std::collections::HashMap;
use std::panic::{catch_unwind, AssertUnwindSafe};

use rustc_ast::token::{BinOpToken, Delimiter, Token, TokenKind};
use rustc_ast::tokenstream::{TokenStream, TokenTree, TokenTreeCursor};
use rustc_ast::tokenstream::{RefTokenTreeCursor, TokenStream, TokenTree};
use rustc_ast::{ast, ptr};
use rustc_ast_pretty::pprust;
use rustc_span::{
Expand Down Expand Up @@ -411,7 +411,7 @@ pub(crate) fn rewrite_macro_def(
}

let ts = def.body.tokens.clone();
let mut parser = MacroParser::new(ts.into_trees());
let mut parser = MacroParser::new(ts.trees());
let parsed_def = match parser.parse() {
Some(def) => def,
None => return snippet,
Expand Down Expand Up @@ -760,9 +760,9 @@ impl MacroArgParser {
self.buf.clear();
}

fn add_meta_variable(&mut self, iter: &mut TokenTreeCursor) -> Option<()> {
fn add_meta_variable(&mut self, iter: &mut RefTokenTreeCursor<'_>) -> Option<()> {
match iter.next() {
Some(TokenTree::Token(
Some(&TokenTree::Token(
Token {
kind: TokenKind::Ident(name, _),
..
Expand Down Expand Up @@ -792,7 +792,7 @@ impl MacroArgParser {
&mut self,
inner: Vec<ParsedMacroArg>,
delim: Delimiter,
iter: &mut TokenTreeCursor,
iter: &mut RefTokenTreeCursor<'_>,
) -> Option<()> {
let mut buffer = String::new();
let mut first = true;
Expand Down Expand Up @@ -892,11 +892,11 @@ impl MacroArgParser {

/// Returns a collection of parsed macro def's arguments.
fn parse(mut self, tokens: TokenStream) -> Option<Vec<ParsedMacroArg>> {
let mut iter = tokens.into_trees();
let mut iter = tokens.trees();

while let Some(tok) = iter.next() {
match tok {
TokenTree::Token(
&TokenTree::Token(
Token {
kind: TokenKind::Dollar,
span,
Expand Down Expand Up @@ -925,7 +925,7 @@ impl MacroArgParser {
self.add_meta_variable(&mut iter)?;
}
TokenTree::Token(ref t, _) => self.update_buffer(t),
TokenTree::Delimited(_delimited_span, delimited, ref tts) => {
&TokenTree::Delimited(_delimited_span, delimited, ref tts) => {
if !self.buf.is_empty() {
if next_space(&self.last_tok.kind) == SpaceState::Always {
self.add_separator();
Expand Down Expand Up @@ -1143,12 +1143,12 @@ pub(crate) fn macro_style(mac: &ast::MacCall, context: &RewriteContext<'_>) -> D

// A very simple parser that just parses a macros 2.0 definition into its branches.
// Currently we do not attempt to parse any further than that.
struct MacroParser {
toks: TokenTreeCursor,
struct MacroParser<'a> {
toks: RefTokenTreeCursor<'a>,
}

impl MacroParser {
const fn new(toks: TokenTreeCursor) -> Self {
impl<'a> MacroParser<'a> {
const fn new(toks: RefTokenTreeCursor<'a>) -> Self {
Self { toks }
}

Expand All @@ -1167,9 +1167,9 @@ impl MacroParser {
let tok = self.toks.next()?;
let (lo, args_paren_kind) = match tok {
TokenTree::Token(..) => return None,
TokenTree::Delimited(delimited_span, d, _) => (delimited_span.open.lo(), d),
&TokenTree::Delimited(delimited_span, d, _) => (delimited_span.open.lo(), d),
};
let args = TokenStream::new(vec![tok]);
let args = TokenStream::new(vec![tok.clone()]);
match self.toks.next()? {
TokenTree::Token(
Token {
Expand Down
2 changes: 1 addition & 1 deletion src/matches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ fn can_flatten_block_around_this(body: &ast::Expr) -> bool {
ast::ExprKind::AddrOf(_, _, ref expr)
| ast::ExprKind::Try(ref expr)
| ast::ExprKind::Unary(_, ref expr)
| ast::ExprKind::Index(ref expr, _)
| ast::ExprKind::Index(ref expr, _, _)
| ast::ExprKind::Cast(ref expr, _) => can_flatten_block_around_this(expr),
_ => false,
}
Expand Down
4 changes: 2 additions & 2 deletions src/pairs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ impl<'a, 'b> PairList<'a, 'b, ast::Expr> {
fn let_chain_count(&self) -> usize {
self.list
.iter()
.filter(|(expr, _)| matches!(expr.kind, ast::ExprKind::Let(_, _, _)))
.filter(|(expr, _)| matches!(expr.kind, ast::ExprKind::Let(..)))
.count()
}

Expand All @@ -284,7 +284,7 @@ impl<'a, 'b> PairList<'a, 'b, ast::Expr> {
}

let fist_item_is_ident = is_ident(self.list[0].0);
let second_item_is_let_chain = matches!(self.list[1].0.kind, ast::ExprKind::Let(_, _, _));
let second_item_is_let_chain = matches!(self.list[1].0.kind, ast::ExprKind::Let(..));

fist_item_is_ident && second_item_is_let_chain
}
Expand Down
2 changes: 1 addition & 1 deletion src/parse/macros/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ fn parse_macro_arg<'a, 'b: 'a>(parser: &'a mut Parser<'b>) -> Option<MacroArg> {
);
parse_macro_arg!(
Pat,
|parser: &mut rustc_parse::parser::Parser<'b>| parser.parse_pat_no_top_alt(None),
|parser: &mut rustc_parse::parser::Parser<'b>| parser.parse_pat_no_top_alt(None, None),
|x: ptr::P<ast::Pat>| Some(x)
);
// `parse_item` returns `Option<ptr::P<ast::Item>>`.
Expand Down
49 changes: 17 additions & 32 deletions src/parse/session.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::path::Path;
use std::sync::atomic::{AtomicBool, Ordering};

use rustc_data_structures::sync::{Lrc, Send};
use rustc_errors::emitter::{Emitter, EmitterWriter};
use rustc_data_structures::sync::{IntoDynSyncSend, Lrc};
use rustc_errors::emitter::{DynEmitter, Emitter, EmitterWriter};
use rustc_errors::translation::Translate;
use rustc_errors::{ColorConfig, Diagnostic, Handler, Level as DiagnosticLevel, TerminalUrl};
use rustc_errors::{ColorConfig, Diagnostic, Handler, Level as DiagnosticLevel};
use rustc_session::parse::ParseSess as RawParseSess;
use rustc_span::{
source_map::{FilePathMapping, SourceMap},
Expand Down Expand Up @@ -48,15 +48,15 @@ impl Emitter for SilentEmitter {
fn emit_diagnostic(&mut self, _db: &Diagnostic) {}
}

fn silent_emitter() -> Box<dyn Emitter + Send> {
fn silent_emitter() -> Box<DynEmitter> {
Box::new(SilentEmitter {})
}

/// Emit errors against every files expect ones specified in the `ignore_path_set`.
struct SilentOnIgnoredFilesEmitter {
ignore_path_set: Lrc<IgnorePathSet>,
ignore_path_set: IntoDynSyncSend<Lrc<IgnorePathSet>>,
source_map: Lrc<SourceMap>,
emitter: Box<dyn Emitter + Send>,
emitter: Box<DynEmitter>,
has_non_ignorable_parser_errors: bool,
can_reset: Lrc<AtomicBool>,
}
Expand Down Expand Up @@ -139,30 +139,15 @@ fn default_handler(
rustc_driver::DEFAULT_LOCALE_RESOURCES.to_vec(),
false,
);
Box::new(EmitterWriter::stderr(
emit_color,
Some(source_map.clone()),
None,
fallback_bundle,
false,
false,
None,
false,
false,
TerminalUrl::No,
))
Box::new(EmitterWriter::stderr(emit_color, fallback_bundle).sm(Some(source_map.clone())))
};
Handler::with_emitter(
true,
None,
Box::new(SilentOnIgnoredFilesEmitter {
has_non_ignorable_parser_errors: false,
source_map,
emitter,
ignore_path_set,
can_reset,
}),
)
Handler::with_emitter(Box::new(SilentOnIgnoredFilesEmitter {
has_non_ignorable_parser_errors: false,
source_map,
emitter,
ignore_path_set: IntoDynSyncSend(ignore_path_set),
can_reset,
}))
}

impl ParseSess {
Expand Down Expand Up @@ -233,7 +218,7 @@ impl ParseSess {
}

pub(crate) fn set_silent_emitter(&mut self) {
self.parse_sess.span_diagnostic = Handler::with_emitter(true, None, silent_emitter());
self.parse_sess.span_diagnostic = Handler::with_emitter(silent_emitter());
}

pub(crate) fn span_to_filename(&self, span: Span) -> FileName {
Expand Down Expand Up @@ -283,7 +268,7 @@ impl ParseSess {
let source_file = self.parse_sess.source_map().lookup_char_pos(span.lo()).file;
SnippetProvider::new(
source_file.start_pos,
source_file.end_pos,
source_file.end_position(),
Lrc::clone(source_file.src.as_ref().unwrap()),
)
}
Expand Down Expand Up @@ -410,7 +395,7 @@ mod tests {
has_non_ignorable_parser_errors: false,
source_map,
emitter: Box::new(emitter_writer),
ignore_path_set,
ignore_path_set: IntoDynSyncSend(ignore_path_set),
can_reset,
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,10 @@ fn handle_result(

// Ignore LF and CRLF difference for Windows.
if !string_eq_ignore_newline_repr(&fmt_text, &text) {
if std::env::var_os("RUSTC_BLESS").is_some_and(|v| v != "0") {
std::fs::write(file_name, fmt_text).unwrap();
continue;
}
let diff = make_diff(&text, &fmt_text, DIFF_CONTEXT_SIZE);
assert!(
!diff.is_empty(),
Expand Down
Loading

0 comments on commit 0bb2acf

Please sign in to comment.