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

rustc: Improve unexpected close delimiter hint #68987

Closed
frostyplanet opened this issue Feb 9, 2020 · 4 comments · Fixed by #104012
Closed

rustc: Improve unexpected close delimiter hint #68987

frostyplanet opened this issue Feb 9, 2020 · 4 comments · Fixed by #104012
Assignees
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

@frostyplanet
Copy link

frostyplanet commented Feb 9, 2020

I found writing rust often confused by missing closing "}" or ")" error message. when editting large file. the line number in error message is completely wrong, looking through my code the changes are no where near the line. It takes a long time for me to spot the syntax problem.

error: unexpected close delimiter: `}`
    --> core/src/tx_file.rs:1218:1
     |
1179 |             None=>{},
     |                   -- this block is empty, you might have not meant to close it
...
1218 | }
     | ^ unexpected close delimiter

@frostyplanet
Copy link
Author

It turns out a match { } block missing a line Ok(_)=> {, but the line 1218 in the error message is the last line of file

@jonas-schievink jonas-schievink added A-diagnostics Area: Messages for errors, warnings, and lints 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. labels Feb 9, 2020
@Centril Centril added the A-parser Area: The parsing of Rust source code to an AST label Feb 10, 2020
@lzybkr
Copy link
Contributor

lzybkr commented Aug 25, 2021

I wasted a minute or two trying to understand the first error message from code like:

fn f(i: u32, j: u32) {
    let res = String::new();
    let mut cnt = i;
    while cnt < j {
        write!&mut res, " ");
    }
}

The error report is:

error: unexpected closing delimiter: `}`
 --> src/lib.rs:8:1
  |
2 | fn f(i: u32, j: u32) {
  |                      - this opening brace...
...
7 |     }
  |     - ...matches this closing brace
8 | }
  | ^ unexpected closing delimiter

error: mismatched closing delimiter: `)`
 --> src/lib.rs:6:28
  |
5 |     while cnt < j {
  |                   - unclosed delimiter
6 |         write!&mut res, " ");
  |                            ^ mismatched closing delimiter

error: aborting due to 2 previous errors

The first error is not helpful because the curly braces match perfectly.

It is somewhat common to ignore all but the first error when you have mismatched delimiters, so even though the second error points to the error exactly, some won't notice it immediately.

@X-m7
Copy link

X-m7 commented Oct 3, 2022

I ran into another instance of this confusing error here:

async fn obstest() -> Result<impl Responder, MyError> {
    let obs_connect = || -> Result<(obws::Version, Vec<obws::responses::scenes::Scene>)>, MyError) {
        async {
            let client = Client::connect("localhost", 4455, Some("1234567890")).await?;
            let version = client.general().version().await?.obs_version;
            let scene_list = client.scenes().list().await?.scenes;
            Ok((version, scene_list))
        }
    }

    if let Ok(version, scene_list) = obs_connect() {
        Ok(HttpResponse::Ok().body(format!("Hello from OBS {:?}\nScenes are: {:?}", version, scene_list)))
    } else {
        Err(MyError { name: "test" })
    }
}

The error messages I get are:

error: unexpected closing delimiter: `}`
  --> src/main.rs:36:1
   |
32 |     } else {
   |            - this opening brace...
33 |         Err(MyError { name: "test" })
34 |     }
   |     - ...matches this closing brace
35 |
36 | }
   | ^ unexpected closing delimiter

error: mismatched closing delimiter: `)`
  --> src/main.rs:20:55
   |
20 | async fn obstest() -> Result<impl Responder, MyError> {
   |                                                       ^ unclosed delimiter
21 |     let obs_connect = || -> Result<(obws::Version, Vec<obws::responses::scenes::Scene>)>, MyError) {
   |                                                                                                  ^ mismatched closing delimiter

error: could not compile `scuffcommander` due to 2 previous errors

Again the first error is misleading because it matches just fine, and as someone who just came back to messing around with Rust after a couple of years it took me a few minutes before I noticed the what the second error is saying. It also does not help that the first thing the second error highlights is again in fact correct.

@chenyukang
Copy link
Member

@rustbot claim

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jan 27, 2023
…indentation, r=petrochenkov

Improve unexpected close and mismatch delimiter hint in TokenTreesReader

Fixes rust-lang#103882
Fixes rust-lang#68987
Fixes rust-lang#69259

The inner indentation mismatching will be covered by outer block, the new added function `report_error_prone_delim_block` will find out the error prone candidates for reporting.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jan 28, 2023
…indentation, r=petrochenkov

Improve unexpected close and mismatch delimiter hint in TokenTreesReader

Fixes rust-lang#103882
Fixes rust-lang#68987
Fixes rust-lang#69259

The inner indentation mismatching will be covered by outer block, the new added function `report_error_prone_delim_block` will find out the error prone candidates for reporting.
@bors bors closed this as completed in e3048c7 Jan 28, 2023
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

Successfully merging a pull request may close this issue.

6 participants