@@ -194,10 +194,10 @@ pub(crate) fn format_expr(
194
194
rewrite_path ( context, PathContext :: Expr , qself, path, shape) . ok ( )
195
195
}
196
196
ast:: ExprKind :: Assign ( ref lhs, ref rhs, _) => {
197
- rewrite_assignment ( context, lhs, rhs, None , shape)
197
+ rewrite_assignment ( context, lhs, rhs, None , shape) . ok ( )
198
198
}
199
199
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 ( )
201
201
}
202
202
ast:: ExprKind :: Continue ( ref opt_label) => {
203
203
let id_str = match * opt_label {
@@ -2050,15 +2050,21 @@ fn rewrite_assignment(
2050
2050
rhs : & ast:: Expr ,
2051
2051
op : Option < & ast:: BinOp > ,
2052
2052
shape : Shape ,
2053
- ) -> Option < String > {
2053
+ ) -> RewriteResult {
2054
2054
let operator_str = match op {
2055
2055
Some ( op) => context. snippet ( op. span ) ,
2056
2056
None => "=" ,
2057
2057
} ;
2058
2058
2059
2059
// 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
+ ) ;
2062
2068
2063
2069
rewrite_assign_rhs (
2064
2070
context,
@@ -2089,7 +2095,7 @@ pub(crate) fn rewrite_assign_rhs<S: Into<String>, R: Rewrite>(
2089
2095
ex : & R ,
2090
2096
rhs_kind : & RhsAssignKind < ' _ > ,
2091
2097
shape : Shape ,
2092
- ) -> Option < String > {
2098
+ ) -> RewriteResult {
2093
2099
rewrite_assign_rhs_with ( context, lhs, ex, shape, rhs_kind, RhsTactics :: Default )
2094
2100
}
2095
2101
@@ -2100,7 +2106,7 @@ pub(crate) fn rewrite_assign_rhs_expr<R: Rewrite>(
2100
2106
shape : Shape ,
2101
2107
rhs_kind : & RhsAssignKind < ' _ > ,
2102
2108
rhs_tactics : RhsTactics ,
2103
- ) -> Option < String > {
2109
+ ) -> RewriteResult {
2104
2110
let last_line_width = last_line_width ( lhs) . saturating_sub ( if lhs. contains ( '\n' ) {
2105
2111
shape. indent . width ( )
2106
2112
} else {
@@ -2122,7 +2128,7 @@ pub(crate) fn rewrite_assign_rhs_expr<R: Rewrite>(
2122
2128
context,
2123
2129
ex,
2124
2130
orig_shape,
2125
- ex. rewrite ( context, orig_shape) ,
2131
+ ex. rewrite_result ( context, orig_shape) ,
2126
2132
rhs_kind,
2127
2133
rhs_tactics,
2128
2134
has_rhs_comment,
@@ -2136,10 +2142,10 @@ pub(crate) fn rewrite_assign_rhs_with<S: Into<String>, R: Rewrite>(
2136
2142
shape : Shape ,
2137
2143
rhs_kind : & RhsAssignKind < ' _ > ,
2138
2144
rhs_tactics : RhsTactics ,
2139
- ) -> Option < String > {
2145
+ ) -> RewriteResult {
2140
2146
let lhs = lhs. into ( ) ;
2141
2147
let rhs = rewrite_assign_rhs_expr ( context, & lhs, ex, shape, rhs_kind, rhs_tactics) ?;
2142
- Some ( lhs + & rhs)
2148
+ Ok ( lhs + & rhs)
2143
2149
}
2144
2150
2145
2151
pub ( 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
2161
2167
} else {
2162
2168
shape
2163
2169
} ;
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) ?;
2166
2171
if contains_comment {
2167
2172
let rhs = rhs. trim_start ( ) ;
2168
2173
combine_strs_with_missing_comments ( context, & lhs, rhs, between_span, shape, allow_extend)
@@ -2175,48 +2180,53 @@ fn choose_rhs<R: Rewrite>(
2175
2180
context : & RewriteContext < ' _ > ,
2176
2181
expr : & R ,
2177
2182
shape : Shape ,
2178
- orig_rhs : Option < String > ,
2183
+ orig_rhs : RewriteResult ,
2179
2184
_rhs_kind : & RhsAssignKind < ' _ > ,
2180
2185
rhs_tactics : RhsTactics ,
2181
2186
has_rhs_comment : bool ,
2182
- ) -> Option < String > {
2187
+ ) -> RewriteResult {
2183
2188
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}" ) )
2189
2192
}
2190
2193
_ => {
2191
2194
// Expression did not fit on the same line as the identifier.
2192
2195
// 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) ;
2195
2202
let new_indent_str = & shape
2196
2203
. indent
2197
2204
. block_indent ( context. config )
2198
2205
. to_string_with_newline ( context. config ) ;
2199
2206
let before_space_str = if has_rhs_comment { "" } else { " " } ;
2200
2207
2201
2208
match ( orig_rhs, new_rhs) {
2202
- ( Some ( ref orig_rhs) , Some ( ref new_rhs) )
2209
+ ( Ok ( ref orig_rhs) , Ok ( ref new_rhs) )
2203
2210
if !filtered_str_fits ( & new_rhs, context. config . max_width ( ) , new_shape) =>
2204
2211
{
2205
- Some ( format ! ( "{before_space_str}{orig_rhs}" ) )
2212
+ Ok ( format ! ( "{before_space_str}{orig_rhs}" ) )
2206
2213
}
2207
- ( Some ( ref orig_rhs) , Some ( ref new_rhs) )
2214
+ ( Ok ( ref orig_rhs) , Ok ( ref new_rhs) )
2208
2215
if prefer_next_line ( orig_rhs, new_rhs, rhs_tactics) =>
2209
2216
{
2210
- Some ( format ! ( "{new_indent_str}{new_rhs}" ) )
2217
+ Ok ( format ! ( "{new_indent_str}{new_rhs}" ) )
2211
2218
}
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 => {
2214
2221
let shape = shape. infinite_width ( ) ;
2215
- expr. rewrite ( context, shape)
2222
+ expr. rewrite_result ( context, shape)
2216
2223
. map ( |s| format ! ( "{}{}" , before_space_str, s) )
2217
2224
}
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}" ) ) ,
2220
2230
}
2221
2231
}
2222
2232
}
0 commit comments