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

[E0670]: async fn is not permitted in Rust 2015 (but I'm using Rust 2022) #12233

Closed
amab8901 opened this issue May 12, 2022 · 39 comments
Closed
Labels
C-support Category: support questions

Comments

@amab8901
Copy link

amab8901 commented May 12, 2022

Here's my code:

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 # 1:

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.


ATTEMPTED SOLUTION # 2

I changed this...

async fn main() {}

into this...

fn main() {}

async fn hello() {}

I run it, and here's the output:

error[E0670]: `async fn` is not permitted in Rust 2015
 --> main.rs:3:1
  |
3 | async fn hello() {}
  | ^^^^^ 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: aborting due to previous error

For more information about this error, try `rustc --explain E0670`.

ATTEMPTED SOLUTION # 3
I changed main.rs contents into the following:

use tokio::net::TcpListener;
use tokio::io::{AsyncReadExt, AsyncWriteExt};

#[tokio::main]
async fn main() {}

And here is my new Cargo.toml:

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

[dependencies]
tokio = { version= "1", features = ["full"] }

Here's the output:

error[E0670]: `async fn` is not permitted in Rust 2015
 --> main.rs:4:1
  |
4 | 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[E0432]: unresolved import `tokio`
 --> main.rs:1:5
  |
1 | use tokio::main;
  |     ^^^^^ maybe a missing crate `tokio`?

error[E0433]: failed to resolve: use of undeclared crate or module `tokio`
 --> main.rs:3:3
  |
3 | #[tokio::main]
  |   ^^^^^ use of undeclared crate or module `tokio`

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

error: aborting due to 4 previous errors

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

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 (9d1b2106e 2022-02-23)

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

@bjorn3
Copy link
Member

bjorn3 commented May 12, 2022

That is not an error produced by rust-analyzer. It is produced by rustc. Do you have edition = "2021" in your Cargo.toml. If the edition field is missing, cargo will default to the 2015 edition for backwards compatibility.

@amab8901
Copy link
Author

That is not an error produced by rust-analyzer. It is produced by rustc. Do you have edition = "2021" in your Cargo.toml. If the edition field is missing, cargo will default to the 2015 edition for backwards compatibility.

I have edition 2021. I added a copy of my Cargo.toml in the main post

@lnicola
Copy link
Member

lnicola commented May 13, 2022

main can't be async, regardless of the edition. The article you've originally linked to has a #[tokio::main] attribute on main, which turns it into a non-async function:

image

@lnicola lnicola closed this as completed May 13, 2022
@amab8901
Copy link
Author

main can't be async, regardless of the edition. The article you've originally linked to has a #[tokio::main] attribute on main, which turns it into a non-async function:

image

I tried new solutions based on your suggestions, but it still doesn't work. See the recent additions on the post.

@lnicola
Copy link
Member

lnicola commented May 13, 2022

Are you sure you're pasting the right code? Looking at the errors:

 --> main.rs:1:5
  |
1 | use tokio::main;
  |     ^^^^^ maybe a missing crate `tokio`?

But your line 1 is use tokio::net::TcpListener;:

use tokio::net::TcpListener;
use tokio::io::{AsyncReadExt, AsyncWriteExt};

#[tokio::main]
async fn main() {}

So the error is for a different version of the code.

And the unresolved import error means that tokio isn't in Cargo.toml, or that the full feature isn't set.

@flodiebold
Copy link
Member

What do you mean by "output"? Where do you see this output / what are you doing to get it? I thought at first this was a rustfmt error, but you wouldn't get name resolution errors from rustfmt.

@Diggsey
Copy link

Diggsey commented May 13, 2022

@amab8901 this is not really a rust-analyzer issue - for general help with Rust there are a few channels where you might get a better response:

If you want people to be able to reproduce your issue, then using links to the rust playground is also a good idea:

https://play.rust-lang.org/

@amab8901
Copy link
Author

amab8901 commented May 13, 2022

What do you mean by "output"? Where do you see this output / what are you doing to get it? I thought at first this was a rustfmt error, but you wouldn't get name resolution errors from rustfmt.

I'm running the code from main.rs through VSCode text editor, using rust-analyzer to run the .rs files.

My file hierarchy is as follows:
Project
--- src
--- --- main
--- --- main.rs
--- --- utils.rs
--- target
--- Cargo.lock
--- Cargo.toml

@amab8901
Copy link
Author

amab8901 commented May 13, 2022

on of the co


I tried using Rust playground and it turns out that the following .rs script works fine:

#[tokio::main]
async fn main() {}

So the problem then isn't the script itself, but rather there seems to be a deeper issue with either .toml configuration or perhaps my VSCode editor. But I'm not sure what it is more specifically or how to solve it.

My Cargo.toml file looks like this:

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

[dependencies]
tokio = { version= "1", features = ["full"] }

@lnicola
Copy link
Member

lnicola commented May 13, 2022

Try uploading your project somewhere (even here) after cargo clean. Or make a new one.

What's src/main?

@amab8901
Copy link
Author

@amab8901 this is not really a rust-analyzer issue - for general help with Rust there are a few channels where you might get a better response:

If you want people to be able to reproduce your issue, then using links to the rust playground is also a good idea:

https://play.rust-lang.org/

Thank you for the advice on using rust playground. I'm not really sure what the issue is, so I can't exclude rust-analyzer from being a potential explanation for the issue. I tried writing the following code on the rust playground...

#[tokio::main]
async fn main() {}

... and it works fine. This tells me that the main.rs script itself isn't the issue. But rather the issue is something else. Maybe it's the Cargo.toml file. Maybe it's the VSCode text editor itself. Or maybe it's the rust-analyzer (which I'm using for my VSCode to run the .rs files). Or maybe it's something else.

@flodiebold
Copy link
Member

Try running cargo run in the terminal from the project folder, outside of VSCode.

@amab8901
Copy link
Author

Try uploading your project somewhere (even here) after cargo clean. Or make a new one.

What's src/main?
Here you go https://github.com/amab8901/first_rust_project/tree/master

@amab8901
Copy link
Author

folder, outside

Here's what happens:

$ cargo run
error: could not find `Cargo.toml` in ` (manually removed path for privacy reasons) ` or any parent directory

@lnicola
Copy link
Member

lnicola commented May 13, 2022

You didn't delete target and src is a symlink, or empty, or something.

@amab8901
Copy link
Author

You didn't delete target and src is a symlink or something.

ok, I deleted the repository and I will try again. Why do I need to delete "target" though? It's auto-generated whenever I save the main.rs file. So even if I delete it, it gets recreated every time I save main.rs.

@lnicola
Copy link
Member

lnicola commented May 13, 2022

Why do I need to delete "target" though?

It's very large and takes a long time to upload and download.

@flodiebold
Copy link
Member

You need to run cargo run inside the folder where the Cargo.toml is located.

@amab8901
Copy link
Author

You need to run cargo run inside the folder where the Cargo.toml is located.

Here's what happens:

$ cargo run
   Compiling cfg-if v1.0.0
   Compiling smallvec v1.8.0
   Compiling scopeguard v1.1.0
   Compiling once_cell v1.10.0
   Compiling bytes v1.1.0
   Compiling pin-project-lite v0.2.9
   Compiling libc v0.2.125
   Compiling log v0.4.17
   Compiling memchr v2.5.0
   Compiling lock_api v0.4.7
   Compiling parking_lot_core v0.9.3
   Compiling socket2 v0.4.4
   Compiling mio v0.8.3
   Compiling signal-hook-registry v1.4.0
   Compiling num_cpus v1.13.1
   Compiling parking_lot v0.12.0
   Compiling tokio v1.18.2
   Compiling visualizer v0.1.0 ( (manually removed path for privacy reasons) )
    Finished dev [unoptimized + debuginfo] target(s) in 11.52s
     Running `target/debug/visualizer`

Running main.rs now gives me the following error:

error[E0670]: `async fn` is not permitted in Rust 2015
 --> main.rs:2:1
  |
2 | 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[E0433]: failed to resolve: use of undeclared crate or module `tokio`
 --> main.rs:1:3
  |
1 | #[tokio::main]
  |   ^^^^^ use of undeclared crate or module `tokio`

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

error: aborting due to 3 previous errors

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

@lnicola
Copy link
Member

lnicola commented May 13, 2022

What do you mean by "running main.rs"? In Rust you run a crate, not a file.

The log above shows that your project compiles and runs fine. How are you getting the errors at the bottom?

@amab8901
Copy link
Author

amab8901 commented May 13, 2022

What do you mean by "running main.rs"? In Rust you run a crate, not a file.

The log above shows that your project compiles and runs fine. How are you getting the errors at the bottom?

I mean that I have the file open in VSCode and I click the "Run Code" button at the top-right, the one with a triangle pointing to the right.

@lnicola
Copy link
Member

lnicola commented May 13, 2022

Can you upload the code again?

@flodiebold
Copy link
Member

Just to make sure, where is that "Run code" button located exactly?

@lnicola We only provide the "Run" code lens, which is not at the top-right, do we? Or am I misremembering?

@amab8901
Copy link
Author

What do you mean by "running main.rs"? In Rust you run a crate, not a file.

The log above shows that your project compiles and runs fine. How are you getting the errors at the bottom?

Oh! After all these things you said about cargo run and running and files and crates, etc, it triggered me to do some research into whether I'm actually supposed to click that "run code" logo on the top right in VSCode. Now I learned that running Rust on VSCode is done quite differently than in other languages such as Python and JavaScript. In Python/JavaScript/etc, you can just click that button at the top right of the VSCode editor. But in Rust, this is wrong. If you want to run the script in Rust, you have to do it via terminal commands, namely cargo build and cargo run. When I put those commands in the terminal (when the work directory is the same as the project's root folder), then it actually executes the code and without complaining. Thank you everyone :D

@lnicola
Copy link
Member

lnicola commented May 13, 2022

Good point, flodiebold, it's this lens:

image

and you can also let VS generate a launch configuration:

image

image

image

I'm not sure what button @amab8901 is talking about.

@amab8901
Copy link
Author

Just to make sure, where is that "Run code" button located exactly?

@lnicola We only provide the "Run" code lens, which is not at the top-right, do we? Or am I misremembering?

"Run code" button is in the VSCode editor (and some other editors too, such as PyCharm). It's literally at the top-right corner of the editor program. Above the code-editing box, above the scroll sidebar, below the buttons for closing/minimizing window. It has a play-button logo.

@flodiebold
Copy link
Member

flodiebold commented May 13, 2022

Just to make sure, where is that "Run code" button located exactly?
@lnicola We only provide the "Run" code lens, which is not at the top-right, do we? Or am I misremembering?

"Run code" button is in the VSCode editor (and some other editors too, such as PyCharm). It's literally at the top-right corner of the editor program. Above the code-editing box, above the scroll sidebar, below the buttons for closing/minimizing window. It has a play-button logo.

I personally don't use use VSCode, so @lnicola correct me if I'm wrong, but I think that button is provided by some other extension, not built-in to VSCode. So that extension might need to be fixed. (It might be https://marketplace.visualstudio.com/items?itemName=formulahendry.code-runner, which does advertise Rust support.)

I would also note that you should open the folder containing Cargo.toml as a workspace, not just open the rs file as a single file, otherwise many rust-analyzer features will not work correctly.

@lnicola
Copy link
Member

lnicola commented May 13, 2022

Yeah, I don't see any button. There's one on the left (see a screenshot above) and a small toolbar when you're already in the debug mode, but I don't know about any button to the right.

The top-right corner looks like this for me:

image

@amab8901
Copy link
Author

amab8901 commented May 13, 2022

Just to make sure, where is that "Run code" button located exactly?
@lnicola We only provide the "Run" code lens, which is not at the top-right, do we? Or am I misremembering?

"Run code" button is in the VSCode editor (and some other editors too, such as PyCharm). It's literally at the top-right corner of the editor program. Above the code-editing box, above the scroll sidebar, below the buttons for closing/minimizing window. It has a play-button logo.

I personally don't use use
VSCode, so @lnicola correct me if I'm wrong, but I think that button is provided by some other extension, not built-in to VSCode. So that extension might need to be fixed. (It might be https://marketplace.visualstudio.com/items?itemName=formulahendry.code-runner, which does advertise Rust support.)

Here is the run button

play button

@lnicola
Copy link
Member

lnicola commented May 13, 2022

Maybe https://marketplace.visualstudio.com/items?itemName=JuanJoseCarracedo.vscode-un-button-script?

What extensions do you have installed?

@amab8901
Copy link
Author

Maybe https://marketplace.visualstudio.com/items?itemName=JuanJoseCarracedo.vscode-un-button-script?

What extensions do you have installed?

I have the following extensions:
[Deprecated]] Bracket Pair Colorizer 2
Babel ES6/ES7
Better C++ Syntax
Beter TOML
C/C++
C/C++ Debugging with Flags
C/C++ Extension Pack
C/C++ Themses
Cmake
Cmake Tools
Code Runner
CodeLLDB
crates
Doxygen Documentation Generator
ES7+ React/Redux/React-Native snippers
Jupyter
Jupyter Keymap
Jupyter Notebook Renderers
Live Server
Live Server (Five Server)
Pylance
Python
Remote - Containers
Remote - SSh
Remote - SSH: Editing Configuration Files
Remote - WSL
Rust and Friends
Rust Doc Viewer
Rust Flash Snippets
Rust Mod Generator
Rust Syntax
Rust Targets
rust-analyzer
Syntax Highlighter
Rust

@lnicola
Copy link
Member

lnicola commented May 13, 2022

Yeah, that's the Code Runner extension. It would have been easier if you had included this line in the log:

[Running] cd "~/Projects/hello/src/" && rustc main.rs && "~/Projects/hello/src/"main

@amab8901
Copy link
Author

Yeah, that's the Code Runner extension. It would have been easier if you had included this line in the log:

[Running] cd "~/Projects/hello/src/" && rustc main.rs && "~/Projects/hello/src/"main

why would it have been easier? Is it because it reveals whether you clicked the run button or ran it through the terminal?

@lnicola
Copy link
Member

lnicola commented May 13, 2022

No, because that's not the right way to compile a Rust project.

@amab8901
Copy link
Author

No, because that's not the right way to compile a Rust project.

Oh, so in Rust you compile the entire project as a single piece, while Python in contrast can run individual scripts without running the rest of the project? And does this have to do with the dichotomy between compiled vs interpreted languages?

@lnicola
Copy link
Member

lnicola commented May 13, 2022

Almost. You compile a project with cargo build (or cargo run), which in turn runs rustc on your code and on the libraries you're using. Notice how in that command line there's:

  • no edition argument, so it defaults to the 2015 edition
  • no reference to tokio, so it complains it doesn't know what tokio is

cargo run takes care of both. What Code Runner does only works for single-file, no-dependencies programs, and not even then.

In the meanwhile, you can use the this link instead, which does the right thing:

image

https://doc.rust-lang.org/stable/book/title-page.html has a good introduction to cargo.

@flodiebold
Copy link
Member

flodiebold commented May 13, 2022

Kind of, yeah. I suspect though that the code runner extension might be doing this because you opened the main.rs as a single file. Try opening the project folder (the one containing Cargo.toml) as a workspace. Maybe the extension will do the right thing in that case. (Unless you already did that, I can't really tell from the screenshots.)

(Edit: Ah no, from the breadcrumbs, it looks like you opened the folder already.)

@lnicola
Copy link
Member

lnicola commented May 13, 2022

Yeah, I opened the directory.

And by the way, I'd uninstall the other Rust extensions. Even Bracket Pair Colorizer is built into Code these days.

@amab8901
Copy link
Author

thank you for the advice, everyone :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-support Category: support questions
Projects
None yet
Development

No branches or pull requests

5 participants