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

Compile_fail for regular tests #11468

Open
hkBst opened this issue Dec 9, 2022 · 5 comments
Open

Compile_fail for regular tests #11468

hkBst opened this issue Dec 9, 2022 · 5 comments
Labels
C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted`

Comments

@hkBst
Copy link

hkBst commented Dec 9, 2022

Would be great to be able to annotate regular tests as failing to compile.

 Is it going to come for regular tests? Such that I can write `#[compile_fail]` above my test functions?

Originally posted by @faulesocke in rust-lang/rust#12335 (comment)

@weihanglo
Copy link
Member

Cargo doesn't know how to parse source code. Without the support from rustc, Cargo can do nothing on attributes unfortunately.

Apart from that, could you describe your expectation and scenario about #[compile_fail]? Is there any existing crate doing similar things a good candidate to integration into Cargo (compiletest-rs maybe)?

I would appreciate if you share more contexts. Thank you.

@weihanglo weihanglo added the C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` label Dec 9, 2022
@epage
Copy link
Contributor

epage commented Dec 9, 2022

A common crate for testing proc macros is trybuild

@hkBst
Copy link
Author

hkBst commented Dec 9, 2022

@weihanglo My direct interest was in using this to assert that certain code does not pass type checking due to variance, but I think that only really makes sense for invariant types. For those one could write tests to assert that they are neither covariant nor contravariant.

@ehuss
Copy link
Contributor

ehuss commented Dec 11, 2022

Another alternative is to use a doctest, which is compiled as a separate file and supports the compile_fail attribute to indicate that the example should fail to compile.

I think it is unlikely that this could be supported with an attribute, as I suspect it would be nearly impossible for rustc to "allow" a subsection of code to fail to compile.

Can you provide an example of the kind of test that you would want to assert a specific kind of variance?

@hkBst
Copy link
Author

hkBst commented Dec 12, 2022

There are several invariant types in std that could have tests written for them, like so:

#[test]
// should fail to compile because Cell is invariant
fn cell_is_covariant_over_inner_type() {
    fn is_covariant<'a>(_c: &'a std::cell::Cell<&'a str>) {}
    let cell_of_static = std::cell::Cell::<&'static str>::new("static-string");
    let _ = is_covariant(&cell_of_static);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted`
Projects
None yet
Development

No branches or pull requests

4 participants