Skip to content

Commit 86b42ab

Browse files
camsteffenMark-Simulacrum
authored andcommitted
Use UnsafeArg in Arguments::new_v1
1 parent 2b6ed3b commit 86b42ab

File tree

7 files changed

+79
-37
lines changed

7 files changed

+79
-37
lines changed

compiler/rustc_builtin_macros/src/format.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -846,26 +846,28 @@ impl<'a, 'b> Context<'a, 'b> {
846846

847847
let args_slice = self.ecx.expr_addr_of(self.macsp, args_match);
848848

849-
// Now create the fmt::Arguments struct with all our locals we created.
850-
let (fn_name, fn_args) = if self.all_pieces_simple {
851-
("new_v1", vec![pieces, args_slice])
852-
} else {
853-
// Build up the static array which will store our precompiled
854-
// nonstandard placeholders, if there are any.
855-
let fmt = self.ecx.expr_vec_slice(self.macsp, self.pieces);
856-
849+
let unsafe_arg = {
857850
let path = self.ecx.std_path(&[sym::fmt, sym::UnsafeArg, sym::new]);
858851
let unsafe_arg = self.ecx.expr_call_global(self.macsp, path, Vec::new());
859-
let unsafe_expr = self.ecx.expr_block(P(ast::Block {
852+
self.ecx.expr_block(P(ast::Block {
860853
stmts: vec![self.ecx.stmt_expr(unsafe_arg)],
861854
id: ast::DUMMY_NODE_ID,
862855
rules: BlockCheckMode::Unsafe(UnsafeSource::CompilerGenerated),
863856
span: self.macsp,
864857
tokens: None,
865858
could_be_bare_literal: false,
866-
}));
859+
}))
860+
};
861+
862+
// Now create the fmt::Arguments struct with all our locals we created.
863+
let (fn_name, fn_args) = if self.all_pieces_simple {
864+
("new_v1", vec![pieces, args_slice, unsafe_arg])
865+
} else {
866+
// Build up the static array which will store our precompiled
867+
// nonstandard placeholders, if there are any.
868+
let fmt = self.ecx.expr_vec_slice(self.macsp, self.pieces);
867869

868-
("new_v1_formatted", vec![pieces, args_slice, fmt, unsafe_expr])
870+
("new_v1_formatted", vec![pieces, args_slice, fmt, unsafe_arg])
869871
};
870872

871873
let path = self.ecx.std_path(&[sym::fmt, sym::Arguments, Symbol::intern(fn_name)]);

library/core/src/fmt/mod.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ impl UnsafeArg {
280280
#[doc(hidden)]
281281
#[unstable(feature = "fmt_internals", reason = "internal to format_args!", issue = "none")]
282282
#[inline(always)]
283-
pub unsafe fn new() -> Self {
283+
pub const unsafe fn new() -> Self {
284284
Self
285285
}
286286
}
@@ -353,6 +353,22 @@ enum FlagV1 {
353353
impl<'a> Arguments<'a> {
354354
/// When using the format_args!() macro, this function is used to generate the
355355
/// Arguments structure.
356+
///
357+
/// An `UnsafeArg` is required because this function is unsafe unless
358+
/// `pieces` is at least as long as `args`.
359+
#[cfg(not(bootstrap))]
360+
#[doc(hidden)]
361+
#[inline]
362+
#[unstable(feature = "fmt_internals", reason = "internal to format_args!", issue = "none")]
363+
#[rustc_const_unstable(feature = "const_fmt_arguments_new", issue = "none")]
364+
pub const fn new_v1(
365+
pieces: &'a [&'static str],
366+
args: &'a [ArgumentV1<'a>],
367+
_unsafe_arg: UnsafeArg,
368+
) -> Arguments<'a> {
369+
Arguments { pieces, fmt: None, args }
370+
}
371+
#[cfg(bootstrap)]
356372
#[doc(hidden)]
357373
#[inline]
358374
#[unstable(feature = "fmt_internals", reason = "internal to format_args!", issue = "none")]

library/core/src/panicking.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,15 @@ pub fn panic(expr: &'static str) -> ! {
4747
// truncation and padding (even though none is used here). Using
4848
// Arguments::new_v1 may allow the compiler to omit Formatter::pad from the
4949
// output binary, saving up to a few kilobytes.
50-
panic_fmt(fmt::Arguments::new_v1(&[expr], &[]));
50+
panic_fmt(fmt::Arguments::new_v1(
51+
&[expr],
52+
&[],
53+
#[cfg(not(bootstrap))]
54+
// SAFETY: Arguments::new_v1 is safe with exactly one str and zero args
55+
unsafe {
56+
fmt::UnsafeArg::new()
57+
},
58+
));
5159
}
5260

5361
#[inline]

src/test/pretty/dollar-crate.pp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
::std::io::_print(::core::fmt::Arguments::new_v1(&["rust\n"],
1414
&match () {
1515
() => [],
16-
}));
16+
},
17+
unsafe {
18+
::core::fmt::UnsafeArg::new()
19+
}));
1720
};
1821
}

src/test/pretty/issue-4264.pp

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -34,27 +34,37 @@
3434
((::alloc::fmt::format as
3535
for<'r> fn(Arguments<'r>) -> String {format})(((::core::fmt::Arguments::new_v1
3636
as
37-
fn(&[&'static str], &[ArgumentV1]) -> Arguments {Arguments::new_v1})((&([("test"
38-
as
39-
&str)]
40-
as
41-
[&str; 1])
42-
as
43-
&[&str; 1]),
44-
(&(match (()
45-
as
46-
())
47-
{
48-
()
49-
=>
50-
([]
51-
as
52-
[ArgumentV1; 0]),
53-
}
54-
as
55-
[ArgumentV1; 0])
56-
as
57-
&[ArgumentV1; 0]))
37+
fn(&[&'static str], &[ArgumentV1], core::fmt::UnsafeArg) -> Arguments {Arguments::new_v1})((&([("test"
38+
as
39+
&str)]
40+
as
41+
[&str; 1])
42+
as
43+
&[&str; 1]),
44+
(&(match (()
45+
as
46+
())
47+
{
48+
()
49+
=>
50+
([]
51+
as
52+
[ArgumentV1; 0]),
53+
}
54+
as
55+
[ArgumentV1; 0])
56+
as
57+
&[ArgumentV1; 0]),
58+
(unsafe
59+
{
60+
((::core::fmt::UnsafeArg::new
61+
as
62+
unsafe fn() -> core::fmt::UnsafeArg {core::fmt::UnsafeArg::new})()
63+
as
64+
core::fmt::UnsafeArg)
65+
}
66+
as
67+
core::fmt::UnsafeArg))
5868
as
5969
Arguments))
6070
as String);

src/test/ui/attributes/key-value-expansion.stderr

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ error: unexpected token: `{
2222
(arg0,) =>
2323
[::core::fmt::ArgumentV1::new(arg0,
2424
::core::fmt::Display::fmt)],
25-
}));
25+
},
26+
unsafe {
27+
::core::fmt::UnsafeArg::new()
28+
}));
2629
res
2730
}.as_str()`
2831
--> $DIR/key-value-expansion.rs:48:23

src/tools/clippy/clippy_utils/src/higher.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ impl FormatArgsExpn<'tcx> {
526526
if let ExprKind::Call(_, args) = expr.kind;
527527
if let Some((strs_ref, args, fmt_expr)) = match args {
528528
// Arguments::new_v1
529-
[strs_ref, args] => Some((strs_ref, args, None)),
529+
[strs_ref, args, _unsafe_arg] => Some((strs_ref, args, None)),
530530
// Arguments::new_v1_formatted
531531
[strs_ref, args, fmt_expr, _unsafe_arg] => Some((strs_ref, args, Some(fmt_expr))),
532532
_ => None,

0 commit comments

Comments
 (0)