Surpassing C in conditional compilation

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

Surpassing C in conditional compilation

Christopher Bergqvist
Hi Rust devs!

First of all, thank you for putting so much effort into creating this promising language!

I would be glad to hear your thoughts on some minor bikeshedding before the 1.0 release. From what I can see, the conditional compilation in Rust shares a weakness with the C preprocessor. The problem is lacking checks against misspelled identifiers, as in this example:

fn foo() { syntaxError }

Here, target_oss has an extra "s", and dragon_fly should not have the "_". However, the compiler will compile the code without notifying the programmer that anything is wrong. I propose that cfg-identifiers should be treated similarly to other identifiers, so that they can trigger unresolved errors.

One way I see to implement this for custom cfg identifiers specified on the command line is going from

$ rustc --cfg some_condition


$ rustc --cfg some_condition=true

And then the code would look like

fn conditional_function() { }

Instead of just

fn conditional_function() { }

Alone, this change would require the programmer to always specify all values of all custom cfg identifiers when building code that uses them. To avoid that, it would be good to be able to define default values of identifiers in .rs files. There could even be .rs files distributed with rustc which contain specifications of all built-in cfg identifiers (and possibly also lists of their allowed values).

(There's also the inconsistency in "=" vs "==" equality operators between cfg-expressions and regular Rust expressions, an inconsistency C does not share, but that is not a big concern).

Thanks for reading this far,
Rust-dev mailing list
[hidden email]