@@ -2,7 +2,7 @@ use std::iter::ExactSizeIterator;
2
2
use std:: ops:: Deref ;
3
3
4
4
use rustc_ast:: ast:: { self , FnRetTy , Mutability } ;
5
- use rustc_span:: { symbol:: kw, BytePos , Span } ;
5
+ use rustc_span:: { symbol:: kw, BytePos , Pos , Span } ;
6
6
7
7
use crate :: config:: lists:: * ;
8
8
use crate :: config:: { IndentStyle , TypeDensity , Version } ;
@@ -648,37 +648,72 @@ impl Rewrite for ast::Ty {
648
648
ast:: TyKind :: Rptr ( ref lifetime, ref mt) => {
649
649
let mut_str = format_mutability ( mt. mutbl ) ;
650
650
let mut_len = mut_str. len ( ) ;
651
- Some ( match * lifetime {
652
- Some ( ref lifetime) => {
653
- let lt_budget = shape. width . checked_sub ( 2 + mut_len) ?;
654
- let lt_str = lifetime. rewrite (
651
+ let mut result = String :: with_capacity ( 128 ) ;
652
+ result. push_str ( "&" ) ;
653
+ let ref_hi = context. snippet_provider . span_after ( self . span ( ) , "&" ) ;
654
+ let mut cmnt_lo = ref_hi;
655
+
656
+ if let Some ( ref lifetime) = * lifetime {
657
+ let lt_budget = shape. width . checked_sub ( 2 + mut_len) ?;
658
+ let lt_str = lifetime. rewrite (
659
+ context,
660
+ Shape :: legacy ( lt_budget, shape. indent + 2 + mut_len) ,
661
+ ) ?;
662
+ let before_lt_span = mk_sp ( cmnt_lo, lifetime. ident . span . lo ( ) ) ;
663
+ if contains_comment ( context. snippet ( before_lt_span) ) {
664
+ result = combine_strs_with_missing_comments (
655
665
context,
656
- Shape :: legacy ( lt_budget, shape. indent + 2 + mut_len) ,
666
+ & result,
667
+ & lt_str,
668
+ before_lt_span,
669
+ shape,
670
+ true ,
657
671
) ?;
658
- let lt_len = lt_str. len ( ) ;
659
- let budget = shape. width . checked_sub ( 2 + mut_len + lt_len) ?;
660
- format ! (
661
- "&{} {}{}" ,
662
- lt_str,
663
- mut_str,
664
- mt. ty. rewrite(
665
- context,
666
- Shape :: legacy( budget, shape. indent + 2 + mut_len + lt_len)
667
- ) ?
668
- )
672
+ } else {
673
+ result. push_str ( & lt_str) ;
669
674
}
670
- None => {
671
- let budget = shape. width . checked_sub ( 1 + mut_len) ?;
672
- format ! (
673
- "&{}{}" ,
675
+ result. push_str ( " " ) ;
676
+ cmnt_lo = lifetime. ident . span . hi ( ) ;
677
+ }
678
+
679
+ if ast:: Mutability :: Mut == mt. mutbl {
680
+ let mut_hi = context. snippet_provider . span_after ( self . span ( ) , "mut" ) ;
681
+ let before_mut_span = mk_sp ( cmnt_lo, mut_hi - BytePos :: from_usize ( 3 ) ) ;
682
+ if contains_comment ( context. snippet ( before_mut_span) ) {
683
+ result = combine_strs_with_missing_comments (
684
+ context,
685
+ result. trim_end ( ) ,
674
686
mut_str,
675
- mt. ty. rewrite(
676
- context,
677
- Shape :: legacy( budget, shape. indent + 1 + mut_len)
678
- ) ?
679
- )
687
+ before_mut_span,
688
+ shape,
689
+ true ,
690
+ ) ?;
691
+ } else {
692
+ result. push_str ( mut_str) ;
680
693
}
681
- } )
694
+ cmnt_lo = mut_hi;
695
+ }
696
+
697
+ let before_ty_span = mk_sp ( cmnt_lo, mt. ty . span . lo ( ) ) ;
698
+ if contains_comment ( context. snippet ( before_ty_span) ) {
699
+ result = combine_strs_with_missing_comments (
700
+ context,
701
+ result. trim_end ( ) ,
702
+ & mt. ty . rewrite ( & context, shape) ?,
703
+ before_ty_span,
704
+ shape,
705
+ true ,
706
+ ) ?;
707
+ } else {
708
+ let used_width = last_line_width ( & result) ;
709
+ let budget = shape. width . checked_sub ( used_width) ?;
710
+ let ty_str = mt
711
+ . ty
712
+ . rewrite ( & context, Shape :: legacy ( budget, shape. indent + used_width) ) ?;
713
+ result. push_str ( & ty_str) ;
714
+ }
715
+
716
+ Some ( result)
682
717
}
683
718
// FIXME: we drop any comments here, even though it's a silly place to put
684
719
// comments.
0 commit comments