-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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 a compile_error!
macro to libstd
#1695
Conversation
I'd love something like this. I'm currently forcing type errors to do these kinds of checks, but that's not a great user experience: https://github.com/sfackler/rust-postgres/blob/306a6a9d187167b864ed5b28147b5da176549f58/src/feature_check.rs |
Tagging as T-libs, but also relevant to the @rust-lang/lang team as it would essentially be adding a new macro the language (e.g. libcore wouldn't define it) |
This would make macro libraries like horrorshow suck significantly less. However, it would actually be nice to pass a custom span to |
What about error message localizations (as there has been some discussion recently about supporting localized compiler error messages)? Even if |
@golddranks I'd imagine that localization will include some sort of way to detect them using #[cfg(locale = "en_US")]
compile_error!("LOOKOUT!");
#[cfg(locale = "de")]
compile_error!("ACHTUNG!"); |
The span given for the failure should be the invocation of the `compile_error!` | ||
macro. The macro must take exactly one argument, which is a string literal. The | ||
macro will then call `span_err` with the provided message on the expansion | ||
context, and will not expand to any further code. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be worth accepting a format string rather than a plain string literal? Errors seem like the kind of thing that would be commonly customised?
Is it also worth accepting alternative spans some how? That seems like a useful tool, but I don't have an idea for how it would work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given that this entirely at compile time, I'm not sure that there's anything that we could format that wouldn't be equally handled by concat!
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The only way I can think of to accept alternative spans would be to take a file/line number/char number triple, which I think is fine as an option if folks are in favor of it.
cc #1146. On nightly there are the static_assert and compile_msg crates. |
I am in favor of this general idea (along with some kind of static-assert, but I guess that (could be) a separate thing). The question of how to "specify" a span is interesting -- I am presuming this error would be reported during macro expansion? |
👍, I think this is a good first step towards improving the readability of macro errors in general. How would custom spans be specified? @sgrif any thoughts on also including a |
Correct. The implementation would essentially just be a procedural macro which passes its single argument to
I'm really not sure that there's a compelling use case for that, as you really don't generally have span information without procedural macros in the first place.
Seems like a reasonable addition if others are in favor of it. |
Is the span information not attached to the tokens passed to a macro? I'd like to be able point out which token in the macro input caused the macro expansion to fail (and why). |
Semantically, this would be just a special case of |
@briansmith // A potential desugaring, assuming CTFE that turns panics into compile errors.
const _: () = assert!(cfg!(locale = "de"), "ACHTUNG!"); I'm not sure if being able to implement |
I always imagined that something like this would be implemented as a |
@shepmaster I would like @solson to confirm it, but I'm pretty sure miri already supports |
@shepmaster @eddyb Miri can't reach the internal panic machinery inside |
Just popping by to note that the "force a type error on a constant" trick doesn't work in macros any more: the compiler just points at the macro invocation, and never shows the erroneous construct. Right now, I have no way of catching and reporting input mistakes to the user. Can we get something, anything for this? |
Ping @sfackler |
Ah yes hello! I'd like to FCP to merge - while this will be implementable in the future via third party syntax extensions, it's a common enough workflow that I feel it's good to natively support it. @rfcbot fcp merge |
Team member @sfackler has proposed to merge this. The next step is review by the rest of the tagged teams: No concerns currently listed. Once these reviewers reach consensus, this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
To someone having permission: Do we include |
Ah good point. I'd probably say we add it since it's a pretty straightforward addition. |
What about |
@iliekturtles Good call, it seems to me like they can and should be in |
I think |
@sgrif, |
ping @BurntSushi (checkbox) |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
As a neat workaround you can (ab)use the I asked if this feature existed some time ago in /r/rust |
The final comment period is now complete. |
The comment period has elapsed, and this RFC is accepted! Tracking issue: rust-lang/rust#40872 |
Rendered