Skip to content

Commit 10b2f3a

Browse files
authored
Merge pull request #1638 from est31/master
Update syntex to 0.59
2 parents 8ac9245 + 4665943 commit 10b2f3a

19 files changed

+257
-118
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ regex = "0.2"
2828
term = "0.4"
2929
strings = "0.1"
3030
diff = "0.1"
31-
syntex_syntax = "0.58"
32-
syntex_errors = "0.58"
31+
syntex_syntax = "0.59"
32+
syntex_errors = "0.59"
3333
log = "0.3"
3434
env_logger = "0.4"
3535
getopts = "0.2"

src/chains.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,15 @@
7878
7979
use Shape;
8080
use rewrite::{Rewrite, RewriteContext};
81-
use utils::{wrap_str, first_line_width, last_line_width};
81+
use utils::{wrap_str, first_line_width, last_line_width, mk_sp};
8282
use expr::rewrite_call;
8383
use config::IndentStyle;
8484
use macros::convert_try_mac;
8585

8686
use std::cmp::min;
8787
use std::iter;
8888
use syntax::{ast, ptr};
89-
use syntax::codemap::{mk_sp, Span};
89+
use syntax::codemap::Span;
9090

9191
pub fn rewrite_chain(expr: &ast::Expr, context: &RewriteContext, shape: Shape) -> Option<String> {
9292
debug!("rewrite_chain {:?}", shape);

src/expr.rs

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use lists::{write_list, itemize_list, ListFormatting, SeparatorTactic, ListTacti
2222
use string::{StringFormat, rewrite_string};
2323
use utils::{extra_offset, last_line_width, wrap_str, binary_search, first_line_width,
2424
semicolon_for_stmt, trimmed_last_line_width, left_most_sub_expr, stmt_expr,
25-
colon_spaces, contains_skip};
25+
colon_spaces, contains_skip, mk_sp};
2626
use visitor::FmtVisitor;
2727
use config::{Config, IndentStyle, MultilineStyle, ControlBraceStyle, Style};
2828
use comment::{FindUncommented, rewrite_comment, contains_comment, recover_comment_removed};
@@ -32,7 +32,7 @@ use chains::rewrite_chain;
3232
use macros::{rewrite_macro, MacroPosition};
3333

3434
use syntax::{ast, ptr};
35-
use syntax::codemap::{CodeMap, Span, BytePos, mk_sp};
35+
use syntax::codemap::{CodeMap, Span, BytePos};
3636
use syntax::parse::classify;
3737

3838
impl Rewrite for ast::Expr {
@@ -253,6 +253,16 @@ fn format_expr(expr: &ast::Expr,
253253
context.config.max_width(),
254254
shape)
255255
}
256+
ast::ExprKind::Catch(ref block) => {
257+
if let rewrite @ Some(_) = try_one_line_block(context, shape, "do catch ", block) {
258+
return rewrite;
259+
}
260+
// 9 = `do catch `
261+
let budget = shape.width.checked_sub(9).unwrap_or(0);
262+
Some(format!("{}{}",
263+
"do catch ",
264+
try_opt!(block.rewrite(&context, Shape::legacy(budget, shape.indent)))))
265+
}
256266
};
257267
match (attr_rw, expr_rw) {
258268
(Some(attr_str), Some(expr_str)) => {
@@ -266,6 +276,22 @@ fn format_expr(expr: &ast::Expr,
266276
}
267277
}
268278

279+
fn try_one_line_block(context: &RewriteContext,
280+
shape: Shape,
281+
prefix: &str,
282+
block: &ast::Block)
283+
-> Option<String> {
284+
if is_simple_block(block, context.codemap) {
285+
let expr_shape = Shape::legacy(shape.width - prefix.len(), shape.indent);
286+
let expr_str = try_opt!(block.stmts[0].rewrite(context, expr_shape));
287+
let result = format!("{}{{ {} }}", prefix, expr_str);
288+
if result.len() <= shape.width && !result.contains('\n') {
289+
return Some(result);
290+
}
291+
}
292+
None
293+
}
294+
269295
pub fn rewrite_pair<LHS, RHS>(lhs: &LHS,
270296
rhs: &RHS,
271297
prefix: &str,
@@ -620,9 +646,7 @@ fn rewrite_closure(capture: ast::CaptureBy,
620646
// means we must re-format.
621647
let block_shape = shape.block().with_max_width(context.config);
622648
let block_str = try_opt!(block.rewrite(&context, block_shape));
623-
Some(format!("{} {}",
624-
prefix,
625-
try_opt!(block_str.rewrite(context, block_shape))))
649+
Some(format!("{} {}", prefix, block_str))
626650
}
627651
}
628652

@@ -687,24 +711,13 @@ impl Rewrite for ast::Block {
687711
} else {
688712
"unsafe ".to_owned()
689713
};
690-
691-
if is_simple_block(self, context.codemap) && prefix.len() < shape.width {
692-
let expr_str =
693-
self.stmts[0].rewrite(context,
694-
Shape::legacy(shape.width - prefix.len(),
695-
shape.indent));
696-
let expr_str = try_opt!(expr_str);
697-
let result = format!("{}{{ {} }}", prefix, expr_str);
698-
if result.len() <= shape.width && !result.contains('\n') {
699-
return Some(result);
700-
}
714+
if let result @ Some(_) = try_one_line_block(context, shape, &prefix, self) {
715+
return result;
701716
}
702-
703717
prefix
704718
}
705719
ast::BlockCheckMode::Default => {
706720
visitor.last_pos = self.span.lo;
707-
708721
String::new()
709722
}
710723
};

src/imports.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
use Shape;
1212
use utils;
13-
use syntax::codemap::{self, BytePos, Span};
13+
use syntax::codemap::{BytePos, Span};
1414
use codemap::SpanUtils;
1515
use lists::{write_list, itemize_list, ListItem, ListFormatting, SeparatorTactic, definitive_tactic};
1616
use types::{rewrite_path, PathContext};
@@ -202,7 +202,7 @@ impl<'a> FmtVisitor<'a> {
202202
// Order the imports by view-path & other import path properties
203203
ordered_use_items.sort_by(|a, b| compare_use_items(a.0, b.0).unwrap());
204204
// First, output the span before the first import
205-
let prev_span_str = self.snippet(codemap::mk_sp(self.last_pos, pos_before_first_use_item));
205+
let prev_span_str = self.snippet(utils::mk_sp(self.last_pos, pos_before_first_use_item));
206206
// Look for purely trailing space at the start of the prefix snippet before a linefeed, or
207207
// a prefix that's entirely horizontal whitespace.
208208
let prefix_span_start = match prev_span_str.find('\n') {
@@ -241,7 +241,7 @@ impl<'a> FmtVisitor<'a> {
241241
Shape::legacy(self.config.max_width() - offset.width() - 1, offset)) {
242242
Some(ref s) if s.is_empty() => {
243243
// Format up to last newline
244-
let prev_span = codemap::mk_sp(self.last_pos, source!(self, span).lo);
244+
let prev_span = utils::mk_sp(self.last_pos, source!(self, span).lo);
245245
let span_end = match self.snippet(prev_span).rfind('\n') {
246246
Some(offset) => self.last_pos + BytePos(offset as u32),
247247
None => source!(self, span).lo,

src/items.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use {Indent, Shape};
1414
use codemap::SpanUtils;
1515
use utils::{format_mutability, format_visibility, contains_skip, end_typaram, wrap_str,
1616
last_line_width, format_unsafety, trim_newlines, stmt_expr, semicolon_for_expr,
17-
trimmed_last_line_width, colon_spaces};
17+
trimmed_last_line_width, colon_spaces, mk_sp};
1818
use lists::{write_list, itemize_list, ListItem, ListFormatting, SeparatorTactic, list_helper,
1919
DefinitiveListTactic, ListTactic, definitive_tactic, format_item_list};
2020
use expr::{is_empty_block, is_simple_block_stmt, rewrite_assign_rhs};
@@ -23,8 +23,8 @@ use visitor::FmtVisitor;
2323
use rewrite::{Rewrite, RewriteContext};
2424
use config::{Config, IndentStyle, Density, ReturnIndent, BraceStyle, Style, TypeDensity};
2525

26-
use syntax::{ast, abi, codemap, ptr, symbol};
27-
use syntax::codemap::{Span, BytePos, mk_sp};
26+
use syntax::{ast, abi, ptr, symbol};
27+
use syntax::codemap::{Span, BytePos};
2828
use syntax::ast::ImplItem;
2929

3030
fn type_annotation_separator(config: &Config) -> &str {
@@ -246,7 +246,7 @@ impl<'a> FmtVisitor<'a> {
246246
let mut newline_brace = newline_for_brace(self.config, &generics.where_clause);
247247
let context = self.get_context();
248248

249-
let block_snippet = self.snippet(codemap::mk_sp(block.span.lo, block.span.hi));
249+
let block_snippet = self.snippet(mk_sp(block.span.lo, block.span.hi));
250250
let has_body = !block_snippet[1..block_snippet.len() - 1].trim().is_empty() ||
251251
!context.config.fn_empty_single_line();
252252

@@ -527,7 +527,7 @@ pub fn format_impl(context: &RewriteContext,
527527
offset: Indent,
528528
where_span_end: Option<BytePos>)
529529
-> Option<String> {
530-
if let ast::ItemKind::Impl(_, _, ref generics, ref trait_ref, _, ref items) = item.node {
530+
if let ast::ItemKind::Impl(_, _, _, ref generics, ref trait_ref, _, ref items) = item.node {
531531
let mut result = String::new();
532532
// First try to format the ref and type without a split at the 'for'.
533533
let mut ref_and_type = try_opt!(format_impl_ref_and_type(context, item, offset, false));
@@ -647,8 +647,13 @@ fn format_impl_ref_and_type(context: &RewriteContext,
647647
offset: Indent,
648648
split_at_for: bool)
649649
-> Option<String> {
650-
if let ast::ItemKind::Impl(unsafety, polarity, ref generics, ref trait_ref, ref self_ty, _) =
651-
item.node {
650+
if let ast::ItemKind::Impl(unsafety,
651+
polarity,
652+
_,
653+
ref generics,
654+
ref trait_ref,
655+
ref self_ty,
656+
_) = item.node {
652657
let mut result = String::new();
653658

654659
result.push_str(&format_visibility(&item.vis));

src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ extern crate term;
3333
use errors::{Handler, DiagnosticBuilder};
3434
use errors::emitter::{ColorConfig, EmitterWriter};
3535
use syntax::ast;
36-
use syntax::codemap::{mk_sp, CodeMap, Span};
36+
use syntax::codemap::{CodeMap, Span, FilePathMapping};
3737
use syntax::parse::{self, ParseSess};
3838

3939
use strings::string_buffer::StringBuffer;
@@ -107,7 +107,7 @@ impl Spanned for ast::Ty {
107107
impl Spanned for ast::Arg {
108108
fn span(&self) -> Span {
109109
if items::is_named_arg(self) {
110-
mk_sp(self.pat.span.lo, self.ty.span.hi)
110+
utils::mk_sp(self.pat.span.lo, self.ty.span.hi)
111111
} else {
112112
self.ty.span
113113
}
@@ -578,7 +578,7 @@ pub fn format_input<T: Write>(input: Input,
578578
if config.disable_all_formatting() {
579579
return Ok((summary, FileMap::new(), FormatReport::new()));
580580
}
581-
let codemap = Rc::new(CodeMap::new());
581+
let codemap = Rc::new(CodeMap::new(FilePathMapping::empty()));
582582

583583
let tty_handler =
584584
Handler::with_tty_emitter(ColorConfig::Auto, true, false, Some(codemap.clone()));

src/lists.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@
1111
use std::cmp;
1212
use std::iter::Peekable;
1313

14-
use syntax::codemap::{self, CodeMap, BytePos};
14+
use syntax::codemap::{CodeMap, BytePos};
1515

1616
use {Indent, Shape};
1717
use comment::{FindUncommented, rewrite_comment, find_comment_end};
1818
use config::{Config, IndentStyle};
1919
use rewrite::RewriteContext;
20+
use utils::mk_sp;
2021

2122
#[derive(Eq, PartialEq, Debug, Copy, Clone)]
2223
/// Formatting tactic for lists. This will be cast down to a
@@ -344,7 +345,7 @@ impl<'a, T, I, F1, F2, F3> Iterator for ListItems<'a, I, F1, F2, F3>
344345
let mut new_lines = false;
345346
// Pre-comment
346347
let pre_snippet = self.codemap
347-
.span_to_snippet(codemap::mk_sp(self.prev_span_end, (self.get_lo)(&item)))
348+
.span_to_snippet(mk_sp(self.prev_span_end, (self.get_lo)(&item)))
348349
.unwrap();
349350
let trimmed_pre_snippet = pre_snippet.trim();
350351
let has_pre_comment = trimmed_pre_snippet.contains("//") ||
@@ -361,7 +362,7 @@ impl<'a, T, I, F1, F2, F3> Iterator for ListItems<'a, I, F1, F2, F3>
361362
None => self.next_span_start,
362363
};
363364
let post_snippet = self.codemap
364-
.span_to_snippet(codemap::mk_sp((self.get_hi)(&item), next_start))
365+
.span_to_snippet(mk_sp((self.get_hi)(&item), next_start))
365366
.unwrap();
366367

367368
let comment_end = match self.inner.peek() {

src/macros.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@
2020
// and those with brackets will be formatted as array literals.
2121

2222
use syntax::ast;
23-
use syntax::codemap::{mk_sp, BytePos};
23+
use syntax::codemap::BytePos;
2424
use syntax::parse::token::Token;
25-
use syntax::parse::tts_to_parser;
25+
use syntax::parse::new_parser_from_tts;
26+
use syntax::tokenstream::TokenStream;
2627
use syntax::symbol;
2728
use syntax::util::ThinVec;
2829

@@ -31,6 +32,7 @@ use codemap::SpanUtils;
3132
use rewrite::{Rewrite, RewriteContext};
3233
use expr::{rewrite_call, rewrite_array, rewrite_pair};
3334
use comment::{FindUncommented, contains_comment};
35+
use utils::mk_sp;
3436

3537
const FORCED_BRACKET_MACROS: &'static [&'static str] = &["vec!"];
3638

@@ -92,7 +94,8 @@ pub fn rewrite_macro(mac: &ast::Mac,
9294
original_style
9395
};
9496

95-
if mac.node.tts.is_empty() && !contains_comment(&context.snippet(mac.span)) {
97+
let ts: TokenStream = mac.node.tts.clone().into();
98+
if ts.is_empty() && !contains_comment(&context.snippet(mac.span)) {
9699
return match style {
97100
MacroStyle::Parens if position == MacroPosition::Item => {
98101
Some(format!("{}();", macro_name))
@@ -103,7 +106,7 @@ pub fn rewrite_macro(mac: &ast::Mac,
103106
};
104107
}
105108

106-
let mut parser = tts_to_parser(context.parse_session, mac.node.tts.clone());
109+
let mut parser = new_parser_from_tts(context.parse_session, ts.trees().collect());
107110
let mut expr_vec = Vec::new();
108111
let mut vec_with_semi = false;
109112

@@ -222,7 +225,8 @@ pub fn rewrite_macro(mac: &ast::Mac,
222225
/// failed).
223226
pub fn convert_try_mac(mac: &ast::Mac, context: &RewriteContext) -> Option<ast::Expr> {
224227
if &format!("{}", mac.node.path)[..] == "try" {
225-
let mut parser = tts_to_parser(context.parse_session, mac.node.tts.clone());
228+
let ts: TokenStream = mac.node.tts.clone().into();
229+
let mut parser = new_parser_from_tts(context.parse_session, ts.trees().collect());
226230

227231
Some(ast::Expr {
228232
id: ast::NodeId::new(0), // dummy value

src/missed_spans.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@
1010

1111
use config::WriteMode;
1212
use visitor::FmtVisitor;
13-
use syntax::codemap::{self, BytePos, Span, Pos};
13+
use syntax::codemap::{BytePos, Span, Pos};
1414
use comment::{CodeCharKind, CommentCodeSlices, rewrite_comment};
1515
use Shape;
16+
use utils::mk_sp;
1617

1718
impl<'a> FmtVisitor<'a> {
1819
fn output_at_start(&self) -> bool {
@@ -65,7 +66,7 @@ impl<'a> FmtVisitor<'a> {
6566
self.codemap.lookup_char_pos(end));
6667

6768
self.last_pos = end;
68-
let span = codemap::mk_sp(start, end);
69+
let span = mk_sp(start, end);
6970

7071
self.write_snippet(span, &process_last_snippet);
7172
}

src/patterns.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use Shape;
1212
use codemap::SpanUtils;
1313
use config::{IndentStyle, MultilineStyle};
1414
use rewrite::{Rewrite, RewriteContext};
15-
use utils::{wrap_str, format_mutability};
15+
use utils::{wrap_str, format_mutability, mk_sp};
1616
use lists::{DefinitiveListTactic, SeparatorTactic, format_item_list, itemize_list, ListItem,
1717
struct_lit_shape, struct_lit_tactic, shape_for_tactic, struct_lit_formatting,
1818
write_list};
@@ -261,14 +261,14 @@ fn rewrite_tuple_pat(pats: &[ptr::P<ast::Pat>],
261261
} else {
262262
pats[pos + 1].span().lo
263263
};
264-
let dot_span = codemap::mk_sp(prev, next);
264+
let dot_span = mk_sp(prev, next);
265265
let snippet = context.snippet(dot_span);
266266
let lo = dot_span.lo + BytePos(snippet.find_uncommented("..").unwrap() as u32);
267267
let span = Span {
268268
lo: lo,
269269
// 2 == "..".len()
270270
hi: lo + BytePos(2),
271-
expn_id: codemap::NO_EXPANSION,
271+
ctxt: codemap::NO_EXPANSION,
272272
};
273273
let dotdot = TuplePatField::Dotdot(span);
274274
pat_vec.insert(pos, dotdot);

src/types.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use {Shape, Spanned};
2121
use codemap::SpanUtils;
2222
use lists::{format_item_list, itemize_list, format_fn_args};
2323
use rewrite::{Rewrite, RewriteContext};
24-
use utils::{extra_offset, format_mutability, colon_spaces, wrap_str};
24+
use utils::{extra_offset, format_mutability, colon_spaces, wrap_str, mk_sp};
2525
use expr::{rewrite_unary_prefix, rewrite_pair, rewrite_tuple_type};
2626
use config::TypeDensity;
2727

@@ -206,9 +206,7 @@ fn rewrite_segment(path_context: PathContext,
206206
.collect::<Vec<_>>();
207207

208208
let next_span_lo = param_list.last().unwrap().get_span().hi + BytePos(1);
209-
let list_lo = context
210-
.codemap
211-
.span_after(codemap::mk_sp(*span_lo, span_hi), "<");
209+
let list_lo = context.codemap.span_after(mk_sp(*span_lo, span_hi), "<");
212210
let separator = if path_context == PathContext::Expr {
213211
"::"
214212
} else {
@@ -686,6 +684,7 @@ impl Rewrite for ast::Ty {
686684
it.rewrite(context, shape)
687685
.map(|it_str| format!("impl {}", it_str))
688686
}
687+
ast::TyKind::Err |
689688
ast::TyKind::Typeof(..) => unreachable!(),
690689
}
691690
}

0 commit comments

Comments
 (0)