0

When talking about Emacs configuration, the rust-analyzer manual mentions

[...] this excellent guide from @rksm.

Which I followed to good effect, in particular:

  (use-package lsp-mode
    :commands lsp
    :custom
    ;; what to use when checking on-save. "check" is default,
    ;; I prefer clippy.
    (lsp-rust-analyzer-cargo-watch-command "clippy")
    (lsp-eldoc-render-all t)
    (lsp-idle-delay 0.6)
    (lsp-rust-analyzer-server-display-inlay-hints t)
    :custom-face
    (lsp-rust-analyzer-inlay-face ((t (:foreground "dim gray"))))
    :config
    (add-hook 'lsp-mode-hook 'lsp-ui-mode))

  (use-package lsp-ui
    :commands lsp-ui-mode
    :custom
    (lsp-ui-peek-always-show t)
    (lsp-ui-sideline-show-hover nil) ; I found this confusing
    (lsp-ui-doc-enable nil))

However when having an async fn in my code, for example as suggested by the rocket.rs guide,

Rust analyzer is complaining about:

error[E0670]: `async fn` is not permitted in Rust 2015
  --> .../hello-rocket/src/main.rs:16:28
   |
16 | #[get("/delay/<seconds>")] async fn delay(seconds: u64) -> String {
   |                            ^^^^^ to use `async fn`, switch to Rust 2018 or later
   |
   = help: set `edition = "2018"` in `Cargo.toml`
   = note: for more on editions, read https://doc.rust-lang.org/edition-guide

despite the fact that I already have in my Cargo.toml:

edition = "2018"

My code compiles just fine, and rust-analyzer diagnostics $PWD seems to complete without errors.

So I guess my question is: How should I tell rust-analyzer via lsp-rust-analyzer to consider my Cargo.toml for the value of edition?

2
  • Try to use the equivalent raw identifier syntax: r#async - from doc.rust-lang.org/stable/edition-guide/editions/index.html.
    – Ian
    Oct 6 at 13:46
  • @Ian, this doesn't makes it any better. In fact after adding r#async this no longer compiles. As noted above the original code compiles without errors (or warnings), but lsp-rust-analyzer still complains. So it seems like an Emacs configuration issue, not a Rust code issue.
    – Chen Levy
    Oct 6 at 16:11
0

It took me some time to realize that rust-analyzer was not the one to complain, but the buffer with the error message mentioned above was *rustfmt*, and indeed running rustfmt on my source file gave me the same error.

As suggested in this (inexplicably closed) issue, one can convince rustfmt to behave by adding edition = "2018" to .rustfmt.toml.

Since I am not expecting to hack on 2015 edition code any time soon, I chosen a different (and more Emacsy) workaround:

  (use-package rustic
    ;; ...
    :custom
    (rustic-rustfmt-config-alist '((edition . "2018")))
    ;; ...
    )

Hopefully, I will find a better solution by the time the 2021 edition will come to fruition.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.