Skip to content

Improve error message for incorrect 'for' input #40782

Closed
@martinlindhe

Description

@martinlindhe
Contributor

Hello, with rustc 1.17.0-nightly (8c4f2c6 2017-03-22), the following code

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

gives this error message:

error: expected one of `@` or `in`, found `0`
  --> src/disasm/mod.rs:27:15
   |
27 |         for i 0..2 {
   | 

An improved error message could add a hint : did you mean 'for i in 0..2'

Activity

added
A-diagnosticsArea: Messages for errors, warnings, and lints
A-parserArea: The lexing & parsing of Rust source code to an AST
on Mar 24, 2017
estebank

estebank commented on Sep 21, 2017

@estebank
Contributor

Current output:

error: expected one of `@` or `in`, found `0`
 --> src/main.rs:2:11
  |
2 |     for i 0..2 {
  |          -^ unexpected token
  |          |
  |          expected one of `@` or `in` here

How would you want to improve upon this? Adding a suggestion?

error: expected one of `@` or `in`, found `0`
 --> src/main.rs:2:11
  |
2 |     for i 0..2 {
  |          -^ unexpected token
  |          |
  |          expected one of `@` or `in` here
  = note: did you mean to write `in` here?
2 |     for i in 0..2 {
  |           ^^

To do so, we could use the same strategy as in #42578: in parse_for_expr if the In keyword is not found, try to continue building the expression as if the In keyword had been found. If we find further errors, we revert the state of the compiler to right after trying to continue and return the error from expect_keyword. Otherwise we cancel that one and create a new custom diagnostic:

error: missing `in` in `for` loop
 --> src/main.rs:2:11
  |
2 |     for i 0..2 {
  |          ^ expected `in` here
  = note: did you mean to write `in` here?
2 |     for i in 0..2 {
  |           ^^

or just modify the existing error with the proposed span_suggestion (so it looks like the 2 above).

added
E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.
on Sep 21, 2017
martinlindhe

martinlindhe commented on Sep 22, 2017

@martinlindhe
ContributorAuthor

Looks like current output already is an improvement over the reported one, but eithwr 2 or 3 would make it even more obvious i guess. No strong opinions here

nikomatsakis

nikomatsakis commented on Sep 25, 2017

@nikomatsakis
Contributor

Seems to me that the third error is the most clear ("error: missing in in for loop"); I suspect people using fancy pattern features will figure out what's going on.

added a commit that references this issue on Oct 30, 2017
6a62ea6
added a commit that references this issue on Nov 4, 2017

Rollup merge of rust-lang#45639 - LaurentMazare:master, r=petrochenkov

ea57265
added 3 commits that reference this issue on Feb 5, 2025

Rollup merge of rust-lang#136536 - DuskyElf:master, r=jieyouxu

1b68465

Rollup merge of rust-lang#136536 - DuskyElf:master, r=jieyouxu

fa1a22c

Rollup merge of rust-lang#136536 - DuskyElf:master, r=jieyouxu

41e93ab
added a commit that references this issue on Feb 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    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.E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.WG-diagnosticsWorking group: Diagnostics

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @sanxiyn@nikomatsakis@martinlindhe@estebank@Mark-Simulacrum

        Issue actions

          Improve error message for incorrect 'for' input · Issue #40782 · rust-lang/rust