Asserting a compile error is thrown

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.

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]?

There are third-party crates for this, like trybuild and compiletest. The latter is a fork of the compiletest crate used internally to test rustc

You can use compile_fail in a doctest, like these:


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