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