Skip to content

Commit

Permalink
Update rustc-ap-* crates to 407.0.0 (rust-lang#3447)
Browse files Browse the repository at this point in the history
  • Loading branch information
topecongiro authored Mar 14, 2019
1 parent 331a050 commit ce5cccc
Show file tree
Hide file tree
Showing 15 changed files with 293 additions and 396 deletions.
390 changes: 225 additions & 165 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ env_logger = "0.6"
getopts = "0.2"
derive-new = "0.5"
cargo_metadata = "0.7"
rustc-ap-rustc_target = "373.0.0"
rustc-ap-syntax = "373.0.0"
rustc-ap-syntax_pos = "373.0.0"
rustc-ap-rustc_target = "407.0.0"
rustc-ap-syntax = "407.0.0"
rustc-ap-syntax_pos = "407.0.0"
failure = "0.1.3"
bytecount = "0.5"
unicode-width = "0.1.5"
Expand Down
8 changes: 4 additions & 4 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ pub mod file_lines;
pub mod license;
pub mod lists;

/// This macro defines configuration options used in rustfmt. Each option
/// is defined as follows:
///
/// `name: value type, default value, is stable, description;`
// This macro defines configuration options used in rustfmt. Each option
// is defined as follows:
//
// `name: value type, default value, is stable, description;`
create_config! {
// Fundamental stuff
max_width: usize, 100, true, "Maximum width of each line";
Expand Down
5 changes: 3 additions & 2 deletions src/config/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,10 +237,11 @@ configuration_option_enum! { Heuristics:
}

impl Density {
pub fn to_list_tactic(self) -> ListTactic {
pub fn to_list_tactic(self, len: usize) -> ListTactic {
match self {
Density::Compressed => ListTactic::Mixed,
Density::Tall => ListTactic::HorizontalVertical,
Density::Vertical if len == 1 => ListTactic::Horizontal,
Density::Vertical => ListTactic::Vertical,
}
}
Expand Down Expand Up @@ -443,7 +444,7 @@ pub trait CliOptions {
fn config_path(&self) -> Option<&Path>;
}

/// The edition of the compiler (RFC 2052)
// The edition of the compiler (RFC 2052)
configuration_option_enum! { Edition:
Edition2015: 2015,
Edition2018: 2018,
Expand Down
6 changes: 3 additions & 3 deletions src/formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ fn format_project<T: FormatHandler>(

// Suppress error output if we have to do any further parsing.
let silent_emitter = silent_emitter(source_map);
parse_session.span_diagnostic = Handler::with_emitter(true, false, silent_emitter);
parse_session.span_diagnostic = Handler::with_emitter(true, None, silent_emitter);

let mut context = FormatContext::new(&krate, report, parse_session, config, handler);

Expand Down Expand Up @@ -657,15 +657,15 @@ fn silent_emitter(source_map: Rc<SourceMap>) -> Box<EmitterWriter> {
fn make_parse_sess(source_map: Rc<SourceMap>, config: &Config) -> ParseSess {
let tty_handler = if config.hide_parse_errors() {
let silent_emitter = silent_emitter(source_map.clone());
Handler::with_emitter(true, false, silent_emitter)
Handler::with_emitter(true, None, silent_emitter)
} else {
let supports_color = term::stderr().map_or(false, |term| term.supports_color());
let color_cfg = if supports_color {
ColorConfig::Auto
} else {
ColorConfig::Never
};
Handler::with_tty_emitter(color_cfg, true, false, Some(source_map.clone()))
Handler::with_tty_emitter(color_cfg, true, None, Some(source_map.clone()))
};

ParseSess::with_span_handler(tty_handler, source_map)
Expand Down
214 changes: 37 additions & 177 deletions src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ use crate::expr::{
format_expr, is_empty_block, is_simple_block_stmt, rewrite_assign_rhs, rewrite_assign_rhs_with,
ExprType, RhsTactics,
};
use crate::lists::{
definitive_tactic, extract_post_comment, extract_pre_comment, get_comment_end,
has_extra_newline, itemize_list, write_list, ListFormatting, ListItem, Separator,
};
use crate::lists::{definitive_tactic, itemize_list, write_list, ListFormatting, Separator};
use crate::macros::{rewrite_macro, MacroPosition};
use crate::overflow;
use crate::rewrite::{Rewrite, RewriteContext};
Expand Down Expand Up @@ -194,7 +191,7 @@ impl<'a> FnSig<'a> {
) -> FnSig<'a> {
FnSig {
unsafety: method_sig.header.unsafety,
is_async: method_sig.header.asyncness,
is_async: method_sig.header.asyncness.node,
constness: method_sig.header.constness.node,
defaultness: ast::Defaultness::Final,
abi: method_sig.header.abi,
Expand All @@ -216,7 +213,7 @@ impl<'a> FnSig<'a> {
generics,
abi: fn_header.abi,
constness: fn_header.constness.node,
is_async: fn_header.asyncness,
is_async: fn_header.asyncness.node,
defaultness,
unsafety: fn_header.unsafety,
visibility: visibility.clone(),
Expand Down Expand Up @@ -1833,7 +1830,9 @@ fn is_empty_infer(ty: &ast::Ty, pat_span: Span) -> bool {

impl Rewrite for ast::Arg {
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
if is_named_arg(self) {
if let Some(ref explicit_self) = self.to_self() {
rewrite_explicit_self(context, explicit_self)
} else if is_named_arg(self) {
let mut result = self
.pat
.rewrite(context, Shape::legacy(shape.width, shape.indent))?;
Expand Down Expand Up @@ -1862,9 +1861,8 @@ impl Rewrite for ast::Arg {
}

fn rewrite_explicit_self(
explicit_self: &ast::ExplicitSelf,
args: &[ast::Arg],
context: &RewriteContext<'_>,
explicit_self: &ast::ExplicitSelf,
) -> Option<String> {
match explicit_self.node {
ast::SelfKind::Region(lt, m) => {
Expand All @@ -1880,10 +1878,7 @@ fn rewrite_explicit_self(
None => Some(format!("&{}self", mut_str)),
}
}
ast::SelfKind::Explicit(ref ty, _) => {
assert!(!args.is_empty(), "&[ast::Arg] shouldn't be empty.");

let mutability = explicit_self_mutability(&args[0]);
ast::SelfKind::Explicit(ref ty, mutability) => {
let type_str = ty.rewrite(
context,
Shape::legacy(context.config.max_width(), Indent::empty()),
Expand All @@ -1895,23 +1890,7 @@ fn rewrite_explicit_self(
type_str
))
}
ast::SelfKind::Value(_) => {
assert!(!args.is_empty(), "&[ast::Arg] shouldn't be empty.");

let mutability = explicit_self_mutability(&args[0]);

Some(format!("{}self", format_mutability(mutability)))
}
}
}

// Hacky solution caused by absence of `Mutability` in `SelfValue` and
// `SelfExplicit` variants of `ast::ExplicitSelf_`.
fn explicit_self_mutability(arg: &ast::Arg) -> ast::Mutability {
if let ast::PatKind::Ident(ast::BindingMode::ByValue(mutability), _, _) = arg.pat.node {
mutability
} else {
unreachable!()
ast::SelfKind::Value(mutability) => Some(format!("{}self", format_mutability(mutability))),
}
}

Expand Down Expand Up @@ -2048,14 +2027,12 @@ fn rewrite_fn_base(
let arg_str = rewrite_args(
context,
&fd.inputs,
fd.get_self().as_ref(),
one_line_budget,
multi_line_budget,
indent,
arg_indent,
args_span,
fd.variadic,
generics_str.contains('\n'),
fd.c_variadic,
)?;

let put_args_in_block = match context.config.indent_style() {
Expand Down Expand Up @@ -2272,19 +2249,13 @@ impl WhereClauseOption {
fn rewrite_args(
context: &RewriteContext<'_>,
args: &[ast::Arg],
explicit_self: Option<&ast::ExplicitSelf>,
one_line_budget: usize,
multi_line_budget: usize,
indent: Indent,
arg_indent: Indent,
span: Span,
variadic: bool,
generics_str_contains_newline: bool,
) -> Option<String> {
let terminator = ")";
let separator = ",";
let next_span_start = span.hi();

if args.len() == 0 {
let comment = context
.snippet(mk_sp(
Expand All @@ -2295,158 +2266,47 @@ fn rewrite_args(
.trim();
return Some(comment.to_owned());
}

let mut arg_item_strs = args
.iter()
.map(|arg| {
let arg_items: Vec<_> = itemize_list(
context.snippet_provider,
args.iter(),
")",
",",
|arg| span_lo_for_arg(arg),
|arg| arg.ty.span.hi(),
|arg| {
arg.rewrite(context, Shape::legacy(multi_line_budget, arg_indent))
.unwrap_or_else(|| context.snippet(arg.span()).to_owned())
})
.collect::<Vec<_>>();

// Account for sugary self.
let mut pre_comment_str = "";
let mut post_comment_str = "";
let min_args = explicit_self
.and_then(|explicit_self| rewrite_explicit_self(explicit_self, args, context))
.map_or(1, |self_str| {
pre_comment_str = context.snippet(mk_sp(span.lo(), args[0].pat.span.lo()));

let next_start = if args.len() > 1 {
args[1].pat.span().lo()
} else {
span.hi()
};
post_comment_str = context.snippet(mk_sp(args[0].ty.span.hi(), next_start));

arg_item_strs[0] = self_str;
2
});

// Comments between args.
let mut arg_items = Vec::new();
if min_args == 2 {
arg_items.push(ListItem::from_str(""));
}

if args.len() >= min_args || variadic {
let comment_span_start = if min_args == 2 {
let remove_comma_byte_pos = context
.snippet_provider
.span_after(mk_sp(args[0].ty.span.hi(), args[1].pat.span.lo()), ",");
let first_post_and_second_pre_span =
mk_sp(remove_comma_byte_pos, args[1].pat.span.lo());
if count_newlines(context.snippet(first_post_and_second_pre_span)) > 0 {
context
.snippet_provider
.span_after(first_post_and_second_pre_span, "\n")
} else {
remove_comma_byte_pos
}
} else {
span.lo()
};

enum ArgumentKind<'a> {
Regular(&'a ast::Arg),
Variadic(BytePos),
}

let variadic_arg = if variadic {
let variadic_span = mk_sp(args.last().unwrap().ty.span.hi(), span.hi());
let variadic_start =
context.snippet_provider.span_after(variadic_span, "...") - BytePos(3);
Some(ArgumentKind::Variadic(variadic_start))
} else {
None
};

let more_items = itemize_list(
context.snippet_provider,
args[min_args - 1..]
.iter()
.map(ArgumentKind::Regular)
.chain(variadic_arg),
terminator,
separator,
|arg| match *arg {
ArgumentKind::Regular(arg) => span_lo_for_arg(arg),
ArgumentKind::Variadic(start) => start,
},
|arg| match *arg {
ArgumentKind::Regular(arg) => arg.ty.span.hi(),
ArgumentKind::Variadic(start) => start + BytePos(3),
},
|arg| match *arg {
ArgumentKind::Regular(..) => None,
ArgumentKind::Variadic(..) => Some("...".to_owned()),
},
comment_span_start,
next_span_start,
false,
);

arg_items.extend(more_items);
}

let arg_items_len = arg_items.len();
let fits_in_one_line = !generics_str_contains_newline
&& (arg_items.is_empty()
|| arg_items_len == 1 && arg_item_strs[0].len() <= one_line_budget);

for (index, (item, arg)) in arg_items.iter_mut().zip(arg_item_strs).enumerate() {
// add pre comment and post comment for first arg(self)
if index == 0 && explicit_self.is_some() {
let (pre_comment, pre_comment_style) = extract_pre_comment(pre_comment_str);
item.pre_comment = pre_comment;
item.pre_comment_style = pre_comment_style;

let comment_end =
get_comment_end(post_comment_str, separator, terminator, arg_items_len == 1);

item.new_lines = has_extra_newline(post_comment_str, comment_end);
item.post_comment = extract_post_comment(post_comment_str, comment_end, separator);
}
item.item = Some(arg);
}

let last_line_ends_with_comment = arg_items
.iter()
.last()
.and_then(|item| item.post_comment.as_ref())
.map_or(false, |s| s.trim().starts_with("//"));

let (indent, trailing_comma) = match context.config.indent_style() {
IndentStyle::Block if fits_in_one_line => {
(indent.block_indent(context.config), SeparatorTactic::Never)
}
IndentStyle::Block => (
indent.block_indent(context.config),
context.config.trailing_comma(),
),
IndentStyle::Visual if last_line_ends_with_comment => {
(arg_indent, context.config.trailing_comma())
}
IndentStyle::Visual => (arg_indent, SeparatorTactic::Never),
};
.or_else(|| Some(context.snippet(arg.span()).to_owned()))
},
span.lo(),
span.hi(),
false,
)
.collect();

let tactic = definitive_tactic(
&arg_items,
context.config.fn_args_density().to_list_tactic(),
context
.config
.fn_args_density()
.to_list_tactic(arg_items.len()),
Separator::Comma,
one_line_budget,
);
let budget = match tactic {
DefinitiveListTactic::Horizontal => one_line_budget,
_ => multi_line_budget,
};

debug!("rewrite_args: budget: {}, tactic: {:?}", budget, tactic);

let indent = match context.config.indent_style() {
IndentStyle::Block => indent.block_indent(context.config),
IndentStyle::Visual => arg_indent,
};
let trailing_separator = if variadic {
SeparatorTactic::Never
} else {
trailing_comma
match context.config.indent_style() {
IndentStyle::Block => context.config.trailing_comma(),
IndentStyle::Visual => SeparatorTactic::Never,
}
};
let fmt = ListFormatting::new(Shape::legacy(budget, indent), context.config)
.tactic(tactic)
Expand Down
Loading

0 comments on commit ce5cccc

Please sign in to comment.