Skip to content

error[E0670]. Cargo.toml "edition = 2021" doesn't work.  #97002

Closed
@amab8901

Description

@amab8901

Here's my code in main.rs :

async fn main() {}

Here's the output:

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

error[E0752]: `main` function is not allowed to be `async`
 --> main.rs:1:1
  |
1 | async fn main() {}
  | ^^^^^^^^^^^^^^^ `main` function is not allowed to be `async`

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0670, E0752.
For more information about an error, try `rustc --explain E0670`.

Here is my Cargo.toml file:

[package]
name = "visualizer"
version = "0.1.0"
edition = '2021'

Here is what it auto-generates in Cargo.lock file:

# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3

[[package]]
name = "visualizer"
version = "0.1.0"

ATTEMPTED SOLUTION:

I removed the async keyword in async fn main() {} so it became fn main() {} in main.rs. Then I did the following in the terminal:

$ cargo fix --edition
    Checking visualizer v0.1.0 ( // I manually removed the path for privacy reasons )
warning: `src/main.rs` is already on the latest edition (2021), unable to migrate further

If you are trying to migrate from the previous edition (2018), the
process requires following these steps:

1. Start with `edition = "2018"` in `Cargo.toml`
2. Run `cargo fix --edition`
3. Modify `Cargo.toml` to set `edition = "2021"`
4. Run `cargo build` or `cargo test` to verify the fixes worked

More details may be found at
https://doc.rust-lang.org/edition-guide/editions/transitioning-an-existing-project-to-a-new-edition.html

    Finished dev [unoptimized + debuginfo] target(s) in 0.29s

So I changed edition = "2021" into edition = "2015" in Cargo.toml. Then I did the following in the terminal:

$ cargo fix --edition
    Checking visualizer v0.1.0 ( // I manually removed the path for privacy reasons  )
   Migrating src/main.rs from 2015 edition to 2018
    Finished dev [unoptimized + debuginfo] target(s) in 0.95s

Then I edited back from edition = "2015" to edition = "2021" and did the following in the terminal:

$ cargo build
   Compiling visualizer v0.1.0 ( // I manually removed the path for privacy reasons )
    Finished dev [unoptimized + debuginfo] target(s) in 0.37s
$ cargo test
   Compiling visualizer v0.1.0 ( // I manually removed the path for privacy reasons  )
    Finished test [unoptimized + debuginfo] target(s) in 0.26s
     Running unittests ( // I manually removed the path for privacy reasons  )

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

Then I changed back from fn main() {} to async fn main() {} in main.rs, and here's the output I get:

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

error[E0752]: `main` function is not allowed to be `async`
 --> main.rs:1:1
  |
1 | async fn main() {}
  | ^^^^^^^^^^^^^^^ `main` function is not allowed to be `async`

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0670, E0752.
For more information about an error, try `rustc --explain E0670`.

[Done] exited with code=1 in 0.1 seconds

Thus, my attempted solution has failed.


Rust Version
rust-analyzer 2022-05-09

Editor:
VSCode

Operating System:
Archlinux

rust-analyzer version: rust-analyzer version: 5d5bbec 2022-05-09 stable

rustc version: rustc 1.59.0 (9d1b210 2022-02-23)

relevant settings: (eg. client settings, or environment variables like CARGO, RUSTUP_HOME or CARGO_HOME)

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions