Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into issue-3779
Browse files Browse the repository at this point in the history
  • Loading branch information
calebcartwright committed Sep 7, 2019
2 parents d3a8a97 + 6fec3a6 commit 9ad9df0
Show file tree
Hide file tree
Showing 17 changed files with 256 additions and 137 deletions.
20 changes: 19 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,27 @@

## [Unreleased]

## [1.4.7] 2019-09-06

### Added

- Add `--config` command line option.

### Changed

- Update `rustc-ap-*` crates to 581.0.0.
- rustfmt now do not warn against trailing whitespaces inside macro calls.

### Fixed

- Fix `merge_imports` generating invalid code.
- Fix removing discriminant values on enum variants.
- Fix modules defined inside `cfg_if!` not being formatted.
- Fix minor formatting issues.

## [1.4.6] 2019-08-28

## Added
### Added

- Add `--message-format` command line option to `cargo-fmt`.
- Add `-l,--files-with-diff` command line option to `rustfmt`.
Expand Down
131 changes: 87 additions & 44 deletions Cargo.lock

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]

name = "rustfmt-nightly"
version = "1.4.6"
version = "1.4.7"
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 Expand Up @@ -48,9 +48,9 @@ env_logger = "0.6"
getopts = "0.2"
derive-new = "0.5"
cargo_metadata = "0.8"
rustc-ap-rustc_target = "546.0.0"
rustc-ap-syntax = "546.0.0"
rustc-ap-syntax_pos = "546.0.0"
rustc-ap-rustc_target = "581.0.0"
rustc-ap-syntax = "581.0.0"
rustc-ap-syntax_pos = "581.0.0"
failure = "0.1.3"
bytecount = "0.5"
unicode-width = "0.1.5"
Expand Down
2 changes: 1 addition & 1 deletion src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ impl Rewrite for ast::Attribute {

let literal_str = literal.as_str();
let doc_comment_formatter =
DocCommentFormatter::new(literal_str.get(), comment_style);
DocCommentFormatter::new(&*literal_str, comment_style);
let doc_comment = format!("{}", doc_comment_formatter);
return rewrite_doc_comment(
&doc_comment,
Expand Down
4 changes: 2 additions & 2 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1316,8 +1316,8 @@ pub(crate) fn can_be_overflowed_expr(
context.config.overflow_delimited_expr()
|| (context.use_block_indent() && args_len == 1)
}
ast::ExprKind::Mac(ref macro_) => {
match (macro_.node.delim, context.config.overflow_delimited_expr()) {
ast::ExprKind::Mac(ref mac) => {
match (mac.delim, context.config.overflow_delimited_expr()) {
(ast::MacDelimiter::Bracket, true) | (ast::MacDelimiter::Brace, true) => true,
_ => context.use_block_indent() && args_len == 1,
}
Expand Down
92 changes: 59 additions & 33 deletions src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -473,8 +473,8 @@ impl<'a> FmtVisitor<'a> {
let discr_ident_lens: Vec<usize> = enum_def
.variants
.iter()
.filter(|var| var.node.disr_expr.is_some())
.map(|var| rewrite_ident(&self.get_context(), var.node.ident).len())
.filter(|var| var.disr_expr.is_some())
.map(|var| rewrite_ident(&self.get_context(), var.ident).len())
.collect();
// cut the list at the point of longest discrim shorter than the threshold
// All of the discrims under the threshold will get padded, and all above - left as is.
Expand All @@ -491,8 +491,8 @@ impl<'a> FmtVisitor<'a> {
"}",
",",
|f| {
if !f.node.attrs.is_empty() {
f.node.attrs[0].span.lo()
if !f.attrs.is_empty() {
f.attrs[0].span.lo()
} else {
f.span.lo()
}
Expand Down Expand Up @@ -533,34 +533,33 @@ impl<'a> FmtVisitor<'a> {
one_line_width: usize,
pad_discrim_ident_to: usize,
) -> Option<String> {
if contains_skip(&field.node.attrs) {
let lo = field.node.attrs[0].span.lo();
if contains_skip(&field.attrs) {
let lo = field.attrs[0].span.lo();
let span = mk_sp(lo, field.span.hi());
return Some(self.snippet(span).to_owned());
}

let context = self.get_context();
// 1 = ','
let shape = self.shape().sub_width(1)?;
let attrs_str = field.node.attrs.rewrite(&context, shape)?;
let attrs_str = field.attrs.rewrite(&context, shape)?;
let lo = field
.node
.attrs
.last()
.map_or(field.span.lo(), |attr| attr.span.hi());
let span = mk_sp(lo, field.span.lo());

let variant_body = match field.node.data {
let variant_body = match field.data {
ast::VariantData::Tuple(..) | ast::VariantData::Struct(..) => format_struct(
&context,
&StructParts::from_variant(field),
self.block_indent,
Some(one_line_width),
)?,
ast::VariantData::Unit(..) => rewrite_ident(&context, field.node.ident).to_owned(),
ast::VariantData::Unit(..) => rewrite_ident(&context, field.ident).to_owned(),
};

let variant_body = if let Some(ref expr) = field.node.disr_expr {
let variant_body = if let Some(ref expr) = field.disr_expr {
let lhs = format!("{:1$} =", variant_body, pad_discrim_ident_to);
rewrite_assign_rhs_with(
&context,
Expand All @@ -585,27 +584,27 @@ impl<'a> FmtVisitor<'a> {
buffer.push((self.buffer.clone(), item.clone()));
self.buffer.clear();
}
// type -> existential -> const -> macro -> method
// type -> opaque -> const -> macro -> method
use crate::ast::ImplItemKind::*;
fn need_empty_line(a: &ast::ImplItemKind, b: &ast::ImplItemKind) -> bool {
match (a, b) {
(Type(..), Type(..))
(TyAlias(..), TyAlias(..))
| (Const(..), Const(..))
| (Existential(..), Existential(..)) => false,
| (OpaqueTy(..), OpaqueTy(..)) => false,
_ => true,
}
}

buffer.sort_by(|(_, a), (_, b)| match (&a.node, &b.node) {
(Type(..), Type(..))
(TyAlias(..), TyAlias(..))
| (Const(..), Const(..))
| (Macro(..), Macro(..))
| (Existential(..), Existential(..)) => a.ident.as_str().cmp(&b.ident.as_str()),
| (OpaqueTy(..), OpaqueTy(..)) => a.ident.as_str().cmp(&b.ident.as_str()),
(Method(..), Method(..)) => a.span.lo().cmp(&b.span.lo()),
(Type(..), _) => Ordering::Less,
(_, Type(..)) => Ordering::Greater,
(Existential(..), _) => Ordering::Less,
(_, Existential(..)) => Ordering::Greater,
(TyAlias(..), _) => Ordering::Less,
(_, TyAlias(..)) => Ordering::Greater,
(OpaqueTy(..), _) => Ordering::Less,
(_, OpaqueTy(..)) => Ordering::Greater,
(Const(..), _) => Ordering::Less,
(_, Const(..)) => Ordering::Greater,
(Macro(..), _) => Ordering::Less,
Expand Down Expand Up @@ -920,9 +919,9 @@ impl<'a> StructParts<'a> {
fn from_variant(variant: &'a ast::Variant) -> Self {
StructParts {
prefix: "",
ident: variant.node.ident,
ident: variant.ident,
vis: &DEFAULT_VISIBILITY,
def: &variant.node.data,
def: &variant.data,
generics: None,
span: variant.span,
}
Expand Down Expand Up @@ -1517,7 +1516,7 @@ pub(crate) fn rewrite_type_alias(
rewrite_type_item(context, indent, "type", " =", ident, ty, generics, vis)
}

pub(crate) fn rewrite_existential_type(
pub(crate) fn rewrite_opaque_type(
context: &RewriteContext<'_>,
indent: Indent,
ident: ast::Ident,
Expand All @@ -1528,8 +1527,8 @@ pub(crate) fn rewrite_existential_type(
rewrite_type_item(
context,
indent,
"existential type",
":",
"type",
" =",
ident,
generic_bounds,
generics,
Expand Down Expand Up @@ -1786,15 +1785,42 @@ pub(crate) fn rewrite_associated_type(
}
}

pub(crate) fn rewrite_existential_impl_type(
struct OpaqueType<'a> {
bounds: &'a ast::GenericBounds,
}

impl<'a> Rewrite for OpaqueType<'a> {
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
let shape = shape.offset_left(5)?; // `impl `
self.bounds
.rewrite(context, shape)
.map(|s| format!("impl {}", s))
}
}

pub(crate) fn rewrite_opaque_impl_type(
context: &RewriteContext<'_>,
ident: ast::Ident,
generics: &ast::Generics,
generic_bounds: &ast::GenericBounds,
indent: Indent,
) -> Option<String> {
rewrite_associated_type(ident, None, generics, Some(generic_bounds), context, indent)
.map(|s| format!("existential {}", s))
let ident_str = rewrite_ident(context, ident);
// 5 = "type "
let generics_shape = Shape::indented(indent, context.config).offset_left(5)?;
let generics_str = rewrite_generics(context, ident_str, generics, generics_shape)?;
let prefix = format!("type {} =", generics_str);
let rhs = OpaqueType {
bounds: generic_bounds,
};

rewrite_assign_rhs(
context,
&prefix,
&rhs,
Shape::indented(indent, context.config).sub_width(1)?,
)
.map(|s| s + ";")
}

pub(crate) fn rewrite_associated_impl_type(
Expand Down Expand Up @@ -1877,7 +1903,7 @@ fn get_missing_arg_comments(
(comment_before_colon, comment_after_colon)
}

impl Rewrite for ast::Arg {
impl Rewrite for ast::Param {
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
if let Some(ref explicit_self) = self.to_self() {
rewrite_explicit_self(context, explicit_self)
Expand Down Expand Up @@ -1941,23 +1967,23 @@ fn rewrite_explicit_self(
}
}

pub(crate) fn span_lo_for_arg(arg: &ast::Arg) -> BytePos {
pub(crate) fn span_lo_for_arg(arg: &ast::Param) -> BytePos {
if is_named_arg(arg) {
arg.pat.span.lo()
} else {
arg.ty.span.lo()
}
}

pub(crate) fn span_hi_for_arg(context: &RewriteContext<'_>, arg: &ast::Arg) -> BytePos {
pub(crate) fn span_hi_for_arg(context: &RewriteContext<'_>, arg: &ast::Param) -> BytePos {
match arg.ty.node {
ast::TyKind::Infer if context.snippet(arg.ty.span) == "_" => arg.ty.span.hi(),
ast::TyKind::Infer if is_named_arg(arg) => arg.pat.span.hi(),
_ => arg.ty.span.hi(),
}
}

pub(crate) fn is_named_arg(arg: &ast::Arg) -> bool {
pub(crate) fn is_named_arg(arg: &ast::Param) -> bool {
if let ast::PatKind::Ident(_, ident, _) = arg.pat.node {
ident.name != symbol::kw::Invalid
} else {
Expand Down Expand Up @@ -2349,7 +2375,7 @@ impl WhereClauseOption {

fn rewrite_args(
context: &RewriteContext<'_>,
args: &[ast::Arg],
args: &[ast::Param],
one_line_budget: usize,
multi_line_budget: usize,
indent: Indent,
Expand Down
15 changes: 7 additions & 8 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ pub(crate) fn rewrite_macro(
) -> Option<String> {
let should_skip = context
.skip_context
.skip_macro(&context.snippet(mac.node.path.span).to_owned());
.skip_macro(&context.snippet(mac.path.span).to_owned());
if should_skip {
None
} else {
Expand Down Expand Up @@ -235,7 +235,7 @@ fn check_keyword<'a, 'b: 'a>(parser: &'a mut Parser<'b>) -> Option<MacroArg> {
{
parser.bump();
let macro_arg =
MacroArg::Keyword(ast::Ident::with_empty_ctxt(keyword), parser.prev_span);
MacroArg::Keyword(ast::Ident::with_dummy_span(keyword), parser.prev_span);
return Some(macro_arg);
}
}
Expand All @@ -259,15 +259,15 @@ fn rewrite_macro_inner(

let original_style = macro_style(mac, context);

let macro_name = rewrite_macro_name(context, &mac.node.path, extra_ident);
let macro_name = rewrite_macro_name(context, &mac.path, extra_ident);

let style = if FORCED_BRACKET_MACROS.contains(&&macro_name[..]) && !is_nested_macro {
DelimToken::Bracket
} else {
original_style
};

let ts: TokenStream = mac.node.stream();
let ts: TokenStream = mac.stream();
let has_comment = contains_comment(context.snippet(mac.span));
if ts.is_empty() && !has_comment {
return match style {
Expand Down Expand Up @@ -1190,8 +1190,8 @@ fn next_space(tok: &TokenKind) -> SpaceState {
/// when the macro is not an instance of `try!` (or parsing the inner expression
/// failed).
pub(crate) fn convert_try_mac(mac: &ast::Mac, context: &RewriteContext<'_>) -> Option<ast::Expr> {
if &mac.node.path.to_string() == "try" {
let ts: TokenStream = mac.node.tts.clone();
if &mac.path.to_string() == "try" {
let ts: TokenStream = mac.tts.clone();
let mut parser = new_parser_from_tts(context.parse_session, ts.trees().collect());

Some(ast::Expr {
Expand Down Expand Up @@ -1532,7 +1532,7 @@ fn rewrite_macro_with_items(
Some(result)
}

const RUST_KW: [Symbol; 60] = [
const RUST_KW: [Symbol; 59] = [
kw::PathRoot,
kw::DollarCrate,
kw::Underscore,
Expand Down Expand Up @@ -1591,6 +1591,5 @@ const RUST_KW: [Symbol; 60] = [
kw::Auto,
kw::Catch,
kw::Default,
kw::Existential,
kw::Union,
];
4 changes: 2 additions & 2 deletions src/modules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ fn parse_inner_attributes<'a>(parser: &mut parser::Parser<'a>) -> PResult<'a, Ve
}
TokenKind::DocComment(s) => {
// we need to get the position of this token before we bump.
let attr = attr::mk_sugared_doc_attr(attr::mk_attr_id(), s, parser.token.span);
let attr = attr::mk_sugared_doc_attr(s, parser.token.span);
if attr.style == ast::AttrStyle::Inner {
attrs.push(attr);
parser.bump();
Expand Down Expand Up @@ -478,7 +478,7 @@ fn parse_mod_items<'a>(parser: &mut parser::Parser<'a>, inner_lo: Span) -> PResu
fn is_cfg_if(item: &ast::Item) -> bool {
match item.node {
ast::ItemKind::Mac(ref mac) => {
if let Some(first_segment) = mac.node.path.segments.first() {
if let Some(first_segment) = mac.path.segments.first() {
if first_segment.ident.name == Symbol::intern("cfg_if") {
return true;
}
Expand Down
9 changes: 3 additions & 6 deletions src/modules/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl<'a, 'ast: 'a> CfgIfVisitor<'a> {
// extern crate cfg_if;
// cfg_if! {..}
// ```
match mac.node.path.segments.first() {
match mac.path.segments.first() {
Some(first_segment) => {
if first_segment.ident.name != Symbol::intern("cfg_if") {
return Err("Expected cfg_if");
Expand All @@ -65,11 +65,8 @@ impl<'a, 'ast: 'a> CfgIfVisitor<'a> {
}
};

let mut parser = stream_to_parser_with_base_dir(
self.parse_sess,
mac.node.tts.clone(),
self.base_dir.clone(),
);
let mut parser =
stream_to_parser_with_base_dir(self.parse_sess, mac.tts.clone(), self.base_dir.clone());
parser.cfg_mods = false;
let mut process_if_cfg = true;

Expand Down
Loading

0 comments on commit 9ad9df0

Please sign in to comment.