Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error message when edition = "2018" has not been set #4454

Closed
johnhamelink opened this issue Oct 4, 2020 · 2 comments
Closed

Improve error message when edition = "2018" has not been set #4454

johnhamelink opened this issue Oct 4, 2020 · 2 comments
Labels
bug Panic, non-idempotency, invalid code, etc.

Comments

@johnhamelink
Copy link

johnhamelink commented Oct 4, 2020

Describe the bug

Hi there,

I just spent about 4 hours puzzling about why my emacs configuration was producing the following error:

error[E0670]: `async fn` is not permitted in the 2015 edition
 --> <stdin>:7:1
  |
7 | async fn async_main() {
  | ^^^^^ to use `async fn`, switch to Rust 2018
  |
  = help: set `edition = "2018"` in `Cargo.toml`
  = note: for more on editions, read https://doc.rust-lang.org/edition-guide

Despite having edition = "2018" set in Cargo.toml, the error was in fact due to edition = "2018" not being set in rustfmt.toml. As you might imagine, this was very confusing!

I am not the only one who's experienced this issue:

brotzeit/rustic#128
brotzeit/rustic#27
rust-lang/rls#1688

To Reproduce

Create a new cargo project, set the edition = "2018" in cargo.toml, add an async fn to the project, and then without any sort of configuration applied to rustfmt, run it as you normally would.

Run cargo check manually to verify that the error is not shown when running cargo.

Expected behavior

If it's not practical to change the default behaviour to edition 2018, I understand fully, but perhaps the error could be improved to point the developer in the direction of rustfmt.toml and not the cargo.toml?

Meta

  • rustfmt version: rustfmt 1.4.18-stable (8157a3f 2020-07-15)
  • From where did you install rustfmt?: rustup
  • How do you run rustfmt: via rustic on emacs
@johnhamelink johnhamelink added the bug Panic, non-idempotency, invalid code, etc. label Oct 4, 2020
@calebcartwright
Copy link
Member

Thanks for reaching out @johnhamelink. That error message is from rustc, and rustfmt does not have any control over the text of rustc error messages. Note that the default rustc edition is still 2015, and so accordingly is rustfmt's.

cargo fmt supports running with the edition specified in the Cargo.toml file, rustfmt (again, like rustc) does not and this is by design.

https://github.com/rust-lang/rustfmt#rusts-editions

Different editor/IDE plugins provide the autoformatting functionality differently, including the way they use/run rustfmt. Again similarly, we do not have any control over how these plugins choose to provide their autoformatting capabilities. Some will inspect the Cargo.toml on your behalf and pass the edition arg in their call to rustfmt. Others do not, and rely on you to incorporate the rustfmt.toml config file in your project. And there are others yet which use the rustfmt library instead of invoking the rustfmt binary.

The best course of action will be for editors to document their behavior accordingly.

We'd very much like to be able to make 2018 the default edition, though there's a lot of constraints and broader considerations that prevent us from being able to do so at this time or for the forseeable future.

As such I'm closing this as there's no action to be taken here.

@danackerson
Copy link

@johnhamelink thx for raising this issue. First time rust user (literally just installed this morning) and I also lost a couple hours before finding out about rustfmt.toml :)

Indy2222 added a commit to DigitalExtinction/Game that referenced this issue Mar 29, 2022
Indy2222 added a commit to DigitalExtinction/Game that referenced this issue Mar 29, 2022
Indy2222 added a commit to DigitalExtinction/Game that referenced this issue Mar 29, 2022
jhelwig added a commit to Technosorcery/sd2snes-lttp-rando-tracker that referenced this issue Apr 1, 2022
rustfmt doesn't get the edition from the Cargo.toml, and provides a
really obtuse error message (that comes from rustc itself) if the
edition isn't specified in the rustfmt.toml

rust-lang/rustfmt#4454
jeckersb added a commit to jeckersb/cincinnati that referenced this issue Dec 15, 2022
Some IDEs/plugins do not proprely infer the edition from the workspace
crates, and instead only check the root and default to 2015 since the
root Cargo.toml does not have a project in which to declare an
edition.

See: rust-lang/rustfmt#4454 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Panic, non-idempotency, invalid code, etc.
Projects
None yet
Development

No branches or pull requests

3 participants