Skip to content

Commit 16268a8

Browse files
committed
Auto merge of #43735 - est31:master, r=alexcrichton
Avoid calling the column!() macro in panic Closes #43057 This "fix" adds a new macro called `__rust_unstable_column` and to use it instead of the `column` macro inside panic. The new macro can be shadowed as well as `column` can, but its very likely that there is no code that does this in practice. There is no real way to make "unstable" macros that are usable by stable macros, so we do the next best thing and prefix the macro with `__rust_unstable` to make sure people recognize it is unstable. r? @alexcrichton
2 parents 57e720d + 5cf9f63 commit 16268a8

File tree

6 files changed

+74
-4
lines changed

6 files changed

+74
-4
lines changed

Diff for: src/libcore/macros.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
#[macro_export]
12+
// This stability attribute is totally useless.
13+
#[stable(feature = "rust1", since = "1.0.0")]
14+
#[cfg(stage0)]
15+
macro_rules! __rust_unstable_column {
16+
() => {
17+
column!()
18+
}
19+
}
20+
1121
/// Entry point of thread panic, for details, see std::macros
1222
#[macro_export]
1323
#[allow_internal_unstable]
@@ -18,7 +28,7 @@ macro_rules! panic {
1828
);
1929
($msg:expr) => ({
2030
static _MSG_FILE_LINE_COL: (&'static str, &'static str, u32, u32) =
21-
($msg, file!(), line!(), column!());
31+
($msg, file!(), line!(), __rust_unstable_column!());
2232
$crate::panicking::panic(&_MSG_FILE_LINE_COL)
2333
});
2434
($fmt:expr, $($arg:tt)*) => ({
@@ -27,7 +37,7 @@ macro_rules! panic {
2737
// insufficient, since the user may have
2838
// `#[forbid(dead_code)]` and which cannot be overridden.
2939
static _MSG_FILE_LINE_COL: (&'static str, u32, u32) =
30-
(file!(), line!(), column!());
40+
(file!(), line!(), __rust_unstable_column!());
3141
$crate::panicking::panic_fmt(format_args!($fmt, $($arg)*), &_MSG_FILE_LINE_COL)
3242
});
3343
}

Diff for: src/libstd/macros.rs

+14-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@
1414
//! library. Each macro is available for use when linking against the standard
1515
//! library.
1616
17+
#[macro_export]
18+
// This stability attribute is totally useless.
19+
#[stable(feature = "rust1", since = "1.0.0")]
20+
#[cfg(stage0)]
21+
macro_rules! __rust_unstable_column {
22+
() => {
23+
column!()
24+
}
25+
}
26+
1727
/// The entry point for panic of Rust threads.
1828
///
1929
/// This macro is used to inject panic into a Rust thread, causing the thread to
@@ -48,7 +58,8 @@ macro_rules! panic {
4858
($msg:expr) => ({
4959
$crate::rt::begin_panic($msg, {
5060
// static requires less code at runtime, more constant data
51-
static _FILE_LINE_COL: (&'static str, u32, u32) = (file!(), line!(), column!());
61+
static _FILE_LINE_COL: (&'static str, u32, u32) = (file!(), line!(),
62+
__rust_unstable_column!());
5263
&_FILE_LINE_COL
5364
})
5465
});
@@ -58,7 +69,8 @@ macro_rules! panic {
5869
// used inside a dead function. Just `#[allow(dead_code)]` is
5970
// insufficient, since the user may have
6071
// `#[forbid(dead_code)]` and which cannot be overridden.
61-
static _FILE_LINE_COL: (&'static str, u32, u32) = (file!(), line!(), column!());
72+
static _FILE_LINE_COL: (&'static str, u32, u32) = (file!(), line!(),
73+
__rust_unstable_column!());
6274
&_FILE_LINE_COL
6375
})
6476
});

Diff for: src/libsyntax/ext/source_util.rs

+10
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,16 @@ pub fn expand_column(cx: &mut ExtCtxt, sp: Span, tts: &[tokenstream::TokenTree])
5252
base::MacEager::expr(cx.expr_u32(topmost, loc.col.to_usize() as u32))
5353
}
5454

55+
/* __rust_unstable_column!(): expands to the current column number */
56+
pub fn expand_column_gated(cx: &mut ExtCtxt, sp: Span, tts: &[tokenstream::TokenTree])
57+
-> Box<base::MacResult+'static> {
58+
if sp.allows_unstable() {
59+
expand_column(cx, sp, tts)
60+
} else {
61+
cx.span_fatal(sp, "the __rust_unstable_column macro is unstable");
62+
}
63+
}
64+
5565
/// file!(): expands to the current filename */
5666
/// The filemap (`loc.file`) contains a bunch more information we could spit
5767
/// out if we wanted.

Diff for: src/libsyntax_ext/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ pub fn register_builtins(resolver: &mut syntax::ext::base::Resolver,
8989
use syntax::ext::source_util::*;
9090
register! {
9191
line: expand_line,
92+
__rust_unstable_column: expand_column_gated,
9293
column: expand_column,
9394
file: expand_file,
9495
stringify: expand_stringify,

Diff for: src/test/compile-fail/rust-unstable-column-gated.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
println!("{}", __rust_unstable_column!());
13+
//~^ERROR the __rust_unstable_column macro is unstable
14+
}

Diff for: src/test/run-pass/issue-43057.rs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
#![allow(unused)]
12+
13+
macro_rules! column {
14+
($i:ident) => {
15+
$i
16+
};
17+
}
18+
19+
fn foo() -> ! {
20+
panic!();
21+
}
22+
23+
fn main() {}

0 commit comments

Comments
 (0)