Skip to content

Commit 84ffc31

Browse files
scampitopecongiro
authored andcommitted
fix span of Try expression when converting from try! macro to the ? keyword (#3940)
1 parent 8c33e22 commit 84ffc31

File tree

4 files changed

+63
-20
lines changed

4 files changed

+63
-20
lines changed

src/chains.rs

+12-19
Original file line numberDiff line numberDiff line change
@@ -331,33 +331,26 @@ impl Chain {
331331
}
332332
}
333333
while let Some(chain_item) = iter.next() {
334-
let comment_snippet = context.snippet(chain_item.span);
335-
// FIXME: Figure out the way to get a correct span when converting `try!` to `?`.
336-
let handle_comment =
337-
!(context.config.use_try_shorthand() || is_tries(comment_snippet.trim()));
338-
339334
// Pre-comment
340-
if handle_comment {
341-
let pre_comment_span = mk_sp(prev_span_end, chain_item.span.lo());
342-
let pre_comment_snippet = trim_tries(context.snippet(pre_comment_span));
343-
let (pre_comment, _) = extract_pre_comment(&pre_comment_snippet);
344-
match pre_comment {
345-
Some(ref comment) if !comment.is_empty() => {
346-
children.push(ChainItem::comment(
347-
pre_comment_span,
348-
comment.to_owned(),
349-
CommentPosition::Top,
350-
));
351-
}
352-
_ => (),
335+
let pre_comment_span = mk_sp(prev_span_end, chain_item.span.lo());
336+
let pre_comment_snippet = trim_tries(context.snippet(pre_comment_span));
337+
let (pre_comment, _) = extract_pre_comment(&pre_comment_snippet);
338+
match pre_comment {
339+
Some(ref comment) if !comment.is_empty() => {
340+
children.push(ChainItem::comment(
341+
pre_comment_span,
342+
comment.to_owned(),
343+
CommentPosition::Top,
344+
));
353345
}
346+
_ => (),
354347
}
355348

356349
prev_span_end = chain_item.span.hi();
357350
children.push(chain_item);
358351

359352
// Post-comment
360-
if !handle_comment || iter.peek().is_none() {
353+
if iter.peek().is_none() {
361354
continue;
362355
}
363356

src/macros.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1199,9 +1199,13 @@ pub(crate) fn convert_try_mac(mac: &ast::Mac, context: &RewriteContext<'_>) -> O
11991199
let ts: TokenStream = mac.tts.clone();
12001200
let mut parser = new_parser_from_tts(context.parse_sess.inner(), ts.trees().collect());
12011201

1202+
let mut kind = parser.parse_expr().ok()?;
1203+
// take the end pos of mac so that the Try expression includes the closing parenthesis of
1204+
// the try! macro
1205+
kind.span = mk_sp(kind.span.lo(), mac.span.hi());
12021206
Some(ast::Expr {
12031207
id: ast::NodeId::root(), // dummy value
1204-
kind: ast::ExprKind::Try(parser.parse_expr().ok()?),
1208+
kind: ast::ExprKind::Try(kind),
12051209
span: mac.span, // incorrect span, but shouldn't matter too much
12061210
attrs: ThinVec::new(),
12071211
})

tests/source/issue-3908.rs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// rustfmt-use_try_shorthand: true
2+
3+
macro_rules! foo {
4+
() => {
5+
bar1()
6+
// baz1
7+
.expect("Qux1");
8+
try!(try!(try!(bar2())
9+
// baz2a
10+
.expect("Qux2a"))
11+
// baz2b
12+
.expect("Qux2b"));
13+
bar3()?
14+
// baz3
15+
.expect("Qux3")?;
16+
};
17+
}
18+
19+
fn foo() {
20+
try!(try!(bar4())
21+
// baz4
22+
.expect("Qux4"));
23+
}

tests/target/issue-3908.rs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// rustfmt-use_try_shorthand: true
2+
3+
macro_rules! foo {
4+
() => {
5+
bar1()
6+
// baz1
7+
.expect("Qux1");
8+
bar2()?
9+
// baz2a
10+
.expect("Qux2a")?
11+
// baz2b
12+
.expect("Qux2b")?;
13+
bar3()?
14+
// baz3
15+
.expect("Qux3")?;
16+
};
17+
}
18+
19+
fn foo() {
20+
bar4()?
21+
// baz4
22+
.expect("Qux4")?;
23+
}

0 commit comments

Comments
 (0)