Skip to content

Commit 95d255e

Browse files
chore: merge rustc-ap changes and resolve conflicts
2 parents 0531683 + 383306e commit 95d255e

Some content is hidden

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

54 files changed

+938
-445
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
### Rust ###
55
# Generated by Cargo
66
# will have compiled files and executables
7-
/target/
7+
/target
88

99
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
1010
# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock

Cargo.lock

Lines changed: 79 additions & 150 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,6 @@ env_logger = "0.6"
4747
getopts = "0.2"
4848
derive-new = "0.5"
4949
cargo_metadata = "0.8"
50-
rustc-ap-rustc_target = "583.0.0"
51-
rustc-ap-syntax = "583.0.0"
52-
rustc-ap-syntax_pos = "583.0.0"
5350
failure = "0.1.3"
5451
bytecount = "0.6"
5552
unicode-width = "0.1.5"
@@ -58,13 +55,24 @@ dirs = "2.0.1"
5855
ignore = "0.4.6"
5956
annotate-snippets = { version = "0.6", features = ["ansi_term"] }
6057
structopt = "0.3"
61-
6258
rustfmt-config_proc_macro = { version = "0.2", path = "config_proc_macro" }
6359

6460
# A noop dependency that changes in the Rust repository, it's a bit of a hack.
6561
# See the `src/tools/rustc-workspace-hack/README.md` file in `rust-lang/rust`
6662
# for more information.
6763
rustc-workspace-hack = "1.0.0"
6864

65+
[dependencies.rustc_target]
66+
package = "rustc-ap-rustc_target"
67+
version = "606.0.0"
68+
69+
[dependencies.syntax]
70+
package = "rustc-ap-syntax"
71+
version = "606.0.0"
72+
73+
[dependencies.syntax_pos]
74+
package = "rustc-ap-syntax_pos"
75+
version = "606.0.0"
76+
6977
[dev-dependencies]
7078
lazy_static = "1.0.0"

src/attr.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ mod doc_comment;
2020

2121
/// Returns attributes on the given statement.
2222
pub(crate) fn get_attrs_from_stmt(stmt: &ast::Stmt) -> &[ast::Attribute] {
23-
match stmt.node {
23+
match stmt.kind {
2424
ast::StmtKind::Local(ref local) => &local.attrs,
2525
ast::StmtKind::Item(ref item) => &item.attrs,
2626
ast::StmtKind::Expr(ref expr) | ast::StmtKind::Semi(ref expr) => &expr.attrs,
@@ -29,7 +29,7 @@ pub(crate) fn get_attrs_from_stmt(stmt: &ast::Stmt) -> &[ast::Attribute] {
2929
}
3030

3131
pub(crate) fn get_span_without_attrs(stmt: &ast::Stmt) -> Span {
32-
match stmt.node {
32+
match stmt.kind {
3333
ast::StmtKind::Local(ref local) => local.span,
3434
ast::StmtKind::Item(ref item) => item.span,
3535
ast::StmtKind::Expr(ref expr) | ast::StmtKind::Semi(ref expr) => expr.span,
@@ -218,7 +218,7 @@ fn has_newlines_before_after_comment(comment: &str) -> (&str, &str) {
218218

219219
impl Rewrite for ast::MetaItem {
220220
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
221-
Some(match self.node {
221+
Some(match self.kind {
222222
ast::MetaItemKind::Word => {
223223
rewrite_path(context, PathContext::Type, None, &self.path, shape)?
224224
}
@@ -495,7 +495,7 @@ fn attr_prefix(attr: &ast::Attribute) -> &'static str {
495495

496496
pub(crate) trait MetaVisitor<'ast> {
497497
fn visit_meta_item(&mut self, meta_item: &'ast ast::MetaItem) {
498-
match meta_item.node {
498+
match meta_item.kind {
499499
ast::MetaItemKind::Word => self.visit_meta_word(meta_item),
500500
ast::MetaItemKind::List(ref list) => self.visit_meta_list(meta_item, list),
501501
ast::MetaItemKind::NameValue(ref lit) => self.visit_meta_name_value(meta_item, lit),

src/cargo-fmt/main.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,9 +519,16 @@ fn get_cargo_metadata(
519519
if let Some(manifest_path) = manifest_path {
520520
cmd.manifest_path(manifest_path);
521521
}
522+
522523
match cmd.exec() {
523524
Ok(metadata) => Ok(metadata),
524-
Err(error) => Err(io::Error::new(io::ErrorKind::Other, error.to_string())),
525+
Err(_) => {
526+
cmd.other_options(&[String::from("--offline")]);
527+
match cmd.exec() {
528+
Ok(metadata) => Ok(metadata),
529+
Err(error) => Err(io::Error::new(io::ErrorKind::Other, error.to_string())),
530+
}
531+
}
525532
}
526533
}
527534

src/chains.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ impl ChainItemKind {
135135
}
136136

137137
fn is_tup_field_access(expr: &ast::Expr) -> bool {
138-
match expr.node {
138+
match expr.kind {
139139
ast::ExprKind::Field(_, ref field) => {
140140
field.name.to_string().chars().all(|c| c.is_digit(10))
141141
}
@@ -144,7 +144,7 @@ impl ChainItemKind {
144144
}
145145

146146
fn from_ast(context: &RewriteContext<'_>, expr: &ast::Expr) -> (ChainItemKind, Span) {
147-
let (kind, span) = match expr.node {
147+
let (kind, span) = match expr.kind {
148148
ast::ExprKind::MethodCall(ref segment, ref expressions) => {
149149
let types = if let Some(ref generic_args) = segment.args {
150150
if let ast::GenericArgs::AngleBracketed(ref data) = **generic_args {
@@ -262,7 +262,7 @@ impl Chain {
262262
let mut rev_children = vec![];
263263
let mut sub_tries = 0;
264264
for subexpr in &subexpr_list {
265-
match subexpr.node {
265+
match subexpr.kind {
266266
ast::ExprKind::Try(_) => sub_tries += 1,
267267
_ => {
268268
rev_children.push(ChainItem::new(context, subexpr, sub_tries));
@@ -390,7 +390,7 @@ impl Chain {
390390
// Returns the expression's subexpression, if it exists. When the subexpr
391391
// is a try! macro, we'll convert it to shorthand when the option is set.
392392
fn pop_expr_chain(expr: &ast::Expr, context: &RewriteContext<'_>) -> Option<ast::Expr> {
393-
match expr.node {
393+
match expr.kind {
394394
ast::ExprKind::MethodCall(_, ref expressions) => {
395395
Some(Self::convert_try(&expressions[0], context))
396396
}
@@ -402,7 +402,7 @@ impl Chain {
402402
}
403403

404404
fn convert_try(expr: &ast::Expr, context: &RewriteContext<'_>) -> ast::Expr {
405-
match expr.node {
405+
match expr.kind {
406406
ast::ExprKind::Mac(ref mac) if context.config.use_try_shorthand() => {
407407
if let Some(subexpr) = convert_try_mac(mac, context) {
408408
subexpr
@@ -647,7 +647,7 @@ impl<'a> ChainFormatterShared<'a> {
647647
Cow::from("")
648648
} else {
649649
// Use new lines.
650-
if *context.force_one_line_chain.borrow() {
650+
if context.force_one_line_chain.get() {
651651
return None;
652652
}
653653
child_shape.to_string_with_newline(context.config)

src/closures.rs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use syntax::{ast, ptr};
44
use crate::config::lists::*;
55
use crate::config::Version;
66
use crate::expr::{block_contains_comment, is_simple_block, is_unsafe_block, rewrite_cond};
7-
use crate::items::{span_hi_for_arg, span_lo_for_arg};
7+
use crate::items::{span_hi_for_param, span_lo_for_param};
88
use crate::lists::{definitive_tactic, itemize_list, write_list, ListFormatting, Separator};
99
use crate::overflow::OverflowableItem;
1010
use crate::rewrite::{Rewrite, RewriteContext};
@@ -40,7 +40,7 @@ pub(crate) fn rewrite_closure(
4040
// 1 = space between `|...|` and body.
4141
let body_shape = shape.offset_left(extra_offset)?;
4242

43-
if let ast::ExprKind::Block(ref block, _) = body.node {
43+
if let ast::ExprKind::Block(ref block, _) = body.kind {
4444
// The body of the closure is an empty block.
4545
if block.stmts.is_empty() && !block_contains_comment(block, context.source_map) {
4646
return body
@@ -89,7 +89,7 @@ fn get_inner_expr<'a>(
8989
prefix: &str,
9090
context: &RewriteContext<'_>,
9191
) -> &'a ast::Expr {
92-
if let ast::ExprKind::Block(ref block, _) = expr.node {
92+
if let ast::ExprKind::Block(ref block, _) = expr.kind {
9393
if !needs_block(block, prefix, context) {
9494
// block.stmts.len() == 1
9595
if let Some(expr) = stmt_expr(&block.stmts[0]) {
@@ -110,7 +110,7 @@ fn needs_block(block: &ast::Block, prefix: &str, context: &RewriteContext<'_>) -
110110
}
111111

112112
fn veto_block(e: &ast::Expr) -> bool {
113-
match e.node {
113+
match e.kind {
114114
ast::ExprKind::Call(..)
115115
| ast::ExprKind::Binary(..)
116116
| ast::ExprKind::Cast(..)
@@ -141,7 +141,7 @@ fn rewrite_closure_with_block(
141141
let block = ast::Block {
142142
stmts: vec![ast::Stmt {
143143
id: ast::NodeId::root(),
144-
node: ast::StmtKind::Expr(ptr::P(body.clone())),
144+
kind: ast::StmtKind::Expr(ptr::P(body.clone())),
145145
span: body.span,
146146
}],
147147
id: ast::NodeId::root(),
@@ -161,7 +161,7 @@ fn rewrite_closure_expr(
161161
shape: Shape,
162162
) -> Option<String> {
163163
fn allow_multi_line(expr: &ast::Expr) -> bool {
164-
match expr.node {
164+
match expr.kind {
165165
ast::ExprKind::Match(..)
166166
| ast::ExprKind::Block(..)
167167
| ast::ExprKind::TryBlock(..)
@@ -232,37 +232,37 @@ fn rewrite_closure_fn_decl(
232232
.sub_width(4)?;
233233

234234
// 1 = |
235-
let argument_offset = nested_shape.indent + 1;
236-
let arg_shape = nested_shape.offset_left(1)?.visual_indent(0);
237-
let ret_str = fn_decl.output.rewrite(context, arg_shape)?;
235+
let param_offset = nested_shape.indent + 1;
236+
let param_shape = nested_shape.offset_left(1)?.visual_indent(0);
237+
let ret_str = fn_decl.output.rewrite(context, param_shape)?;
238238

239-
let arg_items = itemize_list(
239+
let param_items = itemize_list(
240240
context.snippet_provider,
241241
fn_decl.inputs.iter(),
242242
"|",
243243
",",
244-
|arg| span_lo_for_arg(arg),
245-
|arg| span_hi_for_arg(context, arg),
246-
|arg| arg.rewrite(context, arg_shape),
244+
|param| span_lo_for_param(param),
245+
|param| span_hi_for_param(context, param),
246+
|param| param.rewrite(context, param_shape),
247247
context.snippet_provider.span_after(span, "|"),
248248
body.span.lo(),
249249
false,
250250
);
251-
let item_vec = arg_items.collect::<Vec<_>>();
252-
// 1 = space between arguments and return type.
251+
let item_vec = param_items.collect::<Vec<_>>();
252+
// 1 = space between parameters and return type.
253253
let horizontal_budget = nested_shape.width.saturating_sub(ret_str.len() + 1);
254254
let tactic = definitive_tactic(
255255
&item_vec,
256256
ListTactic::HorizontalVertical,
257257
Separator::Comma,
258258
horizontal_budget,
259259
);
260-
let arg_shape = match tactic {
261-
DefinitiveListTactic::Horizontal => arg_shape.sub_width(ret_str.len() + 1)?,
262-
_ => arg_shape,
260+
let param_shape = match tactic {
261+
DefinitiveListTactic::Horizontal => param_shape.sub_width(ret_str.len() + 1)?,
262+
_ => param_shape,
263263
};
264264

265-
let fmt = ListFormatting::new(arg_shape, context.config)
265+
let fmt = ListFormatting::new(param_shape, context.config)
266266
.tactic(tactic)
267267
.preserve_newline(true);
268268
let list_str = write_list(&item_vec, &fmt)?;
@@ -271,7 +271,7 @@ fn rewrite_closure_fn_decl(
271271
if !ret_str.is_empty() {
272272
if prefix.contains('\n') {
273273
prefix.push('\n');
274-
prefix.push_str(&argument_offset.to_string(context.config));
274+
prefix.push_str(&param_offset.to_string(context.config));
275275
} else {
276276
prefix.push(' ');
277277
}
@@ -291,9 +291,9 @@ pub(crate) fn rewrite_last_closure(
291291
shape: Shape,
292292
) -> Option<String> {
293293
if let ast::ExprKind::Closure(capture, ref is_async, movability, ref fn_decl, ref body, _) =
294-
expr.node
294+
expr.kind
295295
{
296-
let body = match body.node {
296+
let body = match body.kind {
297297
ast::ExprKind::Block(ref block, _)
298298
if !is_unsafe_block(block)
299299
&& !context.inside_macro()
@@ -353,7 +353,7 @@ pub(crate) fn rewrite_last_closure(
353353
pub(crate) fn args_have_many_closure(args: &[OverflowableItem<'_>]) -> bool {
354354
args.iter()
355355
.filter_map(OverflowableItem::to_expr)
356-
.filter(|expr| match expr.node {
356+
.filter(|expr| match expr.kind {
357357
ast::ExprKind::Closure(..) => true,
358358
_ => false,
359359
})
@@ -371,7 +371,7 @@ fn is_block_closure_forced(context: &RewriteContext<'_>, expr: &ast::Expr) -> bo
371371
}
372372

373373
fn is_block_closure_forced_inner(expr: &ast::Expr, version: Version) -> bool {
374-
match expr.node {
374+
match expr.kind {
375375
ast::ExprKind::If(..) | ast::ExprKind::While(..) | ast::ExprKind::ForLoop(..) => true,
376376
ast::ExprKind::Loop(..) if version == Version::Two => true,
377377
ast::ExprKind::AddrOf(_, ref expr)
@@ -392,7 +392,7 @@ fn is_block_closure_forced_inner(expr: &ast::Expr, version: Version) -> bool {
392392
/// isn't parsed as (if true {...} else {...} | x) | 5
393393
// From https://github.com/rust-lang/rust/blob/master/src/libsyntax/parse/classify.rs.
394394
fn expr_requires_semi_to_be_stmt(e: &ast::Expr) -> bool {
395-
match e.node {
395+
match e.kind {
396396
ast::ExprKind::If(..)
397397
| ast::ExprKind::Match(..)
398398
| ast::ExprKind::Block(..)

src/comment.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -823,7 +823,8 @@ fn rewrite_comment_inner(
823823
const RUSTFMT_CUSTOM_COMMENT_PREFIX: &str = "//#### ";
824824

825825
fn hide_sharp_behind_comment(s: &str) -> Cow<'_, str> {
826-
if s.trim_start().starts_with("# ") {
826+
let s_trimmed = s.trim();
827+
if s_trimmed.starts_with("# ") || s_trimmed == "#" {
827828
Cow::from(format!("{}{}", RUSTFMT_CUSTOM_COMMENT_PREFIX, s))
828829
} else {
829830
Cow::from(s)

src/config/config_type.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -293,10 +293,12 @@ macro_rules! create_config {
293293
fn set_license_template(&mut self) {
294294
if self.was_set().license_template_path() {
295295
let lt_path = self.license_template_path();
296-
match license::load_and_compile_template(&lt_path) {
297-
Ok(re) => self.license_template = Some(re),
298-
Err(msg) => eprintln!("Warning for license template file {:?}: {}",
299-
lt_path, msg),
296+
if lt_path.len() > 0 {
297+
match license::load_and_compile_template(&lt_path) {
298+
Ok(re) => self.license_template = Some(re),
299+
Err(msg) => eprintln!("Warning for license template file {:?}: {}",
300+
lt_path, msg),
301+
}
300302
}
301303
}
302304
}

src/config/mod.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,20 @@ mod test {
467467
assert_eq!(s.contains("(unstable)"), true);
468468
}
469469

470+
#[test]
471+
fn test_empty_string_license_template_path() {
472+
let toml = r#"license_template_path = """#;
473+
let config = Config::from_toml(toml, Path::new("")).unwrap();
474+
assert!(config.license_template.is_none());
475+
}
476+
477+
#[test]
478+
fn test_valid_license_template_path() {
479+
let toml = r#"license_template_path = "tests/license-template/lt.txt""#;
480+
let config = Config::from_toml(toml, Path::new("")).unwrap();
481+
assert!(config.license_template.is_some());
482+
}
483+
470484
#[test]
471485
fn test_dump_default_config() {
472486
let default_config = format!(

0 commit comments

Comments
 (0)