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

Bad spans from type error within code expanded from proc macro attribute #71968

Closed
euclio opened this issue May 7, 2020 · 5 comments · Fixed by tokio-rs/tokio#3766
Closed
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-proc-macros Area: Procedural macros C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@euclio
Copy link
Contributor

euclio commented May 7, 2020

I tried this code:

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=7d0f526db1b816572e927bbaaad2750e

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

I expected to see this happen: Type error reported on Ok(())

Instead, this happened: Got this diagnostic:

error[E0308]: mismatched types
 --> src/main.rs:1:1
  |
1 | #[tokio::main]
  | ^^^^^^^^^^^^^^- help: try adding a semicolon: `;`
  | |
  | expected `()`, found enum `std::result::Result`
  |
  = note: expected unit type `()`
                  found enum `std::result::Result<(), _>`
  = note: this error originates in an attribute macro (in Nightly builds, run with -Z macro-backtrace for more info)

Meta

rustc --version --verbose:

rustc 1.45.0-nightly (1836e3b42 2020-05-06)
binary: rustc
commit-hash: 1836e3b42a5b2f37fd79104eedbe8f48a5afdee6
commit-date: 2020-05-06
host: x86_64-unknown-linux-gnu
release: 1.45.0-nightly
LLVM version: 9.0
@euclio euclio added the C-bug Category: This is a bug. label May 7, 2020
@csmoe csmoe added the A-diagnostics Area: Messages for errors, warnings, and lints label May 7, 2020
@jonas-schievink jonas-schievink added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label May 7, 2020
@emmanueltouzery
Copy link

Maybe this should be linked to from #43081

@Aaron1011 Aaron1011 added the A-proc-macros Area: Procedural macros label Oct 24, 2020
@Aaron1011
Copy link
Member

This doesn't appear to be a bug in rustc. The #[tokio::main] attribute produces code like this:

fn main() {
    tokio::runtime::Builder::new_multi_thread()
        .enable_all()
        .build()
        .unwrap()
        .block_on(async {
            {
                Ok(())
            }
        })
}

which gives the error message:

error[E0308]: mismatched types
  --> src/main.rs:2:5
   |
1  |   fn main() {
   |             - expected `()` because of default return type
2  | /     tokio::runtime::Builder::new_multi_thread()
3  | |         .enable_all()
4  | |         .build()
5  | |         .unwrap()
...  |
9  | |             }
10 | |         })
   | |          ^- help: try adding a semicolon: `;`
   | |__________|
   |            expected `()`, found enum `std::result::Result`
   |
   = note: expected unit type `()`
                   found enum `std::result::Result<(), _>`

The error message points to the overall expression, which Tokio spans with Span::call_site(). To improve the diagnostics, Tokio could try to change where the error is emitted (e.g. asserting explicitly asserting the type of the main function), or re-span some of the generated code to point to something user-defined.

@Aaron1011
Copy link
Member

Opened https://github.com/tokio-rs/tokio/pull/3039/files

@taiki-e
Copy link
Member

taiki-e commented May 9, 2021

This has been fixed in tokio-rs/tokio#3766.

@estebank
Copy link
Contributor

Current output:

error[E0308]: mismatched types
 --> src/main.rs:3:5
  |
2 | async fn main() {
  |                 - expected `()` because of default return type
3 |     Ok(())
  |     ^^^^^^ expected `()`, found `Result<(), _>`
  |
  = note: expected unit type `()`
                  found enum `Result<(), _>`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-proc-macros Area: Procedural macros C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants