Condiitonal compilation based on type parameter constraints

As far as I understand this is not doable today. But what exactly is blocking this, and do we have anything in the works that would enable us to compile code like this?

This would mean implementing Debug on types whose inner types are not necessarily Debug, but are external to the application sooo much easier.

Specialization is a helpful feature, however I don't think it helps if there are multiple fields which may or may not be Debug, and even if it does, it presents a source of code duplication and an 2^N problem of implementations.

struct Foo<T, U> {
a: u32,
b: T,
c: U,
}

impl<T: Debug,U> fmt::Debug for Foo<T,U> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let ret = f.debug_struct("Point");
        let ret = r.field("a", &self.a);
        let ret = r.field("b", &self.b);

        #[if_satisfied(G: Debug)]
        let ret = r.field("c", &self.c);
        #[else]
        let ret = r.field("c", "<opaque>");
        #[end]

         ret.finish()
    }
}

Conditional compilation like all macros is resolved before typechecking as typechecking needs the output of macro expansion. Your proposal would create a cyclic dependency between macro expansion and typechecking. If you suggest somehow lowering it such that the conditional compilation is preserved after macro expansion, you get the same soundness issues as specialization as you can implement specialization using this.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.