@@ -194,10 +194,10 @@ pub(crate) fn format_expr(
194194 rewrite_path ( context, PathContext :: Expr , qself, path, shape) . ok ( )
195195 }
196196 ast:: ExprKind :: Assign ( ref lhs, ref rhs, _) => {
197- rewrite_assignment ( context, lhs, rhs, None , shape)
197+ rewrite_assignment ( context, lhs, rhs, None , shape) . ok ( )
198198 }
199199 ast:: ExprKind :: AssignOp ( ref op, ref lhs, ref rhs) => {
200- rewrite_assignment ( context, lhs, rhs, Some ( op) , shape)
200+ rewrite_assignment ( context, lhs, rhs, Some ( op) , shape) . ok ( )
201201 }
202202 ast:: ExprKind :: Continue ( ref opt_label) => {
203203 let id_str = match * opt_label {
@@ -2050,15 +2050,21 @@ fn rewrite_assignment(
20502050 rhs : & ast:: Expr ,
20512051 op : Option < & ast:: BinOp > ,
20522052 shape : Shape ,
2053- ) -> Option < String > {
2053+ ) -> RewriteResult {
20542054 let operator_str = match op {
20552055 Some ( op) => context. snippet ( op. span ) ,
20562056 None => "=" ,
20572057 } ;
20582058
20592059 // 1 = space between lhs and operator.
2060- let lhs_shape = shape. sub_width ( operator_str. len ( ) + 1 ) ?;
2061- let lhs_str = format ! ( "{} {}" , lhs. rewrite( context, lhs_shape) ?, operator_str) ;
2060+ let lhs_shape = shape
2061+ . sub_width ( operator_str. len ( ) + 1 )
2062+ . max_width_error ( shape. width , lhs. span ( ) ) ?;
2063+ let lhs_str = format ! (
2064+ "{} {}" ,
2065+ lhs. rewrite_result( context, lhs_shape) ?,
2066+ operator_str
2067+ ) ;
20622068
20632069 rewrite_assign_rhs (
20642070 context,
@@ -2089,7 +2095,7 @@ pub(crate) fn rewrite_assign_rhs<S: Into<String>, R: Rewrite>(
20892095 ex : & R ,
20902096 rhs_kind : & RhsAssignKind < ' _ > ,
20912097 shape : Shape ,
2092- ) -> Option < String > {
2098+ ) -> RewriteResult {
20932099 rewrite_assign_rhs_with ( context, lhs, ex, shape, rhs_kind, RhsTactics :: Default )
20942100}
20952101
@@ -2100,7 +2106,7 @@ pub(crate) fn rewrite_assign_rhs_expr<R: Rewrite>(
21002106 shape : Shape ,
21012107 rhs_kind : & RhsAssignKind < ' _ > ,
21022108 rhs_tactics : RhsTactics ,
2103- ) -> Option < String > {
2109+ ) -> RewriteResult {
21042110 let last_line_width = last_line_width ( lhs) . saturating_sub ( if lhs. contains ( '\n' ) {
21052111 shape. indent . width ( )
21062112 } else {
@@ -2122,7 +2128,7 @@ pub(crate) fn rewrite_assign_rhs_expr<R: Rewrite>(
21222128 context,
21232129 ex,
21242130 orig_shape,
2125- ex. rewrite ( context, orig_shape) ,
2131+ ex. rewrite_result ( context, orig_shape) ,
21262132 rhs_kind,
21272133 rhs_tactics,
21282134 has_rhs_comment,
@@ -2136,10 +2142,10 @@ pub(crate) fn rewrite_assign_rhs_with<S: Into<String>, R: Rewrite>(
21362142 shape : Shape ,
21372143 rhs_kind : & RhsAssignKind < ' _ > ,
21382144 rhs_tactics : RhsTactics ,
2139- ) -> Option < String > {
2145+ ) -> RewriteResult {
21402146 let lhs = lhs. into ( ) ;
21412147 let rhs = rewrite_assign_rhs_expr ( context, & lhs, ex, shape, rhs_kind, rhs_tactics) ?;
2142- Some ( lhs + & rhs)
2148+ Ok ( lhs + & rhs)
21432149}
21442150
21452151pub ( crate ) fn rewrite_assign_rhs_with_comments < S : Into < String > , R : Rewrite + Spanned > (
@@ -2161,8 +2167,7 @@ pub(crate) fn rewrite_assign_rhs_with_comments<S: Into<String>, R: Rewrite + Spa
21612167 } else {
21622168 shape
21632169 } ;
2164- let rhs =
2165- rewrite_assign_rhs_expr ( context, & lhs, ex, shape, rhs_kind, rhs_tactics) . unknown_error ( ) ?;
2170+ let rhs = rewrite_assign_rhs_expr ( context, & lhs, ex, shape, rhs_kind, rhs_tactics) ?;
21662171 if contains_comment {
21672172 let rhs = rhs. trim_start ( ) ;
21682173 combine_strs_with_missing_comments ( context, & lhs, rhs, between_span, shape, allow_extend)
@@ -2175,48 +2180,53 @@ fn choose_rhs<R: Rewrite>(
21752180 context : & RewriteContext < ' _ > ,
21762181 expr : & R ,
21772182 shape : Shape ,
2178- orig_rhs : Option < String > ,
2183+ orig_rhs : RewriteResult ,
21792184 _rhs_kind : & RhsAssignKind < ' _ > ,
21802185 rhs_tactics : RhsTactics ,
21812186 has_rhs_comment : bool ,
2182- ) -> Option < String > {
2187+ ) -> RewriteResult {
21832188 match orig_rhs {
2184- Some ( ref new_str) if new_str. is_empty ( ) => Some ( String :: new ( ) ) ,
2185- Some ( ref new_str)
2186- if !new_str. contains ( '\n' ) && unicode_str_width ( new_str) <= shape. width =>
2187- {
2188- Some ( format ! ( " {new_str}" ) )
2189+ Ok ( ref new_str) if new_str. is_empty ( ) => Ok ( String :: new ( ) ) ,
2190+ Ok ( ref new_str) if !new_str. contains ( '\n' ) && unicode_str_width ( new_str) <= shape. width => {
2191+ Ok ( format ! ( " {new_str}" ) )
21892192 }
21902193 _ => {
21912194 // Expression did not fit on the same line as the identifier.
21922195 // Try splitting the line and see if that works better.
2193- let new_shape = shape_from_rhs_tactic ( context, shape, rhs_tactics) ?;
2194- let new_rhs = expr. rewrite ( context, new_shape) ;
2196+ let new_shape = shape_from_rhs_tactic ( context, shape, rhs_tactics)
2197+ // TODO(ding-young) Ideally, we can replace unknown_error() with max_width_error(),
2198+ // but this requires either implementing the Spanned trait for ast::GenericBounds
2199+ // or grabbing the span from the call site.
2200+ . unknown_error ( ) ?;
2201+ let new_rhs = expr. rewrite_result ( context, new_shape) ;
21952202 let new_indent_str = & shape
21962203 . indent
21972204 . block_indent ( context. config )
21982205 . to_string_with_newline ( context. config ) ;
21992206 let before_space_str = if has_rhs_comment { "" } else { " " } ;
22002207
22012208 match ( orig_rhs, new_rhs) {
2202- ( Some ( ref orig_rhs) , Some ( ref new_rhs) )
2209+ ( Ok ( ref orig_rhs) , Ok ( ref new_rhs) )
22032210 if !filtered_str_fits ( & new_rhs, context. config . max_width ( ) , new_shape) =>
22042211 {
2205- Some ( format ! ( "{before_space_str}{orig_rhs}" ) )
2212+ Ok ( format ! ( "{before_space_str}{orig_rhs}" ) )
22062213 }
2207- ( Some ( ref orig_rhs) , Some ( ref new_rhs) )
2214+ ( Ok ( ref orig_rhs) , Ok ( ref new_rhs) )
22082215 if prefer_next_line ( orig_rhs, new_rhs, rhs_tactics) =>
22092216 {
2210- Some ( format ! ( "{new_indent_str}{new_rhs}" ) )
2217+ Ok ( format ! ( "{new_indent_str}{new_rhs}" ) )
22112218 }
2212- ( None , Some ( ref new_rhs) ) => Some ( format ! ( "{new_indent_str}{new_rhs}" ) ) ,
2213- ( None , None ) if rhs_tactics == RhsTactics :: AllowOverflow => {
2219+ ( Err ( _ ) , Ok ( ref new_rhs) ) => Ok ( format ! ( "{new_indent_str}{new_rhs}" ) ) ,
2220+ ( Err ( _ ) , Err ( _ ) ) if rhs_tactics == RhsTactics :: AllowOverflow => {
22142221 let shape = shape. infinite_width ( ) ;
2215- expr. rewrite ( context, shape)
2222+ expr. rewrite_result ( context, shape)
22162223 . map ( |s| format ! ( "{}{}" , before_space_str, s) )
22172224 }
2218- ( None , None ) => None ,
2219- ( Some ( orig_rhs) , _) => Some ( format ! ( "{before_space_str}{orig_rhs}" ) ) ,
2225+ // When both orig_rhs and new_rhs result in errors, we currently propagate
2226+ // the error from the second attempt since it is more generous with
2227+ // width constraints. This decision is somewhat arbitrary and is open to change.
2228+ ( Err ( _) , Err ( new_rhs_err) ) => Err ( new_rhs_err) ,
2229+ ( Ok ( orig_rhs) , _) => Ok ( format ! ( "{before_space_str}{orig_rhs}" ) ) ,
22202230 }
22212231 }
22222232 }
0 commit comments