From 5cf9f6330a02a0e86f08223289c74b36fba784db Mon Sep 17 00:00:00 2001 From: est31 Date: Thu, 10 Aug 2017 01:42:36 +0200 Subject: [PATCH] Add a feature gate @alexcrichton figured out a way how to do it :) --- src/libsyntax/ext/source_util.rs | 10 ++++++++++ src/libsyntax_ext/lib.rs | 2 +- .../compile-fail/rust-unstable-column-gated.rs | 14 ++++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 src/test/compile-fail/rust-unstable-column-gated.rs diff --git a/src/libsyntax/ext/source_util.rs b/src/libsyntax/ext/source_util.rs index 3cdd3a4b2c3..b293aa8de38 100644 --- a/src/libsyntax/ext/source_util.rs +++ b/src/libsyntax/ext/source_util.rs @@ -52,6 +52,16 @@ pub fn expand_column(cx: &mut ExtCtxt, sp: Span, tts: &[tokenstream::TokenTree]) base::MacEager::expr(cx.expr_u32(topmost, loc.col.to_usize() as u32)) } +/* __rust_unstable_column!(): expands to the current column number */ +pub fn expand_column_gated(cx: &mut ExtCtxt, sp: Span, tts: &[tokenstream::TokenTree]) + -> Box { + if sp.allows_unstable() { + expand_column(cx, sp, tts) + } else { + cx.span_fatal(sp, "the __rust_unstable_column macro is unstable"); + } +} + /// file!(): expands to the current filename */ /// The filemap (`loc.file`) contains a bunch more information we could spit /// out if we wanted. diff --git a/src/libsyntax_ext/lib.rs b/src/libsyntax_ext/lib.rs index 558d2e90310..5eab81dd28f 100644 --- a/src/libsyntax_ext/lib.rs +++ b/src/libsyntax_ext/lib.rs @@ -89,7 +89,7 @@ pub fn register_builtins(resolver: &mut syntax::ext::base::Resolver, use syntax::ext::source_util::*; register! { line: expand_line, - __rust_unstable_column: expand_column, + __rust_unstable_column: expand_column_gated, column: expand_column, file: expand_file, stringify: expand_stringify, diff --git a/src/test/compile-fail/rust-unstable-column-gated.rs b/src/test/compile-fail/rust-unstable-column-gated.rs new file mode 100644 index 00000000000..abc92c86eec --- /dev/null +++ b/src/test/compile-fail/rust-unstable-column-gated.rs @@ -0,0 +1,14 @@ +// 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() { + println!("{}", __rust_unstable_column!()); + //~^ERROR the __rust_unstable_column macro is unstable +}