Skip to content

Commit 847637d

Browse files
committed
Remove core::fmt::rt::UnsafeArg.
We can now just make new_v1_formatted an unsafe fn.
1 parent 7417473 commit 847637d

File tree

5 files changed

+37
-31
lines changed

5 files changed

+37
-31
lines changed

compiler/rustc_ast_lowering/src/format.rs

+20-22
Original file line numberDiff line numberDiff line change
@@ -536,34 +536,32 @@ fn expand_format_args<'hir>(
536536

537537
let call = if let Some(format_options) = format_options {
538538
// Generate:
539-
// <core::fmt::Arguments>::new_v1_formatted(
540-
// lit_pieces,
541-
// args,
542-
// format_options,
543-
// unsafe { ::core::fmt::UnsafeArg::new() }
544-
// )
539+
// unsafe {
540+
// <core::fmt::Arguments>::new_v1_formatted(
541+
// lit_pieces,
542+
// args,
543+
// format_options,
544+
// )
545+
// }
545546
let new_v1_formatted = ctx.arena.alloc(ctx.expr_lang_item_type_relative(
546547
macsp,
547548
hir::LangItem::FormatArguments,
548549
sym::new_v1_formatted,
549550
));
550-
let unsafe_arg_new = ctx.arena.alloc(ctx.expr_lang_item_type_relative(
551-
macsp,
552-
hir::LangItem::FormatUnsafeArg,
553-
sym::new,
554-
));
555-
let unsafe_arg_new_call = ctx.expr_call(macsp, unsafe_arg_new, &[]);
551+
let args = ctx.arena.alloc_from_iter([lit_pieces, args, format_options]);
552+
let call = ctx.expr_call(macsp, new_v1_formatted, args);
556553
let hir_id = ctx.next_id();
557-
let unsafe_arg = ctx.expr_block(ctx.arena.alloc(hir::Block {
558-
stmts: &[],
559-
expr: Some(unsafe_arg_new_call),
560-
hir_id,
561-
rules: hir::BlockCheckMode::UnsafeBlock(hir::UnsafeSource::CompilerGenerated),
562-
span: macsp,
563-
targeted_by_break: false,
564-
}));
565-
let args = ctx.arena.alloc_from_iter([lit_pieces, args, format_options, unsafe_arg]);
566-
hir::ExprKind::Call(new_v1_formatted, args)
554+
hir::ExprKind::Block(
555+
ctx.arena.alloc(hir::Block {
556+
stmts: &[],
557+
expr: Some(call),
558+
hir_id,
559+
rules: hir::BlockCheckMode::UnsafeBlock(hir::UnsafeSource::CompilerGenerated),
560+
span: macsp,
561+
targeted_by_break: false,
562+
}),
563+
None,
564+
)
567565
} else {
568566
// Generate:
569567
// <core::fmt::Arguments>::new_v1(

compiler/rustc_hir/src/lang_items.rs

-1
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,6 @@ language_item_table! {
326326
FormatArguments, sym::format_arguments, format_arguments, Target::Struct, GenericRequirement::None;
327327
FormatCount, sym::format_count, format_count, Target::Enum, GenericRequirement::None;
328328
FormatPlaceholder, sym::format_placeholder, format_placeholder, Target::Struct, GenericRequirement::None;
329-
FormatUnsafeArg, sym::format_unsafe_arg, format_unsafe_arg, Target::Struct, GenericRequirement::None;
330329

331330
ExchangeMalloc, sym::exchange_malloc, exchange_malloc_fn, Target::Fn, GenericRequirement::None;
332331
DropInPlace, sym::drop_in_place, drop_in_place_fn, Target::Fn, GenericRequirement::Minimum(1);

compiler/rustc_span/src/symbol.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1026,7 +1026,6 @@ symbols! {
10261026
format_count,
10271027
format_macro,
10281028
format_placeholder,
1029-
format_unsafe_arg,
10301029
freeze,
10311030
freeze_impls,
10321031
freg,

library/core/src/fmt/mod.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -645,12 +645,23 @@ impl<'a> Arguments<'a> {
645645

646646
/// Specifies nonstandard formatting parameters.
647647
///
648-
/// An `rt::UnsafeArg` is required because the following invariants must be held
649-
/// in order for this function to be safe:
648+
/// Safety:
650649
/// 1. The `pieces` slice must be at least as long as `fmt`.
651650
/// 2. Every `rt::Placeholder::position` value within `fmt` must be a valid index of `args`.
652651
/// 3. Every `rt::Count::Param` within `fmt` must contain a valid index of `args`.
653652
#[inline]
653+
#[cfg(not(bootstrap))]
654+
pub const unsafe fn new_v1_formatted(
655+
pieces: &'a [&'static str],
656+
args: &'a [rt::Argument<'a>],
657+
fmt: &'a [rt::Placeholder],
658+
) -> Arguments<'a> {
659+
Arguments { pieces, fmt: Some(fmt), args }
660+
}
661+
662+
/// Bootstrap only.
663+
#[cfg(bootstrap)]
664+
#[inline]
654665
pub const fn new_v1_formatted(
655666
pieces: &'a [&'static str],
656667
args: &'a [rt::Argument<'a>],

library/core/src/fmt/rt.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -197,17 +197,16 @@ impl Argument<'_> {
197197
}
198198
}
199199

200-
/// This struct represents the unsafety of constructing an `Arguments`.
201-
/// It exists, rather than an unsafe function, in order to simplify the expansion
202-
/// of `format_args!(..)` and reduce the scope of the `unsafe` block.
200+
/// Bootstrap only.
201+
#[cfg(bootstrap)]
203202
#[lang = "format_unsafe_arg"]
204203
pub struct UnsafeArg {
205204
_private: (),
206205
}
207206

207+
#[cfg(bootstrap)]
208208
impl UnsafeArg {
209-
/// See documentation where `UnsafeArg` is required to know when it is safe to
210-
/// create and use `UnsafeArg`.
209+
/// Bootstrap only.
211210
#[inline]
212211
pub const unsafe fn new() -> Self {
213212
Self { _private: () }

0 commit comments

Comments
 (0)