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

incorrect use led to compile error with no location information #95

Closed
davepacheco opened this issue May 7, 2020 · 4 comments
Closed

Comments

@davepacheco
Copy link

I ran into a case where I used async_trait incorrectly and the result was a compiler error with no line number information. This seems more likely a compiler issue, not async_trait, but I had trouble reproducing the issue without async_trait. Here's my example:
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=a41abc2ee0d6fc3714ff09e8172aae94

Copying the code here:

use async_trait::async_trait; // 0.1.30

#[async_trait]
pub trait FooTrait {
    #[async_trait] // <-- This shouldn't be here.
    async fn a();
    async fn foo();
}

struct FooStruct {}

#[async_trait]
impl FooTrait for FooStruct {
    async fn foo() {}
}

Here's the build output:

   Compiling playground v0.0.1 (/playground)
error: expected one of: `unsafe`, `pub`, `trait`, `impl`

error: aborting due to previous error

error: could not compile `playground`.

To learn more, run the command again with --verbose.

Obviously I've applied the macro incorrectly here, but it's unfortunate that the compiler can't tell me where the problem is. Is this a known issue, maybe because the code that doesn't compile was created by the macro? Thanks in advance, and sorry if I'm down the wrong path here!

@davepacheco davepacheco changed the title incorrect use led to compile errors with no location information incorrect use led to compile error with no location information May 7, 2020
@dtolnay
Copy link
Owner

dtolnay commented May 7, 2020

It's a compiler bug: rust-lang/rust#43081. The compiler randomly loses all location information before it even gets to applying macros.

@davepacheco
Copy link
Author

Thanks for that pointer! And sorry to bug you with another instance of that issue.

@Aaron1011
Copy link

This will be fixed by rust-lang/rust#72306

@dtolnay
Copy link
Owner

dtolnay commented May 22, 2020

Confirmed fixed in rustc master. The error is not so good but it is at least correctly located. (The error is the inner #[async_trait] attempting to interpret your async fn as a trait or trait impl.)

error: expected one of: `unsafe`, `pub`, `trait`, `impl`
 --> src/main.rs:6:5
  |
6 |     async fn a();
  |     ^^^^^

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants