Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use snippet instead of pprinting statement #62638

Merged
merged 2 commits into from
Jul 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 13 additions & 17 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4378,9 +4378,10 @@ impl<'a> Parser<'a> {
Ok(Some(self.mk_item(span, ident, ItemKind::MacroDef(def), vis.clone(), attrs.to_vec())))
}

fn parse_stmt_without_recovery(&mut self,
macro_legacy_warnings: bool)
-> PResult<'a, Option<Stmt>> {
fn parse_stmt_without_recovery(
&mut self,
macro_legacy_warnings: bool,
) -> PResult<'a, Option<Stmt>> {
maybe_whole!(self, NtStmt, |x| Some(x));

let attrs = self.parse_outer_attributes()?;
Expand Down Expand Up @@ -4586,20 +4587,15 @@ impl<'a> Parser<'a> {
if self.eat(&token::Semi) {
stmt_span = stmt_span.with_hi(self.prev_span.hi());
}
let sugg = pprust::to_string(|s| {
use crate::print::pprust::INDENT_UNIT;
s.ibox(INDENT_UNIT);
s.bopen();
s.print_stmt(&stmt);
s.bclose_maybe_open(stmt.span, false)
});
e.span_suggestion(
stmt_span,
"try placing this code inside a block",
sugg,
// speculative, has been misleading in the past (closed Issue #46836)
Applicability::MaybeIncorrect
);
if let Ok(snippet) = self.sess.source_map().span_to_snippet(stmt_span) {
e.span_suggestion(
stmt_span,
"try placing this code inside a block",
format!("{{ {} }}", snippet),
// speculative, has been misleading in the past (#46836)
Applicability::MaybeIncorrect,
);
}
}
Err(mut e) => {
self.recover_stmt_(SemiColonMode::Break, BlockMode::Ignore);
Expand Down
3 changes: 1 addition & 2 deletions src/libsyntax/print/pprust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -745,8 +745,7 @@ impl<'a> State<'a> {
self.bclose_maybe_open(span, true)
}

crate fn break_offset_if_not_bol(&mut self, n: usize,
off: isize) {
crate fn break_offset_if_not_bol(&mut self, n: usize, off: isize) {
if !self.s.is_beginning_of_line() {
self.s.break_offset(n, off)
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-39848.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error: expected `{`, found `foo`
--> $DIR/issue-39848.rs:8:19
|
LL | if $tgt.has_$field() {}
| -- - help: try placing this code inside a block: `{ foo(); }`
| -- - help: try placing this code inside a block: `{ ) }`
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is less than ideal, but the test is to avoid ICE regression, not good output.

| |
| this `if` statement has a condition, but no block
...
Expand Down
5 changes: 5 additions & 0 deletions src/test/ui/issues/issue-62554.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
fn main() {}

fn foo(u: u8) { if u8 macro_rules! u8 { (u6) => { fn uuuuuuuuuuu() { use s loo mod u8 {
//~^ ERROR expected `{`, found `macro_rules`
//~ ERROR this file contains an un-closed delimiter
30 changes: 30 additions & 0 deletions src/test/ui/issues/issue-62554.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
error: this file contains an un-closed delimiter
--> $DIR/issue-62554.rs:5:53
|
LL | fn foo(u: u8) { if u8 macro_rules! u8 { (u6) => { fn uuuuuuuuuuu() { use s loo mod u8 {
| - - - - - un-closed delimiter
| | | | |
| | | | un-closed delimiter
| | | un-closed delimiter
| un-closed delimiter un-closed delimiter
LL |
LL |
| ^

error: expected `{`, found `macro_rules`
--> $DIR/issue-62554.rs:3:23
|
LL | fn foo(u: u8) { if u8 macro_rules! u8 { (u6) => { fn uuuuuuuuuuu() { use s loo mod u8 {
| -- ^^^^^^^^^^^ expected `{`
| |
| this `if` statement has a condition, but no block
help: try placing this code inside a block
|
LL | fn foo(u: u8) { if u8 { macro_rules! u8 { (u6) => { fn uuuuuuuuuuu() { use s loo mod u8 {
LL |
LL |
LL | }
|

error: aborting due to 2 previous errors

4 changes: 2 additions & 2 deletions src/test/ui/label/label_break_value_illegal_uses.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ LL | if true 'b: {}
| -- ^^----
| | |
| | expected `{`
| | help: try placing this code inside a block: `{ 'b: { } }`
| | help: try placing this code inside a block: `{ 'b: {} }`
| this `if` statement has a condition, but no block

error: expected `{`, found `'b`
Expand All @@ -21,7 +21,7 @@ LL | if true {} else 'b: {}
| ^^----
| |
| expected `{`
| help: try placing this code inside a block: `{ 'b: { } }`
| help: try placing this code inside a block: `{ 'b: {} }`

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