Auto merge of #44312 - eddyb:static-by-any-other-name, r=alexcrichton

Use rvalue promotion to 'static instead of static items.

Fixes #44240. Among other things, in crates that do a lot of formatting, this could reduce the number of items, although I haven't measured the performance benefits. If there's a codegen slowdown, that should IMO be solved by caching the output of miri, *not* by using `static`.

r? @alexcrichton
This commit is contained in:
bors 2017-09-10 10:09:47 +00:00
commit 23aaeb573b
6 changed files with 16 additions and 79 deletions

View file

@ -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!()))
});
}

View file

@ -29,7 +29,6 @@ use syntax::ast;
use syntax_pos::Span;
use std::fmt;
use std::u32;
pub struct ElaborateDrops;

View file

@ -545,7 +545,7 @@ impl fmt::Debug for CStr {
#[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()) }
}
}

View file

@ -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!()))
});
}

View file

@ -19,7 +19,7 @@ use syntax::ext::base;
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 @@ impl<'a, 'b> Context<'a, 'b> {
}
}
fn static_array(ecx: &mut ExtCtxt,
name: &str,
piece_ty: P<ast::Ty>,
pieces: Vec<P<ast::Expr>>)
-> P<ast::Expr> {
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<ast::Expr> {
@ -537,12 +511,7 @@ impl<'a, 'b> Context<'a, 'b> {
// 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 @@ impl<'a, 'b> Context<'a, 'b> {
} 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])
};

View file

@ -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
())