From 0b29d26079f8e4bb76069b2e3d9c0d8e0f689acf Mon Sep 17 00:00:00 2001 From: Wesley Wiser Date: Sat, 6 May 2017 23:26:45 -0400 Subject: [PATCH] Add compile_error! Related to #40872 --- src/libcore/macros.rs | 11 +++++ src/libstd/macros.rs | 10 +++++ src/libsyntax/ext/expand.rs | 1 + src/libsyntax/feature_gate.rs | 4 ++ src/libsyntax_ext/compile_error.rs | 40 +++++++++++++++++++ src/libsyntax_ext/lib.rs | 2 + src/test/compile-fail/compile_error_macro.rs | 15 +++++++ .../feature-gate-compile_error.rs | 13 ++++++ 8 files changed, 96 insertions(+) create mode 100644 src/libsyntax_ext/compile_error.rs create mode 100644 src/test/compile-fail/compile_error_macro.rs create mode 100644 src/test/compile-fail/feature-gate-compile_error.rs diff --git a/src/libcore/macros.rs b/src/libcore/macros.rs index 91ee064aaba6a..1da91a3b70850 100644 --- a/src/libcore/macros.rs +++ b/src/libcore/macros.rs @@ -562,6 +562,17 @@ macro_rules! unimplemented { /// /// For more information, see documentation for `std`'s macros. mod builtin { + + /// Unconditionally causes compilation to fail with the given error message when encountered. + /// + /// For more information, see the [RFC]. + /// + /// [RFC]: https://github.com/rust-lang/rfcs/blob/master/text/1695-add-error-macro.md + #[unstable(feature = "compile_error_macro", issue = "40872")] + #[macro_export] + #[cfg(dox)] + macro_rules! compile_error { ($msg:expr) => ({ /* compiler built-in */ }) } + /// The core macro for formatted string creation & output. /// /// For more information, see the documentation for [`std::format_args!`]. diff --git a/src/libstd/macros.rs b/src/libstd/macros.rs index 496c014f70e2f..82c4f0830a626 100644 --- a/src/libstd/macros.rs +++ b/src/libstd/macros.rs @@ -238,6 +238,16 @@ macro_rules! assert_approx_eq { /// into libsyntax itself. #[cfg(dox)] pub mod builtin { + + /// Unconditionally causes compilation to fail with the given error message when encountered. + /// + /// For more information, see the [RFC]. + /// + /// [RFC]: https://github.com/rust-lang/rfcs/blob/master/text/1695-add-error-macro.md + #[unstable(feature = "compile_error_macro", issue = "40872")] + #[macro_export] + macro_rules! compile_error { ($msg:expr) => ({ /* compiler built-in */ }) } + /// The core macro for formatted string creation & output. /// /// This macro produces a value of type [`fmt::Arguments`]. This value can be diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs index c91c77719e699..f8a26287bd47b 100644 --- a/src/libsyntax/ext/expand.rs +++ b/src/libsyntax/ext/expand.rs @@ -1065,6 +1065,7 @@ impl<'feat> ExpansionConfig<'feat> { fn enable_allow_internal_unstable = allow_internal_unstable, fn enable_custom_derive = custom_derive, fn proc_macro_enabled = proc_macro, + fn enable_compile_error = compile_error, } } diff --git a/src/libsyntax/feature_gate.rs b/src/libsyntax/feature_gate.rs index 4543378789dfe..d7d3a70f3c7c5 100644 --- a/src/libsyntax/feature_gate.rs +++ b/src/libsyntax/feature_gate.rs @@ -111,6 +111,7 @@ macro_rules! declare_features { declare_features! ( (active, asm, "1.0.0", Some(29722)), + (active, compile_error, "1.20.0", Some(40872)), (active, concat_idents, "1.0.0", Some(29599)), (active, link_args, "1.0.0", Some(29596)), (active, log_syntax, "1.0.0", Some(29598)), @@ -1008,6 +1009,9 @@ pub const EXPLAIN_LOG_SYNTAX: &'static str = pub const EXPLAIN_CONCAT_IDENTS: &'static str = "`concat_idents` is not stable enough for use and is subject to change"; +pub const EXPLAIN_COMPILE_ERROR: &'static str = + "`compile_error` is not stable enough for use and is subject to change"; + pub const EXPLAIN_TRACE_MACROS: &'static str = "`trace_macros` is not stable enough for use and is subject to change"; pub const EXPLAIN_ALLOW_INTERNAL_UNSTABLE: &'static str = diff --git a/src/libsyntax_ext/compile_error.rs b/src/libsyntax_ext/compile_error.rs new file mode 100644 index 0000000000000..bb496716d8c46 --- /dev/null +++ b/src/libsyntax_ext/compile_error.rs @@ -0,0 +1,40 @@ +// Copyright 2012-2017 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// The compiler code necessary to support the compile_error! extension. + +use syntax::ext::base::*; +use syntax::ext::base; +use syntax::feature_gate; +use syntax_pos::Span; +use syntax::tokenstream; + +pub fn expand_compile_error<'cx>(cx: &'cx mut ExtCtxt, + sp: Span, + tts: &[tokenstream::TokenTree]) + -> Box { + if !cx.ecfg.enable_compile_error() { + feature_gate::emit_feature_err(&cx.parse_sess, + "compile_error", + sp, + feature_gate::GateIssue::Language, + feature_gate::EXPLAIN_COMPILE_ERROR); + return DummyResult::expr(sp); + } + + let var = match get_single_str_from_tts(cx, sp, tts, "compile_error!") { + None => return DummyResult::expr(sp), + Some(v) => v, + }; + + cx.span_err(sp, &var); + + DummyResult::any(sp) +} diff --git a/src/libsyntax_ext/lib.rs b/src/libsyntax_ext/lib.rs index 60f5d24ac9755..d28b00edc5f48 100644 --- a/src/libsyntax_ext/lib.rs +++ b/src/libsyntax_ext/lib.rs @@ -34,6 +34,7 @@ extern crate rustc_errors as errors; mod asm; mod cfg; +mod compile_error; mod concat; mod concat_idents; mod env; @@ -109,6 +110,7 @@ pub fn register_builtins(resolver: &mut syntax::ext::base::Resolver, option_env: env::expand_option_env, log_syntax: log_syntax::expand_syntax_ext, trace_macros: trace_macros::expand_trace_macros, + compile_error: compile_error::expand_compile_error, } // format_args uses `unstable` things internally. diff --git a/src/test/compile-fail/compile_error_macro.rs b/src/test/compile-fail/compile_error_macro.rs new file mode 100644 index 0000000000000..2a2c3fd809232 --- /dev/null +++ b/src/test/compile-fail/compile_error_macro.rs @@ -0,0 +1,15 @@ +// Copyright 2017 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(compile_error)] + +fn main() { + compile_error!("a very descriptive error message"); //~ ERROR: a very descriptive error message +} diff --git a/src/test/compile-fail/feature-gate-compile_error.rs b/src/test/compile-fail/feature-gate-compile_error.rs new file mode 100644 index 0000000000000..545c6852961c7 --- /dev/null +++ b/src/test/compile-fail/feature-gate-compile_error.rs @@ -0,0 +1,13 @@ +// Copyright 2017 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +fn main() { + compile_error!("test"); //~ ERROR: `compile_error` is not stable enough +}