Description
Reproduction Steps
- Create a new project with
cargo new temp
. - In
bin.rs
, define the following function:
fn func() {
let closure = |a: i64| {
let b = 2 * a;
(a, b)
};
}
- Place the cursor after
|a: i64|
, just before the opening{
. - Type
-> (i64, i64)
Expected behavior
After adding the return type annotation to the closure, the editor should have the following text:
fn func() {
let closure = |a: i64| -> (i64, i64) {
let b = 2 * a;
(a, b)
};
}
Observed behavior
After adding the return type annotation to the closure, the editor has the following text.
fn func() {
let closure = |a: i64| -> (i64, i64) {
let b = 2 * a;
(a, b)
});
}
The extra closing )
occurs at the end of the closure definition, causing a compilation error due to unmatched parentheses. Because the insertion of the extra )
may be several lines away from the point where the user is typing, this is difficult to spot as it occurs.
Suspected Mechanism
- The opening
(
in(i64, i64)
triggers theon_char_typed
, which then delegates toon_opening_bracket_typed
on_opening_bracket_typed
callsbracket_expr
, to determine if the(
occurred just prior to an expression that should be parenthesized.bracket_expr
recognizes the body of the lambda as a block expression, and inserts a closing)
between the closing}
of the lambda and the;
rust-analyzer version:
- First noticed in
rust-analyzer 1.74.1 (a28077b 2023-12-04)
- Last nightly build without the error:
rust-analyzer 1.74.0-nightly (203c57d 2023-09-17)
- First nightly build with the error:
rust-analyzer 1.74.0-nightly (65ea825 2023-09-18)
The list of rustc commits, starting at the first known bad commit is here. The subtree updates for rust-analyzer
in this window are from this PR. This PR included commit 0f1cde70
, which was an upstreaming of the rust-analyzer
PR #15532, and seems the most likely commit to have introduced this error.
rustc version: rustc 1.74.1 (a28077b28 2023-12-04)
relevant settings: (eg. client settings, or environment variables like CARGO
, RUSTC
, RUSTUP_HOME
or CARGO_HOME
)