Skip to content

Commit 1cda84b

Browse files
authored
Rollup merge of rust-lang#53360 - PramodBisht:issue/51602, r=estebank
Addressed rust-lang#51602 Fixed rust-lang#51602 r? @estebank here I have addressed the case where `in` was not expected right after `if` block. Speaking of `type ascription` I am not sure if this the best approach which I have implemented. Plus I think one more test case can be added to test `type-ascription` case, though I don't have any at this point of time. I will ping you again if all existing testcases pass.
2 parents e606882 + b70be5b commit 1cda84b

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

src/libsyntax/parse/parser.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -4719,7 +4719,12 @@ impl<'a> Parser<'a> {
47194719
if !self.eat(&token::OpenDelim(token::Brace)) {
47204720
let sp = self.span;
47214721
let tok = self.this_token_to_string();
4722+
let mut do_not_suggest_help = false;
47224723
let mut e = self.span_fatal(sp, &format!("expected `{{`, found `{}`", tok));
4724+
if self.token.is_keyword(keywords::In) || self.token == token::Colon {
4725+
do_not_suggest_help = true;
4726+
e.span_label(sp, "expected `{`");
4727+
}
47234728

47244729
// Check to see if the user has written something like
47254730
//
@@ -4729,7 +4734,8 @@ impl<'a> Parser<'a> {
47294734
// Which is valid in other languages, but not Rust.
47304735
match self.parse_stmt_without_recovery(false) {
47314736
Ok(Some(stmt)) => {
4732-
if self.look_ahead(1, |t| t == &token::OpenDelim(token::Brace)) {
4737+
if self.look_ahead(1, |t| t == &token::OpenDelim(token::Brace))
4738+
|| do_not_suggest_help {
47334739
// if the next token is an open brace (e.g., `if a b {`), the place-
47344740
// inside-a-block suggestion would be more likely wrong than right
47354741
return Err(e);

src/test/ui/issue-51602.rs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn main(){
12+
if i in 1..10 {
13+
break;
14+
}
15+
}

src/test/ui/issue-51602.stderr

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: expected `{`, found `in`
2+
--> $DIR/issue-51602.rs:12:10
3+
|
4+
LL | if i in 1..10 {
5+
| -- ^^ expected `{`
6+
| |
7+
| this `if` statement has a condition, but no block
8+
9+
error: aborting due to previous error
10+

0 commit comments

Comments
 (0)