Skip to content

Commit d749532

Browse files
committed
Work around removal of beginning_vert field from ast::Arm
`ast::Arm` used to have `beginning_vert` field whose type is `Option<Span>` and holds a span of the beginning `|` if available. This field is now removed. This commit works around that. Since we only need a `BytePos` of the `|`, the type of `beginning_vert` in `ArmWrapper` is `Option<BytePos>`.
1 parent 5416c4d commit d749532

File tree

2 files changed

+53
-13
lines changed

2 files changed

+53
-13
lines changed

src/expr.rs

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,23 +1231,43 @@ pub fn is_unsafe_block(block: &ast::Block) -> bool {
12311231
}
12321232
}
12331233

1234-
// A simple wrapper type against ast::Arm. Used inside write_list().
1234+
/// A simple wrapper type against ast::Arm. Used inside write_list().
12351235
struct ArmWrapper<'a> {
12361236
pub arm: &'a ast::Arm,
1237-
// True if the arm is the last one in match expression. Used to decide on whether we should add
1238-
// trailing comma to the match arm when `config.trailing_comma() == Never`.
1237+
/// True if the arm is the last one in match expression. Used to decide on whether we should add
1238+
/// trailing comma to the match arm when `config.trailing_comma() == Never`.
12391239
pub is_last: bool,
1240+
/// Holds a byte position of `|` at the beginning of the arm pattern, if available.
1241+
pub beginning_vert: Option<BytePos>,
12401242
}
12411243

12421244
impl<'a> ArmWrapper<'a> {
1243-
pub fn new(arm: &'a ast::Arm, is_last: bool) -> ArmWrapper<'a> {
1244-
ArmWrapper { arm, is_last }
1245+
pub fn new(
1246+
arm: &'a ast::Arm,
1247+
is_last: bool,
1248+
beginning_vert: Option<BytePos>,
1249+
) -> ArmWrapper<'a> {
1250+
ArmWrapper {
1251+
arm,
1252+
is_last,
1253+
beginning_vert,
1254+
}
1255+
}
1256+
}
1257+
1258+
impl<'a> Spanned for ArmWrapper<'a> {
1259+
fn span(&self) -> Span {
1260+
if let Some(lo) = self.beginning_vert {
1261+
mk_sp(lo, self.arm.span().hi())
1262+
} else {
1263+
self.arm.span()
1264+
}
12451265
}
12461266
}
12471267

12481268
impl<'a> Rewrite for ArmWrapper<'a> {
12491269
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
1250-
rewrite_match_arm(context, self.arm, shape, self.is_last)
1270+
rewrite_match_arm(context, self.arm, shape, self.is_last, self.beginning_vert)
12511271
}
12521272
}
12531273

@@ -1344,6 +1364,23 @@ fn arm_comma(config: &Config, body: &ast::Expr, is_last: bool) -> &'static str {
13441364
}
13451365
}
13461366

1367+
/// Collect a byte position of the beginning `|` for each arm, if available.
1368+
fn collect_beginning_verts(
1369+
context: &RewriteContext,
1370+
arms: &[ast::Arm],
1371+
span: Span,
1372+
) -> Vec<Option<BytePos>> {
1373+
let mut beginning_verts = Vec::with_capacity(arms.len());
1374+
let mut lo = context.snippet_provider.span_after(span, "{");
1375+
for arm in arms {
1376+
let hi = arm.pats[0].span.lo();
1377+
let missing_span = mk_sp(lo, hi);
1378+
beginning_verts.push(context.snippet_provider.opt_span_before(missing_span, "|"));
1379+
lo = arm.span().hi();
1380+
}
1381+
beginning_verts
1382+
}
1383+
13471384
fn rewrite_match_arms(
13481385
context: &RewriteContext,
13491386
arms: &[ast::Arm],
@@ -1359,15 +1396,17 @@ fn rewrite_match_arms(
13591396
let is_last_iter = repeat(false)
13601397
.take(arm_len.checked_sub(1).unwrap_or(0))
13611398
.chain(repeat(true));
1399+
let beginning_verts = collect_beginning_verts(context, arms, span);
13621400
let items = itemize_list(
13631401
context.snippet_provider,
13641402
arms.iter()
13651403
.zip(is_last_iter)
1366-
.map(|(arm, is_last)| ArmWrapper::new(arm, is_last)),
1404+
.zip(beginning_verts.into_iter())
1405+
.map(|((arm, is_last), beginning_vert)| ArmWrapper::new(arm, is_last, beginning_vert)),
13671406
"}",
13681407
"|",
1369-
|arm| arm.arm.span().lo(),
1370-
|arm| arm.arm.span().hi(),
1408+
|arm| arm.span().lo(),
1409+
|arm| arm.span().hi(),
13711410
|arm| arm.rewrite(context, arm_shape),
13721411
open_brace_pos,
13731412
span.hi(),
@@ -1394,6 +1433,7 @@ fn rewrite_match_arm(
13941433
arm: &ast::Arm,
13951434
shape: Shape,
13961435
is_last: bool,
1436+
beginning_vert: Option<BytePos>,
13971437
) -> Option<String> {
13981438
let (missing_span, attrs_str) = if !arm.attrs.is_empty() {
13991439
if contains_skip(&arm.attrs) {
@@ -1417,7 +1457,7 @@ fn rewrite_match_arm(
14171457
context,
14181458
&arm.pats,
14191459
&arm.guard,
1420-
arm.beginning_vert.is_some(),
1460+
beginning_vert.is_some(),
14211461
shape,
14221462
).and_then(|pats_str| {
14231463
combine_strs_with_missing_comments(

src/spanned.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,10 @@ impl Spanned for ast::Ty {
8989

9090
impl Spanned for ast::Arm {
9191
fn span(&self) -> Span {
92-
let lo = if let Some(sp) = self.beginning_vert {
93-
sp.lo()
94-
} else {
92+
let lo = if self.attrs.is_empty() {
9593
self.pats[0].span.lo()
94+
} else {
95+
self.attrs[0].span.lo()
9696
};
9797
span_with_attrs_lo_hi!(self, lo, self.body.span.hi())
9898
}

0 commit comments

Comments
 (0)