Skip to content

Commit 1db96e3

Browse files
committed
Simplify format_args expansion
Avoids two nested `matche`es but widens the scope of the unsafe block.
1 parent 6492931 commit 1db96e3

File tree

4 files changed

+49
-63
lines changed

4 files changed

+49
-63
lines changed

compiler/rustc_builtin_macros/src/format.rs

+9-18
Original file line numberDiff line numberDiff line change
@@ -838,15 +838,12 @@ impl<'a, 'b> Context<'a, 'b> {
838838
//
839839
// But the nested match expression is proved to perform not as well
840840
// as series of let's; the first approach does.
841-
let args_match = {
842-
let pat = self.ecx.pat_tuple(self.macsp, pats);
843-
let arm = self.ecx.arm(self.macsp, pat, args_array);
844-
let head = self.ecx.expr(self.macsp, ast::ExprKind::Tup(heads));
845-
self.ecx.expr_match(self.macsp, head, vec![arm])
846-
};
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+
let result = self.ecx.expr_match(self.macsp, head, vec![arm]);
847845

848-
let ident = Ident::from_str_and_span("args", self.macsp);
849-
let args_slice = self.ecx.expr_ident(self.macsp, ident);
846+
let args_slice = self.ecx.expr_addr_of(self.macsp, result);
850847

851848
// Now create the fmt::Arguments struct with all our locals we created.
852849
let (fn_name, fn_args) = if self.all_pieces_simple {
@@ -860,20 +857,14 @@ impl<'a, 'b> Context<'a, 'b> {
860857
};
861858

862859
let path = self.ecx.std_path(&[sym::fmt, sym::Arguments, Symbol::intern(fn_name)]);
863-
let arguments = self.ecx.expr_call_global(self.macsp, path, fn_args);
864-
let body = self.ecx.expr_block(P(ast::Block {
865-
stmts: vec![self.ecx.stmt_expr(arguments)],
860+
let call = self.ecx.expr_call_global(self.macsp, path, fn_args);
861+
self.ecx.expr_block(P(ast::Block {
862+
stmts: vec![self.ecx.stmt_expr(call)],
866863
id: ast::DUMMY_NODE_ID,
867864
rules: BlockCheckMode::Unsafe(UnsafeSource::CompilerGenerated),
868865
span: self.macsp,
869866
tokens: None,
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])
867+
}))
877868
}
878869

879870
fn format_arg(

src/test/pretty/dollar-crate.pp

+5-5
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
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-
}
13+
::std::io::_print(unsafe {
14+
::core::fmt::Arguments::new_v1(&["rust\n"],
15+
&match () {
16+
() => [],
17+
})
1818
});
1919
};
2020
}

src/test/pretty/issue-4264.pp

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

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

+9-9
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ 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-
}
20+
::alloc::fmt::format(unsafe {
21+
::core::fmt::Arguments::new_v1(&[""],
22+
&match (&"u8",)
23+
{
24+
(arg0,)
25+
=>
26+
[::core::fmt::ArgumentV1::new(arg0,
27+
::core::fmt::Display::fmt)],
28+
})
2929
});
3030
res
3131
}.as_str()`

0 commit comments

Comments
 (0)