Skip to content

Confusing suggestion when if is missing in else if #49361

@stokhos

Description

@stokhos
pub fn nth(n:u32)->Option<u32>{
    let mut x:u32 = 2;
    let mut m:u32 = 1;
    let mut p:Vec<u32> = vec![2];
    'outer: loop {
        x += 1;
        'inner: for i in p.iter() {
            if x % i ==0{
                continue 'outer;
            }
        }
        p.push(x);
        m+=1;
        if n < 1 {
            return None;        
        } else if n == 1 {
            return Some(2);
        } else m == n { // should be else if here
            return Some(x);
        }
    }
}

fn main(){
    println!("{:?}", nth(1));
}

The error here is if is missing in else if

but compiler reports: error:

error: expected identifier, found keyword `return`
  --> src/main.rs:19:13
   |
19 |             return Some(x);
   |             ^^^^^^

Activity

estebank

estebank commented on Mar 26, 2018

@estebank
Contributor

The output in the current beta already points out at the place where the else if should be, even if it doesn't suggest it:

error: expected identifier, found keyword `return`
  --> src/main.rs:19:13
   |
18 |         } else m == n { // should be else if here
   |                     - while parsing this struct
19 |             return Some(x);
   |             ^^^^^^ expected identifier, found keyword

error: expected `{`, found `m`
  --> src/main.rs:18:16
   |
18 |           } else m == n { // should be else if here
   |  ________________-
19 | |             return Some(x);
20 | |         }
   | |_________- help: try placing this code inside a block: `{ m == n{}; }`

error: aborting due to 2 previous errors
added
C-enhancementCategory: An issue proposing an enhancement or a PR with one.
A-diagnosticsArea: Messages for errors, warnings, and lints
on Mar 26, 2018
added
A-parserArea: The lexing & parsing of Rust source code to an AST
D-papercutDiagnostics: An error or lint that needs small tweaks.
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
on Oct 18, 2019
estebank

estebank commented on May 20, 2022

@estebank
Contributor

The current output is:

error: expected identifier, found keyword `return`
  --> src/main.rs:19:13
   |
18 |         } else m == n { // should be else if here
   |                     - while parsing this struct
19 |             return Some(x);
   |             ^^^^^^ expected identifier, found keyword

error: expected `{`, found `m`
  --> src/main.rs:18:16
   |
18 |         } else m == n { // should be else if here
   |                ^ expected `{`
   |
help: try placing this code inside a block
   |
18 ~         } else { m == n { // should be else if here
19 |             return Some(x);
20 ~         } }
   |

I still wish the parser to detect that the code immediately following else is a binary operation followed by a block, so that we can instead emit

error: expected `{` or `if`, found `m`
  --> src/main.rs:18:16
   |
18 |         } else m == n { // should be else if here
   |                ^ expected `{`
   |
help: you might be missing an `if` keyword here
   |
18 |         } else if m == n { // should be else if here
   |                ++
estebank

estebank commented on May 20, 2022

@estebank
Contributor

CC @compiler-errors, in case you might be interested in looking at the parser :)

compiler-errors

compiler-errors commented on May 20, 2022

@compiler-errors
Member

this seems like a neat fix, i'll take a stab at it

added 2 commits that reference this issue on May 24, 2022
bc1fe98
ebbfb6b

2 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-parserArea: The lexing & parsing of Rust source code to an ASTC-enhancementCategory: An issue proposing an enhancement or a PR with one.D-papercutDiagnostics: An error or lint that needs small tweaks.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Development

    Participants

    @estebank@compiler-errors@stokhos

    Issue actions

      Confusing suggestion when if is missing in else if · Issue #49361 · rust-lang/rust