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

futures-macro: improve diagnostics on type mismatch #2433

Merged
merged 1 commit into from May 10, 2021

Conversation

taiki-e
Copy link
Member

@taiki-e taiki-e commented May 10, 2021

Port of my patch (tokio-rs/tokio#3766) in tokio.

Code:

#[futures_test::test]
async fn f() {
    Ok(())
}

Before:

error[E0308]: mismatched types
 --> futures/tests/test_macro.rs:1:1
  |
1 | #[futures_test::test]
  | ^^^^^^^^^^^^^^^^^^^^^ expected `()`, found enum `Result`
  |
  = note: expected unit type `()`
                  found enum `Result<(), _>`
  = note: this error originates in an attribute macro (in Nightly builds, run with -Z macro-backtrace for more info)

After:

error[E0308]: mismatched types
 --> futures/tests/test_macro.rs:3:5
  |
3 |     Ok(())
  |     ^^^^^^ expected `()`, found enum `Result`
  |
  = note: expected unit type `()`
                  found enum `Result<(), _>`
help: consider using a semicolon here
  |
3 |     Ok(());
  |           ^
help: try adding a return type
  |
2 | async fn f() -> Result<(), _> {
  |              ^^^^^^^^^^^^^^^^

@taiki-e taiki-e added A-macro Area: macro related 0.3-backport: pending The maintainer accepted to backport this to the 0.3 branch, but backport has not been done yet. labels May 10, 2021
Copy link
Member

@ibraheemdev ibraheemdev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, do we want to add ui tests?

@taiki-e
Copy link
Member Author

taiki-e commented May 10, 2021

trybuild runs cargo run/cargo build/cargo check, not cargo test, so this error cannot be tested by trybuild. (The error is silently ignored because it occurs after macro generates the #[test] function. rustdoc has a similar problem.)

@taiki-e taiki-e merged commit 961af12 into master May 10, 2021
@taiki-e taiki-e deleted the macro-diagnostics branch May 10, 2021 16:28
@taiki-e
Copy link
Member Author

taiki-e commented May 10, 2021

trybuild runs cargo run/cargo build/cargo check, not cargo test, so this error cannot be tested by trybuild. (The error is silently ignored because it occurs after macro generates the #[test] function. rustdoc has a similar problem.)

However, errors generated by macro can be tested, so it is probably a good idea to have ui tests that test for errors like "invalid argument" and "Only async functions are supported".

@taiki-e taiki-e added 0.3-backport: completed and removed 0.3-backport: pending The maintainer accepted to backport this to the 0.3 branch, but backport has not been done yet. labels May 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants