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 the on_char_typed, which then delegates to on_opening_bracket_typed
on_opening_bracket_typed calls bracket_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)
- emacs version 27.1
rust-mode version 20230805.1558 (latest version on melpa)
lsp-mode version 20231124.833 (~1 week away from latest version on melpa)
Reproduction Steps
cargo new temp.bin.rs, define the following function:|a: i64|, just before the opening{.-> (i64, i64)Expected behavior
After adding the return type annotation to the closure, the editor should have the following text:
Observed behavior
After adding the return type annotation to the closure, the editor has the following text.
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
(in(i64, i64)triggers theon_char_typed, which then delegates toon_opening_bracket_typedon_opening_bracket_typedcallsbracket_expr, to determine if the(occurred just prior to an expression that should be parenthesized.bracket_exprrecognizes the body of the lambda as a block expression, and inserts a closing)between the closing}of the lambda and the;rust-analyzer version:
rust-analyzer 1.74.1 (a28077b 2023-12-04)rust-analyzer 1.74.0-nightly (203c57d 2023-09-17)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-analyzerin this window are from this PR. This PR included commit0f1cde70, which was an upstreaming of therust-analyzerPR #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_HOMEorCARGO_HOME)rust-modeversion 20230805.1558 (latest version on melpa)lsp-modeversion 20231124.833 (~1 week away from latest version on melpa)