rust-lang / rust-lang/rust-mode

Indentation messes up in incredibly specific condition.

Open
#493 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Emacs Lisp
Stars
1.3k
Forks
198
PR merge metrics
No merged PRs in 30d

Description

Consider the following 3 lines of code:

fn main() {
    let _ = 0. < 1.;
}

If you put your cursor at the end of line 2, then press enter, you'd expect the next line to be indented at the same level, however this happens:

fn main() {
    let _ = 0. < 1.;
                // It's indented to here
}

Also, if you do the same with the last line:

fn main() {
    let _ = 0. < 1.;
    }
    // Indented to here

This is because of the 0. < bit. If you change it to 0.0 < or 0. >, indentation works perfectly fine, so it's likely due to the combination of missing the decimal part and having the < character, which is also used as a bracket in rust.

I may have narrowed it down via a few hours of trial and error to this regex:
https://github.com/rust-lang/rust-mode/blob/22fff6a049402584e7120146c3db141c6f530bf6/rust-mode.el#L629

Which does not match with the original code, but does with the fixed code.
I don't believe that this is the cause of the issue though, since I also found that in that match, for the working code it will match an empty string $ (expected, since the cursor is at the end of the line). For the original code however, it matches 1\.;$, and it's the exact same if I increase the line's length. This means that something has moved the cursor to just after the <.

I confirmed this by changing the < to [, the same thing happens.

It looks like this is done by this line:
https://github.com/rust-lang/rust-mode/blob/22fff6a049402584e7120146c3db141c6f530bf6/rust-mode.el#L827

Commenting that out means it only indents by one extra level, which is bearable. Unfortunately, this is a hack, and probably breaks another indentation feature.

The thing that confuses me is that backward-up-list only goes back to the < if the decimal part is left out. It's probably due to this regex:
https://github.com/rust-lang/rust-mode/blob/22fff6a049402584e7120146c3db141c6f530bf6/rust-mode.el#L620

Which matches the ., assuming it's an operator. I managed to get the regex to work as intended for a few minutes, then I broke it again ([-=!%&*/:<>[{(|^;}]\\|[^[:digit:]]\\.). If somebody else more versed with lisp could help that would be great since I've run out of ideas.

If anyone else has this problem, a temporary solution is to replace rust-is-lt-char-operator:
https://github.com/rust-lang/rust-mode/blob/22fff6a049402584e7120146c3db141c6f530bf6/rust-mode.el#L1216-L1262

with:

(defun rust-is-lt-char-operator () t)

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue is in rust-mode.el, especially the indentation regex around lines 620 and 629, backward-up-list handling near line 827, and rust-is-lt-char-operator around lines 1216-1262. Reproduce the three-line Rust example, inspect how the cursor moves during indentation, and verify that the original and corrected forms indent at the expected level without regressing related indentation behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
emacs-lisp, rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.