Skip to content

Commit 669f6ec

Browse files
committed
Revert "Make Arguments constructors unsafe"
This reverts commit 975bc18.
1 parent c10e80a commit 669f6ec

File tree

8 files changed

+43
-87
lines changed

8 files changed

+43
-87
lines changed

compiler/rustc_builtin_macros/src/format.rs

+7-24
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use Position::*;
33

44
use rustc_ast as ast;
55
use rustc_ast::ptr::P;
6+
use rustc_ast::token;
67
use rustc_ast::tokenstream::TokenStream;
7-
use rustc_ast::{token, BlockCheckMode, UnsafeSource};
88
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
99
use rustc_errors::{pluralize, Applicability, DiagnosticBuilder};
1010
use rustc_expand::base::{self, *};
@@ -837,15 +837,12 @@ impl<'a, 'b> Context<'a, 'b> {
837837
//
838838
// But the nested match expression is proved to perform not as well
839839
// as series of let's; the first approach does.
840-
let args_match = {
841-
let pat = self.ecx.pat_tuple(self.macsp, pats);
842-
let arm = self.ecx.arm(self.macsp, pat, args_array);
843-
let head = self.ecx.expr(self.macsp, ast::ExprKind::Tup(heads));
844-
self.ecx.expr_match(self.macsp, head, vec![arm])
845-
};
840+
let pat = self.ecx.pat_tuple(self.macsp, pats);
841+
let arm = self.ecx.arm(self.macsp, pat, args_array);
842+
let head = self.ecx.expr(self.macsp, ast::ExprKind::Tup(heads));
843+
let result = self.ecx.expr_match(self.macsp, head, vec![arm]);
846844

847-
let ident = Ident::from_str_and_span("args", self.macsp);
848-
let args_slice = self.ecx.expr_ident(self.macsp, ident);
845+
let args_slice = self.ecx.expr_addr_of(self.macsp, result);
849846

850847
// Now create the fmt::Arguments struct with all our locals we created.
851848
let (fn_name, fn_args) = if self.all_pieces_simple {
@@ -859,21 +856,7 @@ impl<'a, 'b> Context<'a, 'b> {
859856
};
860857

861858
let path = self.ecx.std_path(&[sym::fmt, sym::Arguments, Symbol::intern(fn_name)]);
862-
let arguments = self.ecx.expr_call_global(self.macsp, path, fn_args);
863-
let body = self.ecx.expr_block(P(ast::Block {
864-
stmts: vec![self.ecx.stmt_expr(arguments)],
865-
id: ast::DUMMY_NODE_ID,
866-
rules: BlockCheckMode::Unsafe(UnsafeSource::CompilerGenerated),
867-
span: self.macsp,
868-
tokens: None,
869-
could_be_bare_literal: false,
870-
}));
871-
872-
let ident = Ident::from_str_and_span("args", self.macsp);
873-
let binding_mode = ast::BindingMode::ByRef(ast::Mutability::Not);
874-
let pat = self.ecx.pat_ident_binding_mode(self.macsp, ident, binding_mode);
875-
let arm = self.ecx.arm(self.macsp, pat, body);
876-
self.ecx.expr_match(self.macsp, args_match, vec![arm])
859+
self.ecx.expr_call_global(self.macsp, path, fn_args)
877860
}
878861

879862
fn format_arg(

library/core/src/fmt/mod.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -338,13 +338,7 @@ impl<'a> Arguments<'a> {
338338
#[inline]
339339
#[unstable(feature = "fmt_internals", reason = "internal to format_args!", issue = "none")]
340340
#[rustc_const_unstable(feature = "const_fmt_arguments_new", issue = "none")]
341-
pub const unsafe fn new_v1(
342-
pieces: &'a [&'static str],
343-
args: &'a [ArgumentV1<'a>],
344-
) -> Arguments<'a> {
345-
if pieces.len() < args.len() || pieces.len() > args.len() + 1 {
346-
panic!("invalid args");
347-
}
341+
pub const fn new_v1(pieces: &'a [&'static str], args: &'a [ArgumentV1<'a>]) -> Arguments<'a> {
348342
Arguments { pieces, fmt: None, args }
349343
}
350344

@@ -358,7 +352,7 @@ impl<'a> Arguments<'a> {
358352
#[inline]
359353
#[unstable(feature = "fmt_internals", reason = "internal to format_args!", issue = "none")]
360354
#[rustc_const_unstable(feature = "const_fmt_arguments_new", issue = "none")]
361-
pub const unsafe fn new_v1_formatted(
355+
pub const fn new_v1_formatted(
362356
pieces: &'a [&'static str],
363357
args: &'a [ArgumentV1<'a>],
364358
fmt: &'a [rt::v1::Argument],

library/core/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@
113113
//
114114
// Language features:
115115
#![feature(abi_unadjusted)]
116-
#![feature(allow_internal_unsafe)]
117116
#![feature(allow_internal_unstable)]
118117
#![feature(asm)]
119118
#![feature(associated_type_bounds)]

library/core/src/macros/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -828,7 +828,6 @@ pub(crate) mod builtin {
828828
/// assert_eq!(s, format!("hello {}", "world"));
829829
/// ```
830830
#[stable(feature = "rust1", since = "1.0.0")]
831-
#[allow_internal_unsafe]
832831
#[allow_internal_unstable(fmt_internals)]
833832
#[rustc_builtin_macro]
834833
#[macro_export]

library/core/src/panicking.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,7 @@ 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(
51-
// SAFETY: Arguments::new_v1 is safe with exactly one str and zero args
52-
unsafe { fmt::Arguments::new_v1(&[expr], &[]) },
53-
);
50+
panic_fmt(fmt::Arguments::new_v1(&[expr], &[]));
5451
}
5552

5653
#[inline]

src/test/pretty/dollar-crate.pp

+4-6
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,9 @@
1010

1111
fn main() {
1212
{
13-
::std::io::_print(match match () { () => [], } {
14-
ref args => unsafe {
15-
::core::fmt::Arguments::new_v1(&["rust\n"],
16-
args)
17-
}
18-
});
13+
::std::io::_print(::core::fmt::Arguments::new_v1(&["rust\n"],
14+
&match () {
15+
() => [],
16+
}));
1917
};
2018
}

src/test/pretty/issue-4264.pp

+23-33
Original file line numberDiff line numberDiff line change
@@ -32,39 +32,29 @@
3232
({
3333
let res =
3434
((::alloc::fmt::format as
35-
for<'r> fn(Arguments<'r>) -> String {format})((match (match (()
36-
as
37-
())
38-
{
39-
()
40-
=>
41-
([]
42-
as
43-
[ArgumentV1; 0]),
44-
}
45-
as
46-
[ArgumentV1; 0])
47-
{
48-
ref args
49-
=>
50-
unsafe
51-
{
52-
((::core::fmt::Arguments::new_v1
53-
as
54-
unsafe fn(&[&'static str], &[ArgumentV1]) -> Arguments {Arguments::new_v1})((&([("test"
55-
as
56-
&str)]
57-
as
58-
[&str; 1])
59-
as
60-
&[&str; 1]),
61-
(args
62-
as
63-
&[ArgumentV1; 0]))
64-
as
65-
Arguments)
66-
}
67-
}
35+
for<'r> fn(Arguments<'r>) -> String {format})(((::core::fmt::Arguments::new_v1
36+
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]))
6858
as
6959
Arguments))
7060
as String);

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

+6-10
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,12 @@ LL | bug!();
1717

1818
error: unexpected token: `{
1919
let res =
20-
::alloc::fmt::format(match match (&"u8",) {
21-
(arg0,) =>
22-
[::core::fmt::ArgumentV1::new(arg0,
23-
::core::fmt::Display::fmt)],
24-
} {
25-
ref args => unsafe {
26-
::core::fmt::Arguments::new_v1(&[""],
27-
args)
28-
}
29-
});
20+
::alloc::fmt::format(::core::fmt::Arguments::new_v1(&[""],
21+
&match (&"u8",) {
22+
(arg0,) =>
23+
[::core::fmt::ArgumentV1::new(arg0,
24+
::core::fmt::Display::fmt)],
25+
}));
3026
res
3127
}.as_str()`
3228
--> $DIR/key-value-expansion.rs:48:23

0 commit comments

Comments
 (0)