Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upAdd a `compile_error!` macro to libstd #1695
Conversation
This comment has been minimized.
This comment has been minimized.
|
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 |
alexcrichton
added
the
T-libs
label
Aug 1, 2016
This comment has been minimized.
This comment has been minimized.
|
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 comment has been minimized.
This comment has been minimized.
|
This would make macro libraries like horrorshow suck significantly less. However, it would actually be nice to pass a custom span to |
This comment has been minimized.
This comment has been minimized.
golddranks
commented
Aug 1, 2016
|
What about error message localizations (as there has been some discussion recently about supporting localized compiler error messages)? Even if |
This comment has been minimized.
This comment has been minimized.
|
@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!"); |
nrc
reviewed
Aug 1, 2016
| 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. |
This comment has been minimized.
This comment has been minimized.
nrc
Aug 1, 2016
Member
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.
This comment has been minimized.
This comment has been minimized.
sgrif
Aug 1, 2016
Author
Contributor
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!.
This comment has been minimized.
This comment has been minimized.
sgrif
Aug 1, 2016
Author
Contributor
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.
nrc
reviewed
Aug 1, 2016
| # Drawbacks | ||
| [drawbacks]: #drawbacks | ||
|
|
||
| None |
This comment has been minimized.
This comment has been minimized.
nrc
Aug 1, 2016
Member
It adds another language-aware macro.
When we do have proc macros, there will be overlap between this feature and more sophisticated error handling for proc macros which might be exposed to macro_rules somehow.
nrc
reviewed
Aug 1, 2016
| [alternatives]: #alternatives | ||
|
|
||
| Wait for the stabilization of procedural macros, at which point a crate could | ||
| provide this functionality. |
This comment has been minimized.
This comment has been minimized.
nrc
Aug 1, 2016
Member
Seems there is some overlap with static_assert!, which iirc, was removed. It would be worth contrasting the two and motivating why this is worth adding, but static_assert! was removed.
This comment has been minimized.
This comment has been minimized.
kennytm
Aug 2, 2016
Member
IIRC Rust did not had static_assert!, but the awkward #[static_assert] (#1096).
sfackler
self-assigned this
Aug 1, 2016
This comment has been minimized.
This comment has been minimized.
|
cc #1146. On nightly there are the static_assert and compile_msg crates. |
This comment has been minimized.
This comment has been minimized.
|
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? |
This comment has been minimized.
This comment has been minimized.
sciyoshi
commented
Aug 4, 2016
|
|
This comment has been minimized.
This comment has been minimized.
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. |
This comment has been minimized.
This comment has been minimized.
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). |
This comment has been minimized.
This comment has been minimized.
briansmith
commented
Aug 20, 2016
|
Semantically, this would be just a special case of |
This comment has been minimized.
This comment has been minimized.
|
@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 |
This comment has been minimized.
This comment has been minimized.
|
I always imagined that something like this would be implemented as a |
This comment has been minimized.
This comment has been minimized.
|
@shepmaster I would like @solson to confirm it, but I'm pretty sure miri already supports |
This comment has been minimized.
This comment has been minimized.
|
@shepmaster @eddyb Miri can't reach the internal panic machinery inside |
This comment has been minimized.
This comment has been minimized.
DanielKeep
commented
Nov 7, 2016
|
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? |
This comment has been minimized.
This comment has been minimized.
|
Ping @sfackler |
This comment has been minimized.
This comment has been minimized.
|
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 |
This comment has been minimized.
This comment has been minimized.
rfcbot
commented
Feb 1, 2017
•
|
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. |
This comment has been minimized.
This comment has been minimized.
|
To someone having permission: Do we include |
This comment has been minimized.
This comment has been minimized.
|
Ah good point. I'd probably say we add it since it's a pretty straightforward addition. |
This comment has been minimized.
This comment has been minimized.
iliekturtles
commented
Feb 19, 2017
|
What about |
This comment has been minimized.
This comment has been minimized.
|
@iliekturtles Good call, it seems to me like they can and should be in |
This comment has been minimized.
This comment has been minimized.
|
I think |
This comment has been minimized.
This comment has been minimized.
KalitaAlexey
commented
Feb 27, 2017
|
@sgrif, |
This comment has been minimized.
This comment has been minimized.
|
ping @BurntSushi (checkbox) |
This comment has been minimized.
This comment has been minimized.
rfcbot
commented
Mar 14, 2017
|
|
rfcbot
added
the
final-comment-period
label
Mar 14, 2017
This comment has been minimized.
This comment has been minimized.
CasualX
commented
Mar 23, 2017
•
|
As a neat workaround you can (ab)use the I asked if this feature existed some time ago in /r/rust |
This comment has been minimized.
This comment has been minimized.
rfcbot
commented
Mar 24, 2017
|
The final comment period is now complete. |
sfackler
referenced this pull request
Mar 28, 2017
Closed
Tracking issue for the addition of the compile_error! macro #40872
sfackler
merged commit a4418bc
into
rust-lang:master
Mar 28, 2017
This comment has been minimized.
This comment has been minimized.
|
The comment period has elapsed, and this RFC is accepted! Tracking issue: rust-lang/rust#40872 |
sgrif commentedAug 1, 2016
Rendered