Skip to content

Commit 2307d08

Browse files
committed
Use UFCS instead of method calls in derive(Debug). See issue 81211 for discussion.
1 parent 02b85d7 commit 2307d08

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

compiler/rustc_builtin_macros/src/deriving/debug.rs

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ use rustc_expand::base::{Annotatable, ExtCtxt};
88
use rustc_span::symbol::{sym, Ident};
99
use rustc_span::{Span, DUMMY_SP};
1010

11+
fn make_mut_borrow(cx: &mut ExtCtxt<'_>, sp: Span, expr: P<Expr>) -> P<Expr> {
12+
cx.expr(sp, ast::ExprKind::AddrOf(ast::BorrowKind::Ref, ast::Mutability::Mut, expr))
13+
}
14+
1115
pub fn expand_deriving_debug(
1216
cx: &mut ExtCtxt<'_>,
1317
span: Span,
@@ -67,34 +71,37 @@ fn show_substructure(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>
6771
let fmt = substr.nonself_args[0].clone();
6872

6973
let mut stmts = Vec::with_capacity(fields.len() + 2);
74+
let fn_path_finish;
7075
match vdata {
7176
ast::VariantData::Tuple(..) | ast::VariantData::Unit(..) => {
7277
// tuple struct/"normal" variant
73-
let expr =
74-
cx.expr_method_call(span, fmt, Ident::new(sym::debug_tuple, span), vec![name]);
78+
let fn_path_debug_tuple = cx.std_path(&[sym::fmt, sym::Formatter, sym::debug_tuple]);
79+
let expr = cx.expr_call_global(span, fn_path_debug_tuple, vec![fmt, name]);
7580
stmts.push(cx.stmt_let(span, true, builder, expr));
7681

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

82-
let expr = cx.expr_method_call(
83-
span,
84-
builder_expr.clone(),
85-
Ident::new(sym::field, span),
86-
vec![field],
87-
);
87+
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]);
8890

8991
// Use `let _ = expr;` to avoid triggering the
9092
// unused_results lint.
9193
stmts.push(stmt_let_underscore(cx, span, expr));
9294
}
95+
96+
fn_path_finish = cx.std_path(&[sym::fmt, sym::DebugTuple, sym::finish]);
9397
}
9498
ast::VariantData::Struct(..) => {
9599
// normal struct/struct variant
96-
let expr =
97-
cx.expr_method_call(span, fmt, Ident::new(sym::debug_struct, span), vec![name]);
100+
let fn_path_debug_struct =
101+
cx.std_path(&[sym::fmt, sym::Formatter, sym::debug_struct]);
102+
let expr = cx.expr_call_global(
103+
span, fn_path_debug_struct, vec![fmt, name]
104+
);
98105
stmts.push(cx.stmt_let(DUMMY_SP, true, builder, expr));
99106

100107
for field in fields {
@@ -104,20 +111,23 @@ fn show_substructure(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>
104111
);
105112

106113
// Use double indirection to make sure this works for unsized types
114+
let fn_path_field = cx.std_path(&[sym::fmt, sym::DebugStruct, sym::field]);
107115
let field = cx.expr_addr_of(field.span, field.self_.clone());
108116
let field = cx.expr_addr_of(field.span, field);
109-
let expr = cx.expr_method_call(
117+
let builder_recv = make_mut_borrow(cx, span, builder_expr.clone());
118+
let expr = cx.expr_call_global(
110119
span,
111-
builder_expr.clone(),
112-
Ident::new(sym::field, span),
113-
vec![name, field],
120+
fn_path_field,
121+
vec![builder_recv, name, field],
114122
);
115123
stmts.push(stmt_let_underscore(cx, span, expr));
116124
}
125+
fn_path_finish = cx.std_path(&[sym::fmt, sym::DebugStruct, sym::finish]);
117126
}
118127
}
119128

120-
let expr = cx.expr_method_call(span, builder_expr, Ident::new(sym::finish, span), vec![]);
129+
let builder_recv = make_mut_borrow(cx, span, builder_expr);
130+
let expr = cx.expr_call_global(span, fn_path_finish, vec![builder_recv]);
121131

122132
stmts.push(cx.stmt_expr(expr));
123133
let block = cx.block(span, stmts);

compiler/rustc_span/src/symbol.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ symbols! {
133133
Copy,
134134
Count,
135135
Debug,
136+
DebugStruct,
137+
DebugTuple,
136138
Decodable,
137139
Decoder,
138140
Default,

0 commit comments

Comments
 (0)