Auto merge of #89139 - camsteffen:write-perf, r=Mark-Simulacrum
Use ZST for fmt unsafety as suggested here - https://github.com/rust-lang/rust/pull/83302#issuecomment-923529151.
This commit is contained in:
commit
67365d64bc
9 changed files with 111 additions and 103 deletions
|
@ -845,8 +845,7 @@ impl<'a, 'b> Context<'a, 'b> {
|
||||||
self.ecx.expr_match(self.macsp, head, vec![arm])
|
self.ecx.expr_match(self.macsp, head, vec![arm])
|
||||||
};
|
};
|
||||||
|
|
||||||
let ident = Ident::from_str_and_span("args", self.macsp);
|
let args_slice = self.ecx.expr_addr_of(self.macsp, args_match);
|
||||||
let args_slice = self.ecx.expr_ident(self.macsp, ident);
|
|
||||||
|
|
||||||
// Now create the fmt::Arguments struct with all our locals we created.
|
// Now create the fmt::Arguments struct with all our locals we created.
|
||||||
let (fn_name, fn_args) = if self.all_pieces_simple {
|
let (fn_name, fn_args) = if self.all_pieces_simple {
|
||||||
|
@ -856,25 +855,22 @@ impl<'a, 'b> Context<'a, 'b> {
|
||||||
// nonstandard placeholders, if there are any.
|
// nonstandard placeholders, if there are any.
|
||||||
let fmt = self.ecx.expr_vec_slice(self.macsp, self.pieces);
|
let fmt = self.ecx.expr_vec_slice(self.macsp, self.pieces);
|
||||||
|
|
||||||
("new_v1_formatted", vec![pieces, args_slice, fmt])
|
let path = self.ecx.std_path(&[sym::fmt, sym::UnsafeArg, sym::new]);
|
||||||
|
let unsafe_arg = self.ecx.expr_call_global(self.macsp, path, Vec::new());
|
||||||
|
let unsafe_expr = self.ecx.expr_block(P(ast::Block {
|
||||||
|
stmts: vec![self.ecx.stmt_expr(unsafe_arg)],
|
||||||
|
id: ast::DUMMY_NODE_ID,
|
||||||
|
rules: BlockCheckMode::Unsafe(UnsafeSource::CompilerGenerated),
|
||||||
|
span: self.macsp,
|
||||||
|
tokens: None,
|
||||||
|
could_be_bare_literal: false,
|
||||||
|
}));
|
||||||
|
|
||||||
|
("new_v1_formatted", vec![pieces, args_slice, fmt, unsafe_expr])
|
||||||
};
|
};
|
||||||
|
|
||||||
let path = self.ecx.std_path(&[sym::fmt, sym::Arguments, Symbol::intern(fn_name)]);
|
let path = self.ecx.std_path(&[sym::fmt, sym::Arguments, Symbol::intern(fn_name)]);
|
||||||
let arguments = self.ecx.expr_call_global(self.macsp, path, fn_args);
|
self.ecx.expr_call_global(self.macsp, path, fn_args)
|
||||||
let body = self.ecx.expr_block(P(ast::Block {
|
|
||||||
stmts: vec![self.ecx.stmt_expr(arguments)],
|
|
||||||
id: ast::DUMMY_NODE_ID,
|
|
||||||
rules: BlockCheckMode::Unsafe(UnsafeSource::CompilerGenerated),
|
|
||||||
span: self.macsp,
|
|
||||||
tokens: None,
|
|
||||||
could_be_bare_literal: false,
|
|
||||||
}));
|
|
||||||
|
|
||||||
let ident = Ident::from_str_and_span("args", self.macsp);
|
|
||||||
let binding_mode = ast::BindingMode::ByRef(ast::Mutability::Not);
|
|
||||||
let pat = self.ecx.pat_ident_binding_mode(self.macsp, ident, binding_mode);
|
|
||||||
let arm = self.ecx.arm(self.macsp, pat, body);
|
|
||||||
self.ecx.expr_match(self.macsp, args_match, vec![arm])
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn format_arg(
|
fn format_arg(
|
||||||
|
|
|
@ -253,6 +253,7 @@ symbols! {
|
||||||
TyCtxt,
|
TyCtxt,
|
||||||
TyKind,
|
TyKind,
|
||||||
Unknown,
|
Unknown,
|
||||||
|
UnsafeArg,
|
||||||
Vec,
|
Vec,
|
||||||
Yield,
|
Yield,
|
||||||
_DECLS,
|
_DECLS,
|
||||||
|
|
|
@ -265,6 +265,26 @@ pub struct ArgumentV1<'a> {
|
||||||
formatter: fn(&Opaque, &mut Formatter<'_>) -> Result,
|
formatter: fn(&Opaque, &mut Formatter<'_>) -> Result,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// This struct represents the unsafety of constructing an `Arguments`.
|
||||||
|
/// It exists, rather than an unsafe function, in order to simplify the expansion
|
||||||
|
/// of `format_args!(..)` and reduce the scope of the `unsafe` block.
|
||||||
|
#[allow(missing_debug_implementations)]
|
||||||
|
#[doc(hidden)]
|
||||||
|
#[non_exhaustive]
|
||||||
|
#[unstable(feature = "fmt_internals", reason = "internal to format_args!", issue = "none")]
|
||||||
|
pub struct UnsafeArg;
|
||||||
|
|
||||||
|
impl UnsafeArg {
|
||||||
|
/// See documentation where `UnsafeArg` is required to know when it is safe to
|
||||||
|
/// create and use `UnsafeArg`.
|
||||||
|
#[doc(hidden)]
|
||||||
|
#[unstable(feature = "fmt_internals", reason = "internal to format_args!", issue = "none")]
|
||||||
|
#[inline(always)]
|
||||||
|
pub unsafe fn new() -> Self {
|
||||||
|
Self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// This guarantees a single stable value for the function pointer associated with
|
// This guarantees a single stable value for the function pointer associated with
|
||||||
// indices/counts in the formatting infrastructure.
|
// indices/counts in the formatting infrastructure.
|
||||||
//
|
//
|
||||||
|
@ -337,10 +357,7 @@ impl<'a> Arguments<'a> {
|
||||||
#[inline]
|
#[inline]
|
||||||
#[unstable(feature = "fmt_internals", reason = "internal to format_args!", issue = "none")]
|
#[unstable(feature = "fmt_internals", reason = "internal to format_args!", issue = "none")]
|
||||||
#[rustc_const_unstable(feature = "const_fmt_arguments_new", issue = "none")]
|
#[rustc_const_unstable(feature = "const_fmt_arguments_new", issue = "none")]
|
||||||
pub const unsafe fn new_v1(
|
pub const fn new_v1(pieces: &'a [&'static str], args: &'a [ArgumentV1<'a>]) -> Arguments<'a> {
|
||||||
pieces: &'a [&'static str],
|
|
||||||
args: &'a [ArgumentV1<'a>],
|
|
||||||
) -> Arguments<'a> {
|
|
||||||
if pieces.len() < args.len() || pieces.len() > args.len() + 1 {
|
if pieces.len() < args.len() || pieces.len() > args.len() + 1 {
|
||||||
panic!("invalid args");
|
panic!("invalid args");
|
||||||
}
|
}
|
||||||
|
@ -348,11 +365,29 @@ impl<'a> Arguments<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// This function is used to specify nonstandard formatting parameters.
|
/// This function is used to specify nonstandard formatting parameters.
|
||||||
/// The `pieces` array must be at least as long as `fmt` to construct
|
///
|
||||||
/// a valid Arguments structure. Also, any `Count` within `fmt` that is
|
/// An `UnsafeArg` is required because the following invariants must be held
|
||||||
/// `CountIsParam` or `CountIsNextParam` has to point to an argument
|
/// in order for this function to be safe:
|
||||||
/// created with `argumentusize`. However, failing to do so doesn't cause
|
/// 1. The `pieces` slice must be at least as long as `fmt`.
|
||||||
/// unsafety, but will ignore invalid .
|
/// 2. Every [`rt::v1::Argument::position`] value within `fmt` must be a
|
||||||
|
/// valid index of `args`.
|
||||||
|
/// 3. Every [`Count::Param`] within `fmt` must contain a valid index of
|
||||||
|
/// `args`.
|
||||||
|
#[cfg(not(bootstrap))]
|
||||||
|
#[doc(hidden)]
|
||||||
|
#[inline]
|
||||||
|
#[unstable(feature = "fmt_internals", reason = "internal to format_args!", issue = "none")]
|
||||||
|
#[rustc_const_unstable(feature = "const_fmt_arguments_new", issue = "none")]
|
||||||
|
pub const fn new_v1_formatted(
|
||||||
|
pieces: &'a [&'static str],
|
||||||
|
args: &'a [ArgumentV1<'a>],
|
||||||
|
fmt: &'a [rt::v1::Argument],
|
||||||
|
_unsafe_arg: UnsafeArg,
|
||||||
|
) -> Arguments<'a> {
|
||||||
|
Arguments { pieces, fmt: Some(fmt), args }
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(bootstrap)]
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
#[inline]
|
#[inline]
|
||||||
#[unstable(feature = "fmt_internals", reason = "internal to format_args!", issue = "none")]
|
#[unstable(feature = "fmt_internals", reason = "internal to format_args!", issue = "none")]
|
||||||
|
|
|
@ -47,10 +47,7 @@ pub fn panic(expr: &'static str) -> ! {
|
||||||
// truncation and padding (even though none is used here). Using
|
// truncation and padding (even though none is used here). Using
|
||||||
// Arguments::new_v1 may allow the compiler to omit Formatter::pad from the
|
// Arguments::new_v1 may allow the compiler to omit Formatter::pad from the
|
||||||
// output binary, saving up to a few kilobytes.
|
// output binary, saving up to a few kilobytes.
|
||||||
panic_fmt(
|
panic_fmt(fmt::Arguments::new_v1(&[expr], &[]));
|
||||||
// SAFETY: Arguments::new_v1 is safe with exactly one str and zero args
|
|
||||||
unsafe { fmt::Arguments::new_v1(&[expr], &[]) },
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
|
|
|
@ -10,11 +10,9 @@ extern crate std;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
{
|
{
|
||||||
::std::io::_print(match match () { () => [], } {
|
::std::io::_print(::core::fmt::Arguments::new_v1(&["rust\n"],
|
||||||
ref args => unsafe {
|
&match () {
|
||||||
::core::fmt::Arguments::new_v1(&["rust\n"],
|
() => [],
|
||||||
args)
|
}));
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,39 +32,29 @@ pub fn bar() ({
|
||||||
({
|
({
|
||||||
let res =
|
let res =
|
||||||
((::alloc::fmt::format as
|
((::alloc::fmt::format as
|
||||||
for<'r> fn(Arguments<'r>) -> String {format})((match (match (()
|
for<'r> fn(Arguments<'r>) -> String {format})(((::core::fmt::Arguments::new_v1
|
||||||
as
|
as
|
||||||
())
|
fn(&[&'static str], &[ArgumentV1]) -> Arguments {Arguments::new_v1})((&([("test"
|
||||||
{
|
as
|
||||||
()
|
&str)]
|
||||||
=>
|
as
|
||||||
([]
|
[&str; 1])
|
||||||
as
|
as
|
||||||
[ArgumentV1; 0]),
|
&[&str; 1]),
|
||||||
}
|
(&(match (()
|
||||||
as
|
as
|
||||||
[ArgumentV1; 0])
|
())
|
||||||
{
|
{
|
||||||
ref args
|
()
|
||||||
=>
|
=>
|
||||||
unsafe
|
([]
|
||||||
{
|
as
|
||||||
((::core::fmt::Arguments::new_v1
|
[ArgumentV1; 0]),
|
||||||
as
|
}
|
||||||
unsafe fn(&[&'static str], &[ArgumentV1]) -> Arguments {Arguments::new_v1})((&([("test"
|
as
|
||||||
as
|
[ArgumentV1; 0])
|
||||||
&str)]
|
as
|
||||||
as
|
&[ArgumentV1; 0]))
|
||||||
[&str; 1])
|
|
||||||
as
|
|
||||||
&[&str; 1]),
|
|
||||||
(args
|
|
||||||
as
|
|
||||||
&[ArgumentV1; 0]))
|
|
||||||
as
|
|
||||||
Arguments)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
as
|
as
|
||||||
Arguments))
|
Arguments))
|
||||||
as String);
|
as String);
|
||||||
|
|
|
@ -31,15 +31,15 @@
|
||||||
24| 1| println!("{:?}", Foo(1));
|
24| 1| println!("{:?}", Foo(1));
|
||||||
25| 1|
|
25| 1|
|
||||||
26| 1| assert_ne!(Foo(0), Foo(5), "{}", if is_true { "true message" } else { "false message" });
|
26| 1| assert_ne!(Foo(0), Foo(5), "{}", if is_true { "true message" } else { "false message" });
|
||||||
^0 ^0 ^0 ^0
|
^0 ^0 ^0
|
||||||
27| 1| assert_ne!(
|
27| 1| assert_ne!(
|
||||||
28| | Foo(0)
|
28| | Foo(0)
|
||||||
29| | ,
|
29| | ,
|
||||||
30| | Foo(5)
|
30| | Foo(5)
|
||||||
31| | ,
|
31| | ,
|
||||||
32| 0| "{}"
|
32| 0| "{}"
|
||||||
33| | ,
|
33| 0| ,
|
||||||
34| | if
|
34| 0| if
|
||||||
35| 0| is_true
|
35| 0| is_true
|
||||||
36| | {
|
36| | {
|
||||||
37| 0| "true message"
|
37| 0| "true message"
|
||||||
|
|
|
@ -17,16 +17,12 @@ LL | bug!();
|
||||||
|
|
||||||
error: unexpected token: `{
|
error: unexpected token: `{
|
||||||
let res =
|
let res =
|
||||||
::alloc::fmt::format(match match (&"u8",) {
|
::alloc::fmt::format(::core::fmt::Arguments::new_v1(&[""],
|
||||||
(arg0,) =>
|
&match (&"u8",) {
|
||||||
[::core::fmt::ArgumentV1::new(arg0,
|
(arg0,) =>
|
||||||
::core::fmt::Display::fmt)],
|
[::core::fmt::ArgumentV1::new(arg0,
|
||||||
} {
|
::core::fmt::Display::fmt)],
|
||||||
ref args => unsafe {
|
}));
|
||||||
::core::fmt::Arguments::new_v1(&[""],
|
|
||||||
args)
|
|
||||||
}
|
|
||||||
});
|
|
||||||
res
|
res
|
||||||
}.as_str()`
|
}.as_str()`
|
||||||
--> $DIR/key-value-expansion.rs:48:23
|
--> $DIR/key-value-expansion.rs:48:23
|
||||||
|
|
|
@ -523,28 +523,12 @@ impl FormatArgsExpn<'tcx> {
|
||||||
if let ExpnKind::Macro(_, name) = expr.span.ctxt().outer_expn_data().kind;
|
if let ExpnKind::Macro(_, name) = expr.span.ctxt().outer_expn_data().kind;
|
||||||
let name = name.as_str();
|
let name = name.as_str();
|
||||||
if name.ends_with("format_args") || name.ends_with("format_args_nl");
|
if name.ends_with("format_args") || name.ends_with("format_args_nl");
|
||||||
|
if let ExprKind::Call(_, args) = expr.kind;
|
||||||
if let ExprKind::Match(inner_match, [arm], _) = expr.kind;
|
if let Some((strs_ref, args, fmt_expr)) = match args {
|
||||||
|
|
||||||
// `match match`, if you will
|
|
||||||
if let ExprKind::Match(args, [inner_arm], _) = inner_match.kind;
|
|
||||||
if let ExprKind::Tup(value_args) = args.kind;
|
|
||||||
if let Some(value_args) = value_args
|
|
||||||
.iter()
|
|
||||||
.map(|e| match e.kind {
|
|
||||||
ExprKind::AddrOf(_, _, e) => Some(e),
|
|
||||||
_ => None,
|
|
||||||
})
|
|
||||||
.collect();
|
|
||||||
if let ExprKind::Array(args) = inner_arm.body.kind;
|
|
||||||
|
|
||||||
if let ExprKind::Block(Block { stmts: [], expr: Some(expr), .. }, _) = arm.body.kind;
|
|
||||||
if let ExprKind::Call(_, call_args) = expr.kind;
|
|
||||||
if let Some((strs_ref, fmt_expr)) = match call_args {
|
|
||||||
// Arguments::new_v1
|
// Arguments::new_v1
|
||||||
[strs_ref, _] => Some((strs_ref, None)),
|
[strs_ref, args] => Some((strs_ref, args, None)),
|
||||||
// Arguments::new_v1_formatted
|
// Arguments::new_v1_formatted
|
||||||
[strs_ref, _, fmt_expr] => Some((strs_ref, Some(fmt_expr))),
|
[strs_ref, args, fmt_expr, _unsafe_arg] => Some((strs_ref, args, Some(fmt_expr))),
|
||||||
_ => None,
|
_ => None,
|
||||||
};
|
};
|
||||||
if let ExprKind::AddrOf(BorrowKind::Ref, _, strs_arr) = strs_ref.kind;
|
if let ExprKind::AddrOf(BorrowKind::Ref, _, strs_arr) = strs_ref.kind;
|
||||||
|
@ -560,6 +544,17 @@ impl FormatArgsExpn<'tcx> {
|
||||||
None
|
None
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
if let ExprKind::AddrOf(BorrowKind::Ref, _, args) = args.kind;
|
||||||
|
if let ExprKind::Match(args, [arm], _) = args.kind;
|
||||||
|
if let ExprKind::Tup(value_args) = args.kind;
|
||||||
|
if let Some(value_args) = value_args
|
||||||
|
.iter()
|
||||||
|
.map(|e| match e.kind {
|
||||||
|
ExprKind::AddrOf(_, _, e) => Some(e),
|
||||||
|
_ => None,
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
if let ExprKind::Array(args) = arm.body.kind;
|
||||||
then {
|
then {
|
||||||
Some(FormatArgsExpn {
|
Some(FormatArgsExpn {
|
||||||
format_string_span: strs_ref.span,
|
format_string_span: strs_ref.span,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue