Skip to content

Commit d326a29

Browse files
committed
Merge pull request #479 from marcusklaas/moar-types
Format more type variants
2 parents d7019ce + 0193706 commit d326a29

File tree

6 files changed

+111
-66
lines changed

6 files changed

+111
-66
lines changed

src/expr.rs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,10 @@ impl Rewrite for ast::Expr {
161161
wrap_str("return".to_owned(), context.config.max_width, width, offset)
162162
}
163163
ast::Expr_::ExprRet(Some(ref expr)) => {
164-
rewrite_unary_prefix(context, "return ", expr, width, offset)
164+
rewrite_unary_prefix(context, "return ", &**expr, width, offset)
165165
}
166166
ast::Expr_::ExprBox(ref expr) => {
167-
rewrite_unary_prefix(context, "box ", expr, width, offset)
167+
rewrite_unary_prefix(context, "box ", &**expr, width, offset)
168168
}
169169
ast::Expr_::ExprAddrOf(mutability, ref expr) => {
170170
rewrite_expr_addrof(context, mutability, expr, width, offset)
@@ -210,15 +210,15 @@ impl Rewrite for ast::Expr {
210210
}
211211
}
212212

213-
fn rewrite_pair<LHS, RHS>(lhs: &LHS,
214-
rhs: &RHS,
215-
prefix: &str,
216-
infix: &str,
217-
suffix: &str,
218-
context: &RewriteContext,
219-
width: usize,
220-
offset: Indent)
221-
-> Option<String>
213+
pub fn rewrite_pair<LHS, RHS>(lhs: &LHS,
214+
rhs: &RHS,
215+
prefix: &str,
216+
infix: &str,
217+
suffix: &str,
218+
context: &RewriteContext,
219+
width: usize,
220+
offset: Indent)
221+
-> Option<String>
222222
where LHS: Rewrite,
223223
RHS: Rewrite
224224
{
@@ -1470,16 +1470,16 @@ fn rewrite_binary_op(context: &RewriteContext,
14701470
rhs_result))
14711471
}
14721472

1473-
fn rewrite_unary_prefix(context: &RewriteContext,
1474-
prefix: &str,
1475-
expr: &ast::Expr,
1476-
width: usize,
1477-
offset: Indent)
1478-
-> Option<String> {
1479-
expr.rewrite(context,
1480-
try_opt!(width.checked_sub(prefix.len())),
1481-
offset + prefix.len())
1482-
.map(|r| format!("{}{}", prefix, r))
1473+
pub fn rewrite_unary_prefix<R: Rewrite>(context: &RewriteContext,
1474+
prefix: &str,
1475+
rewrite: &R,
1476+
width: usize,
1477+
offset: Indent)
1478+
-> Option<String> {
1479+
rewrite.rewrite(context,
1480+
try_opt!(width.checked_sub(prefix.len())),
1481+
offset + prefix.len())
1482+
.map(|r| format!("{}{}", prefix, r))
14831483
}
14841484

14851485
fn rewrite_unary_op(context: &RewriteContext,

src/items.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ impl<'a> FmtVisitor<'a> {
521521

522522
let variadic_arg = if variadic {
523523
let variadic_span = codemap::mk_sp(args.last().unwrap().ty.span.hi, span.hi);
524-
let variadic_start = span_after(variadic_span, "...", self.codemap) - BytePos(1);
524+
let variadic_start = span_after(variadic_span, "...", self.codemap) - BytePos(3);
525525
Some(ArgumentKind::Variadic(variadic_start))
526526
} else {
527527
None

src/types.rs

Lines changed: 77 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use syntax::ast;
11+
use syntax::ast::{self, Mutability};
1212
use syntax::print::pprust;
1313
use syntax::codemap::{self, Span, BytePos, CodeMap};
1414

1515
use Indent;
1616
use lists::{format_item_list, itemize_list, format_fn_args, list_helper, ListTactic};
1717
use rewrite::{Rewrite, RewriteContext};
1818
use utils::{extra_offset, span_after, format_mutability, wrap_str};
19+
use expr::{rewrite_unary_prefix, rewrite_pair};
1920

2021
impl Rewrite for ast::Path {
2122
fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Option<String> {
@@ -129,21 +130,25 @@ impl<'a> SegmentParam<'a> {
129130
}
130131

131132
impl<'a> Rewrite for SegmentParam<'a> {
132-
// FIXME: doesn't always use width, offset.
133133
fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Option<String> {
134-
Some(match *self {
134+
match *self {
135135
SegmentParam::LifeTime(ref lt) => {
136-
pprust::lifetime_to_string(lt)
136+
wrap_str(pprust::lifetime_to_string(lt),
137+
context.config.max_width,
138+
width,
139+
offset)
137140
}
138141
SegmentParam::Type(ref ty) => {
139-
try_opt!(ty.rewrite(context, width, offset))
142+
ty.rewrite(context, width, offset)
140143
}
141144
SegmentParam::Binding(ref binding) => {
142-
format!("{} = {}",
143-
binding.ident,
144-
try_opt!(binding.ty.rewrite(context, width, offset)))
145+
let mut result = format!("{} = ", binding.ident);
146+
let budget = try_opt!(width.checked_sub(result.len()));
147+
let rewrite = try_opt!(binding.ty.rewrite(context, budget, offset + result.len()));
148+
result.push_str(&rewrite);
149+
Some(result)
145150
}
146-
})
151+
}
147152
}
148153
}
149154

@@ -163,9 +168,7 @@ fn get_path_separator(codemap: &CodeMap,
163168
for c in snippet.chars().rev() {
164169
if c == ':' {
165170
return "::";
166-
} else if c.is_whitespace() || c == '<' {
167-
continue;
168-
} else {
171+
} else if !c.is_whitespace() && c != '<' {
169172
return "";
170173
}
171174
}
@@ -271,8 +274,7 @@ fn rewrite_segment(segment: &ast::PathSegment,
271274
impl Rewrite for ast::WherePredicate {
272275
fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Option<String> {
273276
// TODO: dead spans?
274-
// TODO: don't assume we'll always fit on one line...
275-
Some(match *self {
277+
let result = match *self {
276278
ast::WherePredicate::BoundPredicate(ast::WhereBoundPredicate { ref bound_lifetimes,
277279
ref bounded_ty,
278280
ref bounds,
@@ -335,23 +337,27 @@ impl Rewrite for ast::WherePredicate {
335337
let path_str = try_opt!(path.rewrite(context, budget, offset + used_width));
336338
format!("{} = {}", path_str, ty_str)
337339
}
338-
})
340+
};
341+
342+
wrap_str(result, context.config.max_width, width, offset)
339343
}
340344
}
341345

342346
impl Rewrite for ast::LifetimeDef {
343-
fn rewrite(&self, _: &RewriteContext, _: usize, _: Indent) -> Option<String> {
344-
if self.bounds.is_empty() {
345-
Some(pprust::lifetime_to_string(&self.lifetime))
347+
fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Option<String> {
348+
let result = if self.bounds.is_empty() {
349+
pprust::lifetime_to_string(&self.lifetime)
346350
} else {
347-
Some(format!("{}: {}",
348-
pprust::lifetime_to_string(&self.lifetime),
349-
self.bounds
350-
.iter()
351-
.map(pprust::lifetime_to_string)
352-
.collect::<Vec<_>>()
353-
.join(" + ")))
354-
}
351+
format!("{}: {}",
352+
pprust::lifetime_to_string(&self.lifetime),
353+
self.bounds
354+
.iter()
355+
.map(pprust::lifetime_to_string)
356+
.collect::<Vec<_>>()
357+
.join(" + "))
358+
};
359+
360+
wrap_str(result, context.config.max_width, width, offset)
355361
}
356362
}
357363

@@ -366,7 +372,10 @@ impl Rewrite for ast::TyParamBound {
366372
Some(format!("?{}", try_opt!(tref.rewrite(context, budget, offset + 1))))
367373
}
368374
ast::TyParamBound::RegionTyParamBound(ref l) => {
369-
Some(pprust::lifetime_to_string(l))
375+
wrap_str(pprust::lifetime_to_string(l),
376+
context.config.max_width,
377+
width,
378+
offset)
370379
}
371380
}
372381
}
@@ -377,11 +386,10 @@ impl Rewrite for ast::TyParamBounds {
377386
let strs: Vec<_> = try_opt!(self.iter()
378387
.map(|b| b.rewrite(context, width, offset))
379388
.collect());
380-
Some(strs.join(" + "))
389+
wrap_str(strs.join(" + "), context.config.max_width, width, offset)
381390
}
382391
}
383392

384-
// FIXME: this assumes everything will fit on one line
385393
impl Rewrite for ast::TyParam {
386394
fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Option<String> {
387395
let mut result = String::with_capacity(128);
@@ -404,11 +412,10 @@ impl Rewrite for ast::TyParam {
404412
result.push_str(&rewrite);
405413
}
406414

407-
Some(result)
415+
wrap_str(result, context.config.max_width, width, offset)
408416
}
409417
}
410418

411-
// FIXME: this assumes everything will fit on one line
412419
impl Rewrite for ast::PolyTraitRef {
413420
fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Option<String> {
414421
if !self.bound_lifetimes.is_empty() {
@@ -432,12 +439,8 @@ impl Rewrite for ast::PolyTraitRef {
432439
}
433440

434441
impl Rewrite for ast::Ty {
435-
// FIXME doesn't always use width, offset
436442
fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Option<String> {
437443
match self.node {
438-
ast::TyPath(None, ref p) => {
439-
p.rewrite(context, width, offset)
440-
}
441444
ast::TyObjectSum(ref ty, ref bounds) => {
442445
let ty_str = try_opt!(ty.rewrite(context, width, offset));
443446
let overhead = ty_str.len() + 3;
@@ -447,6 +450,14 @@ impl Rewrite for ast::Ty {
447450
try_opt!(width.checked_sub(overhead)),
448451
offset + overhead))))
449452
}
453+
ast::TyPtr(ref mt) => {
454+
let prefix = match mt.mutbl {
455+
Mutability::MutMutable => "*mut ",
456+
Mutability::MutImmutable => "*const ",
457+
};
458+
459+
rewrite_unary_prefix(context, prefix, &*mt.ty, width, offset)
460+
}
450461
ast::TyRptr(ref lifetime, ref mt) => {
451462
let mut_str = format_mutability(mt.mutbl);
452463
let mut_len = mut_str.len();
@@ -470,37 +481,60 @@ impl Rewrite for ast::Ty {
470481
}
471482
})
472483
}
484+
// FIXME: we drop any comments here, even though it's a silly place to put
485+
// comments.
473486
ast::TyParen(ref ty) => {
474487
let budget = try_opt!(width.checked_sub(2));
475488
ty.rewrite(context, budget, offset + 1).map(|ty_str| format!("({})", ty_str))
476489
}
477-
ast::TyTup(ref tup_ret) => {
490+
ast::TyVec(ref ty) => {
478491
let budget = try_opt!(width.checked_sub(2));
492+
ty.rewrite(context, budget, offset + 1).map(|ty_str| format!("[{}]", ty_str))
493+
}
494+
ast::TyTup(ref tup_ret) => {
479495
if tup_ret.is_empty() {
480-
Some("()".to_string())
496+
Some("()".to_owned())
481497
} else if let [ref item] = &**tup_ret {
498+
let budget = try_opt!(width.checked_sub(3));
482499
let inner = try_opt!(item.rewrite(context, budget, offset + 1));
483500
let ret = format!("({},)", inner);
484501
wrap_str(ret, context.config.max_width, budget, offset + 1)
485502
} else {
503+
let budget = try_opt!(width.checked_sub(2));
486504
let items = itemize_list(context.codemap,
487505
tup_ret.iter(),
488506
")",
489507
|item| item.span.lo,
490508
|item| item.span.hi,
491509
|item| item.rewrite(context, budget, offset + 1),
492-
tup_ret[0].span.lo,
510+
span_after(self.span, "(", context.codemap),
493511
self.span.hi);
494512

495-
496513
list_helper(items, budget, offset + 1, context.config, ListTactic::Mixed)
497514
.map(|s| format!("({})", s))
498515
}
499516
}
500-
_ => wrap_str(pprust::ty_to_string(self),
501-
context.config.max_width,
502-
width,
503-
offset),
517+
ast::TyPolyTraitRef(ref trait_ref) => trait_ref.rewrite(context, width, offset),
518+
ast::TyPath(ref q_self, ref path) => {
519+
rewrite_path(context, q_self.as_ref(), path, width, offset)
520+
}
521+
ast::TyFixedLengthVec(ref ty, ref repeats) => {
522+
rewrite_pair(&**ty, &**repeats, "[", "; ", "]", context, width, offset)
523+
}
524+
ast::TyInfer => {
525+
if width >= 1 {
526+
Some("_".to_owned())
527+
} else {
528+
None
529+
}
530+
}
531+
ast::TyBareFn(..) => {
532+
wrap_str(pprust::ty_to_string(self),
533+
context.config.max_width,
534+
width,
535+
offset)
536+
}
537+
ast::TyMac(..) | ast::TyTypeof(..) => unreachable!(),
504538
}
505539
}
506540
}

src/utils.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ pub fn extra_offset(text: &str, offset: Indent) -> usize {
3232
#[inline]
3333
pub fn span_after(original: Span, needle: &str, codemap: &CodeMap) -> BytePos {
3434
let snippet = codemap.span_to_snippet(original).unwrap();
35+
let offset = snippet.find_uncommented(needle).unwrap() + needle.len();
3536

36-
original.lo + BytePos(snippet.find_uncommented(needle).unwrap() as u32 + 1)
37+
original.lo + BytePos(offset as u32)
3738
}
3839

3940
#[inline]

tests/source/type.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
fn types() {
2+
let x: [ Vec < _ > ] = [];
3+
let y: * mut [ SomeType ; konst_funk() ] = expr();
4+
let z: (/*#digits*/ usize, /*exp*/ i16) = funk();
5+
}

tests/target/type.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
fn types() {
2+
let x: [Vec<_>] = [];
3+
let y: *mut [SomeType; konst_funk()] = expr();
4+
let z: (/* #digits */ usize, /* exp */ i16) = funk();
5+
}

0 commit comments

Comments
 (0)