Compiler confusion over delimiters

If my code has, for example, an extra or missing right parenthesis, rustc complains of 'mismatched closing delimiter' or 'unexpected closing delimiter', but the delimiter it highlights as 'wrong' is typically a brace rather than a parenthesis. They aren't interchangeable AFAIK, so couldn't it match them up a bit better?

1 Like

I think it already tries to match them up. Can you show a specific minimal example that fails?

Here's one example I've seen a lot. There's a extra closing paren after an expression:

fn main() {
    Some(0).and_then(|x| {
        println!("{}", x)
    }));
}

This generates two error messages. The second error message is very useful, but the first one is not. (These messages are from the nightly toolchain, which is a bit more useful than the output on stable currently.)

error: unexpected closing delimiter: `}`
 --> src/main.rs:5:1
  |
2 |     Some(0).and_then(|x| {
  |                          - this opening brace...
3 |         println!("{}", x)
4 |     }));
  |     - ...matches this closing brace
5 | }
  | ^ unexpected closing delimiter

error: mismatched closing delimiter: `)`
 --> src/main.rs:4:7
  |
1 | fn main() {
  |           - unclosed delimiter
...
4 |     }));
  |       ^ mismatched closing delimiter

error: aborting due to 2 previous errors

An even simpler one:

fn main() {
    println!("Hello, world!"));
}

which gives (with stable toolchain)

error: unexpected closing delimiter: `}`
 --> src/main.rs:3:1
  |
3 | }
  | ^ unexpected closing delimiter

error: mismatched closing delimiter: `)`
 --> src/main.rs:2:30
  |
1 | fn main() {
  |           - unclosed delimiter
2 |     println!("Hello, world!"));
  |                              ^ mismatched closing delimiter

The first message is saying that there is a problem with the braces, though they are in fact correct. The second message is pointing to the opening brace and closing parenthesis, and saying that they are not matched. Well, of course they're not, they are of different species; the real problem is that the extra closing parenthesis is not actually matching an opening one.

I would expect that the first message should not happen at all, and the second one should just point to the extra delimiter, and report it as 'superfluous' rather than as 'mismatched'.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.