From 10f66bd6e494c5b329f6b21b31e388e0ed3d69c1 Mon Sep 17 00:00:00 2001 From: Eduard-Mihai Burtescu Date: Mon, 4 Sep 2017 13:23:49 +0300 Subject: [PATCH] Use rvalue promotion to 'static instead of static items. --- src/libcore/macros.rs | 13 ++----- src/librustc_mir/transform/elaborate_drops.rs | 1 - src/libstd/ffi/c_str.rs | 2 +- src/libstd/macros.rs | 18 ++------- src/libsyntax_ext/format.rs | 39 ++----------------- src/test/pretty/issue-4264.pp | 22 +++-------- 6 files changed, 16 insertions(+), 79 deletions(-) diff --git a/src/libcore/macros.rs b/src/libcore/macros.rs index 83f8f9988f4..d64c984ea7d 100644 --- a/src/libcore/macros.rs +++ b/src/libcore/macros.rs @@ -17,18 +17,11 @@ macro_rules! panic { panic!("explicit panic") ); ($msg:expr) => ({ - static _MSG_FILE_LINE_COL: (&'static str, &'static str, u32, u32) = - ($msg, file!(), line!(), __rust_unstable_column!()); - $crate::panicking::panic(&_MSG_FILE_LINE_COL) + $crate::panicking::panic(&($msg, file!(), line!(), __rust_unstable_column!())) }); ($fmt:expr, $($arg:tt)*) => ({ - // The leading _'s are to avoid dead code warnings if this is - // used inside a dead function. Just `#[allow(dead_code)]` is - // insufficient, since the user may have - // `#[forbid(dead_code)]` and which cannot be overridden. - static _MSG_FILE_LINE_COL: (&'static str, u32, u32) = - (file!(), line!(), __rust_unstable_column!()); - $crate::panicking::panic_fmt(format_args!($fmt, $($arg)*), &_MSG_FILE_LINE_COL) + $crate::panicking::panic_fmt(format_args!($fmt, $($arg)*), + &(file!(), line!(), __rust_unstable_column!())) }); } diff --git a/src/librustc_mir/transform/elaborate_drops.rs b/src/librustc_mir/transform/elaborate_drops.rs index 417083c4ff8..d6477f2babf 100644 --- a/src/librustc_mir/transform/elaborate_drops.rs +++ b/src/librustc_mir/transform/elaborate_drops.rs @@ -29,7 +29,6 @@ use syntax_pos::Span; use std::fmt; -use std::u32; pub struct ElaborateDrops; diff --git a/src/libstd/ffi/c_str.rs b/src/libstd/ffi/c_str.rs index 7392a153e3b..7992aefcb42 100644 --- a/src/libstd/ffi/c_str.rs +++ b/src/libstd/ffi/c_str.rs @@ -545,7 +545,7 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { #[stable(feature = "cstr_default", since = "1.10.0")] impl<'a> Default for &'a CStr { fn default() -> &'a CStr { - static SLICE: &'static [c_char] = &[0]; + const SLICE: &'static [c_char] = &[0]; unsafe { CStr::from_ptr(SLICE.as_ptr()) } } } diff --git a/src/libstd/macros.rs b/src/libstd/macros.rs index 72d561fae3b..8089671f309 100644 --- a/src/libstd/macros.rs +++ b/src/libstd/macros.rs @@ -66,23 +66,11 @@ macro_rules! panic { panic!("explicit panic") }); ($msg:expr) => ({ - $crate::rt::begin_panic($msg, { - // static requires less code at runtime, more constant data - static _FILE_LINE_COL: (&'static str, u32, u32) = (file!(), line!(), - __rust_unstable_column!()); - &_FILE_LINE_COL - }) + $crate::rt::begin_panic($msg, &(file!(), line!(), __rust_unstable_column!())) }); ($fmt:expr, $($arg:tt)+) => ({ - $crate::rt::begin_panic_fmt(&format_args!($fmt, $($arg)+), { - // The leading _'s are to avoid dead code warnings if this is - // used inside a dead function. Just `#[allow(dead_code)]` is - // insufficient, since the user may have - // `#[forbid(dead_code)]` and which cannot be overridden. - static _FILE_LINE_COL: (&'static str, u32, u32) = (file!(), line!(), - __rust_unstable_column!()); - &_FILE_LINE_COL - }) + $crate::rt::begin_panic_fmt(&format_args!($fmt, $($arg)+), + &(file!(), line!(), __rust_unstable_column!())) }); } diff --git a/src/libsyntax_ext/format.rs b/src/libsyntax_ext/format.rs index 3e20bc481bd..63c533df198 100644 --- a/src/libsyntax_ext/format.rs +++ b/src/libsyntax_ext/format.rs @@ -19,7 +19,7 @@ use syntax::ext::build::AstBuilder; use syntax::parse::token; use syntax::ptr::P; -use syntax::symbol::{Symbol, keywords}; +use syntax::symbol::Symbol; use syntax_pos::{Span, DUMMY_SP}; use syntax::tokenstream; @@ -501,32 +501,6 @@ fn trans_piece(&mut self, } } - fn static_array(ecx: &mut ExtCtxt, - name: &str, - piece_ty: P, - pieces: Vec>) - -> P { - let sp = piece_ty.span; - let ty = ecx.ty_rptr(sp, - ecx.ty(sp, ast::TyKind::Slice(piece_ty)), - Some(ecx.lifetime(sp, keywords::StaticLifetime.ident())), - ast::Mutability::Immutable); - let slice = ecx.expr_vec_slice(sp, pieces); - // static instead of const to speed up codegen by not requiring this to be inlined - let st = ast::ItemKind::Static(ty, ast::Mutability::Immutable, slice); - - let name = ecx.ident_of(name); - let item = ecx.item(sp, name, vec![], st); - let stmt = ast::Stmt { - id: ast::DUMMY_NODE_ID, - node: ast::StmtKind::Item(item), - span: sp, - }; - - // Wrap the declaration in a block so that it forms a single expression. - ecx.expr_block(ecx.block(sp, vec![stmt, ecx.stmt_expr(ecx.expr_ident(sp, name))])) - } - /// Actually builds the expression which the format_args! block will be /// expanded to fn into_expr(self) -> P { @@ -537,12 +511,7 @@ fn into_expr(self) -> P { // First, build up the static array which will become our precompiled // format "string" - let static_lifetime = self.ecx.lifetime(self.fmtsp, keywords::StaticLifetime.ident()); - let piece_ty = self.ecx.ty_rptr(self.fmtsp, - self.ecx.ty_ident(self.fmtsp, self.ecx.ident_of("str")), - Some(static_lifetime), - ast::Mutability::Immutable); - let pieces = Context::static_array(self.ecx, "__STATIC_FMTSTR", piece_ty, self.str_pieces); + let pieces = self.ecx.expr_vec_slice(self.fmtsp, self.str_pieces); // Before consuming the expressions, we have to remember spans for // count arguments as they are now generated separate from other @@ -623,9 +592,7 @@ fn into_expr(self) -> P { } else { // Build up the static array which will store our precompiled // nonstandard placeholders, if there are any. - let piece_ty = self.ecx - .ty_path(self.ecx.path_global(self.macsp, Context::rtpath(self.ecx, "Argument"))); - let fmt = Context::static_array(self.ecx, "__STATIC_FMTARGS", piece_ty, self.pieces); + let fmt = self.ecx.expr_vec_slice(self.macsp, self.pieces); ("new_v1_formatted", vec![pieces, args_slice, fmt]) }; diff --git a/src/test/pretty/issue-4264.pp b/src/test/pretty/issue-4264.pp index 6c74e7758c4..14a499644df 100644 --- a/src/test/pretty/issue-4264.pp +++ b/src/test/pretty/issue-4264.pp @@ -42,23 +42,13 @@ pub fn bar() ({ ((::fmt::format as fn(std::fmt::Arguments<'_>) -> std::string::String {std::fmt::format})(((<::std::fmt::Arguments>::new_v1 as - fn(&[&str], &[std::fmt::ArgumentV1<'_>]) -> std::fmt::Arguments<'_> {std::fmt::Arguments<'_>::new_v1})(({ - static __STATIC_FMTSTR: - &'static [&'static str] - = - (&([("test" - as - &'static str)] - as - [&'static str; 1]) - as - &'static [&'static str; 1]); - (__STATIC_FMTSTR - as - &'static [&'static str]) - } + fn(&[&str], &[std::fmt::ArgumentV1<'_>]) -> std::fmt::Arguments<'_> {std::fmt::Arguments<'_>::new_v1})((&([("test" + as + &'static str)] + as + [&str; 1]) as - &[&str]), + &[&str; 1]), (&(match (() as ())