Skip to content

#[should_panic]'s return-type check ignores procedural-macro transformations #80143

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
meiomorphism opened this issue Dec 18, 2020 · 3 comments
Labels
A-proc-macros Area: Procedural macros C-bug Category: This is a bug.

Comments

@meiomorphism
Copy link
Contributor

When a procedural macro which transforms the return type of a function from ... -> SomeType to -> () is applied, the resulting function can be used with the attribute #[test] as expected. However, adding the attribute #[should_panic] to that test results in a compile-time error.

A (slightly simplified) motivating example:

#[test]
#[should_panic]
#[test_utils::matches_image("tests/data/blue_pixel.png")]
fn fails_on_mismatching_data() -> Box<[u32]> {
    Box::new([0; 1])
}

Result:

error: functions using `#[should_panic]` must return `()`

As a simple workaround, the transformed function can be wrapped with a separate #[test] #[should_panic] function returning () and invoked manually within that.

Tested on rustc 1.48.0 (7eac88abb 2020-11-16) and rustc 1.50.0-nightly (eb4fc71dc 2020-12-17).

@meiomorphism meiomorphism added the C-bug Category: This is a bug. label Dec 18, 2020
@camelid camelid added the A-proc-macros Area: Procedural macros label Dec 18, 2020
@meiomorphism
Copy link
Contributor Author

meiomorphism commented Dec 18, 2020

(Despite the title of this bug, I suspect the ideal fix would be to have #[test] and libtest differentiate between panics and errors, so that the type-check guard for #[should_panic] is no longer needed. I haven't looked too deeply into what that would involve, though.

Reference to relevant previous issues: #48854, #49909.)

@Aaron1011
Copy link
Member

I think this is an expected result of the left-to-right/top-to-bottom order of attribute invocation. #[test] gets invoked with the input:

#[should_panic]
#[test_utils::matches_image("tests/data/blue_pixel.png")]
fn fails_on_mismatching_data() -> Box<[u32]> {
    Box::new([0; 1])
}

so it sees a return type of (). You should be able to fix this by moving #[test] after #[test_utils::matches_image("tests/data/blue_pixel.png")]

@meiomorphism
Copy link
Contributor Author

Unfortunately, all six orderings of the three attributes yield identical results.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-proc-macros Area: Procedural macros C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

3 participants