Skip to content

Commit

Permalink
Merge pull request #5036 from calebcartwright/1.4.38-subtree
Browse files Browse the repository at this point in the history
sync subtree
  • Loading branch information
calebcartwright authored Oct 20, 2021
2 parents 1ae5c35 + 5f79583 commit b9178dc
Show file tree
Hide file tree
Showing 21 changed files with 69 additions and 149 deletions.
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@

name = "rustfmt-nightly"
version = "1.4.37"
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"
readme = "README.md"
license = "Apache-2.0/MIT"
build = "build.rs"
categories = ["development-tools"]
edition = "2018"
edition = "2021"

[[bin]]
name = "rustfmt"
Expand Down
1 change: 0 additions & 1 deletion config_proc_macro/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[package]
name = "rustfmt-config_proc_macro"
version = "0.2.0"
authors = ["topecongiro <seuchida@gmail.com>"]
edition = "2018"
description = "A collection of procedural macros for rustfmt"
license = "Apache-2.0/MIT"
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-2021-07-23"
channel = "nightly-2021-10-20"
components = ["rustc-dev"]
1 change: 1 addition & 0 deletions src/closures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ fn rewrite_closure_with_block(
.first()
.map(|attr| attr.span.to(body.span))
.unwrap_or(body.span),
could_be_bare_literal: false,
};
let block = crate::expr::rewrite_block_with_visitor(
context,
Expand Down
2 changes: 1 addition & 1 deletion src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,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
20 changes: 13 additions & 7 deletions src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::cmp::{max, min, Ordering};
use regex::Regex;
use rustc_ast::visit;
use rustc_ast::{ast, ptr};
use rustc_span::{symbol, BytePos, Span};
use rustc_span::{symbol, BytePos, Span, DUMMY_SP};

use crate::attr::filter_inline_attrs;
use crate::comment::{
Expand All @@ -31,7 +31,12 @@ use crate::stmt::Stmt;
use crate::utils::*;
use crate::vertical::rewrite_with_alignment;
use crate::visitor::FmtVisitor;
use crate::DEFAULT_VISIBILITY;

const DEFAULT_VISIBILITY: ast::Visibility = ast::Visibility {
kind: ast::VisibilityKind::Inherited,
span: DUMMY_SP,
tokens: None,
};

fn type_annotation_separator(config: &Config) -> &str {
colon_spaces(config)
Expand All @@ -48,7 +53,7 @@ impl Rewrite for ast::Local {

skip_out_of_file_lines_range!(context, self.span);

if contains_skip(&self.attrs) {
if contains_skip(&self.attrs) || matches!(self.kind, ast::LocalKind::InitElse(..)) {
return None;
}

Expand Down Expand Up @@ -97,7 +102,7 @@ impl Rewrite for ast::Local {
infix.push_str(&rewrite);
}

if self.init.is_some() {
if self.kind.init().is_some() {
infix.push_str(" =");
}

Expand All @@ -106,11 +111,12 @@ impl Rewrite for ast::Local {

result.push_str(&infix);

if let Some(ref ex) = self.init {
if let Some((init, _els)) = self.kind.init_else_opt() {
// 1 = trailing semicolon;
let nested_shape = shape.sub_width(1)?;

result = rewrite_assign_rhs(context, result, &**ex, nested_shape)?;
result = rewrite_assign_rhs(context, result, init, nested_shape)?;
// todo else
}

result.push(';');
Expand Down Expand Up @@ -972,7 +978,7 @@ impl<'a> StructParts<'a> {
format_header(context, self.prefix, self.ident, self.vis, offset)
}

pub(crate) fn from_variant(variant: &'a ast::Variant) -> Self {
fn from_variant(variant: &'a ast::Variant) -> Self {
StructParts {
prefix: "",
ident: variant.ident,
Expand Down
7 changes: 1 addition & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use std::path::PathBuf;
use std::rc::Rc;

use rustc_ast::ast;
use rustc_span::{symbol, DUMMY_SP};
use rustc_span::symbol;
use thiserror::Error;

use crate::comment::LineClasses;
Expand Down Expand Up @@ -96,11 +96,6 @@ mod types;
mod vertical;
pub(crate) mod visitor;

const DEFAULT_VISIBILITY: ast::Visibility = ast::Visibility {
kind: ast::VisibilityKind::Inherited,
span: DUMMY_SP,
tokens: None,
};
/// The various errors that can occur during formatting. Note that not all of
/// these can currently be propagated to clients.
#[derive(Error, Debug)]
Expand Down
46 changes: 14 additions & 32 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ fn rewrite_macro_inner(
handle_vec_semi(context, shape, arg_vec, macro_name, style)
} else {
// If we are rewriting `vec!` macro or other special macros,
// then we can rewrite this as an usual array literal.
// then we can rewrite this as a usual array literal.
// Otherwise, we must preserve the original existence of trailing comma.
let macro_name = &macro_name.as_str();
let mut force_trailing_comma = if trailing_comma {
Expand Down Expand Up @@ -762,7 +762,6 @@ impl MacroArgKind {
#[derive(Debug, Clone)]
struct ParsedMacroArg {
kind: MacroArgKind,
span: Span,
}

impl ParsedMacroArg {
Expand All @@ -780,14 +779,10 @@ impl ParsedMacroArg {
struct MacroArgParser {
/// Either a name of the next metavariable, a separator, or junk.
buf: String,
/// The start position on the current buffer.
lo: BytePos,
/// The first token of the current buffer.
start_tok: Token,
/// `true` if we are parsing a metavariable or a repeat.
is_meta_var: bool,
/// The position of the last token.
hi: BytePos,
/// The last token parsed.
last_tok: Token,
/// Holds the parsed arguments.
Expand All @@ -807,8 +802,6 @@ fn last_tok(tt: &TokenTree) -> Token {
impl MacroArgParser {
fn new() -> MacroArgParser {
MacroArgParser {
lo: BytePos(0),
hi: BytePos(0),
buf: String::new(),
is_meta_var: false,
last_tok: Token {
Expand All @@ -824,7 +817,6 @@ impl MacroArgParser {
}

fn set_last_tok(&mut self, tok: &TokenTree) {
self.hi = tok.span().hi();
self.last_tok = last_tok(tok);
}

Expand All @@ -836,7 +828,6 @@ impl MacroArgParser {
};
self.result.push(ParsedMacroArg {
kind: MacroArgKind::Separator(self.buf.clone(), prefix),
span: mk_sp(self.lo, self.hi),
});
self.buf.clear();
}
Expand All @@ -849,7 +840,6 @@ impl MacroArgParser {
};
self.result.push(ParsedMacroArg {
kind: MacroArgKind::Other(self.buf.clone(), prefix),
span: mk_sp(self.lo, self.hi),
});
self.buf.clear();
}
Expand All @@ -858,11 +848,10 @@ impl MacroArgParser {
match iter.next() {
Some(TokenTree::Token(Token {
kind: TokenKind::Ident(name, _),
span,
..
})) => {
self.result.push(ParsedMacroArg {
kind: MacroArgKind::MetaVariable(name, self.buf.clone()),
span: mk_sp(self.lo, span.hi()),
});

self.buf.clear();
Expand All @@ -873,10 +862,9 @@ impl MacroArgParser {
}
}

fn add_delimited(&mut self, inner: Vec<ParsedMacroArg>, delim: DelimToken, span: Span) {
fn add_delimited(&mut self, inner: Vec<ParsedMacroArg>, delim: DelimToken) {
self.result.push(ParsedMacroArg {
kind: MacroArgKind::Delimited(delim, inner),
span,
});
}

Expand All @@ -886,19 +874,15 @@ impl MacroArgParser {
inner: Vec<ParsedMacroArg>,
delim: DelimToken,
iter: &mut Cursor,
span: Span,
) -> Option<()> {
let mut buffer = String::new();
let mut first = true;
let mut lo = span.lo();
let mut hi = span.hi();

// Parse '*', '+' or '?.
for tok in iter {
self.set_last_tok(&tok);
if first {
first = false;
lo = tok.span().lo();
}

match tok {
Expand All @@ -918,7 +902,6 @@ impl MacroArgParser {
}
TokenTree::Token(ref t) => {
buffer.push_str(&pprust::token_to_string(&t));
hi = t.span.hi();
}
_ => return None,
}
Expand All @@ -930,20 +913,17 @@ impl MacroArgParser {
} else {
Some(Box::new(ParsedMacroArg {
kind: MacroArgKind::Other(buffer, "".to_owned()),
span: mk_sp(lo, hi),
}))
};

self.result.push(ParsedMacroArg {
kind: MacroArgKind::Repeat(delim, inner, another, self.last_tok.clone()),
span: mk_sp(self.lo, self.hi),
});
Some(())
}

fn update_buffer(&mut self, t: &Token) {
if self.buf.is_empty() {
self.lo = t.span.lo();
self.start_tok = t.clone();
} else {
let needs_space = match next_space(&self.last_tok.kind) {
Expand Down Expand Up @@ -999,7 +979,6 @@ impl MacroArgParser {

// Start keeping the name of this metavariable in the buffer.
self.is_meta_var = true;
self.lo = span.lo();
self.start_tok = Token {
kind: TokenKind::Dollar,
span,
Expand All @@ -1012,7 +991,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 All @@ -1022,16 +1001,14 @@ impl MacroArgParser {
}

// Parse the stuff inside delimiters.
let mut parser = MacroArgParser::new();
parser.lo = delimited_span.open.lo();
let parser = MacroArgParser::new();
let delimited_arg = parser.parse(tts.clone())?;

let span = delimited_span.entire();
if self.is_meta_var {
self.add_repeat(delimited_arg, delimited, &mut iter, span)?;
self.add_repeat(delimited_arg, delimited, &mut iter)?;
self.is_meta_var = false;
} else {
self.add_delimited(delimited_arg, delimited, span);
self.add_delimited(delimited_arg, delimited);
}
}
}
Expand Down Expand Up @@ -1270,7 +1247,12 @@ impl MacroParser {
let data = delimited_span.entire().data();
(
data.hi,
Span::new(data.lo + BytePos(1), data.hi - BytePos(1), data.ctxt),
Span::new(
data.lo + BytePos(1),
data.hi - BytePos(1),
data.ctxt,
data.parent,
),
delimited_span.entire(),
)
}
Expand Down Expand Up @@ -1417,7 +1399,7 @@ impl MacroBranch {
}
}

/// Format `lazy_static!` from https://crates.io/crates/lazy_static.
/// Format `lazy_static!` from <https://crates.io/crates/lazy_static>.
///
/// # Expected syntax
///
Expand Down
2 changes: 0 additions & 2 deletions src/modules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ type FileModMap<'ast> = BTreeMap<FileName, Module<'ast>>;
pub(crate) struct Module<'a> {
ast_mod_kind: Option<Cow<'a, ast::ModKind>>,
pub(crate) items: Cow<'a, Vec<rustc_ast::ptr::P<ast::Item>>>,
attrs: Cow<'a, Vec<ast::Attribute>>,
inner_attr: Vec<ast::Attribute>,
pub(crate) span: Span,
}
Expand All @@ -46,7 +45,6 @@ impl<'a> Module<'a> {
.collect();
Module {
items: mod_items,
attrs: mod_attrs,
inner_attr,
span: mod_span,
ast_mod_kind,
Expand Down
4 changes: 2 additions & 2 deletions src/patterns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ use crate::utils::{format_mutability, mk_sp, mk_sp_lo_plus_one, rewrite_ident};
/// Returns `true` if the given pattern is "short".
/// A short pattern is defined by the following grammar:
///
/// [small, ntp]:
/// `[small, ntp]`:
/// - single token
/// - `&[single-line, ntp]`
///
/// [small]:
/// `[small]`:
/// - `[small, ntp]`
/// - unary tuple constructor `([small, ntp])`
/// - `&[small]`
Expand Down
2 changes: 1 addition & 1 deletion src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ pub(crate) fn rewrite_string<'a>(
wrap_str(result, fmt.config.max_width(), fmt.shape)
}

/// Returns the index to the end of the URL if the split at index of the given string includes an
/// Returns the index to the end of the URL if the split at index of the given string includes a
/// URL or alike. Otherwise, returns `None`.
fn detect_url(s: &[&str], index: usize) -> Option<usize> {
let start = match s[..=index].iter().rposition(|g| is_whitespace(g)) {
Expand Down
1 change: 1 addition & 0 deletions src/syntux/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ mod tests {
suggestions: vec![],
span: span.unwrap_or_else(MultiSpan::new),
sort_span: DUMMY_SP,
is_lint: false,
}
}

Expand Down
Loading

0 comments on commit b9178dc

Please sign in to comment.