# Asserting a compile error is thrown

**URL:** <https://internals.rust-lang.org/t/asserting-a-compile-error-is-thrown/17386>\
**Category:** language design\
**Created:** [September 14, 2022, 3:52am UTC](https://internals.rust-lang.org/t/asserting-a-compile-error-is-thrown/17386 "2022-09-14T03:52:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![simbleau](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/simbleau/32/9053_2.png) [@simbleau](https://internals.rust-lang.org/u/simbleau)\
**Post date:** [September 14, 2022, 3:52am UTC](https://internals.rust-lang.org/t/asserting-a-compile-error-is-thrown/17386/1 "2022-09-14T03:52:26Z")

</div>

Is there a way at all to ensure a compile error is thrown?

Let's take the following scenario. Someone is making a special geometry library.

```rust
trait Calculable<A> : std::ops::Add {}
struct Length;
struct Angle;
impl Calculable<Length> for Length {}
impl std::ops::Add<Length> for Length {
   ....
}
impl Calculable<Angle> for Angle {}
impl std::ops::Add<Angle> for Angle {
   ....
}

```

This example describes a situation where `Length + Length` is valid, and `Angle + Angle` is valid, but `Length + Angle` is not, because `Add<Angle>` is not implemented for `Length`.

One would actually consider it a bug if this were to be allowed, in some way.

So, similar to `#[should_panic]`, is there anything like `#[shouldnt_compile]`?

---

<div class="post-metadata">

**Author:** ![Jules-Bertholet](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jules-bertholet/32/10671_2.png) [@Jules-Bertholet](https://internals.rust-lang.org/u/Jules-Bertholet)\
**Post date:** [September 14, 2022, 4:32am UTC](https://internals.rust-lang.org/t/asserting-a-compile-error-is-thrown/17386/2 "2022-09-14T04:32:28Z")

</div>

There are third-party crates for this, like [trybuild](https://github.com/dtolnay/trybuild) and [compiletest](https://github.com/Manishearth/compiletest-rs). The latter is a fork of the `compiletest` crate used internally to test `rustc`

---

<div class="post-metadata">

**Author:** ![cuviper](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cuviper/32/1897_2.png) [@cuviper](https://internals.rust-lang.org/u/cuviper)\
**Post date:** [September 14, 2022, 5:50am UTC](https://internals.rust-lang.org/t/asserting-a-compile-error-is-thrown/17386/3 "2022-09-14T05:50:26Z")

</div>

You can use `compile_fail` in a doctest, like these:

> **[rayon/src/compile\_fail at master · rayon-rs/rayon](https://github.com/rayon-rs/rayon/tree/master/src%2Fcompile_fail)**
>
> master/src%2Fcompile\_fail

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [December 13, 2022, 5:50am UTC](https://internals.rust-lang.org/t/asserting-a-compile-error-is-thrown/17386/4 "2022-12-13T05:50:49Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
