Skip to content

Borrow builder only once in debug derive #81760

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions compiler/rustc_builtin_macros/src/deriving/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,17 @@ fn show_substructure(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>
// tuple struct/"normal" variant
let fn_path_debug_tuple = cx.std_path(&[sym::fmt, sym::Formatter, sym::debug_tuple]);
let expr = cx.expr_call_global(span, fn_path_debug_tuple, vec![fmt, name]);
stmts.push(cx.stmt_let(span, true, builder, expr));
let expr = make_mut_borrow(cx, span, expr);
stmts.push(cx.stmt_let(span, false, builder, expr));

for field in fields {
// Use double indirection to make sure this works for unsized types
let field = cx.expr_addr_of(field.span, field.self_.clone());
let field = cx.expr_addr_of(field.span, field);

let fn_path_field = cx.std_path(&[sym::fmt, sym::DebugTuple, sym::field]);
let builder_recv = make_mut_borrow(cx, span, builder_expr.clone());
let expr = cx.expr_call_global(span, fn_path_field, vec![builder_recv, field]);
let expr =
cx.expr_call_global(span, fn_path_field, vec![builder_expr.clone(), field]);

// Use `let _ = expr;` to avoid triggering the
// unused_results lint.
Expand All @@ -99,7 +100,8 @@ fn show_substructure(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>
// normal struct/struct variant
let fn_path_debug_struct = cx.std_path(&[sym::fmt, sym::Formatter, sym::debug_struct]);
let expr = cx.expr_call_global(span, fn_path_debug_struct, vec![fmt, name]);
stmts.push(cx.stmt_let(DUMMY_SP, true, builder, expr));
let expr = make_mut_borrow(cx, span, expr);
stmts.push(cx.stmt_let(DUMMY_SP, false, builder, expr));

for field in fields {
let name = cx.expr_lit(
Expand All @@ -111,17 +113,18 @@ fn show_substructure(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>
let fn_path_field = cx.std_path(&[sym::fmt, sym::DebugStruct, sym::field]);
let field = cx.expr_addr_of(field.span, field.self_.clone());
let field = cx.expr_addr_of(field.span, field);
let builder_recv = make_mut_borrow(cx, span, builder_expr.clone());
let expr =
cx.expr_call_global(span, fn_path_field, vec![builder_recv, name, field]);
let expr = cx.expr_call_global(
span,
fn_path_field,
vec![builder_expr.clone(), name, field],
);
stmts.push(stmt_let_underscore(cx, span, expr));
}
fn_path_finish = cx.std_path(&[sym::fmt, sym::DebugStruct, sym::finish]);
}
}

let builder_recv = make_mut_borrow(cx, span, builder_expr);
let expr = cx.expr_call_global(span, fn_path_finish, vec![builder_recv]);
let expr = cx.expr_call_global(span, fn_path_finish, vec![builder_expr]);

stmts.push(cx.stmt_expr(expr));
let block = cx.block(span, stmts);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,41 +73,38 @@
4:17-4:22: @0[2]: _3 = &amp;((*_1).0: usize)
4:17-4:22: @0[4]: _4 = &amp;((*_1).1: usize)
4:17-4:22: @0[6]: _5 = &amp;((*_1).2: usize)
4:17-4:22: @0[9]: _7 = &amp;mut (*_2)
4:17-4:22: @0[12]: _9 = const &quot;Version&quot;
4:17-4:22: @0[13]: _8 = &amp;(*_9)
4:17-4:22: @0.Call: _6 = std::fmt::Formatter::debug_struct(move _7, move _8) -&gt; [return: bb1, unwind: bb6]
4:17-4:22: @1[2]: FakeRead(ForLet, _6)
4:17-4:22: @1[7]: _12 = &amp;mut _6
4:17-4:22: @1[8]: _11 = &amp;mut (*_12)
4:17-4:22: @1[11]: _14 = const &quot;major&quot;
4:17-4:22: @1[12]: _13 = &amp;(*_14)
4:17-4:22: @1[17]: _18 = &amp;(*_3)
4:17-4:22: @1[18]: _17 = &amp;_18
4:17-4:22: @1[19]: _16 = &amp;(*_17)
4:17-4:22: @1[20]: _15 = move _16 as &amp;dyn std::fmt::Debug (Pointer(Unsize))
4:17-4:22: @1.Call: _10 = std::fmt::DebugStruct::field(move _11, move _13, move _15) -&gt; [return: bb2, unwind: bb6]
4:17-4:22: @2[11]: _21 = &amp;mut _6
4:17-4:22: @2[12]: _20 = &amp;mut (*_21)
4:17-4:22: @2[15]: _23 = const &quot;minor&quot;
4:17-4:22: @2[16]: _22 = &amp;(*_23)
4:17-4:22: @2[21]: _27 = &amp;(*_4)
4:17-4:22: @2[22]: _26 = &amp;_27
4:17-4:22: @2[23]: _25 = &amp;(*_26)
4:17-4:22: @2[24]: _24 = move _25 as &amp;dyn std::fmt::Debug (Pointer(Unsize))
4:17-4:22: @2.Call: _19 = std::fmt::DebugStruct::field(move _20, move _22, move _24) -&gt; [return: bb3, unwind: bb6]
4:17-4:22: @3[11]: _30 = &amp;mut _6
4:17-4:22: @3[12]: _29 = &amp;mut (*_30)
4:17-4:22: @3[15]: _32 = const &quot;patch&quot;
4:17-4:22: @3[16]: _31 = &amp;(*_32)
4:17-4:22: @3[21]: _36 = &amp;(*_5)
4:17-4:22: @3[22]: _35 = &amp;_36
4:17-4:22: @3[23]: _34 = &amp;(*_35)
4:17-4:22: @3[24]: _33 = move _34 as &amp;dyn std::fmt::Debug (Pointer(Unsize))
4:17-4:22: @3.Call: _28 = std::fmt::DebugStruct::field(move _29, move _31, move _33) -&gt; [return: bb4, unwind: bb6]
4:17-4:22: @4[10]: _38 = &amp;mut _6
4:17-4:22: @4[11]: _37 = &amp;mut (*_38)
4:17-4:22: @4.Call: _0 = std::fmt::DebugStruct::finish(move _37) -&gt; [return: bb5, unwind: bb6]
4:17-4:22: @0[10]: _8 = &amp;mut (*_2)
4:17-4:22: @0[13]: _10 = const &quot;Version&quot;
4:17-4:22: @0[14]: _9 = &amp;(*_10)
4:17-4:22: @0.Call: _7 = std::fmt::Formatter::debug_struct(move _8, move _9) -&gt; [return: bb1, unwind: bb6]
4:17-4:22: @1[2]: _6 = &amp;mut _7
4:17-4:22: @1[3]: FakeRead(ForLet, _6)
4:17-4:22: @1[7]: _12 = &amp;mut (*_6)
4:17-4:22: @1[10]: _14 = const &quot;major&quot;
4:17-4:22: @1[11]: _13 = &amp;(*_14)
4:17-4:22: @1[16]: _18 = &amp;(*_3)
4:17-4:22: @1[17]: _17 = &amp;_18
4:17-4:22: @1[18]: _16 = &amp;(*_17)
4:17-4:22: @1[19]: _15 = move _16 as &amp;dyn std::fmt::Debug (Pointer(Unsize))
4:17-4:22: @1.Call: _11 = std::fmt::DebugStruct::field(move _12, move _13, move _15) -&gt; [return: bb2, unwind: bb6]
4:17-4:22: @2[9]: _20 = &amp;mut (*_6)
4:17-4:22: @2[12]: _22 = const &quot;minor&quot;
4:17-4:22: @2[13]: _21 = &amp;(*_22)
4:17-4:22: @2[18]: _26 = &amp;(*_4)
4:17-4:22: @2[19]: _25 = &amp;_26
4:17-4:22: @2[20]: _24 = &amp;(*_25)
4:17-4:22: @2[21]: _23 = move _24 as &amp;dyn std::fmt::Debug (Pointer(Unsize))
4:17-4:22: @2.Call: _19 = std::fmt::DebugStruct::field(move _20, move _21, move _23) -&gt; [return: bb3, unwind: bb6]
4:17-4:22: @3[9]: _28 = &amp;mut (*_6)
4:17-4:22: @3[12]: _30 = const &quot;patch&quot;
4:17-4:22: @3[13]: _29 = &amp;(*_30)
4:17-4:22: @3[18]: _34 = &amp;(*_5)
4:17-4:22: @3[19]: _33 = &amp;_34
4:17-4:22: @3[20]: _32 = &amp;(*_33)
4:17-4:22: @3[21]: _31 = move _32 as &amp;dyn std::fmt::Debug (Pointer(Unsize))
4:17-4:22: @3.Call: _27 = std::fmt::DebugStruct::field(move _28, move _29, move _31) -&gt; [return: bb4, unwind: bb6]
4:17-4:22: @4[8]: _35 = &amp;mut (*_6)
4:17-4:22: @4.Call: _0 = std::fmt::DebugStruct::finish(move _35) -&gt; [return: bb5, unwind: bb6]
4:22-4:22: @5.Return: return"><span class="annotation">@0,1,2,3,4,5⦊</span>Debug<span class="annotation">⦉@0,1,2,3,4,5</span></span></span></span></div>
</body>
</html>