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

Add #![cfg_assert] #1563

Closed
wants to merge 2 commits into from
Closed

Add #![cfg_assert] #1563

wants to merge 2 commits into from

Conversation

Amanieu
Copy link
Member

@Amanieu Amanieu commented Mar 31, 2016

The idea came from this thread: #![cfg_assert] allows a user to place guards on his code to fail early on incompatible configurations.

Rendered

@nrc nrc added the T-lang Relevant to the language team, which will review and decide on the RFC. label Mar 31, 2016

#![cfg_assert(any(target_arch = "x86", target_arch = "x86_64"), "This library only works on x86")]

#![cfg_assert(all(feature = "foo", feature = "bar"), "The `foo` and `bar` features can't both be enabled at once")]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing not(...)

@brendanzab
Copy link
Member

I agree that it would be nice to trigger build errors, but it might also be nice to be able to trigger warnings as well (without having to create a custom lint). Not sure how this would fit in with this design.

@seanmonstar
Copy link
Contributor

This looks quite similar to static_assert, which was removed: https://github.com/rust-lang/rfcs/blob/master/text/1096-remove-static-assert.md

@oli-obk
Copy link
Contributor

oli-obk commented Apr 1, 2016

with the static_assert crate you can write static_assert!(cfg!(target_os = "linux"))

@nikomatsakis nikomatsakis self-assigned this Apr 7, 2016
@pnkfelix
Copy link
Member

pnkfelix commented Apr 7, 2016

@Amanieu will #![cfg_assert("this will never compile in any cfg")] work? I cannot tell from the RFC whether having no cfg-inputs at all is legal.

@Amanieu
Copy link
Member Author

Amanieu commented Apr 7, 2016

@pnkfelix That isn't part of the RFC, however I can see it being useful in some cases.

@nikomatsakis
Copy link
Contributor

We discussed this briefly in yesterday's @rust-lang/lang meeting. It seemed like, once we have even rudimentary plugins stabilized, we could make a specialized plugin that implements this functionality relatively easily. In the meantime, one could certainly achieve the same effect by having some code that fails to compile which is conditionally included:

#[cfg(...)] const error: usize = "this configuration is not supported";

Inelegant, but it works.

However, reading over the thread where this suggestion originated, we were wondering something that perhaps @Amanieu or @alexcrichton could clarify: is there some other use for this metadata beyond simply halting compilation? How frequent is the desire for this?

In other words: is there a strong reason for this to be logic that the compiler itself understands, versus something supplied externally or achieved via hackery?

@Amanieu
Copy link
Member Author

Amanieu commented Apr 8, 2016

I guess there isn't a very urgent need for this feature, so I'm ok with closing this RFC and waiting for compiler plugins or something similar.

@alexcrichton
Copy link
Member

@nikomatsakis In the past I've used static assertions in C for just verifying sanity of various properties like size/alignment, but that's not covered by this definition of #[cfg_assert], so beyond that I don't personally know if there's much need beyond halting the compile with a nice user defined error message.

@nikomatsakis
Copy link
Contributor

Personally, I'd be inclined to close and revisit once plugins stabilization has progressed a bit more.

@aturon
Copy link
Member

aturon commented Jul 28, 2016

Triaging old RFCs: based on the last few comments, I'm going to go ahead and close.

@aturon aturon closed this Jul 28, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-lang Relevant to the language team, which will review and decide on the RFC.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants