Skip to content

Commit 0b29d26

Browse files
committed
Add compile_error!
Related to #40872
1 parent 0414594 commit 0b29d26

File tree

8 files changed

+96
-0
lines changed

8 files changed

+96
-0
lines changed

src/libcore/macros.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,17 @@ macro_rules! unimplemented {
562562
///
563563
/// For more information, see documentation for `std`'s macros.
564564
mod builtin {
565+
566+
/// Unconditionally causes compilation to fail with the given error message when encountered.
567+
///
568+
/// For more information, see the [RFC].
569+
///
570+
/// [RFC]: https://github.com/rust-lang/rfcs/blob/master/text/1695-add-error-macro.md
571+
#[unstable(feature = "compile_error_macro", issue = "40872")]
572+
#[macro_export]
573+
#[cfg(dox)]
574+
macro_rules! compile_error { ($msg:expr) => ({ /* compiler built-in */ }) }
575+
565576
/// The core macro for formatted string creation & output.
566577
///
567578
/// For more information, see the documentation for [`std::format_args!`].

src/libstd/macros.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,16 @@ macro_rules! assert_approx_eq {
238238
/// into libsyntax itself.
239239
#[cfg(dox)]
240240
pub mod builtin {
241+
242+
/// Unconditionally causes compilation to fail with the given error message when encountered.
243+
///
244+
/// For more information, see the [RFC].
245+
///
246+
/// [RFC]: https://github.com/rust-lang/rfcs/blob/master/text/1695-add-error-macro.md
247+
#[unstable(feature = "compile_error_macro", issue = "40872")]
248+
#[macro_export]
249+
macro_rules! compile_error { ($msg:expr) => ({ /* compiler built-in */ }) }
250+
241251
/// The core macro for formatted string creation & output.
242252
///
243253
/// This macro produces a value of type [`fmt::Arguments`]. This value can be

src/libsyntax/ext/expand.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,6 +1065,7 @@ impl<'feat> ExpansionConfig<'feat> {
10651065
fn enable_allow_internal_unstable = allow_internal_unstable,
10661066
fn enable_custom_derive = custom_derive,
10671067
fn proc_macro_enabled = proc_macro,
1068+
fn enable_compile_error = compile_error,
10681069
}
10691070
}
10701071

src/libsyntax/feature_gate.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ macro_rules! declare_features {
111111

112112
declare_features! (
113113
(active, asm, "1.0.0", Some(29722)),
114+
(active, compile_error, "1.20.0", Some(40872)),
114115
(active, concat_idents, "1.0.0", Some(29599)),
115116
(active, link_args, "1.0.0", Some(29596)),
116117
(active, log_syntax, "1.0.0", Some(29598)),
@@ -1008,6 +1009,9 @@ pub const EXPLAIN_LOG_SYNTAX: &'static str =
10081009
pub const EXPLAIN_CONCAT_IDENTS: &'static str =
10091010
"`concat_idents` is not stable enough for use and is subject to change";
10101011

1012+
pub const EXPLAIN_COMPILE_ERROR: &'static str =
1013+
"`compile_error` is not stable enough for use and is subject to change";
1014+
10111015
pub const EXPLAIN_TRACE_MACROS: &'static str =
10121016
"`trace_macros` is not stable enough for use and is subject to change";
10131017
pub const EXPLAIN_ALLOW_INTERNAL_UNSTABLE: &'static str =

src/libsyntax_ext/compile_error.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright 2012-2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// The compiler code necessary to support the compile_error! extension.
12+
13+
use syntax::ext::base::*;
14+
use syntax::ext::base;
15+
use syntax::feature_gate;
16+
use syntax_pos::Span;
17+
use syntax::tokenstream;
18+
19+
pub fn expand_compile_error<'cx>(cx: &'cx mut ExtCtxt,
20+
sp: Span,
21+
tts: &[tokenstream::TokenTree])
22+
-> Box<base::MacResult + 'cx> {
23+
if !cx.ecfg.enable_compile_error() {
24+
feature_gate::emit_feature_err(&cx.parse_sess,
25+
"compile_error",
26+
sp,
27+
feature_gate::GateIssue::Language,
28+
feature_gate::EXPLAIN_COMPILE_ERROR);
29+
return DummyResult::expr(sp);
30+
}
31+
32+
let var = match get_single_str_from_tts(cx, sp, tts, "compile_error!") {
33+
None => return DummyResult::expr(sp),
34+
Some(v) => v,
35+
};
36+
37+
cx.span_err(sp, &var);
38+
39+
DummyResult::any(sp)
40+
}

src/libsyntax_ext/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ extern crate rustc_errors as errors;
3434

3535
mod asm;
3636
mod cfg;
37+
mod compile_error;
3738
mod concat;
3839
mod concat_idents;
3940
mod env;
@@ -109,6 +110,7 @@ pub fn register_builtins(resolver: &mut syntax::ext::base::Resolver,
109110
option_env: env::expand_option_env,
110111
log_syntax: log_syntax::expand_syntax_ext,
111112
trace_macros: trace_macros::expand_trace_macros,
113+
compile_error: compile_error::expand_compile_error,
112114
}
113115

114116
// format_args uses `unstable` things internally.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(compile_error)]
12+
13+
fn main() {
14+
compile_error!("a very descriptive error message"); //~ ERROR: a very descriptive error message
15+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn main() {
12+
compile_error!("test"); //~ ERROR: `compile_error` is not stable enough
13+
}

0 commit comments

Comments
 (0)