Skip to content

Commit

Permalink
Auto merge of rust-lang#81294 - pnkfelix:issue-81211-use-ufcs-in-deri…
Browse files Browse the repository at this point in the history
…ve-debug, r=oli-obk

Use ufcs in derive(Debug)

Cc rust-lang#81211.

(Arguably this *is* the fix for it.)
  • Loading branch information
bors committed Feb 3, 2021
2 parents 6ad11e2 + 1783c47 commit 186f7ae
Show file tree
Hide file tree
Showing 6 changed files with 315 additions and 43 deletions.
38 changes: 21 additions & 17 deletions compiler/rustc_builtin_macros/src/deriving/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ use rustc_expand::base::{Annotatable, ExtCtxt};
use rustc_span::symbol::{sym, Ident};
use rustc_span::{Span, DUMMY_SP};

fn make_mut_borrow(cx: &mut ExtCtxt<'_>, sp: Span, expr: P<Expr>) -> P<Expr> {
cx.expr(sp, ast::ExprKind::AddrOf(ast::BorrowKind::Ref, ast::Mutability::Mut, expr))
}

pub fn expand_deriving_debug(
cx: &mut ExtCtxt<'_>,
span: Span,
Expand Down Expand Up @@ -67,34 +71,34 @@ fn show_substructure(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>
let fmt = substr.nonself_args[0].clone();

let mut stmts = Vec::with_capacity(fields.len() + 2);
let fn_path_finish;
match vdata {
ast::VariantData::Tuple(..) | ast::VariantData::Unit(..) => {
// tuple struct/"normal" variant
let expr =
cx.expr_method_call(span, fmt, Ident::new(sym::debug_tuple, span), vec![name]);
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));

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 expr = cx.expr_method_call(
span,
builder_expr.clone(),
Ident::new(sym::field, span),
vec![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]);

// Use `let _ = expr;` to avoid triggering the
// unused_results lint.
stmts.push(stmt_let_underscore(cx, span, expr));
}

fn_path_finish = cx.std_path(&[sym::fmt, sym::DebugTuple, sym::finish]);
}
ast::VariantData::Struct(..) => {
// normal struct/struct variant
let expr =
cx.expr_method_call(span, fmt, Ident::new(sym::debug_struct, span), vec![name]);
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));

for field in fields {
Expand All @@ -104,20 +108,20 @@ fn show_substructure(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>
);

// Use double indirection to make sure this works for unsized types
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 expr = cx.expr_method_call(
span,
builder_expr.clone(),
Ident::new(sym::field, span),
vec![name, 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]);
stmts.push(stmt_let_underscore(cx, span, expr));
}
fn_path_finish = cx.std_path(&[sym::fmt, sym::DebugStruct, sym::finish]);
}
}

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

stmts.push(cx.stmt_expr(expr));
let block = cx.block(span, stmts);
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ symbols! {
Copy,
Count,
Debug,
DebugStruct,
DebugTuple,
Decodable,
Decoder,
Default,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,32 +78,36 @@
4:17-4:22: @0[13]: _8 = &amp;(*_9)
4:17-4:22: @0.Call: _6 = 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[6]: _11 = &amp;mut _6
4:17-4:22: @1[9]: _13 = const &quot;major&quot;
4:17-4:22: @1[10]: _12 = &amp;(*_13)
4:17-4:22: @1[15]: _17 = &amp;(*_3)
4:17-4:22: @1[16]: _16 = &amp;_17
4:17-4:22: @1[17]: _15 = &amp;(*_16)
4:17-4:22: @1[18]: _14 = move _15 as &amp;dyn std::fmt::Debug (Pointer(Unsize))
4:17-4:22: @1.Call: _10 = DebugStruct::field(move _11, move _12, move _14) -&gt; [return: bb2, unwind: bb6]
4:17-4:22: @2[9]: _19 = &amp;mut _6
4:17-4:22: @2[12]: _21 = const &quot;minor&quot;
4:17-4:22: @2[13]: _20 = &amp;(*_21)
4:17-4:22: @2[18]: _25 = &amp;(*_4)
4:17-4:22: @2[19]: _24 = &amp;_25
4:17-4:22: @2[20]: _23 = &amp;(*_24)
4:17-4:22: @2[21]: _22 = move _23 as &amp;dyn std::fmt::Debug (Pointer(Unsize))
4:17-4:22: @2.Call: _18 = DebugStruct::field(move _19, move _20, move _22) -&gt; [return: bb3, unwind: bb6]
4:17-4:22: @3[9]: _27 = &amp;mut _6
4:17-4:22: @3[12]: _29 = const &quot;patch&quot;
4:17-4:22: @3[13]: _28 = &amp;(*_29)
4:17-4:22: @3[18]: _33 = &amp;(*_5)
4:17-4:22: @3[19]: _32 = &amp;_33
4:17-4:22: @3[20]: _31 = &amp;(*_32)
4:17-4:22: @3[21]: _30 = move _31 as &amp;dyn std::fmt::Debug (Pointer(Unsize))
4:17-4:22: @3.Call: _26 = DebugStruct::field(move _27, move _28, move _30) -&gt; [return: bb4, unwind: bb6]
4:17-4:22: @4[8]: _34 = &amp;mut _6
4:17-4:22: @4.Call: _0 = DebugStruct::finish(move _34) -&gt; [return: bb5, unwind: bb6]
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 = 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 = 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 = 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 = DebugStruct::finish(move _37) -&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>
40 changes: 40 additions & 0 deletions src/test/ui/derives/derive-Debug-use-ufcs-struct.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// run-pass
#![allow(warnings)]

#[derive(Debug)]
pub struct Bar { pub t: () }

impl<T> Access for T {}
pub trait Access {
fn field(&self, _: impl Sized, _: impl Sized) {
panic!("got into Access::field");
}

fn finish(&self) -> Result<(), std::fmt::Error> {
panic!("got into Access::finish");
}

fn debug_struct(&self, _: impl Sized, _: impl Sized) {
panic!("got into Access::debug_struct");
}
}

impl<T> MutAccess for T {}
pub trait MutAccess {
fn field(&mut self, _: impl Sized, _: impl Sized) {
panic!("got into MutAccess::field");
}

fn finish(&mut self) -> Result<(), std::fmt::Error> {
panic!("got into MutAccess::finish");
}

fn debug_struct(&mut self, _: impl Sized, _: impl Sized) {
panic!("got into MutAccess::debug_struct");
}
}

fn main() {
let bar = Bar { t: () };
assert_eq!("Bar { t: () }", format!("{:?}", bar));
}
32 changes: 32 additions & 0 deletions src/test/ui/derives/derive-Debug-use-ufcs-tuple.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// run-pass
#![allow(warnings)]

#[derive(Debug)]
pub struct Foo<T>(pub T);

use std::fmt;

impl<T> Field for T {}
impl<T> Finish for T {}
impl Dt for &mut fmt::Formatter<'_> {}

pub trait Field {
fn field(&self, _: impl Sized) {
panic!("got into field");
}
}
pub trait Finish {
fn finish(&self) -> Result<(), std::fmt::Error> {
panic!("got into finish");
}
}
pub trait Dt {
fn debug_tuple(&self, _: &str) {
panic!("got into debug_tuple");
}
}

fn main() {
let foo = Foo(());
assert_eq!("Foo(())", format!("{:?}", foo));
}
Loading

0 comments on commit 186f7ae

Please sign in to comment.