Skip to content

Commit 3cbc91e

Browse files
ding-youngytmimi
authored andcommitted
modify rewrite_path and impl rewrite_result for related AST nodes
1 parent 7cdb426 commit 3cbc91e

File tree

4 files changed

+63
-34
lines changed

4 files changed

+63
-34
lines changed

src/attr.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -276,10 +276,11 @@ impl Rewrite for ast::MetaItem {
276276
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
277277
Some(match self.kind {
278278
ast::MetaItemKind::Word => {
279-
rewrite_path(context, PathContext::Type, &None, &self.path, shape)?
279+
rewrite_path(context, PathContext::Type, &None, &self.path, shape).ok()?
280280
}
281281
ast::MetaItemKind::List(ref list) => {
282-
let path = rewrite_path(context, PathContext::Type, &None, &self.path, shape)?;
282+
let path =
283+
rewrite_path(context, PathContext::Type, &None, &self.path, shape).ok()?;
283284
let has_trailing_comma = crate::expr::span_ends_with_comma(context, self.span);
284285
overflow::rewrite_with_parens(
285286
context,
@@ -297,7 +298,8 @@ impl Rewrite for ast::MetaItem {
297298
)?
298299
}
299300
ast::MetaItemKind::NameValue(ref lit) => {
300-
let path = rewrite_path(context, PathContext::Type, &None, &self.path, shape)?;
301+
let path =
302+
rewrite_path(context, PathContext::Type, &None, &self.path, shape).ok()?;
301303
// 3 = ` = `
302304
let lit_shape = shape.shrink_left(path.len() + 3)?;
303305
// `rewrite_literal` returns `None` when `lit` exceeds max

src/expr.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ pub(crate) fn format_expr(
185185
rewrite_match(context, cond, arms, shape, expr.span, &expr.attrs, kind)
186186
}
187187
ast::ExprKind::Path(ref qself, ref path) => {
188-
rewrite_path(context, PathContext::Expr, qself, path, shape)
188+
rewrite_path(context, PathContext::Expr, qself, path, shape).ok()
189189
}
190190
ast::ExprKind::Assign(ref lhs, ref rhs, _) => {
191191
rewrite_assignment(context, lhs, rhs, None, shape)
@@ -1614,7 +1614,7 @@ fn rewrite_struct_lit<'a>(
16141614

16151615
// 2 = " {".len()
16161616
let path_shape = shape.sub_width(2)?;
1617-
let path_str = rewrite_path(context, PathContext::Expr, qself, path, path_shape)?;
1617+
let path_str = rewrite_path(context, PathContext::Expr, qself, path, path_shape).ok()?;
16181618

16191619
let has_base_or_rest = match struct_rest {
16201620
ast::StructRest::None if fields.is_empty() => return Some(format!("{path_str} {{}}")),

src/patterns.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -254,10 +254,11 @@ impl Rewrite for Pat {
254254
}
255255
PatKind::Tuple(ref items) => rewrite_tuple_pat(items, None, self.span, context, shape),
256256
PatKind::Path(ref q_self, ref path) => {
257-
rewrite_path(context, PathContext::Expr, q_self, path, shape)
257+
rewrite_path(context, PathContext::Expr, q_self, path, shape).ok()
258258
}
259259
PatKind::TupleStruct(ref q_self, ref path, ref pat_vec) => {
260-
let path_str = rewrite_path(context, PathContext::Expr, q_self, path, shape)?;
260+
let path_str =
261+
rewrite_path(context, PathContext::Expr, q_self, path, shape).ok()?;
261262
rewrite_tuple_pat(pat_vec, Some(path_str), self.span, context, shape)
262263
}
263264
PatKind::Lit(ref expr) => expr.rewrite(context, shape),
@@ -315,7 +316,7 @@ fn rewrite_struct_pat(
315316
) -> Option<String> {
316317
// 2 = ` {`
317318
let path_shape = shape.sub_width(2)?;
318-
let path_str = rewrite_path(context, PathContext::Expr, qself, path, path_shape)?;
319+
let path_str = rewrite_path(context, PathContext::Expr, qself, path, path_shape).ok()?;
319320

320321
if fields.is_empty() && !ellipsis {
321322
return Some(format!("{path_str} {{}}"));

src/types.rs

+52-26
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ pub(crate) fn rewrite_path(
4040
qself: &Option<ptr::P<ast::QSelf>>,
4141
path: &ast::Path,
4242
shape: Shape,
43-
) -> Option<String> {
43+
) -> RewriteResult {
4444
let skip_count = qself.as_ref().map_or(0, |x| x.position);
4545

4646
// 32 covers almost all path lengths measured when compiling core, and there isn't a big
@@ -56,7 +56,7 @@ pub(crate) fn rewrite_path(
5656
if let Some(qself) = qself {
5757
result.push('<');
5858

59-
let fmt_ty = qself.ty.rewrite(context, shape)?;
59+
let fmt_ty = qself.ty.rewrite_result(context, shape)?;
6060
result.push_str(&fmt_ty);
6161

6262
if skip_count > 0 {
@@ -66,7 +66,7 @@ pub(crate) fn rewrite_path(
6666
}
6767

6868
// 3 = ">::".len()
69-
let shape = shape.sub_width(3)?;
69+
let shape = shape.sub_width(3).max_width_error(shape.width, path.span)?;
7070

7171
result = rewrite_path_segments(
7272
PathContext::Type,
@@ -102,7 +102,7 @@ fn rewrite_path_segments<'a, I>(
102102
span_hi: BytePos,
103103
context: &RewriteContext<'_>,
104104
shape: Shape,
105-
) -> Option<String>
105+
) -> RewriteResult
106106
where
107107
I: Iterator<Item = &'a ast::PathSegment>,
108108
{
@@ -121,7 +121,9 @@ where
121121
}
122122

123123
let extra_offset = extra_offset(&buffer, shape);
124-
let new_shape = shape.shrink_left(extra_offset)?;
124+
let new_shape = shape
125+
.shrink_left(extra_offset)
126+
.max_width_error(shape.width, mk_sp(span_lo, span_hi))?;
125127
let segment_string = rewrite_segment(
126128
path_context,
127129
segment,
@@ -134,7 +136,7 @@ where
134136
buffer.push_str(&segment_string);
135137
}
136138

137-
Some(buffer)
139+
Ok(buffer)
138140
}
139141

140142
#[derive(Debug)]
@@ -183,8 +185,12 @@ impl<'a> Rewrite for SegmentParam<'a> {
183185

184186
impl Rewrite for ast::PreciseCapturingArg {
185187
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
188+
self.rewrite_result(context, shape).ok()
189+
}
190+
191+
fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult {
186192
match self {
187-
ast::PreciseCapturingArg::Lifetime(lt) => lt.rewrite(context, shape),
193+
ast::PreciseCapturingArg::Lifetime(lt) => lt.rewrite_result(context, shape),
188194
ast::PreciseCapturingArg::Arg(p, _) => {
189195
rewrite_path(context, PathContext::Type, &None, p, shape)
190196
}
@@ -194,13 +200,20 @@ impl Rewrite for ast::PreciseCapturingArg {
194200

195201
impl Rewrite for ast::AssocItemConstraint {
196202
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
203+
self.rewrite_result(context, shape).ok()
204+
}
205+
206+
fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult {
197207
use ast::AssocItemConstraintKind::{Bound, Equality};
198208

199209
let mut result = String::with_capacity(128);
200210
result.push_str(rewrite_ident(context, self.ident));
201211

202212
if let Some(ref gen_args) = self.gen_args {
203-
let budget = shape.width.checked_sub(result.len())?;
213+
let budget = shape
214+
.width
215+
.checked_sub(result.len())
216+
.max_width_error(shape.width, self.span)?;
204217
let shape = Shape::legacy(budget, shape.indent + result.len());
205218
let gen_str = rewrite_generic_args(gen_args, context, shape, gen_args.span())?;
206219
result.push_str(&gen_str);
@@ -213,12 +226,15 @@ impl Rewrite for ast::AssocItemConstraint {
213226
};
214227
result.push_str(infix);
215228

216-
let budget = shape.width.checked_sub(result.len())?;
229+
let budget = shape
230+
.width
231+
.checked_sub(result.len())
232+
.max_width_error(shape.width, self.span)?;
217233
let shape = Shape::legacy(budget, shape.indent + result.len());
218-
let rewrite = self.kind.rewrite(context, shape)?;
234+
let rewrite = self.kind.rewrite_result(context, shape)?;
219235
result.push_str(&rewrite);
220236

221-
Some(result)
237+
Ok(result)
222238
}
223239
}
224240

@@ -255,16 +271,17 @@ fn rewrite_segment(
255271
span_hi: BytePos,
256272
context: &RewriteContext<'_>,
257273
shape: Shape,
258-
) -> Option<String> {
274+
) -> RewriteResult {
259275
let mut result = String::with_capacity(128);
260276
result.push_str(rewrite_ident(context, segment.ident));
261277

262278
let ident_len = result.len();
263279
let shape = if context.use_block_indent() {
264-
shape.offset_left(ident_len)?
280+
shape.offset_left(ident_len)
265281
} else {
266-
shape.shrink_left(ident_len)?
267-
};
282+
shape.shrink_left(ident_len)
283+
}
284+
.max_width_error(shape.width, mk_sp(*span_lo, span_hi))?;
268285

269286
if let Some(ref args) = segment.args {
270287
let generics_str = rewrite_generic_args(args, context, shape, mk_sp(*span_lo, span_hi))?;
@@ -295,7 +312,7 @@ fn rewrite_segment(
295312
result.push_str(&generics_str)
296313
}
297314

298-
Some(result)
315+
Ok(result)
299316
}
300317

301318
fn format_function_type<'a, I>(
@@ -498,7 +515,7 @@ fn rewrite_generic_args(
498515
context: &RewriteContext<'_>,
499516
shape: Shape,
500517
span: Span,
501-
) -> Option<String> {
518+
) -> RewriteResult {
502519
match gen_args {
503520
ast::GenericArgs::AngleBracketed(ref data) if !data.args.is_empty() => {
504521
let args = data
@@ -515,6 +532,7 @@ fn rewrite_generic_args(
515532
.collect::<Vec<_>>();
516533

517534
overflow::rewrite_with_angle_brackets(context, "", args.iter(), shape, span)
535+
.unknown_error()
518536
}
519537
ast::GenericArgs::Parenthesized(ref data) => format_function_type(
520538
data.inputs.iter().map(|x| &**x),
@@ -523,9 +541,8 @@ fn rewrite_generic_args(
523541
data.span,
524542
context,
525543
shape,
526-
)
527-
.ok(),
528-
_ => Some("".to_owned()),
544+
),
545+
_ => Ok("".to_owned()),
529546
}
530547
}
531548

@@ -717,23 +734,32 @@ impl Rewrite for ast::GenericParam {
717734

718735
impl Rewrite for ast::PolyTraitRef {
719736
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
737+
self.rewrite_result(context, shape).ok()
738+
}
739+
740+
fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult {
720741
if let Some(lifetime_str) = rewrite_bound_params(context, shape, &self.bound_generic_params)
721742
{
722743
// 6 is "for<> ".len()
723744
let extra_offset = lifetime_str.len() + 6;
724-
let path_str = self
725-
.trait_ref
726-
.rewrite(context, shape.offset_left(extra_offset)?)?;
745+
let shape = shape
746+
.offset_left(extra_offset)
747+
.max_width_error(shape.width, self.span)?;
748+
let path_str = self.trait_ref.rewrite_result(context, shape)?;
727749

728-
Some(format!("for<{lifetime_str}> {path_str}"))
750+
Ok(format!("for<{lifetime_str}> {path_str}"))
729751
} else {
730-
self.trait_ref.rewrite(context, shape)
752+
self.trait_ref.rewrite_result(context, shape)
731753
}
732754
}
733755
}
734756

735757
impl Rewrite for ast::TraitRef {
736758
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
759+
self.rewrite_result(context, shape).ok()
760+
}
761+
762+
fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult {
737763
rewrite_path(context, PathContext::Type, &None, &self.path, shape)
738764
}
739765
}
@@ -912,7 +938,7 @@ impl Rewrite for ast::Ty {
912938
ast::TyKind::AnonStruct(..) => Ok(context.snippet(self.span).to_owned()),
913939
ast::TyKind::AnonUnion(..) => Ok(context.snippet(self.span).to_owned()),
914940
ast::TyKind::Path(ref q_self, ref path) => {
915-
rewrite_path(context, PathContext::Type, q_self, path, shape).unknown_error()
941+
rewrite_path(context, PathContext::Type, q_self, path, shape)
916942
}
917943
ast::TyKind::Array(ref ty, ref repeats) => rewrite_pair(
918944
&**ty,

0 commit comments

Comments
 (0)