Skip to content

Commit 83e4eed

Browse files
committed
Auto merge of #62638 - estebank:issue-62554, r=petrochenkov
Use snippet instead of pprinting statement Fix #62554.
2 parents 85a360e + a86aac1 commit 83e4eed

File tree

6 files changed

+52
-22
lines changed

6 files changed

+52
-22
lines changed

src/libsyntax/parse/parser.rs

+13-17
Original file line numberDiff line numberDiff line change
@@ -4378,9 +4378,10 @@ impl<'a> Parser<'a> {
43784378
Ok(Some(self.mk_item(span, ident, ItemKind::MacroDef(def), vis.clone(), attrs.to_vec())))
43794379
}
43804380

4381-
fn parse_stmt_without_recovery(&mut self,
4382-
macro_legacy_warnings: bool)
4383-
-> PResult<'a, Option<Stmt>> {
4381+
fn parse_stmt_without_recovery(
4382+
&mut self,
4383+
macro_legacy_warnings: bool,
4384+
) -> PResult<'a, Option<Stmt>> {
43844385
maybe_whole!(self, NtStmt, |x| Some(x));
43854386

43864387
let attrs = self.parse_outer_attributes()?;
@@ -4586,20 +4587,15 @@ impl<'a> Parser<'a> {
45864587
if self.eat(&token::Semi) {
45874588
stmt_span = stmt_span.with_hi(self.prev_span.hi());
45884589
}
4589-
let sugg = pprust::to_string(|s| {
4590-
use crate::print::pprust::INDENT_UNIT;
4591-
s.ibox(INDENT_UNIT);
4592-
s.bopen();
4593-
s.print_stmt(&stmt);
4594-
s.bclose_maybe_open(stmt.span, false)
4595-
});
4596-
e.span_suggestion(
4597-
stmt_span,
4598-
"try placing this code inside a block",
4599-
sugg,
4600-
// speculative, has been misleading in the past (closed Issue #46836)
4601-
Applicability::MaybeIncorrect
4602-
);
4590+
if let Ok(snippet) = self.sess.source_map().span_to_snippet(stmt_span) {
4591+
e.span_suggestion(
4592+
stmt_span,
4593+
"try placing this code inside a block",
4594+
format!("{{ {} }}", snippet),
4595+
// speculative, has been misleading in the past (#46836)
4596+
Applicability::MaybeIncorrect,
4597+
);
4598+
}
46034599
}
46044600
Err(mut e) => {
46054601
self.recover_stmt_(SemiColonMode::Break, BlockMode::Ignore);

src/libsyntax/print/pprust.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -745,8 +745,7 @@ impl<'a> State<'a> {
745745
self.bclose_maybe_open(span, true)
746746
}
747747

748-
crate fn break_offset_if_not_bol(&mut self, n: usize,
749-
off: isize) {
748+
crate fn break_offset_if_not_bol(&mut self, n: usize, off: isize) {
750749
if !self.s.is_beginning_of_line() {
751750
self.s.break_offset(n, off)
752751
} else {

src/test/ui/issues/issue-39848.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: expected `{`, found `foo`
22
--> $DIR/issue-39848.rs:8:19
33
|
44
LL | if $tgt.has_$field() {}
5-
| -- - help: try placing this code inside a block: `{ foo(); }`
5+
| -- - help: try placing this code inside a block: `{ ) }`
66
| |
77
| this `if` statement has a condition, but no block
88
...

src/test/ui/issues/issue-62554.rs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
fn main() {}
2+
3+
fn foo(u: u8) { if u8 macro_rules! u8 { (u6) => { fn uuuuuuuuuuu() { use s loo mod u8 {
4+
//~^ ERROR expected `{`, found `macro_rules`
5+
//~ ERROR this file contains an un-closed delimiter

src/test/ui/issues/issue-62554.stderr

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
error: this file contains an un-closed delimiter
2+
--> $DIR/issue-62554.rs:5:53
3+
|
4+
LL | fn foo(u: u8) { if u8 macro_rules! u8 { (u6) => { fn uuuuuuuuuuu() { use s loo mod u8 {
5+
| - - - - - un-closed delimiter
6+
| | | | |
7+
| | | | un-closed delimiter
8+
| | | un-closed delimiter
9+
| un-closed delimiter un-closed delimiter
10+
LL |
11+
LL |
12+
| ^
13+
14+
error: expected `{`, found `macro_rules`
15+
--> $DIR/issue-62554.rs:3:23
16+
|
17+
LL | fn foo(u: u8) { if u8 macro_rules! u8 { (u6) => { fn uuuuuuuuuuu() { use s loo mod u8 {
18+
| -- ^^^^^^^^^^^ expected `{`
19+
| |
20+
| this `if` statement has a condition, but no block
21+
help: try placing this code inside a block
22+
|
23+
LL | fn foo(u: u8) { if u8 { macro_rules! u8 { (u6) => { fn uuuuuuuuuuu() { use s loo mod u8 {
24+
LL |
25+
LL |
26+
LL | }
27+
|
28+
29+
error: aborting due to 2 previous errors
30+

src/test/ui/label/label_break_value_illegal_uses.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ LL | if true 'b: {}
1111
| -- ^^----
1212
| | |
1313
| | expected `{`
14-
| | help: try placing this code inside a block: `{ 'b: { } }`
14+
| | help: try placing this code inside a block: `{ 'b: {} }`
1515
| this `if` statement has a condition, but no block
1616

1717
error: expected `{`, found `'b`
@@ -21,7 +21,7 @@ LL | if true {} else 'b: {}
2121
| ^^----
2222
| |
2323
| expected `{`
24-
| help: try placing this code inside a block: `{ 'b: { } }`
24+
| help: try placing this code inside a block: `{ 'b: {} }`
2525

2626
error: expected one of `.`, `?`, `{`, or an operator, found `'b`
2727
--> $DIR/label_break_value_illegal_uses.rs:18:17

0 commit comments

Comments
 (0)