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

Rustc doesn't report line numbers for type errors within proc macro async fn within macro_rules macro #68430

Closed
lily-mara opened this issue Jan 21, 2020 · 10 comments · Fixed by #72388
Labels
A-async-await Area: Async & Await A-diagnostics Area: Messages for errors, warnings, and lints A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) A-proc-macros Area: Procedural macros AsyncAwait-Triaged Async-await issues that have been triaged during a working group meeting. C-bug Category: This is a bug. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. P-high High priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@lily-mara
Copy link

When using a macro to generate an async fn with a proc macro inside it, if the generated function includes a type error, rustc will not report any line number information. This makes finding and debugging type errors in large async applications extremely difficult.

I tried this code:

async fn foo() {}

macro_rules! gen_test {
    ($before:expr) => {
        #[tokio::test]
        async fn test_foo() {
            $before;
            let x: () = foo();
        }
    }
}

gen_test!(dbg!(4));

I expected to see this happen:

Error including line number information (you will get this output if you just comment out the $beforeline in the test fn)

error[E0308]: mismatched types
  --> src/lib.rs:8:25
   |
8  |             let x: () = foo();
   |                         ^^^^^
   |                         |
   |                         expected (), found opaque type
   |                         help: consider using `.await` here: `foo().await`
...
13 | gen_test!(dbg!(4));
   | ------------------- in this macro invocation
   |
   = note: expected type `()`
              found type `impl std::future::Future`

Instead, this happened:

Error including no line number information

error[E0308]: mismatched types
  |
  = note: expected type `()`
             found type `impl std::future::Future`

error: aborting due to previous error

Meta

rustc --version --verbose: Tested on Playground with stable 1.40.0 and nightly 1.42.0 (2020-01-20)

@jonas-schievink jonas-schievink added A-async-await Area: Async & Await A-diagnostics Area: Messages for errors, warnings, and lints A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jan 21, 2020
@swilcox3
Copy link

I can also add that incorrect line information is reported for panics, it's says src/main.rs:1:1 instead of the actual file and line.

thread 'tests::test_get_all_files_query_encoded' panicked at 'assertion failed: path.is_dir() || path.extension().unwrap() == "vwxp"', src\main.rs:1:1

@tmandry tmandry added AsyncAwait-OnDeck AsyncAwait-Triaged Async-await issues that have been triaged during a working group meeting. labels Jan 28, 2020
@Centril
Copy link
Contributor

Centril commented Jan 28, 2020

cc @petrochenkov @eddyb

@Sushisource
Copy link

Sushisource commented Feb 3, 2020

I'm seeing the same thing, at the trait/impl level. I have a macro that wraps async_trait (and can only be applied to trait defs or impls):

pub fn async_sync_trait(args: TokenStream, input: TokenStream) -> TokenStream {
    let args = parse_macro_input!(args as Args);
    let mut item = parse_macro_input!(input as Item);
    // Adds some methods
    expand(&mut item, args.local);
    if args.local {
        TokenStream::from(quote! {
            #[async_trait::async_trait(?Send)]
            #item
        })
    } else {
        TokenStream::from(quote! {
            #[async_trait::async_trait]
            #item
        })
    }
}

If I don't emit the #[async_trait::async_trait] attribute, line numbers work fine. If I don't use my macro at all and only use async_trait, line numbers work fine. The act of one macro emitting another seems to be the problem.

@petrochenkov
Copy link
Contributor

petrochenkov commented Feb 24, 2020

[triagebot] Can't say anything without investigating, won't investigate due to low priority.

@maackle
Copy link

maackle commented Feb 24, 2020

I'm having this problem too. I made a repo with a minimal reproduction FWIW.

I'm not sure if this completely explains the problem, but in this repo I defined two trivial proc macro attributes, one which spits out the code unchanged (#[noop]), another which quotes the code and then spits that out unchanged ([#quoter]). I tried various combinations of these two macros, and all of them preserve line numbers for type errors except when applying noop and then quoter. Here is the line that demonstrates the combination that causes line numbers to be erased: https://github.com/maackle/nested-proc-macro-mcve/blob/master/app/src/main.rs#L21

@tmandry tmandry added P-high High priority D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. labels Mar 3, 2020
@najamelan
Copy link
Contributor

@petrochenkov It's P-high now, I posted a small repro in another issue: #51635 (comment) That doesn't need anything async to show this behavior, so it might just be more general.

@nellshamrell
Copy link
Contributor

Taking a look at this.

@pnkfelix
Copy link
Member

pnkfelix commented May 6, 2020

triage: visited as part of general query of unassigned P-high non-ICE issues. But it looks like @nellshamrell is taking a look at this, so I'm marking as assigned to nell for now.

@nellshamrell
Copy link
Contributor

Hello all - looking at my list of things to do, I realized I'm not going to be able to get to this in the immediate future. Taking my name off the issue for now so someone else can pick it up if they have time before I do.

@nellshamrell nellshamrell removed their assignment May 11, 2020
@Aaron1011
Copy link
Member

This looks like another instance of #43081

Aaron1011 added a commit to Aaron1011/rust that referenced this issue Aug 22, 2020
Fixes rust-lang#68430

This is a re-attempt of PR rust-lang#72388, which was previously reverted due to
a large number of breakages. All of the known breakages should now be
patched upstream.
@tmandry tmandry moved this to Done in wg-async work Dec 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-async-await Area: Async & Await A-diagnostics Area: Messages for errors, warnings, and lints A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) A-proc-macros Area: Procedural macros AsyncAwait-Triaged Async-await issues that have been triaged during a working group meeting. C-bug Category: This is a bug. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. P-high High priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.