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

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

Closed
amab8901 opened this issue May 13, 2022 · 2 comments
Closed

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

amab8901 opened this issue May 13, 2022 · 2 comments
Labels
C-bug Category: This is a bug.

Comments

@amab8901
Copy link
Contributor

amab8901 commented May 13, 2022

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)

@amab8901 amab8901 added the C-bug Category: This is a bug. label May 13, 2022
@amab8901 amab8901 changed the title Cargo.toml "edition = 2021" doesn't work. error[E0670]: Cargo.toml "edition = 2021" doesn't work. May 13, 2022
@amab8901 amab8901 changed the title error[E0670]: Cargo.toml "edition = 2021" doesn't work. error[E0670]. Cargo.toml "edition = 2021" doesn't work. May 13, 2022
@ehuss
Copy link
Contributor

ehuss commented May 14, 2022

That's strange. For some reason, the edition field is not being picked up. Are those error messages coming directly from running cargo in a terminal? Or are you using something else? I noticed you say you have rust-analyzer and vscode, perhaps you are somehow checking the code in there? If so, what exact steps are you taking to get that output?

I notice that your error message has a path like main.rs. That is unusual in a cargo project, as main.rs is usually inside a src directory, and the messages should say src/main.rs. Do you have a standard cargo project layout?

@bjorn3
Copy link
Member

bjorn3 commented May 15, 2022

See rust-lang/rust-analyzer#12233

tl;dr: some vscode plugin this user used was running rustc directly without the --edition flag.

@bjorn3 bjorn3 closed this as completed May 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

3 participants