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

error: expected one of { or an operator, found } #36611

Closed
glandium opened this issue Sep 21, 2016 · 4 comments
Closed

error: expected one of { or an operator, found } #36611

glandium opened this issue Sep 21, 2016 · 4 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The parsing of Rust source code to an AST. C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@glandium
Copy link
Contributor

I did a stupid typo in a program, where I typed in twice in a for statement:

fn main() {
    for i in in 1..2 {
        println!("{}", i);
    }
}

The compiler pointed the error on the closing brace, which, in the real code, with much more things happening in the loop (plus nested loops) was completely unhelpful.

error: expected one of `{` or an operator, found `}`
 --> <anon>:5:1
  |
5 | }
  | ^

error: aborting due to previous error
@bstrie bstrie added the A-diagnostics Area: Messages for errors, warnings, and lints label Sep 21, 2016
@TimNN
Copy link
Contributor

TimNN commented Sep 21, 2016

Fixing the errors reported by the compiler reveals why this parses that far:

fn main() {
    for i in in 1..2 {
        println!("{}", i);
    }
{ } }
error: placement-in expression syntax is experimental and subject to change. (see issue #27779)
 --> <anon>:2:14
  |
2 |     for i in in 1..2 {
  |              ^

@steveklabnik steveklabnik added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. and removed T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 9, 2017
@Mark-Simulacrum Mark-Simulacrum added the A-parser Area: The parsing of Rust source code to an AST. label Jun 4, 2017
@Mark-Simulacrum Mark-Simulacrum added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jul 26, 2017
@estebank
Copy link
Contributor

Current output points at the char immediately after the closing of the for scope, still needs some improvement:

error: expected one of `{` or an operator, found `}`
 --> src/main.rs:5:1
  |
4 |     }
  |      - expected one of `{` or an operator here
5 | }
  | ^ unexpected token

@estebank
Copy link
Contributor

estebank commented Apr 10, 2018

Closed by #48333:

error: expected expression, found keyword `in`
 --> src/main.rs:2:14
  |
2 |     for i in in 1..2 {
  |              ^^ expected expression

That PR was reverted :(

@estebank estebank reopened this Sep 13, 2018
pietroalbini added a commit to pietroalbini/rust that referenced this issue Sep 22, 2018
Detect `for _ in in bar {}` typo

Fix rust-lang#36611, rust-lang#52964, without modifying the parsing of emplacement `in` to avoid further problems like rust-lang#50832.
@cczufish
Copy link

cczufish commented Sep 2, 2019

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=81c748636e6aab123c5592ffc1df8436

use std::collections::HashMap;

fn main() {
    let vec:Vec<i32> = vec![2,2,1];
    single_number(vec);
}

fn single_number(nums: Vec<i32>) -> i32 {
    let mut result = HashMap::new();

    for v in vec! {
       // 类型转换
       let str_key = v.to_string();
       //插入之前先判断,当前的key对应的value有值则加1,没值初始化为1
       if result.get(&str_key) == None {
        result.insert(str_key,1);
       }else{
            let str_key1 = v.to_string();
            let _strValue = result.get(&str_key1);
        
            let value = _strValue.unwrap();
            result.insert(str_key,value + 1);

       }
    }
   
   
   // 遍历result
   
    let mut resultK = 0;
    for (k,v) in &result {
        println!("{},{}",k,v);
        let value = *v as i32;
        if value == 1
        {
          resultK = k.parse::<i32>().unwrap();;
        }
    }
    resultK
}
 Compiling playground v0.0.1 (/playground)
error: expected one of `.`, `?`, `{`, or an operator, found `let`
  --> src/main.rs:30:5
   |
25 |     }
   |      - expected one of `.`, `?`, `{`, or an operator here
...
30 |     let mut resultK = 0;
   |     ^^^ unexpected token

error: aborting due to previous error

error: Could not compile `playground`.

To learn more, run the command again with --verbose.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The parsing of Rust source code to an AST. C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

7 participants