Skip to content

Commit e9920ef

Browse files
committed
Auto merge of #81760 - tmiasko:debug-borrow, r=pnkfelix
Borrow builder only once in debug derive
2 parents 26e5bcd + 1cf9505 commit e9920ef

File tree

2 files changed

+44
-44
lines changed

2 files changed

+44
-44
lines changed

compiler/rustc_builtin_macros/src/deriving/debug.rs

+12-9
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,17 @@ fn show_substructure(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>
7777
// tuple struct/"normal" variant
7878
let fn_path_debug_tuple = cx.std_path(&[sym::fmt, sym::Formatter, sym::debug_tuple]);
7979
let expr = cx.expr_call_global(span, fn_path_debug_tuple, vec![fmt, name]);
80-
stmts.push(cx.stmt_let(span, true, builder, expr));
80+
let expr = make_mut_borrow(cx, span, expr);
81+
stmts.push(cx.stmt_let(span, false, builder, expr));
8182

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

8788
let fn_path_field = cx.std_path(&[sym::fmt, sym::DebugTuple, sym::field]);
88-
let builder_recv = make_mut_borrow(cx, span, builder_expr.clone());
89-
let expr = cx.expr_call_global(span, fn_path_field, vec![builder_recv, field]);
89+
let expr =
90+
cx.expr_call_global(span, fn_path_field, vec![builder_expr.clone(), field]);
9091

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

104106
for field in fields {
105107
let name = cx.expr_lit(
@@ -111,17 +113,18 @@ fn show_substructure(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>
111113
let fn_path_field = cx.std_path(&[sym::fmt, sym::DebugStruct, sym::field]);
112114
let field = cx.expr_addr_of(field.span, field.self_.clone());
113115
let field = cx.expr_addr_of(field.span, field);
114-
let builder_recv = make_mut_borrow(cx, span, builder_expr.clone());
115-
let expr =
116-
cx.expr_call_global(span, fn_path_field, vec![builder_recv, name, field]);
116+
let expr = cx.expr_call_global(
117+
span,
118+
fn_path_field,
119+
vec![builder_expr.clone(), name, field],
120+
);
117121
stmts.push(stmt_let_underscore(cx, span, expr));
118122
}
119123
fn_path_finish = cx.std_path(&[sym::fmt, sym::DebugStruct, sym::finish]);
120124
}
121125
}
122126

123-
let builder_recv = make_mut_borrow(cx, span, builder_expr);
124-
let expr = cx.expr_call_global(span, fn_path_finish, vec![builder_recv]);
127+
let expr = cx.expr_call_global(span, fn_path_finish, vec![builder_expr]);
125128

126129
stmts.push(cx.stmt_expr(expr));
127130
let block = cx.block(span, stmts);

src/test/run-make-fulldeps/coverage-spanview/expected_mir_dump.partial_eq/partial_eq.{impl#7}-fmt.-------.InstrumentCoverage.0.html

+32-35
Original file line numberDiff line numberDiff line change
@@ -73,41 +73,38 @@
7373
4:17-4:22: @0[2]: _3 = &amp;((*_1).0: usize)
7474
4:17-4:22: @0[4]: _4 = &amp;((*_1).1: usize)
7575
4:17-4:22: @0[6]: _5 = &amp;((*_1).2: usize)
76-
4:17-4:22: @0[9]: _7 = &amp;mut (*_2)
77-
4:17-4:22: @0[12]: _9 = const &quot;Version&quot;
78-
4:17-4:22: @0[13]: _8 = &amp;(*_9)
79-
4:17-4:22: @0.Call: _6 = std::fmt::Formatter::debug_struct(move _7, move _8) -&gt; [return: bb1, unwind: bb6]
80-
4:17-4:22: @1[2]: FakeRead(ForLet, _6)
81-
4:17-4:22: @1[7]: _12 = &amp;mut _6
82-
4:17-4:22: @1[8]: _11 = &amp;mut (*_12)
83-
4:17-4:22: @1[11]: _14 = const &quot;major&quot;
84-
4:17-4:22: @1[12]: _13 = &amp;(*_14)
85-
4:17-4:22: @1[17]: _18 = &amp;(*_3)
86-
4:17-4:22: @1[18]: _17 = &amp;_18
87-
4:17-4:22: @1[19]: _16 = &amp;(*_17)
88-
4:17-4:22: @1[20]: _15 = move _16 as &amp;dyn std::fmt::Debug (Pointer(Unsize))
89-
4:17-4:22: @1.Call: _10 = std::fmt::DebugStruct::field(move _11, move _13, move _15) -&gt; [return: bb2, unwind: bb6]
90-
4:17-4:22: @2[11]: _21 = &amp;mut _6
91-
4:17-4:22: @2[12]: _20 = &amp;mut (*_21)
92-
4:17-4:22: @2[15]: _23 = const &quot;minor&quot;
93-
4:17-4:22: @2[16]: _22 = &amp;(*_23)
94-
4:17-4:22: @2[21]: _27 = &amp;(*_4)
95-
4:17-4:22: @2[22]: _26 = &amp;_27
96-
4:17-4:22: @2[23]: _25 = &amp;(*_26)
97-
4:17-4:22: @2[24]: _24 = move _25 as &amp;dyn std::fmt::Debug (Pointer(Unsize))
98-
4:17-4:22: @2.Call: _19 = std::fmt::DebugStruct::field(move _20, move _22, move _24) -&gt; [return: bb3, unwind: bb6]
99-
4:17-4:22: @3[11]: _30 = &amp;mut _6
100-
4:17-4:22: @3[12]: _29 = &amp;mut (*_30)
101-
4:17-4:22: @3[15]: _32 = const &quot;patch&quot;
102-
4:17-4:22: @3[16]: _31 = &amp;(*_32)
103-
4:17-4:22: @3[21]: _36 = &amp;(*_5)
104-
4:17-4:22: @3[22]: _35 = &amp;_36
105-
4:17-4:22: @3[23]: _34 = &amp;(*_35)
106-
4:17-4:22: @3[24]: _33 = move _34 as &amp;dyn std::fmt::Debug (Pointer(Unsize))
107-
4:17-4:22: @3.Call: _28 = std::fmt::DebugStruct::field(move _29, move _31, move _33) -&gt; [return: bb4, unwind: bb6]
108-
4:17-4:22: @4[10]: _38 = &amp;mut _6
109-
4:17-4:22: @4[11]: _37 = &amp;mut (*_38)
110-
4:17-4:22: @4.Call: _0 = std::fmt::DebugStruct::finish(move _37) -&gt; [return: bb5, unwind: bb6]
76+
4:17-4:22: @0[10]: _8 = &amp;mut (*_2)
77+
4:17-4:22: @0[13]: _10 = const &quot;Version&quot;
78+
4:17-4:22: @0[14]: _9 = &amp;(*_10)
79+
4:17-4:22: @0.Call: _7 = std::fmt::Formatter::debug_struct(move _8, move _9) -&gt; [return: bb1, unwind: bb6]
80+
4:17-4:22: @1[2]: _6 = &amp;mut _7
81+
4:17-4:22: @1[3]: FakeRead(ForLet, _6)
82+
4:17-4:22: @1[7]: _12 = &amp;mut (*_6)
83+
4:17-4:22: @1[10]: _14 = const &quot;major&quot;
84+
4:17-4:22: @1[11]: _13 = &amp;(*_14)
85+
4:17-4:22: @1[16]: _18 = &amp;(*_3)
86+
4:17-4:22: @1[17]: _17 = &amp;_18
87+
4:17-4:22: @1[18]: _16 = &amp;(*_17)
88+
4:17-4:22: @1[19]: _15 = move _16 as &amp;dyn std::fmt::Debug (Pointer(Unsize))
89+
4:17-4:22: @1.Call: _11 = std::fmt::DebugStruct::field(move _12, move _13, move _15) -&gt; [return: bb2, unwind: bb6]
90+
4:17-4:22: @2[9]: _20 = &amp;mut (*_6)
91+
4:17-4:22: @2[12]: _22 = const &quot;minor&quot;
92+
4:17-4:22: @2[13]: _21 = &amp;(*_22)
93+
4:17-4:22: @2[18]: _26 = &amp;(*_4)
94+
4:17-4:22: @2[19]: _25 = &amp;_26
95+
4:17-4:22: @2[20]: _24 = &amp;(*_25)
96+
4:17-4:22: @2[21]: _23 = move _24 as &amp;dyn std::fmt::Debug (Pointer(Unsize))
97+
4:17-4:22: @2.Call: _19 = std::fmt::DebugStruct::field(move _20, move _21, move _23) -&gt; [return: bb3, unwind: bb6]
98+
4:17-4:22: @3[9]: _28 = &amp;mut (*_6)
99+
4:17-4:22: @3[12]: _30 = const &quot;patch&quot;
100+
4:17-4:22: @3[13]: _29 = &amp;(*_30)
101+
4:17-4:22: @3[18]: _34 = &amp;(*_5)
102+
4:17-4:22: @3[19]: _33 = &amp;_34
103+
4:17-4:22: @3[20]: _32 = &amp;(*_33)
104+
4:17-4:22: @3[21]: _31 = move _32 as &amp;dyn std::fmt::Debug (Pointer(Unsize))
105+
4:17-4:22: @3.Call: _27 = std::fmt::DebugStruct::field(move _28, move _29, move _31) -&gt; [return: bb4, unwind: bb6]
106+
4:17-4:22: @4[8]: _35 = &amp;mut (*_6)
107+
4:17-4:22: @4.Call: _0 = std::fmt::DebugStruct::finish(move _35) -&gt; [return: bb5, unwind: bb6]
111108
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>
112109
</body>
113110
</html>

0 commit comments

Comments
 (0)