Skip to content

Commit ef25b7d

Browse files
committed
Change the rt::unwind line argument type from usize to u32.
1 parent c87ec1e commit ef25b7d

File tree

6 files changed

+75
-11
lines changed

6 files changed

+75
-11
lines changed

src/libcore/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
//! provided by the [rlibc crate](https://crates.io/crates/rlibc).
3939
//!
4040
//! * `rust_begin_unwind` - This function takes three arguments, a
41-
//! `fmt::Arguments`, a `&str`, and a `usize`. These three arguments dictate
41+
//! `fmt::Arguments`, a `&str`, and a `u32`. These three arguments dictate
4242
//! the panic message, the file at which panic was invoked, and the line.
4343
//! It is up to consumers of this core library to define this panic
4444
//! function; it is only required to never return.

src/libcore/panicking.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
//! interface for panicking is:
1717
//!
1818
//! ```ignore
19-
//! fn panic_impl(fmt: fmt::Arguments, &(&'static str, usize)) -> !;
19+
//! fn panic_impl(fmt: fmt::Arguments, &(&'static str, u32)) -> !;
2020
//! ```
2121
//!
2222
//! This definition allows for panicking with any general message, but it does not
@@ -58,8 +58,8 @@ pub fn panic_fmt(fmt: fmt::Arguments, file_line: &(&'static str, u32)) -> ! {
5858
#[allow(improper_ctypes)]
5959
extern {
6060
#[lang = "panic_fmt"]
61-
fn panic_impl(fmt: fmt::Arguments, file: &'static str, line: usize) -> !;
61+
fn panic_impl(fmt: fmt::Arguments, file: &'static str, line: u32) -> !;
6262
}
6363
let (file, line) = *file_line;
64-
unsafe { panic_impl(fmt, file, line as usize) }
64+
unsafe { panic_impl(fmt, file, line) }
6565
}

src/libstd/macros.rs

+48-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#[macro_export]
3939
#[stable(feature = "rust1", since = "1.0.0")]
4040
#[allow_internal_unstable]
41+
#[cfg(stage0)]
4142
macro_rules! panic {
4243
() => ({
4344
panic!("explicit panic")
@@ -55,7 +56,53 @@ macro_rules! panic {
5556
// used inside a dead function. Just `#[allow(dead_code)]` is
5657
// insufficient, since the user may have
5758
// `#[forbid(dead_code)]` and which cannot be overridden.
58-
static _FILE_LINE: (&'static str, usize) = (file!(), line!() as usize);
59+
static _FILE_LINE: (&'static str, u32) = (file!(), line!());
60+
&_FILE_LINE
61+
})
62+
});
63+
}
64+
65+
/// The entry point for panic of Rust tasks.
66+
///
67+
/// This macro is used to inject panic into a Rust task, causing the task to
68+
/// unwind and panic entirely. Each task's panic can be reaped as the
69+
/// `Box<Any>` type, and the single-argument form of the `panic!` macro will be
70+
/// the value which is transmitted.
71+
///
72+
/// The multi-argument form of this macro panics with a string and has the
73+
/// `format!` syntax for building a string.
74+
///
75+
/// # Examples
76+
///
77+
/// ```should_panic
78+
/// # #![allow(unreachable_code)]
79+
/// panic!();
80+
/// panic!("this is a terrible mistake!");
81+
/// panic!(4); // panic with the value of 4 to be collected elsewhere
82+
/// panic!("this is a {} {message}", "fancy", message = "message");
83+
/// ```
84+
#[macro_export]
85+
#[stable(feature = "rust1", since = "1.0.0")]
86+
#[allow_internal_unstable]
87+
#[cfg(not(stage0))]
88+
macro_rules! panic {
89+
() => ({
90+
panic!("explicit panic")
91+
});
92+
($msg:expr) => ({
93+
$crate::rt::begin_unwind($msg, {
94+
// static requires less code at runtime, more constant data
95+
static _FILE_LINE: (&'static str, u32) = (file!(), line!());
96+
&_FILE_LINE
97+
})
98+
});
99+
($fmt:expr, $($arg:tt)+) => ({
100+
$crate::rt::begin_unwind_fmt(format_args!($fmt, $($arg)+), {
101+
// The leading _'s are to avoid dead code warnings if this is
102+
// used inside a dead function. Just `#[allow(dead_code)]` is
103+
// insufficient, since the user may have
104+
// `#[forbid(dead_code)]` and which cannot be overridden.
105+
static _FILE_LINE: (&'static str, u32) = (file!(), line!());
59106
&_FILE_LINE
60107
})
61108
});

src/libstd/panicking.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ thread_local! {
2626
}
2727
}
2828

29-
pub fn on_panic(obj: &(Any+Send), file: &'static str, line: usize) {
29+
pub fn on_panic(obj: &(Any+Send), file: &'static str, line: u32) {
3030
let msg = match obj.downcast_ref::<&'static str>() {
3131
Some(s) => *s,
3232
None => match obj.downcast_ref::<String>() {

src/libstd/rt/unwind.rs

+21-4
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ struct Exception {
7878
cause: Option<Box<Any + Send + 'static>>,
7979
}
8080

81-
pub type Callback = fn(msg: &(Any + Send), file: &'static str, line: usize);
81+
pub type Callback = fn(msg: &(Any + Send), file: &'static str, line: u32);
8282

8383
// Variables used for invoking callbacks when a thread starts to unwind.
8484
//
@@ -484,7 +484,7 @@ pub mod eabi {
484484
/// Entry point of panic from the libcore crate.
485485
#[lang = "panic_fmt"]
486486
pub extern fn rust_begin_unwind(msg: fmt::Arguments,
487-
file: &'static str, line: usize) -> ! {
487+
file: &'static str, line: u32) -> ! {
488488
begin_unwind_fmt(msg, &(file, line))
489489
}
490490

@@ -495,7 +495,7 @@ pub extern fn rust_begin_unwind(msg: fmt::Arguments,
495495
/// on (e.g.) the inlining of other functions as possible), by moving
496496
/// the actual formatting into this shared place.
497497
#[inline(never)] #[cold]
498-
pub fn begin_unwind_fmt(msg: fmt::Arguments, file_line: &(&'static str, usize)) -> ! {
498+
pub fn begin_unwind_fmt(msg: fmt::Arguments, file_line: &(&'static str, u32)) -> ! {
499499
use fmt::Write;
500500

501501
// We do two allocations here, unfortunately. But (a) they're
@@ -510,6 +510,7 @@ pub fn begin_unwind_fmt(msg: fmt::Arguments, file_line: &(&'static str, usize))
510510

511511
/// This is the entry point of unwinding for panic!() and assert!().
512512
#[inline(never)] #[cold] // avoid code bloat at the call sites as much as possible
513+
#[cfg(stage0)]
513514
pub fn begin_unwind<M: Any + Send>(msg: M, file_line: &(&'static str, usize)) -> ! {
514515
// Note that this should be the only allocation performed in this code path.
515516
// Currently this means that panic!() on OOM will invoke this code path,
@@ -518,6 +519,22 @@ pub fn begin_unwind<M: Any + Send>(msg: M, file_line: &(&'static str, usize)) ->
518519
// be performed in the parent of this thread instead of the thread that's
519520
// panicking.
520521

522+
// see below for why we do the `Any` coercion here.
523+
let (file, line) = *file_line;
524+
begin_unwind_inner(Box::new(msg), &(file, line as u32))
525+
}
526+
527+
/// This is the entry point of unwinding for panic!() and assert!().
528+
#[inline(never)] #[cold] // avoid code bloat at the call sites as much as possible
529+
#[cfg(not(stage0))]
530+
pub fn begin_unwind<M: Any + Send>(msg: M, file_line: &(&'static str, u32)) -> ! {
531+
// Note that this should be the only allocation performed in this code path.
532+
// Currently this means that panic!() on OOM will invoke this code path,
533+
// but then again we're not really ready for panic on OOM anyway. If
534+
// we do start doing this, then we should propagate this allocation to
535+
// be performed in the parent of this thread instead of the thread that's
536+
// panicking.
537+
521538
// see below for why we do the `Any` coercion here.
522539
begin_unwind_inner(Box::new(msg), file_line)
523540
}
@@ -533,7 +550,7 @@ pub fn begin_unwind<M: Any + Send>(msg: M, file_line: &(&'static str, usize)) ->
533550
/// }` from ~1900/3700 (-O/no opts) to 180/590.
534551
#[inline(never)] #[cold] // this is the slow path, please never inline this
535552
fn begin_unwind_inner(msg: Box<Any + Send>,
536-
file_line: &(&'static str, usize)) -> ! {
553+
file_line: &(&'static str, u32)) -> ! {
537554
// Make sure the default failure handler is registered before we look at the
538555
// callbacks. We also use a raw sys-based mutex here instead of a
539556
// `std::sync` one as accessing TLS can cause weird recursive problems (and

src/libsyntax/ext/build.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -767,7 +767,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
767767
let loc = self.codemap().lookup_char_pos(span.lo);
768768
let expr_file = self.expr_str(span,
769769
token::intern_and_get_ident(&loc.file.name));
770-
let expr_line = self.expr_usize(span, loc.line);
770+
let expr_line = self.expr_u32(span, loc.line as u32);
771771
let expr_file_line_tuple = self.expr_tuple(span, vec!(expr_file, expr_line));
772772
let expr_file_line_ptr = self.expr_addr_of(span, expr_file_line_tuple);
773773
self.expr_call_global(

0 commit comments

Comments
 (0)